首页 > 编程语言 > 详细

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

时间:2014-03-19 23:53:07      阅读:522      评论:0      收藏:0      [点我收藏+]

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

bubuko.com,布布扣
 1 import pyes
 2 conn = pyes.ES(127.0.0.1:9200)
 3 conn.create_index("human") #human 是一个新的索引库,相当于create database操作
 4 mapping = {ufirstname: {index: analyzed, #使用分词器
 5                          type: ustring,
 6                          analyzer:ik}, #分词器为ik
 7            ulastname: {index: not_analyzed,
 8                         type: ustring},
 9            uage: {index: not_analyzed, #不使用分词器
10                    type: ulong}} #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
bubuko.com,布布扣

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客户端使用笔记,布布扣,bubuko.com

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

原文:http://www.cnblogs.com/Tigerlee/p/3611995.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!