python连接es_Elasticsearch --- 3. ik中文分词器, python操作es
一.IK中文分词器
1.下载安装
2.测试
#显示结果
{"tokens": [
{"token" : "上海","start_offset" : 0,"end_offset" : 2,"type" : "CN_WORD","position" : 0},
{"token" : "自来水","start_offset" : 2,"end_offset" : 5,"type" : "CN_WORD","position" : 1},
{"token" : "自来","start_offset" : 2,"end_offset" : 4,"type" : "CN_WORD","position" : 2},
{"token" : "水","start_offset" : 4,"end_offset" : 5,"type" : "CN_CHAR","position" : 3},
{"token" : "来自","start_offset" : 5,"end_offset" : 7,"type" : "CN_WORD","position" : 4},
{"token" : "海上","start_offset" : 7,"end_offset" : 9,"type" : "CN_WORD","position" : 5}
]
}
二. Ik 分词器的基本操作
1.ik_max_word(最细粒度的拆分)
#建立索引
PUT ik1
{"mappings": {"doc": {"dynamic": false,"properties": {"content": {"type": "text","analyzer": "ik_max_word"}
}
}
}
}
#添加数据
PUT ik1/doc/1{"content":"今天是个好日子"}
PUT ik1/doc/2{"content":"心想的事儿都能成"}
PUT ik1/doc/3{"content":"我今天不活了"}
开始查询
GET ik1/_search
{"query": {"match": {"content": "心想"}
}
}
显示结果
{"took" : 1,"timed_out" : false,"_shards": {"total" : 5,"successful" : 5,"skipped" : 0,"failed" : 0},"hits": {"total" : 1,"max_score" : 0.2876821,"hits": [
{"_index" : "ik1","_type" : "doc","_id" : "2","_score" : 0.2876821,"_source": {"content" : "心想的事儿都能成"}
}
]
}
}
2.ik_smart(最粗粒度的拆分)
①以最粗粒度拆分
GET _analyze
{"analyzer": "ik_smart","text": "今天是个好日子"}
结果是:
{"tokens": [
{"token" : "今天是","start_offset" : 0,"end_offset" : 3,"type" : "CN_WORD","position" : 0},
{"token" : "个","start_offset" : 3,"end_offset" : 4,"type" : "CN_CHAR","position" : 1},
{"token" : "好日子","start_offset" : 4,"end_offset" : 7,"type" : "CN_WORD","position" : 2}
]
}
②以最细粒度拆分文档
GET _analyze
{"analyzer": "ik_max_word","text": "今天是个好日子"}
结果是:
{"tokens": [
{"token" : "今天是","start_offset" : 0,"end_offset" : 3,"type" : "CN_WORD","position" : 0},
{"token" : "今天","start_offset" : 0,"end_offset" : 2,"type" : "CN_WORD","position" : 1},
{"token" : "是","start_offset" : 2,"end_offset" : 3,"type" : "CN_CHAR","position" : 2},
{"token" : "个","start_offset" : 3,"end_offset" : 4,"type" : "CN_CHAR","position" : 3},
{"token" : "好日子","start_offset" : 4,"end_offset" : 7,"type" : "CN_WORD","position" : 4},
{"token" : "日子","start_offset" : 5,"end_offset" : 7,"type" : "CN_WORD","position" : 5}
]
}
3.短语查询(即match_phrase)
GET ik1/_search
{"query": {"match_phrase": {"content": "今天"}
}
}
4.短语前缀查询(match_phrase_prefix)
GET ik1/_search
{"query": {"match_phrase_prefix": {"content": {"query": "今天好日子","slop": 2}
}
}
}
三.python操作elasticsearch
1.安装elasticsearch模块
pip install elasticsearch
# 豆瓣源
pip install-i https://pypi.doubanio.com/simple/ elasticsearch
2.连接
fromelasticsearch import Elasticsearch
# es=Elasticsearch() # 默认连接本地elasticsearch
# es= Elasticsearch(['127.0.0.1:9200']) # 连接本地9200端口
es=Elasticsearch(
["192.168.1.10", "192.168.1.11", "192.168.1.12"], # 连接集群,以列表的形式存放各节点的IP地址
sniff_on_start=True, # 连接前测试
sniff_on_connection_fail=True, # 节点无响应时刷新节点
sniff_timeout=60# 设置超时时间
)
配置忽略响应状态码
es = Elasticsearch(['127.0.0.1:9200'],ignore=400) # 忽略返回的400状态码
es= Elasticsearch(['127.0.0.1:9200'],ignore=[400, 405, 502]) # 以列表的形式忽略多个状态码
3.常用的连接方式
fromelasticsearch import Elasticsearch
es=Elasticsearch() # 默认连接本地elasticsearch
#创建
print(es.index(index='py2', doc_type='doc', id=1, body={'name': "张开", "age": 18}))
#查询指定文档
print(es.get(index='py2', doc_type='doc', id=1))
4.结果过滤
filter_path参数用于减少elasticsearch返回的响应
还支持*通配符以匹配字段名称、任何字段或者字段部分:
①
print(es.search(index='py2', filter_path=['hits.total', 'hits.hits._source']))
# 可以省略type类型
print(es.search(index='w2', doc_type='doc')) # 可以指定type类型
print(es.search(index='w2', doc_type='doc', filter_path=['hits.total']))
②
print(es.search(index='py2', filter_path=['hits.*']))
print(es.search(index='py2', filter_path=['hits.hits._*']))
print(es.search(index='py2', filter_path=['hits.to*'])) # 仅返回响应数据的total
print(es.search(index='w2', doc_type='doc', filter_path=['hits.hits._*']))
# 可以加上可选的type类型
5.基本操作
①es.index,向指定索引添加或更新文档,如果索引不存在,首先会创建该索引,然后再执行添加或者更新操作。
# print(es.index(index='w2', doc_type='doc', id='4', body={"name":"可可", "age": 18})) # 正常
# print(es.index(index='w2', doc_type='doc', id=5, body={"name":"卡卡西", "age":22})) # 正常
# print(es.index(index='w2', id=6, body={"name": "鸣人", "age": 22})) # 会报错,TypeError: index() missing 1 required positional argument: 'doc_type'print(es.index(index='w2', doc_type='doc', body={"name": "鸣人", "age": 22})) # 可以不指定id,默认生成一个id
② es.get,查询索引中指定文档。
print(es.get(index='w2', doc_type='doc', id=5)) # 正常
print(es.get(index='w2', doc_type='doc')) # TypeError: get() missing 1 required positional argument: 'id'print(es.get(index='w2', id=5)) # TypeError: get() missing 1 required positional argument: 'doc_type'
③es.search,执行搜索查询并获取与查询匹配的搜索匹配。这个用的最多,可以跟复杂的查询条件。
index要搜索的以逗号分隔的索引名称列表; 使用_all 或空字符串对所有索引执行操作。
doc_type 要搜索的以逗号分隔的文档类型列表; 留空以对所有类型执行操作。
body 使用Query DSL(QueryDomain Specific Language查询表达式)的搜索定义。
_source 返回_source字段的true或false,或返回的字段列表,返回指定字段。
_source_exclude要从返回的_source字段中排除的字段列表,返回的所有字段中,排除哪些字段。
_source_include从_source字段中提取和返回的字段列表,跟_source差不多。
print(es.search(index='py3', doc_type='doc', body={"query": {"match":{"age": 20}}})) # 一般查询
print(es.search(index='py3', doc_type='doc', body={"query": {"match":{"age": 19}}},_source=['name', 'age'])) # 结果字段过滤
print(es.search(index='py3', doc_type='doc', body={"query": {"match":{"age": 19}}},_source_exclude =[ 'age']))
print(es.search(index='py3', doc_type='doc', body={"query": {"match":{"age": 19}}},_source_include =[ 'age']))
④ es.get_source,通过索引、类型和ID获取文档的来源,其实,直接返回想要的字典。
print(es.get_source(index='py3', doc_type='doc', id='1')) # {'name': '王五', 'age': 19}
⑤es.count,执行查询并获取该查询的匹配数。比如查询年龄是18的文档。
body ={"query": {"match": {"age": 18}
}
}
print(es.count(index='py2', doc_type='doc', body=body))
# {'count': 1, '_shards': {'total': 5, 'successful': 5, 'skipped': 0, 'failed': 0}}
print(es.count(index='py2', doc_type='doc', body=body)['count'])
# 1print(es.count(index='w2'))
# {'count': 6, '_shards': {'total': 5, 'successful': 5, 'skipped': 0, 'failed': 0}}
print(es.count(index='w2', doc_type='doc'))
# {'count': 6, '_shards': {'total': 5, 'successful': 5, 'skipped': 0, 'failed':
⑥es.delete,删除指定的文档。比如删除文章id为4的文档,但不能删除仅只删除索引,
如果想要删除索引,还需要es.indices.delete来处理
print(es.delete(index='py3', doc_type='doc', id='4'))
⑦ es.delete_by_query,删除与查询匹配的所有文档。
index 要搜索的以逗号分隔的索引名称列表; 使用_all 或空字符串对所有索引执行操作。
doc_type 要搜索的以逗号分隔的文档类型列表; 留空以对所有类型执行操作。
body使用Query DSL的搜索定义。
print(es.delete_by_query(index='py3', doc_type='doc', body={"query": {"match":{"age": 20}}}))
⑧es.exists,查询elasticsearch中是否存在指定的文档,返回一个布尔值。
print(es.exists(index='py3', doc_type='doc', id='1'))
⑨es.info,获取当前集群的基本信息。
print(es.info())
⑩ es.ping,如果群集已启动,则返回True,否则返回False。
print(es.ping())
6.Indices(es.indices )
① es.indices.create,在Elasticsearch中创建索引,用的最多。
比如创建一个严格模式、有4个字段、并为title字段指定ik_max_word查询粒度的mappings。
并应用到py4索引中。这也是常用的创建自定义索引的方式。
body ={"mappings": {"doc": {"dynamic": "strict","properties": {"title": {"type": "text","analyzer": "ik_max_word"},"url": {"type": "text"},"action_type": {"type": "text"},"content": {"type": "text"}
}
}
}
}
es.indices.create('py4', body=body)
② es.indices.delete,在Elasticsearch中删除索引
print(es.indices.delete(index='py4'))
print(es.indices.delete(index='w3')) # {'acknowledged': True}
③ es.indices.put_alias,为一个或多个索引创建别名,查询多个索引的时候,可以使用这个别名。
index 别名应指向的逗号分隔的索引名称列表(支持通配符),使用_all对所有索引执行操作。
name要创建或更新的别名的名称。
body别名的设置,例如路由或过滤器。
print(es.indices.put_alias(index='py4', name='py4_alias')) # 为单个索引创建别名
print(es.indices.put_alias(index=['py3', 'py2'], name='py23_alias')) # 为多个索引创建同一个别名,联查用
④es.indices.delete_alias,删除一个或多个别名。
print(es.indices.delete_alias(index='alias1'))
print(es.indices.delete_alias(index=['alias1, alias2']))
以下查看详细
Cluster(集群相关)
Node(节点相关)
Cat(一种查询方式)
Snapshot(快照相关)
Task(任务相关)
python连接es_Elasticsearch --- 3. ik中文分词器, python操作es相关推荐
- ES7 IK中文分词器
IK中文分词器的安装 ES默认是没有IK中文分词器的,我们要将IK中文分词器作为一个插件安装到ES中,安装的步骤也很简单: 从GitHub上下载适合自己ES版本的IK中文分词器,地址如下:https: ...
- 使用Docker快速安装部署ES和Kibana并配置IK中文分词器以及自定义分词拓展词库
使用Docker快速安装部署ES和Kibana的前提:首先需要确保已经安装了Docker环境 如果没有安装Docker的话,可以参考上一篇的内容:Linux上安装Docker 有了Docker环境后, ...
- 30彻底掌握IK中文分词_上机动手实战IK中文分词器的安装和使用
之前大家会发现,字段全部是用英文 其实我们用来进行搜索的,绝大多数,都是中文应用,很少做英文的 默认分词器standard:没有办法对中文进行合理分词的,只是将每个中文字符一个一个的切割开来,比如说中 ...
- 学习 ES 的笔记、全文检索、倒排索引、Lucene、ik中文分词器、Kibana使用Dev Tools
文章目录 感悟 新接触的单词 知识点一:ES是什么? 知识点二:ES基本概念 知识点三:1.1 什么是全文检索和Lucene? 知识点四:1.2 什么是倒排索引,Lucene实现全文检索的流程是怎样? ...
- Solr7.2.1环境搭建和配置ik中文分词器
solr7.2.1环境搭建和配置ik中文分词器 安装环境:Jdk 1.8. windows 10 安装包准备: solr 各种版本集合下载:http://archive.apache.org/dist ...
- Kafka:ZK+Kafka+Spark Streaming集群环境搭建(十九)ES6.2.2 安装Ik中文分词器
注: elasticsearch 版本6.2.2 1)集群模式,则每个节点都需要安装ik分词,安装插件完毕后需要重启服务,创建mapping前如果有机器未安装分词,则可能该索引可能为RED,需要删除后 ...
- 分词器以及ik中文分词器
文章目录 分词器以及ik中文分词器 概念 ik分词器的安装 环境准备 设置jdk环境变量 下载maven安装包并解压 设置path 验证maven是否安装成功 下载IK分词器并安装 使用IK分词器 查 ...
- Elasticsearch:IK 中文分词器
Elasticsearch 内置的分词器对中文不友好,只会一个字一个字的分,无法形成词语,比如: POST /_analyze {"text": "我爱北京天安门&quo ...
- 玩转ES,一文教你掌握IK中文分词器
前言 ES默认的分词器对中文分词并不友好,所以我们一般会安装中文分词插件,以便能更好的支持中文分词检索. 而ES的中文分词器中,最流行的必然是IK分词器. 一.IK分词器介绍 IK分词器在是一款基于词 ...
最新文章
- 学习 Message(17): WM_SYSCOMMAND 消息[一]
- linux 信号 core,Shell 信号发送与捕捉
- kaggle较好的竞赛经验
- 编程语言注释方法大全
- Android 核心分析之十二Android GEWS窗口管理之基本架构原理
- C语言经典面试题目(转的,不过写的的确好!)
- php 异常 重试,Python中异常重试的解决方案详解
- leetcode第 46 场双周赛
- Promise--优雅的异步回调解决方案
- 如何更好的使用大数据
- 小知识 | 谈谈 损失函数, 成本函数, 目标函数 的区别
- 和别人老公上床后的感觉~
- 有效集法(Active Set),内点法(Interior-Point)及序列二次规划(SQP)法
- 圆通JAVA职级_圆通小编升职啦,快看看你的职级有没有变化
- jmeter学习问题记录
- git bash安装SDKman遇到需要安装zip的问题
- linux 登陆 历史,linux记录每次登陆的历史命令
- UE使用技巧之---去空格
- 换硬币将一笔零钱换成5分、2分和1分的硬币,要求每种硬币至少有一枚,有几种不同的换法?
- 问题: Schrodinger方程的非齐次Strichartz估计
热门文章
- android社会化分享
- Android学习之Android studio篇-Android Studio快捷键总结(mac)
- POJ 1067 取石子游戏
- Codeforces Round #345 (Div. 2)
- http://blog.csdn.net/itplus/article/details/10088625
- 【学习】程序员技术练级攻略
- 第十二章:二叉查找树(1)
- 网站安全编程 黑客入侵 脚本黑客 高级语法入侵 C/C++ C# PHP JSP 编程
- jsp 防止sql注入 之 preparestatement篇(转载)
- C# 依据鼠标坐标取网页内成员坐标.ie