elasticsearch completion字段不返回使用在\u analyze api响应中返回的令牌进行搜索的建议

8dtrkrch  于 2021-06-13  发布在  ElasticSearch
关注(0)|答案(1)|浏览(280)

我试图用elasticsearch completion field suggester实现自动完成功能。
步骤1:创建测试索引:

curl --location --request PUT 'http://localhost:9200/test_index?pretty' \
--header 'Content-Type: application/json' \
--data-raw '{"settings": {"number_of_shards": 1, "max_ngram_diff": 7, "number_of_replicas": "0", "analysis": {"filter": {"edge_ngram_completion_filter": {"token_chars": ["whitespace", "digit"], "min_gram": "3", "type": "edge_ngram", "max_gram": "10"}}, "analyzer": {"edge_ngram_completion": {"filter": ["lowercase", "edge_ngram_completion_filter"], "type": "custom", "tokenizer": "standard"}}}}, "mappings": {"properties": {"id": {"type": "integer"}, "name": {"type": "text", "fields": {"raw": {"type": "keyword"}, "suggest": {"type": "completion", "analyzer": "edge_ngram_completion", "search_analyzer": "simple", "preserve_separators": true, "preserve_position_increments": true, "max_input_length": 100}}}}}}
'

步骤2:索引下列文档

curl --location --request POST 'http://localhost:9200/test_index/_doc?pretty' \
--header 'Content-Type: application/json' \
--data-raw '{
    "name": "PANTOCID DSR CAP",
    "id": 1
}'

第三步:点击analyze api for“pantocid dsr cap”,我得到[“pan”,“pant”,“panto”,“pantoc”,“pantoci”,“pantocid”,“dsr”,“cap”]标记

curl --location --request POST 'http://localhost:9200/test_index/_analyze?pretty' \
--header 'Content-Type: application/json' \
--data-raw '{
  "analyzer" : "edge_ngram_completion",
  "text" : "PANTOCID DSR CAP"  
}
'

第四步:但是当我用“dsr”搜索时,我没有得到任何建议:

curl --location --request POST 'http://localhost:9200/test_index/_search?pretty' \
--header 'Content-Type: application/json' \
--data-raw '{
  "suggest": {
    "egde_ngram_suggest" : {
       "text": "dsr", 
       "completion" : {
            "field" : "name.suggest"
       }
    }
  }
}
'

为什么?我的意思是,如果搜索的文本是生成的标记之一,那么它必须导致一个建议匹配,对吗?我是不是漏了什么?
感谢您的帮助。提前谢谢。

mjqavswn

mjqavswn1#

令人困惑的是 _analyze 步骤。虽然声明了正确的分析器,但请尝试通过特别请求该字段来验证该字段的标记化:

curl --location --request POST 'http://localhost:9200/test_index/_analyze?pretty' \
--header 'Content-Type: application/json' \
--data-raw '{
  "field" : "name.suggest",              <---
  "text" : "PANTOCID DSR CAP"  
}
'

当你运行它时,你会看到文本从一开始就被n-grammed了:

pandsrcap
pant dsr cap
...

所有这些象征性的变化都不会从 dsr 扔掉那些垃圾 pan 前缀。
这告诉我们,completion字段可以正常工作——它是用于自动完成实现的,而不是用于文本搜索的中间部分。

相关问题