pyes-elasticsearch的python客户端使用笔记
elasticsearch入门:
一.重要的概念
http://834945712.iteye.com/blog/1915432 这篇文章很多类比做的很好,便于快速理解pyes的结构
http://blog.plotcup.com/a/106 很清晰的示例代码
1. 使用pip install pyes 或者 easy_install pyes安装pye
2. 测试使用pyes官方文档或者其他pyes文档的API的增删改
import pyes
conn = pyes.ES('127.0.0.1:9200')
conn.create_index("human") #human 是一个新的索引库,相当于create database操作
mapping = {u'firstname': {'index': 'analyzed', #使用分词器
'type': u'string',
'analyzer':'ik'}, #分词器为ik
u'lastname': {'index': 'not_analyzed',
'type': u'string'},
u'age': {'index': 'not_analyzed', #不使用分词器
'type': u'long'}} #mapping 是字段,相当于数据库的表的列名
conn.put_mapping("man", {'properties':mapping}, ["human"]) #在human库中创建man,相当于create table操作
conn.put_mapping("woman", {'properties':mapping}, ["human"]) #woman同样相当于一张表
conn.index({'firstname':'David', 'lastname':'White', 'age':18}, 'human', 'man', 'David White', True) #向human的man中添加索引数据,相当于insert into操作
conn.index({'firstname':'Suzan', 'lastname':'Black', 'age':28}, 'human', 'woman', 'Suzan Black', True) #向human的woman中添加索引数据
conn.index({'firstname':'Uni', 'lastname':'Lavender', 'age':18}, 'human', 'man', 'Uni Lavender', True)
conn.index({'firstname':'Jann', 'lastname':'White', 'age':18}, 'human', 'woman', 'Jann White', True)
conn.index({'firstname':'Suzan', 'lastname':'White', 'age':18}, 'human', 'woman', 'Suzan White', True) #注意第四个参数是index的id,具有唯一性,因此更新数据,可以按照id使用index即可
18 conn.index({'firstname':'Jann', 'lastname':'White', 'age':28}, 'human', 'woman', 'Jann White', True) #将年龄由18更新到28
3. 测试使用pyes官方文档的API的查询
使用res = conn.search(pyes.BoolQuery(must=must), 'human', 'woman', start=0, size=10, sort='age')查询,支持分页
a. 查找firstname为Suzan的女人的index数据
条件:must = pyes.StringQuery('Suzan', ['firstname',]) #must = pyes.StringQuery('Suzan', 'firstname')
相当于sql查询 select * from human.woman where firstname regexp '[^a-zA-Z]Suzan[^a-zA-Z]'
b. 查找lastname为white的女人的index数据
条件:must = pyes.StringQuery('White', ['lastname',]) #must = pyes.StringQuery('White', ['lastname',])或者must = pyes.TermQuery('lastname', 'White')
相当于sql查询 select * from human.woman where lastname = 'White'
c. 查找age为18,20,28的女人的index数据
条件:must = pyes.TermsQuery('age', [18,28])
相当于sql查询 select * from human.woman where age=18 or age = 28
d. 查找age为18,28并且firstname为Suzan的女人的index数据
条件:must = [pyes.TermsQuery('age', [18,28]), pyes.StringQuery('Suzan', 'firstname')]
相当于sql查询 select * from human.woman where (age=18 or age = 28) and firstname = 'Suzan'
e. 查找firstname或者lastname中出现Rich单词的女人的index数据
条件:must = pyes.StringQuery('Suzan', ['firstname',‘lastname’], default_operator='or')
相当于sql查询 select * from human.woman where firstname regexp '[^a-zA-Z]Suzan[^a-zA-Z]' or lastname regexp '[^a-zA-Z]Suzan[^a-zA-Z]'
f. 查找firstname并且lastname中出现Rich单词的女人的index数据
条件:must = pyes.StringQuery('Suzan', ['firstname',‘lastname’], default_operator='and')
相当于sql查询 select * from human.woman where firstname regexp '[^a-zA-Z]Suzan[^a-zA-Z]' and lastname regexp '[^a-zA-Z]Suzan[^a-zA-Z]'
g. 查找年龄在18到28之间的女人的index数据
条件:must = pyes.RangeQuery(pyes.ESRange('age', from_value=18, to_value=28))
相当于sql查询 select * from human.woman where age between 18 and 28]
h. 查找以Whi开头的lastname的女人的index数据
条件:must = pyes.PrefixQuery('lastname', 'Whi')
相当于sql查询 select * from human.woman where lastname like 'Whi%'
文章末尾:
这个很好用还是
搜索小知识:可以借助百度实现站内全文搜索,将http://www.baidu.com/s?wd=网络中心&pn=10&ct=2097152&ie=utf-8&si=www.stcsm.gov.cn&format=json(绿色部分替换成关键词,红色部分替换站内地址)
二.好用的工具
linux下的es命令行工具,es2unix
这个工具挺好用,不过使用时候遇到一些问题,
1.es2unix find or load main class
2./bin/sh: 1: lein: not found
了解到一个好不从的工具,Leiningen教程中文版
3.Clojure
Clojure(发音类似"closure")[2]是一套现代的Lisp语言的动态语言版。它是一个函数式多用途的语言。
http://zh.wikipedia.org/zh-cn/Clojure
pyes-elasticsearch的python客户端使用笔记的更多相关文章
- c#实例化继承类,必须对被继承类的程序集做引用 .net core Redis分布式缓存客户端实现逻辑分析及示例demo 数据库笔记之索引和事务 centos 7下安装python 3.6笔记 你大波哥~ C#开源框架(转载) JSON C# Class Generator ---由json字符串生成C#实体类的工具
c#实例化继承类,必须对被继承类的程序集做引用 0x00 问题 类型“Model.NewModel”在未被引用的程序集中定义.必须添加对程序集“Model, Version=1.0.0.0, Cu ...
- 【原】Learning Spark (Python版) 学习笔记(三)----工作原理、调优与Spark SQL
周末的任务是更新Learning Spark系列第三篇,以为自己写不完了,但为了改正拖延症,还是得完成给自己定的任务啊 = =.这三章主要讲Spark的运行过程(本地+集群),性能调优以及Spark ...
- python核心编程--笔记
python核心编程--笔记 的解释器options: 1.1 –d 提供调试输出 1.2 –O 生成优化的字节码(生成.pyo文件) 1.3 –S 不导入site模块以在启动时查找pyt ...
- 《简明python教程》笔记一
读<简明Python教程>笔记: 本书的官方网站是www.byteofpython.info 安装就不说了,网上很多,这里就记录下我在安装时的问题,首先到python官网下载,选好安装路 ...
- python 正则使用笔记
python正则使用笔记 def remove_br(content): """去除两边换行符""" content = content.r ...
- Redis的Python客户端redis-py的初步使用
1. Redis的安装 sudo pip install redis sudo pip install hiredis Parser可以控制如何解析redis响应的内容.redis-py包含两个Par ...
- python 库安装笔记
python 库安装笔记 zoerywzhou@163.com http://www.cnblogs.com/swje/ 作者:Zhouwan 2017-2-22 友情提示 安装python库的过程中 ...
- Python Click 学习笔记(转)
原文链接:Python Click 学习笔记 Click 是 Flask 的团队 pallets 开发的优秀开源项目,它为命令行工具的开发封装了大量方法,使开发者只需要专注于功能实现.恰好我最近在开发 ...
- thrift例子:python客户端/java服务端
java服务端的代码请看上文. 1.说明: 这两篇文章其实解决的问题是,当使用python去访问大数据线上集群的时候,遇到两个问题: 1)python-hadoop和python-hive相关包链接不 ...
随机推荐
- PHP获取IP所在地区(转)
1.获取IP地址的API新浪的IP地址查询接口:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js新浪多地域测试方法:http:/ ...
- mysql监控管理工具--innotop
https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/innotop/innotop-1.9. ...
- maven跳过单元测试
24.跳过单元测试 <plugin><groupId>org.apache.maven.plugins</groupId><artifactId>mav ...
- mysql左外连接,右外连接,全连接
- Velocity 模板引擎介绍
一.变量 1. 变量定义 #set($name =“velocity”) 2. 变量的使用 在模板文件中使用$name 或者${name} 来使用定义的变量.推荐使用${name} 这种格式,因为在模 ...
- JVM内存模型以及垃圾收集策略解析
http://xmuzyq.iteye.com/blog/599750 一 JVM内存模型 1.1 Java栈 Java栈是与每一个线程关联的,JVM在创建每一个线程的时候,会分配一定的栈空间给线程. ...
- Timestamp的作用及与字符串的相互转换 .
一.Timestamp的介绍 每一个数据库都有一个计数器,这个计数器记录着数据行的插入.更新行为.如果我们为一个表中增加 timestamp 列,那么,该列将记录每一个数据行的计数器值.假如数据库中当 ...
- ios Objective-C的动态特性
这是一篇译文,原文在此,上一篇文章就是受这篇文章启发,这次干脆都翻译过来. 过去的几年中涌现了大量的Objective-C开发者.有些是从动态语言转过来的,比如Ruby或Python,有些是从强类型语 ...
- oracle输出多行多列数据
--方法一 匿名块中直接 dbms_output输出declare v_sql varchar2(200); v_cursor sys_refcursor; type v_type is ...
- C语言-求1-20的阶乘的和(函数的递归)
// // main.c // C语言 // // Created by wanghy on 15/9/5. // Copyright (c) 2015年 wanghy. All rights ...