设为首页 收藏本站
查看: 724|回复: 0

[经验分享] ElasticSearch的python使用--pyes

[复制链接]

尚未签到

发表于 2017-4-29 11:56:58 | 显示全部楼层 |阅读模式
  问题描述: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%'  (未完待续.....)
  搜索小知识:可以借助百度实现站内全文搜索,将http://www.baidu.com/s?wd=网络中心&pn=10&ct=2097152&ie=utf-8&si=www.stcsm.gov.cn&format=json(绿色部分替换成关键词,红色部分替换站内地址)

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-370754-1-1.html 上篇帖子: GPU, Python and Maya 下篇帖子: python序列学习-基本操作
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表