【问题标题】:Autocomplete in elastic search弹性搜索中的自动完成
【发布时间】:2023-04-09 07:49:01
【问题描述】:

我正计划为电子商务网站制作一个基于弹性搜索的自动完成模块。我正在使用 edge_ngram 提供建议。我正在尝试这种配置。

**My index creation :**

PUT my_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "autocomplete": {
          "tokenizer": "autocomplete",
          "filter": [
            "lowercase"
          ]
        },
        "autocomplete_search": {
          "tokenizer": "lowercase"
        }
      },
      "tokenizer": {
        "autocomplete": {
          "type": "edge_ngram",
          "min_gram": 1,
          "max_gram": 10,
          "token_chars": [
            "letter","digit"
          ]
        }
      }
    }
  },
  "mappings": {
    "doc": {
      "properties": {
        "title": {
          "type": "text",
          "analyzer": "autocomplete",
          "search_analyzer": "autocomplete_search"
        }
      }
    }
  }
}

**Inserting Data**

PUT my_index/doc/1
{
  "title": "iphone s" 
}

PUT my_index/doc/9
{
  "title": "iphone ka" 
}

PUT my_index/doc/11
{
  "title": "iphone ka t" 
}

PUT my_index/doc/15
{
  "title": "iphone 6" 
}

PUT my_index/doc/14
{
  "title": "iphone 6 16GB" 
}

PUT my_index/doc/3
{
  "title": "iphone k" 
}

POST my_index/_refresh

POST my_index/_analyze
{
  "tokenizer": "autocomplete",
  "text": "iphone 6"
}

POST my_index/_analyze
{
  "analyzer": "pattern",
  "text": "iphone 6"
}

**Autocomplete suggestions**
When i am trying to find out closets match to iphone 6.It is not showing correct result.

GET my_index/_search
{
  "query": {
    "match": {
      "title": {
        "query": "iphone 6", 
        "operator": "and"
      }
    }
  }
}


**Above query yielding :**
{
  "took": 0,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 7,
    "max_score": 0.28582606,
    "hits": [
      {
        "_index": "my_index",
        "_type": "doc",
        "_id": "1",
        "_score": 0.28582606,
        "_source": {
          "title": "iphone s"
        }
      },
      {
        "_index": "my_index",
        "_type": "doc",
        "_id": "9",
        "_score": 0.25811607,
        "_source": {
          "title": "iphone ka"
        }
      },
      {
        "_index": "my_index",
        "_type": "doc",
        "_id": "14",
        "_score": 0.24257512,
        "_source": {
          "title": "iphone 6 16GB"
        }
      },
      {
        "_index": "my_index",
        "_type": "doc",
        "_id": "3",
        "_score": 0.19100356,
        "_source": {
          "title": "iphone k"
        }
      },
      {
        "_index": "my_index",
        "_type": "doc",
        "_id": "15",
        "_score": 0.1862728,
        "_source": {
          "title": "iphone 6"
        }
      },
      {
        "_index": "my_index",
        "_type": "doc",
        "_id": "11",
        "_score": 0.16358379,
        "_source": {
          "title": "iphone ka t"
        }
      },
      {
        "_index": "my_index",
        "_type": "doc",
        "_id": "2",
        "_score": 0.15861572,
        "_source": {
          "title": "iphone 5 s"
        }
      }
    ]
  }
}

但结果应该是:

     {
        "_index": "my_index",
        "_type": "doc",
        "_id": "15",
        "_score": 1,
        "_source": {
          "title": "iphone 6"
        }
      }

如果我在这方面遗漏了什么,请告诉我,我是新手,所以不知道任何其他可能产生更好结果的方法。

【问题讨论】:

    标签: elasticsearch autocomplete elasticsearch-5 kibana-5


    【解决方案1】:

    您正在使用autocomplete_search 作为您的search_analyzer。如果您查看如何使用您指定的搜索分析器分析您的文本。

    POST my_index/_analyze
    {
     "analyzer": "autocomplete_search",
     "text": "iphone 6"
    }
    

    你会得到

     {
     "tokens": [
      {
         "token": "iphone",           ===> Only one token
         "start_offset": 0,
         "end_offset": 6,
         "type": "word",
         "position": 0
         }
       ]
     }
    

    由于所有文档在 reverse index 中都有这个 (iphone) 令牌。所以所有的文件都返回了

    如果您想匹配所需的结果,可以使用索引时使用的相同分析器。

    {
     "query": {
     "match": {
      "title": {
        "query": "iphone 6", 
        "operator": "and",
        "analyzer" : "autocomplete"
       }
      } 
     }
    }
    

    【讨论】:

    • 您能告诉我们如何获得所需的结果吗?
    • 感谢您的帮助。此外,我需要使用短语提示器。我可以这样做吗?如果用户键入 ipone 5 并建议使用 iphone 5,我想更正。
    • 您必须更改您的analyzer。一些将 5 索引为 5 的分析器。
    • 让我们说现在我不考虑这种情况(ipone 5)。假设用户搜索 ipone 5 将产生所需的结果。我用这个来搜索。 “建议”:{“DidYouMean”:{“文本”:“iphne 5”,“短语”:{“分析器”:“自动完成”,“字段”:“名称”,“突出显示”:{“pre_tag”:“ ", "post_tag": "" } } }}
    • 得到奇怪的输出那是因为我认为分析器自动完成。 { "text": "i ip iph ipho iphone 5", "highlighted": "i ip iph ipho iphone 5", "score": 4.105417e-7 }, { "text": " i ip iph iphon iphone 5", "highlighted": "i ip iph iphon iphone 5", "score": 4.105417e-7 },
    猜你喜欢
    • 2018-12-07
    • 2020-08-14
    • 1970-01-01
    • 1970-01-01
    • 2013-09-30
    • 1970-01-01
    • 2017-06-14
    • 2021-03-03
    相关资源
    最近更新 更多