gpt4 book ai didi

Elasticsearch 完成建议使用多词输入进行搜索

转载 作者:行者123 更新时间:2023-11-29 02:46:30 25 4
gpt4 key购买 nike

使用 Elasticsearch 完成建议器时,我在返回与单词查询匹配的多词输入建议时遇到问题。

示例结构:

PUT /test_index/
{
"mappings": {
"item": {
"properties": {
"test_suggest": {
"type": "completion",
"index_analyzer": "whitespace",
"search_analyzer": "whitespace",
"payloads": false
}
}
}
}
}

PUT /test_index/item/1
{
"test_suggest": {
"input": [
"cat dog",
"elephant"
]
}
}

工作查询:

POST /test_index/_suggest
{
"test_suggest":{
"text":"cat",
"completion": {
"field" : "test_suggest"
}
}
}

结果

{
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"test_suggest": [
{
"text": "cat",
"offset": 0,
"length": 3,
"options": [
{
"text": "cat dog",
"score": 1
}
]
}
]
}

查询失败:

POST /test_index/_suggest
{
"test_suggest":{
"text":"dog",
"completion": {
"field" : "test_suggest"
}
}
}

结果

{
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"test_suggest": [
{
"text": "dog",
"offset": 0,
"length": 3,
"options": []
}
]
}

我希望得到与工作查询相同的结果,匹配“cat dog”。有什么建议是什么问题以及如何使失败的查询正常工作?使用标准分析器而不是空白分析器时,我得到了相同的结果。我想在每个输入字符串中使用多个单词,如上例所示。

最佳答案

完成建议是一个 prefix suggester ,这意味着它会尝试将您的查询与所提供输入的前几个字符相匹配。如果您希望发布的文档与文本“dog”匹配,则需要将“dog”指定为输入。

PUT /test_index/item/1
{
"test_suggest": {
"input": [
"cat dog",
"elephant",
"dog"
]
}
}

根据我的经验,必须指定要匹配的输入的限制使得完成建议不如其他实现前缀匹配的方法有用。我喜欢edge ngrams以此目的。我最近写了一篇关于使用 ngrams 的博客文章,您可能会发现它有帮助:http://blog.qbox.io/an-introduction-to-ngrams-in-elasticsearch

作为一个简单的例子,这里有一个你可以使用的映射

PUT /test_index
{
"settings": {
"analysis": {
"filter": {
"edge_ngram_filter": {
"type": "edge_ngram",
"min_gram": 2,
"max_gram": 20
}
},
"analyzer": {
"edge_ngram_analyzer": {
"type": "custom",
"tokenizer": "standard",
"filter": [
"lowercase",
"edge_ngram_filter"
]
}
}
}
},
"mappings": {
"item": {
"properties": {
"text_field": {
"type": "string",
"index_analyzer": "edge_ngram_analyzer",
"search_analyzer": "standard"
}
}
}
}
}

然后像这样索引文档:

PUT /test_index/item/1
{
"text_field": [
"cat dog",
"elephant"
]
}

并且任何这些查询都会返回它:

POST /test_index/_search
{
"query": {
"match": {
"text_field": "dog"
}
}
}

POST /test_index/_search
{
"query": {
"match": {
"text_field": "ele"
}
}
}

POST /test_index/_search
{
"query": {
"match": {
"text_field": "ca"
}
}
}

下面是全部代码:

http://sense.qbox.io/gist/4a08fbb6e42c34ff8904badfaaeecc01139f96cf

关于Elasticsearch 完成建议使用多词输入进行搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29753971/

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