gpt4 book ai didi

elasticsearch - Elasticsearch - 获取不同的标签

转载 作者:行者123 更新时间:2023-11-29 02:56:20 27 4
gpt4 key购买 nike

我有以下格式的文件:

{
_id :"1",
tags:["guava","apple","mango", "banana", "gulmohar"]
}


{
_id:"2",
tags: ["orange","guava", "mango shakes", "apple pie", "grammar"]
}

{

_id:"3",
tags: ["apple","grapes", "water", "gulmohar","water-melon", "green"]
}

现在,我想从前缀 g* 开始的整个文档“标签字段”中获取唯一标签值,以便标签建议者显示这些唯一标签(Stackoverflow 网站是一个例子) .

例如:每当用户键入“g”时:"guava"、"gulmohar"、"grammar"、"grapes"和 "green" 应作为结果返回。IE。查询应返回带有前缀 g* 的不同标签。

我到处都试过了,浏览了整个文档,搜索了 es 论坛,但我没有找到任何线索,这让我很沮丧。

我尝试了聚合,但聚合返回了标签字段中整个单词/标记的不同计数。它不会返回以“g”开头的唯一标签列表。

"query": {
"filtered": {
"query": {
"bool": {
"should": [
{
"query_string": {
"allow_leading_wildcard": false,
"fields": [
"tags"
],
"query": "g*",
"fuzziness":0
}
}
]
}
},
"filter": {
//some condition on other field...
}

}

},
"aggs": {
"distinct_tags": {
"terms": {
"field": "tags",
"size": 10
}
}
},

上面的结果:guava(w), apple(q), mango(1),...

有人可以建议我获取所有带有前缀 input_prefix* 的不同标签的正确方法吗?

最佳答案

这有点 hack,但这似乎可以完成您想要的。

我创建了一个索引并添加了您的文档:

DELETE /test_index

PUT /test_index
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 0
}
}

POST /test_index/_bulk
{"index":{"_index":"test_index","_type":"doc","_id":1}}
{"tags":["guava","apple","mango", "banana", "gulmohar"]}
{"index":{"_index":"test_index","_type":"doc","_id":2}}
{"tags": ["orange","guava", "mango shakes", "apple pie", "grammar"]}
{"index":{"_index":"test_index","_type":"doc","_id":3}}
{"tags": ["guava","apple","grapes", "water", "grammar","gulmohar","water-melon", "green"]}

然后我使用了 prefix query 的组合和 highlighting如下:

POST /test_index/_search
{
"query": {
"prefix": {
"tags": {
"value": "g"
}
}
},
"fields": [ ],
"highlight": {
"pre_tags": [""],
"post_tags": [""],
"fields": {
"tags": {}
}
}
}
...
{
"took": 5,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 1,
"hits": [
{
"_index": "test_index",
"_type": "doc",
"_id": "1",
"_score": 1,
"highlight": {
"tags": [
"guava",
"gulmohar"
]
}
},
{
"_index": "test_index",
"_type": "doc",
"_id": "2",
"_score": 1,
"highlight": {
"tags": [
"guava",
"grammar"
]
}
},
{
"_index": "test_index",
"_type": "doc",
"_id": "3",
"_score": 1,
"highlight": {
"tags": [
"guava",
"grapes",
"grammar",
"gulmohar",
"green"
]
}
}
]
}
}

这是我使用的代码: http://sense.qbox.io/gist/c14675ee8bd3934389a6cb0c85ff57621a17bf11

当然,您尝试执行的操作相当于自动完成,并且可能有比我在上面发布的方法更好的方法(尽管它们涉及更多)。以下是我们写的几篇关于设置自动完成方法的博文:

http://blog.qbox.io/quick-and-dirty-autocomplete-with-elasticsearch-completion-suggest

http://blog.qbox.io/multi-field-partial-word-autocomplete-in-elasticsearch-using-ngrams

关于elasticsearch - Elasticsearch - 获取不同的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28303752/

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