栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

面向单词的完成提示器(ElasticSearch 5.x)

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

面向单词的完成提示器(ElasticSearch 5.x)

如评论中所暗示的,在不获取重复文档的情况下实现此目的的另一种方法是为

firstname
包含ngram
个字段的字段创建一个子字段。首先,您要像这样定义映射:

PUT my-index{  "settings": {    "analysis": {      "analyzer": {        "completion_analyzer": {          "type": "custom",          "filter": [ "lowercase", "completion_filter"          ],          "tokenizer": "keyword"        }      },      "filter": {        "completion_filter": {          "type": "edge_ngram",          "min_gram": 1,          "max_gram": 24        }      }    }  },  "mappings": {    "users": {      "properties": {        "autocomplete": {          "type": "text",          "fields": { "raw": {   "type": "keyword" }, "completion": {   "type": "text",   "analyzer": "completion_analyzer",   "search_analyzer": "standard" }          }        },        "firstName": {          "type": "text"        },        "lastName": {          "type": "text"        }      }    }  }}

然后您索引一些文档:

POST my-index/users/_bulk{"index":{}}{ "firstName": "John", "lastName": "Doe", "autocomplete": "John Doe"}{"index":{}}{ "firstName": "John", "lastName": "Deere", "autocomplete": "John Deere" }{"index":{}}{ "firstName": "Johnny", "lastName": "Cash", "autocomplete": "Johnny Cash" }

然后您可以查询

joh
并获得一个结果,
John
而另一个获得
Johnny

{  "size": 0,  "query": {    "term": {      "autocomplete.completion": "john d"    }  },  "aggs": {    "suggestions": {      "terms": {        "field": "autocomplete.raw"      }    }  }}

结果:

{  "aggregations": {    "suggestions": {      "doc_count_error_upper_bound": 0,      "sum_other_doc_count": 0,      "buckets": [        {          "key": "John Doe",          "doc_count": 1        },        {          "key": "John Deere",          "doc_count": 1        }      ]    }  }}

更新(2019年6月25日):

ES
7.2引入了一种称为的新数据类型

search_as_you_type
,该数据类型本身就允许这种行为。有关更多信息,请访问:https :
//www.elastic.co/guide/en/elasticsearch/reference/7.2/search-as-you-
type.html



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/394516.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号