Elasticsearch查询:获取所有前缀与查询匹配的结果,以实现typeahead、autocomplete功能


查询我叫什么名字什么是谷歌什么是stackoverflow如何搜索

有多种方法可以实现您想要的,但最简单的是使用前缀查询,如果您只将数据直接索引到Elasticsearch,而不定义映射,它将自动为您创建两个字段,在.keyword字段上,您可以使用前缀查询如下所示。

索引样本文档

PUT <your-es-index>/_doc/1
{
"title" : "what is my name"
}
PUT <your-es-index>/_doc/2
{
"title" : "what is google"
}
PUT <your-es-index>/_doc/3
{
"title" : "what is stackoverflow"
}
PUT <your-es-index>/_doc/4
{
"title" : "how to search"
}

搜索查询

POST/搜索

{
"query": {
"prefix": {
"title.keyword": {
"value": "what is"
}
}
}
}

您期望的搜索结果

"hits": [
{
"_index": "72391510",
"_id": "1",
"_score": 1.0,
"_source": {
"title": "what is my name"
}
},
{
"_index": "72391510",
"_id": "2",
"_score": 1.0,
"_source": {
"title": "what is google"
}
},
{
"_index": "72391510",
"_id": "3",
"_score": 1.0,
"_source": {
"title": "what is stackoverflow"
}
}

相关内容

  • 没有找到相关文章

最新更新