如果我有文件:
1: { "name": "red yellow" }
2: { "name": "green yellow" }
我想用“红棕黄”查询并获取文档1。
我的意思是查询至少应包含我文档中的字词,但可以包含更多字词。如果文档中包含查询中没有的令牌,则不应单击。
我该怎么做?相反,这很容易...
首先,您必须将字段声明为fielddata : true
才能在其上执行脚本:
PUT test
{
"mappings": {
"properties": {
"name": {
"type": "text",
"fielddata": true
}
}
}
}
然后,您可以使用脚本进行查询:
POST test/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "red brown yellow"
}
},
{
"script": {
"script": {
"source": """
boolean res = true;
for (item in doc['name']) {
res = 'red brown yellow'.contains(item) && res;
}
return res;
""",
"lang": "painless"
}
}
}
]
}
}
}
请注意,文本字段上的fielddata可能会花费很多,最好是fou可以将该字段作为数组中的Keyword进行索引,如下所示:
1: { "name": ["red","yellow"] }
2: { "name": ["green", "yellow"] }
搜索请求可以完全相同