ElasticSearch之Completion Suggester

写在前面

通过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算法的实现过程

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值