elasticsearch补全功能之只补全筛选后的部分数据context suggester
官方文档https://www.elastic.co/guide/en/elasticsearch/reference/5.0/suggester-context.html
下面所有演示基于elasticsearch5.x和Python3.x
最近项目使用elasticsearch的补全功能时,需要对于所有文章(article)的作者名字(author)的搜索做补全,文章的mapping大致如下
ARTICLE = {
'properties': {
'id': {
'type': 'integer',
'index': 'not_analyzed',
},
'author': {
'type': 'text',
},
'author_completion': {
'type': 'completion',
},
'removed': {
'type': 'boolean',
}
}
} MAPPINGS = {
'mappings': {
'article': ARTICLE,
}
}
现在的需求是,针对于下架状态removed为True的不做补全提示。
作为演示先插入部分数据,代码如下
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from elasticsearch.helpers import bulk
from elasticsearch import Elasticsearch ES_HOSTS = [{'host': 'localhost', 'port': 9200}, ] ES = Elasticsearch(hosts=ES_HOSTS) INDEX = 'test_article'
TYPE = 'article' ARTICLE = {
'properties': {
'id': {
'type': 'integer',
'index': 'not_analyzed',
},
'author': {
'type': 'text',
},
'author_completion': {
'type': 'completion',
},
'removed': {
'type': 'boolean',
}
}
} MAPPINGS = {
'mappings': {
'article': ARTICLE,
}
} def create_index():
"""
插入数据前创建对应的index
"""
ES.indices.delete(index=INDEX, ignore=404)
ES.indices.create(index=INDEX, body=MAPPINGS) def insert_data():
"""
添加测试数据
:return:
"""
test_datas = [
{
'id': 1,
'author': 'tom',
'author_completion': 'tom',
'removed': False
},
{
'id': 2,
'author': 'tom_cat',
'author_completion': 'tom_cat',
'removed': True
},
{
'id': 3,
'author': 'kitty',
'author_completion': 'kitty',
'removed': False
},
{
'id': 4,
'author': 'tomato',
'author_completion': 'tomato',
'removed': False
},
]
bulk_data = []
for data in test_datas:
action = {
'_index': INDEX,
'_type': TYPE,
'_id': data.get('id'),
'_source': data
}
bulk_data.append(action) success, failed = bulk(client=ES, actions=bulk_data, stats_only=True) print('success', success, 'failed', failed) if __name__ == '__main__':
create_index()
insert_data()
成功插入4条测试数据,下面测试获取作者名称补全建议,代码如下
def get_suggestions(keywords):
body = {
# 'size': 0, # 这里是不返回相关搜索结果的字段,如author,id等,作为测试这里返回
'_source': 'suggest',
'suggest': {
'author_prefix_suggest': {
'prefix': keywords,
'completion': {
'field': 'author_completion',
'size': 10,
}
}
},
# 对于下架数据,我单纯的以为加上下面的筛选就行了
'query': {
'term': {
'removed': False
}
}
}
suggest_data = ES.search(index=INDEX, doc_type=TYPE, body=body)
return suggest_data if __name__ == '__main__':
# create_index()
# insert_data() suggestions = get_suggestions('t')
print(suggestions)
"""
suggestions = {
'took': 0,
'timed_out': False,
'_shards': {
'total': 5,
'successful': 5,
'skipped': 0,
'failed': 0
},
'hits': {
'total': 3,
'max_score': 0.6931472,
'hits': [
{'_index': 'test_article', '_type': 'article', '_id': '4', '_score': 0.6931472,
'_source': {}},
{'_index': 'test_article', '_type': 'article', '_id': '1', '_score': 0.2876821,
'_source': {}},
{'_index': 'test_article', '_type': 'article', '_id': '3', '_score': 0.2876821,
'_source': {}}]},
'suggest': {
'author_prefix_suggest': [{'text': 't', 'offset': 0, 'length': 1, 'options': [
{'text': 'tom', '_index': 'test_article', '_type': 'article', '_id': '1', '_score': 1.0,
'_source': {}},
{'text': 'tom_cat', '_index': 'test_article', '_type': 'article', '_id': '2', '_score': 1.0,
'_source': {}},
{'text': 'tomato', '_index': 'test_article', '_type': 'article', '_id': '4', '_score': 1.0,
'_source': {}}]}]
}
}
"""
发现,removed为True的tom_cat赫然在列,明明加了
'query': {
'term': {
'removed': False
}
}
却没有起作用,难道elasticsearch不支持这种需求!?怎么可能……
查阅文档发现解决方法为https://www.elastic.co/guide/en/elasticsearch/reference/5.0/suggester-context.html
找到问题所在,首先改造mapping,并重新录入测试数据如下
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from elasticsearch.helpers import bulk
from elasticsearch import Elasticsearch ES_HOSTS = [{'host': 'localhost', 'port': 9200}, ] ES = Elasticsearch(hosts=ES_HOSTS) INDEX = 'test_article'
TYPE = 'article' ARTICLE = {
'properties': {
'id': {
'type': 'integer',
'index': 'not_analyzed'
},
'author': {
'type': 'text',
},
'author_completion': {
'type': 'completion',
'contexts': [ # 这里是关键所在
{
'name': 'removed_tab',
'type': 'category',
'path': 'removed'
}
]
},
'removed': {
'type': 'boolean',
}
}
} MAPPINGS = {
'mappings': {
'article': ARTICLE,
}
} def create_index():
"""
插入数据前创建对应的index
"""
ES.indices.delete(index=INDEX, ignore=404)
ES.indices.create(index=INDEX, body=MAPPINGS) def insert_data():
"""
添加测试数据
:return:
"""
test_datas = [
{
'id': 1,
'author': 'tom',
'author_completion': 'tom',
'removed': False
},
{
'id': 2,
'author': 'tom_cat',
'author_completion': 'tom_cat',
'removed': True
},
{
'id': 3,
'author': 'kitty',
'author_completion': 'kitty',
'removed': False
},
{
'id': 4,
'author': 'tomato',
'author_completion': 'tomato',
'removed': False
},
]
bulk_data = []
for data in test_datas:
action = {
'_index': INDEX,
'_type': TYPE,
'_id': data.get('id'),
'_source': data
}
bulk_data.append(action) success, failed = bulk(client=ES, actions=bulk_data, stats_only=True) print('success', success, 'failed', failed) if __name__ == '__main__':
create_index()
insert_data()
Duang!意想不到的问题出现了
elasticsearch.helpers.BulkIndexError: ('4 document(s) failed to index.', [{'index': {'_index': 'test_article', '_type': 'article', '_id': '1', 'status': 400, 'error': {'type': 'illegal_argument_exception', 'reason': 'Failed to parse context field [removed], only keyword and text fields are accepted'}, 'data': {'id': 1, 'author': 'tom', 'author_completion': 'tom', 'removed': False}}}, {'index': {'_index': 'test_article', '_type': 'article', '_id': '2', 'status': 400, 'error': {'type': 'illegal_argument_exception', 'reason': 'Failed to parse context field [removed], only keyword and text fields are accepted'}, 'data': {'id': 2, 'author': 'tom_cat', 'author_completion': 'tom_cat', 'removed': True}}}, {'index': {'_index': 'test_article', '_type': 'article', '_id': '3', 'status': 400, 'error': {'type': 'illegal_argument_exception', 'reason': 'Failed to parse context field [removed], only keyword and text fields are accepted'}, 'data': {'id': 3, 'author': 'kitty', 'author_completion': 'kitty', 'removed': False}}}, {'index': {'_index': 'test_article', '_type': 'article', '_id': '4', 'status': 400, 'error': {'type': 'illegal_argument_exception', 'reason': 'Failed to parse context field [removed], only keyword and text fields are accepted'}, 'data': {'id': 4, 'author': 'tomato', 'author_completion': 'tomato', 'removed': False}}}])
意思是context只支持keyword和text类型,而上面removed类型为boolean,好吧,再改造mapping,将mapping的removed改为keyword类型……
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from elasticsearch.helpers import bulk
from elasticsearch import Elasticsearch ES_HOSTS = [{'host': 'localhost', 'port': 9200}, ] ES = Elasticsearch(hosts=ES_HOSTS) INDEX = 'test_article'
TYPE = 'article' ARTICLE = {
'properties': {
'id': {
'type': 'integer',
'index': 'not_analyzed'
},
'author': {
'type': 'text',
},
'author_completion': {
'type': 'completion',
'contexts': [ # 这里是关键所在
{
'name': 'removed_tab',
'type': 'category',
'path': 'removed'
}
]
},
'removed': {
'type': 'keyword',
}
}
} MAPPINGS = {
'mappings': {
'article': ARTICLE,
}
} def create_index():
"""
插入数据前创建对应的index
"""
ES.indices.delete(index=INDEX, ignore=404)
ES.indices.create(index=INDEX, body=MAPPINGS) def insert_data():
"""
添加测试数据
:return:
"""
test_datas = [
{
'id': 1,
'author': 'tom',
'author_completion': 'tom',
'removed': 'False'
},
{
'id': 2,
'author': 'tom_cat',
'author_completion': 'tom_cat',
'removed': 'True'
},
{
'id': 3,
'author': 'kitty',
'author_completion': 'kitty',
'removed': 'False'
},
{
'id': 4,
'author': 'tomato',
'author_completion': 'tomato',
'removed': 'False'
},
]
bulk_data = []
for data in test_datas:
action = {
'_index': INDEX,
'_type': TYPE,
'_id': data.get('id'),
'_source': data
}
bulk_data.append(action) success, failed = bulk(client=ES, actions=bulk_data, stats_only=True) print('success', success, 'failed', failed) if __name__ == '__main__':
create_index()
insert_data()
mission success。看看表结构ok
接下来就是获取补全建议
def get_suggestions(keywords):
body = {
'size': 0,
'_source': 'suggest',
'suggest': {
'author_prefix_suggest': {
'prefix': keywords,
'completion': {
'field': 'author_completion',
'size': 10,
'contexts': {
'removed_tab': ['False', ] # 筛选removed为'False'的补全,contexts不能包含多个tab,如加上一个'state_tab':['1',]的话contexts将失效
}
}
}
},
}
suggest_data = ES.search(index=INDEX, doc_type=TYPE, body=body)
return suggest_data if __name__ == '__main__':
# create_index()
# insert_data()
suggestions = get_suggestions('t')
print(suggestions) """
suggestions = {
'took': 0,
'timed_out': False,
'_shards': {
'total': 5,
'successful': 5,
'skipped': 0, 'failed': 0
},
'hits': {
'total': 0,
'max_score': 0.0,
'hits': []
},
'suggest': {
'author_prefix_suggest': [
{'text': 't', 'offset': 0, 'length': 1, 'options': [
{'text': 'tom', '_index': 'test_article', '_type': 'article', '_id': '1', '_score': 1.0,
'_source': {},
'contexts': {'removed_tab': ['False']}},
{'text': 'tomato', '_index': 'test_article', '_type': 'article', '_id': '4', '_score': 1.0,
'_source': {},
'contexts': {'removed_tab': ['False']}}]}]}}
"""
发现,removed为'True'的tom_cat被筛选掉了,大功告成!
elasticsearch补全功能之只补全筛选后的部分数据context suggester的更多相关文章
- CentOS 5 全功能WWW服务器搭建全教程 V3.0
http://hx100.blog.51cto.com/44326/339949/ 一.基本系统安装1.下载CentOS 5我是下载的DVD版本,大家也可以下载服务器CD安装版本,其实都差不多.大家可 ...
- Eclipse自动补全功能轻松设置 || 不需要修改编辑任何文件
本文介绍如何设置Eclipse代码自动补全功能.轻松实现输入任意字母均可出现代码补全提示框. Eclipse代码自动补全功能默认只包括 点"." ,即只有输入”." ...
- Eclipse使用技巧 - 2. Eclipse自动补全功能轻松设置
本文介绍如何设置Eclipse代码自动补全功能.轻松实现输入任意字母均可出现代码补全提示框. Eclipse代码自动补全功能默认只包括 点”.” ,即只有输入”.”后才出现自动补全的提示框.想要自动补 ...
- 转:Eclipse自动补全功能轻松设置
Eclipse自动补全功能轻松设置 || 不需要修改编辑任何文件 2012-03-08 21:29:02| 分类: Java | 标签:eclipse 自动补全 设置 |举报|字号 订阅 ...
- vim基础学习之自动补全功能
本章我们学习自动补全功能1.自动补全优先从当前的编辑区获得补全列表例如:我们写下如下内容 aaaaa aabbb aaab 当我们再次输入aa,然后我们按下Tab的时候,会弹出一个包含 aaaaa a ...
- 第三百六十八节,Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)用Django实现搜索的自动补全功能
第三百六十八节,Python分布式爬虫打造搜索引擎Scrapy精讲—用Django实现搜索的自动补全功能 elasticsearch(搜索引擎)提供了自动补全接口 官方说明:https://www.e ...
- 四十七 Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)用Django实现搜索的自动补全功能
elasticsearch(搜索引擎)提供了自动补全接口 官方说明:https://www.elastic.co/guide/en/elasticsearch/reference/current/se ...
- java整合Elasticsearch,实现crud以及高级查询的分页,范围,排序功能,泰文分词器的使用,分组,最大,最小,平均值,以及自动补全功能
//为index创建mapping,index相当于mysql的数据库,数据库里的表也要给各个字段创建类型,所以index也要给字段事先设置好类型: 使用postMan或者其他工具创建:(此处我使用p ...
- linux命令补全 忘记命令只记得开头
linux的shell不仅提供上下箭头来翻阅历史使用过的命令,还提供命令补全功能. 例如,你想创建一个文件夹,只记得是m开头的命令,此时可以: ①输入m ②按键盘上的Tab键两次 (有可能还出现这句话 ...
随机推荐
- qml demo分析(text-字体展示)
上一篇文章分析了一个小游戏,使用qml编写界面+js进行复杂逻辑控制,算是一个比较完整的qml示例代码了,今天就不那么继续变态啦,来看一个简单的字体示例程序吧,该示例代码比较简单,主要是展示了几个简单 ...
- nodejs 开发企业微信第三方应用入门教程
最近公司要开发企业微信端的 Worktile,以前做的是企业微信内部应用,所以只适用于私有部署客户,而对于公有云客户就无法使用,所有就准备开发企业微信的第三方应用,本文主要介绍在调研阶段遇到的山珍海味 ...
- 使用EOLINKER做接口测试最佳路径(上)
本文内容: 测试脚本管理:讲述如何在 EOLINKER 上设计测试项目目录结构. 编写测试脚本:讲述如何在 EOLINKER 上编写接口测试脚本. 测试脚本执行及报告:讲述如何在 EOLINKER 上 ...
- 使用nginx搭建高可用,高并发的wcf集群
很多情况下基于wcf的复杂均衡都首选zookeeper,这样可以拥有更好的控制粒度,但zk对C# 不大友好,实现起来相对来说比较麻烦,实际情况下,如果 你的负载机制粒度很粗糙的话,优先使用nginx就 ...
- 详解TypScript数据类型转换
最近在用TypeScript(后面简称TS),发现TS虽然语法和C#差不多但是在很多地方还是不够高级(和C#相比),这里主要聚焦在数据类型强转上面,直接看下面案例吧 string转number 案例如 ...
- foreach(Element elem in selections.Elements)无法实现
因为版本问题 原:foreach(Element elem in selections.Elements){ } 现: ElementSet selections = new ElementSet() ...
- 前端入门21-JavaScript的ES6新特性
声明 本篇内容全部摘自阮一峰的:ECMAScript 6 入门 阮一峰的这本书,我个人觉得写得挺好的,不管是描述方面,还是例子,都讲得挺通俗易懂,每个新特性基本都还会跟 ES5 旧标准做比较,说明为什 ...
- Numpy库的学习(五)
今天继续学习一下Numpy库,废话不多说,整起走 先说下Numpy中,经常会犯错的地方,就是数据的复制 这个问题不仅仅是在numpy中有,其他地方也同样会出现 import numpy as np a ...
- SQL常用语句(二)
--语 句 功 能--数据操作SELECT --从数据库表中检索数据行和列INSERT --向数据库表添加新数据行DELETE --从数据库表中删除数据行UPDATE --更新数据库表中的数据 --数 ...
- Go 反射
基本了解 在Go语言中,大多数时候值/类型/函数非常直接,要的话,定义一个.你想要个Struct type Foo struct { A int B string } 你想要一个值,你定义出来 var ...