Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4447039
  • 博文数量: 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-02-28 00:19:06

文章来源:http://834945712.iteye.com/blog/1915432

问题描述: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. 测试使用或者的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. 测试使用的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(绿色部分替换成关键词,红色部分替换站内地址)

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