ElasticSearch的python使用--pyes
问题描述: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的增删改?
?????????????????? 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(绿色部分替换成关键词,红色部分替换站内地址)