圣玺企业网站建设第三方平台推广引流
Elasticsearch 自带一种自动补全类型 completion 这种类型不在mapping文档里面有点坑。
先直接上例子。
建立 index,把我们要自动补全的字段设置为 completion 类型
或者直接设置为子类型
PUT /blogs_completion/
{"mappings": {"tech": {"properties": {"body": {"type": "completion"}}}}
}// 设置为子类型
PUT /blogs_completion/
{"mappings": {"properties": {"body": {"type": "text","fields": {"suggest":{"type":"completion"},"keyword":{"type":"keyword"}}}}}
}//填充测试用例inter开头文本,和inter 的变形单词inber等
POST _bulk/?refresh=true
{ "index" : { "_index" : "blogs_completion", "_type" : "tech" } }
{ "body": "interface test1"}
{ "index" : { "_index" : "blogs_completion", "_type" : "tech" } }
{ "body": "interf test2"}
{ "index" : { "_index" : "blogs_completion", "_type" : "tech" } }
{ "body": "inter test3"}
{ "index" : { "_index" : "blogs_completion", "_type" : "tech" } }
{ "body": "inber test"}
{ "index" : { "_index" : "blogs_completion", "_type" : "tech" } }
{ "body": "sinter test"}// 使用带模糊匹配的 completion suggester 查询POST blogs_completion/_search?pretty
{ "size": 0,"suggest": {"blog-suggest": {"prefix": "inter","completion": {"size":"5","field": "body","fuzzy" : {"fuzziness" : 2}}}}
}// 结果{"took" : 1,"timed_out" : false,"_shards" : {"total" : 5,"successful" : 5,"skipped" : 0,"failed" : 0},"hits" : {"total" : 0,"max_score" : 0.0,"hits" : [ ]},"suggest" : {"blog-suggest" : [{"text" : "inter","offset" : 0,"length" : 5,"options" : [{"text" : "inter test","_index" : "blogs_completion","_type" : "tech","_id" : "Fd_wOHABW8nrxgV2Ej6u","_score" : 3.0,"_source" : {"body" : "inter test"}},{"text" : "interf test","_index" : "blogs_completion","_type" : "tech","_id" : "FN_wOHABW8nrxgV2Ej6u","_score" : 3.0,"_source" : {"body" : "interf test"}},{"text" : "interface test","_index" : "blogs_completion","_type" : "tech","_id" : "E9_wOHABW8nrxgV2Ej6u","_score" : 3.0,"_source" : {"body" : "interface test"}},{"text" : "inber test","_index" : "blogs_completion","_type" : "tech","_id" : "HN__OHABW8nrxgV2lD4C","_score" : 2.0,"_source" : {"body" : "inber test"}}]}]}
}
概述:
Elasticsearch 自带 suggest query 的功能,作为自动补全和查询建议的工具,称为 suggester
分为四种:
- Term suggester
- Phrase Suggester
- Completion Suggester
- Context Suggester
Completion Suggester 已经可以满足我们一般的带模糊查询的前缀自动补全功能,
如果要做更复杂的自动补全,可以结合 term suggester 和 phrase suggester 和 completion,同时查询,合并结果,得到更复杂的结果
一般详细文档:
https://blog.csdn.net/wwd0501/article/details/80595201
官方详情查考
https://www.elastic.co/guide/en/elasticsearch/reference/current/search-suggesters.html#context-suggester