gpt4 book ai didi

elasticsearch - Elasticsearch:查找包含不超过查询中术语的文档

转载 作者:行者123 更新时间:2023-12-02 22:21:21 24 4
gpt4 key购买 nike

如果我有文件:

1: { "name": "red yellow" }
2: { "name": "green yellow" }

我想查询“红棕黄”并获取文件1。

我的意思是查询至少应包含我文档中的术语,但可以包含更多术语。如果文档中包含查询中没有的 token ,则不应单击。

我怎样才能做到这一点?反之亦然...

最佳答案

首先,您必须将字段声明为fielddata : true,以便对其执行脚本:

PUT test
{
"mappings": {
"properties": {
"name": {
"type": "text",
"fielddata": true
}
}
}
}

然后,您可以使用查询脚本来过滤结果:
POST test/_search
{
"query": {
"bool": {
"filter": {
"script": {
"script": {
"source": """
boolean res = true;
for (item in doc['name']) {
res = 'red brown yellow'.contains(item) && res;
}
return res;
""",
"lang": "painless"
}
}
},
"must": [
{
"match": {
"name": "red brown yellow"
}
}
]
}
}
}

请注意,文本字段上的fielddata可能会花费很多,最好是fou可以将该字段作为关键字索引到数组上,如下所示:
1: { "name": ["red","yellow"] }
2: { "name": ["green", "yellow"] }

搜索请求可以完全相同

关于elasticsearch - Elasticsearch:查找包含不超过查询中术语的文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61016510/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com