写在前面
通过completion suggester可以实现如下的效果:
其实就是做的like xxx%
这种。通过FST这种数据结构来存储,实现快速的前缀匹配,并且可以将es所有的数据加载到内存中所以速度completion的查询速度非常快。
需要注意,如果是某个字段想要使用completion suggester的功能,需要将其类型设置为completion,也就是我们需要显示的设置mapping来指定。
1:例子
首先来创建索引并指定mapping:
DELETE articles
PUT articles
{
"mappings": {
"properties": {
"title": {
"type": "text",
"fields": {
"title_use_completion": {
"type": "completion"
}
}
}
}
}
}
接着插入数据:
POST articles/_bulk
{ "index": {} }
{ "title": "lucene is very cool" }
{ "index": {} }
{ "title": "Elasticsearch builds on top of lucene" }
{ "index": {} }
{ "title": "Elasticsearch rocks" }
{ "index": {} }
{ "title": "elastic is the company behind ELK stack" }
{ "index": {} }
{ "title": "Elk stack rocks" }
查询:
POST articles/_search
{
"size": 0,
"suggest": {
"article-suggester": {
"prefix": "luc",
"completion": {
"field": "title.title_use_completion"
}
}
}
}
另外,es还支持一种基于上下文的suggestion,Context Suggerter
,如下:
context分为两类,category和geo,如下:
以context为里来看下。
- 首先来定义mapping
在mapping中指定context的信息:
# 删除
DELETE comments
# 创建
PUT comments
# 指定mapping
PUT comments/_mapping
{
"properties": {
"comment_autocomplete": {
"type": "completion",
"contexts": [
{
"type": "category",
"name": "comment_category"
}
]
}
}
}
数据:
# 录入数据并指定上下文是movies
POST comments/_doc
{
"comment": "I love the star war movies",
"comment_autocomplete": {
"input": ["start wars"],
"contexts": {
"comment_category": "movies"
}
}
}
# 录入数据并指定上下文是coffee
POST comments/_doc
{
"comment": "Where can I find a Starbucks",
"comment_autocomplete": {
"input": ["starbucks"],
"contexts": {
"comment_category": "coffee"
}
}
}
movies
上下文查询:
# 如果是movie上下文,返回start wars
POST comments/_search
{
"suggest": {
"MY_SUGGESTION": {
"prefix": "sta",
"completion": {
"field": "comment_autocomplete",
"contexts": {
"comment_category": "movies"
}
}
}
}
}
coffee
上下文查询:
# 如果是coffee上下文,返回starbucks
POST comments/_search
{
"suggest": {
"MY_SUGGESTION": {
"prefix": "sta",
"completion": {
"field": "comment_autocomplete",
"contexts": {
"comment_category": "coffee"
}
}
}
}
}
最后看下term,phrase,completion三者的对比:
写在后面
参考文章列表
倒排索引:ES倒排索引底层原理及FST算法的实现过程 。