pyes-elasticsearch的python客戶端使用筆記


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 import pyes
 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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM