pyes-elasticsearch的python客户端使用笔记

摘要:
弹性搜索入门:http://www.qwolf.com/?p=13871、重要概念http://834945712.iteye.com/blog/1915432本文中的许多类比都做得很好,便于快速理解pyes的结构http://blog.plotcup.com/a/106非常清晰的示例代码1.使用pipingsstallpyes或easy_Installpyes安装py2.使用

elasticsearch入门:

http://www.qwolf.com/?p=1387

一.重要的概念

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的增删改

1 importpyes
2 conn = pyes.ES('127.0.0.1:9200')
3 conn.create_index("human") #human 是一个新的索引库,相当于create database操作
4 mapping = {u'firstname': {'index': 'analyzed', #使用分词器
5                          'type': u'string',
6                          'analyzer':'ik'}, #分词器为ik
7            u'lastname': {'index': 'not_analyzed',
8                         'type': u'string'},
9            u'age': {'index': 'not_analyzed', #不使用分词器
10                    'type': u'long'}} #mapping 是字段,相当于数据库的表的列名
11 conn.put_mapping("man", {'properties':mapping}, ["human"]) #在human库中创建man,相当于create table操作
12 conn.put_mapping("woman", {'properties':mapping}, ["human"]) #woman同样相当于一张表
13 conn.index({'firstname':'David', 'lastname':'White', 'age':18}, 'human', 'man', 'David White', True) #向human的man中添加索引数据,相当于insert into操作
14 conn.index({'firstname':'Suzan', 'lastname':'Black', 'age':28}, 'human', 'woman', 'Suzan Black', True) #向human的woman中添加索引数据
15 conn.index({'firstname':'Uni', 'lastname':'Lavender', 'age':18}, 'human', 'man', 'Uni Lavender', True)
16 conn.index({'firstname':'Jann', 'lastname':'White', 'age':18}, 'human', 'woman', 'Jann White', True)
17 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客户端使用笔记》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇百度地图(23)-GL 地图属性Web墨卡托坐标与WGS84坐标互转下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Python web前端 06 运算符 循环

Python web前端 06 运算符 循环 一、运算符 #JS六大数据类型 #number 数字 #string 字符串 #boolean 布尔型 #function 函数 #undefined 未定义:一个变量声明之后没有复制就是未定义 #object 对象 在JS里面null属于对象类型,但是它不具有很多对象的共性,所以很多资料将他归为单...

centos7安装python 与卸载python

安装python 下载Python安装包 1 cd /usr/local/src 编译时要提前装好gcc编译器和zlib zlib-devel 1、下载文件 1 wget https://www.python.org/ftp/python/3.6.0/Python-3.6.0.tgz 2、解压 1 tar -zxvf Python-3.6.0...

ansible使用jinja2模板

jinja2基本语法 控制结构 {% %}             jinja2中的for循环用于迭代Python的数据类型,包括列表,元组和字典          2.变量取值 {{ }}             jinja2模板中使用 {{ }} 语法表示一个变量,它是一种特殊的占位符。当利用jinja2进行渲染的时候,它会把这些特殊的占位符进行填充/...

python 获取当天日期

取得时间相关的信息的话,要用到python time模块,python time模块里面有很多非常好用的功能,你可以去官方文档了解下,要取的当前时间的话,要取得当前时间的时间戳,时间戳好像是1970年到现在时间相隔的时间。你可以试下下面的方式来取得当前时间的时间戳:import timeprint time.time()输出的结果是:1357723206....

2.3、Python迭代器、列表解析及生成器(0530)

1、动态语言 sys.getrefcount()      //查看对象的引用计数 增加对象的引用计数场景 对象创建时:以赋值的方式,创建变量名的同时就会创建变量 将对象添加进容器时:类似list.append() 当对象被当作参数传递给函数时 多重目标赋值时:s1 = s2 = s3 = 'abc' 为对象创建另外的变量名 减少引用计数 引用此对象...

python websocket Django 实时消息推送

概述: WebSocket 是什么? WebSocket 是 HTML5 提供的一种浏览器与服务器间进行全双工通讯的协议。依靠这种协议可以实现客户端和服务器端 ,一次握手,双向实时通信。 WebSocket 服务端: 用的是 dwebsocket,安装命令pip installdwebsocket. WebSocket 基本方法: 1.request.i...