Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4530120
  • 博文数量: 1214
  • 博客积分: 13195
  • 博客等级: 上将
  • 技术积分: 9105
  • 用 户 组: 普通用户
  • 注册时间: 2007-01-19 14:41
个人简介

C++,python,热爱算法和机器学习

文章分类

全部博文(1214)

文章存档

2021年(13)

2020年(49)

2019年(14)

2018年(27)

2017年(69)

2016年(100)

2015年(106)

2014年(240)

2013年(5)

2012年(193)

2011年(155)

2010年(93)

2009年(62)

2008年(51)

2007年(37)

分类: Python/Ruby

2014-03-02 07:23:29

文章来源:

問題描述:ElasticSearch是一個基於Lucene構建的開源,分散式,RESTful搜索引擎。設計用於雲計算中,能夠達到實時搜索,穩定,可靠,快速,安裝使用方便。支持通過HTTP使用JSON進行數據索引。在工作中的後台網站要提供基於ElasticSearch的後台服務,而後台的主要語言工具是python,操作ElasticSearch要用到pyes庫,就需要了解裡面的函數,英文API不好懂,個人根據理解做了些測試。

 

環境工具:python2.6  ElasticSearch0.90.2  pyes

 

解決過程: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即可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%'

(未完待續.....)

 

搜索小知識:可以藉助百度實現站內全文搜索,將網路中心&pn=10&ct=2097152&ie=utf-8&si=&format=json(綠色部分替換成關鍵詞,紅色部分替換站內地址)

阅读(4755) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~