python中使用elasticsearch做为搜索引擎

#coding:utf-8

import pyes

conn = pyes.ES(['127.0.0.1:9200'])#连接es

conn.create_index('test-index')#新建一个索引

#定义索引存储结构
mapping = { u'parsedtext': {'boost': 1.0,
                      'index': 'analyzed',
                      'store': 'yes',
                      'type': u'string',
                      "term_vector" : "with_positions_offsets"},
              u'name': {'boost': 1.0,
                         'index': 'analyzed',
                         'store': 'yes',
                         'type': u'string',
                         "term_vector" : "with_positions_offsets"},
              u'title': {'boost': 1.0,
                         'index': 'analyzed',
                         'store': 'yes',
                         'type': u'string',
                         "term_vector" : "with_positions_offsets"},
              u'position': {'store': 'yes',
                         'type': u'integer'},
              u'uuid': {'boost': 1.0,
                        'index': 'not_analyzed',
                        'store': 'yes',
                        'type': u'string'}
        }

conn.put_mapping("test-type", {'properties':mapping}, ["test-index"])#定义test-type
conn.put_mapping("test-type2", {"_parent" : {"type" : "test-type"}}, ["test-index"])#从test-type继承

#插入索引数据
#{"name":"Joe Tester", "parsedtext":"Joe Testere nice guy", "uuid":"11111", "position":1}: 文档数据
#test-index:索引名称
#test-type: 类型
#1: id 注:id可以不给,系统会自动生成
conn.index({"name":"Joe Tester", "parsedtext":"Joe Testere nice guy", "uuid":"11111", "position":1}, "test-index", "test-type", 1)

conn.index({"name":"data1", "value":"value1"}, "test-index", "test-type2", 1, parent=1)
conn.index({"name":"Bill Baloney", "parsedtext":"Bill Testere nice guy", "uuid":"22222", "position":2}, "test-index", "test-type", 2)
conn.index({"name":"data2", "value":"value2"}, "test-index", "test-type2", 2, parent=2)
conn.index({"name":u"百 度 中 国"}, "test-index", "test-type")#这个相当于中文的一元切分吧-_-
conn.index({"name":u"百 中 度"}, "test-index", "test-type")

conn.default_indices=["test-index"]#设置默认的索引
conn.refresh()#刷新以获得最新插入的文档

q = pyes.TermQuery("name", "bill")#查询name中包含bill的记录
results = conn.search(q)

for r in results:
    print r

#查询name中包含 百度 的数据
q = pyes.StringQuery(u"百 度",'name')
results = conn.search(q)

for r in results:
    print r

#查询name中包含 百度 或着 中度 的数据
q = pyes.StringQuery(u"百 度 OR 中 度",'name')
results = conn.search(q)

for r in results:
    print r

#MatchAllQuery进行全搜索
q = pyes.<span style="font-family: Arial, Helvetica, sans-serif;">MatchAllQuery()</span><pre name="code" class="python">
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值