加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

在python中使用elasticsearch做为搜索引擎

发布时间:2020-12-17 17:22:26 所属栏目:Python 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 一直想找一个快速全文搜索的工具,目前找到的有Sphinx,xapian,Lucene,solr,elasticsearch,whoosh,hyper estraier等,原本一直不太喜欢用java系的,内

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

一直想找一个快速全文搜索的工具,目前找到的有Sphinx,xapian,Lucene,solr,elasticsearch,whoosh,hyper estraier等,原本一直不太喜欢用java系的,内存大户伤不起啊。尝试了sphinx,hyper estraier,其中xapian资料太少,hyper estraier虽然比较简单,但资料也少。sphinx到是有一个中文化的分支coreseek,然后看到文档里面提到sphinx支持一元切分,但根 据查询的例子去查的结果不是我想要的,不知道是不是我的查询语句用错了。而且因为我是在windows上测试的,而我的python又是2.7的版本,无 法在 coreseek 上直接使用,应该需要重新编译。后来看到 elasticsearch ,真是亮瞎老夫的狗眼啊,这货直接可以用restful json操作又有pyes,pyelasticsearch这些已经封装好的操作库。 elasticsearch 还是支持分布式,扩展也方便了。由于是java开发的,跨平台也无问题,默认单机尝试的时候无须改配置,直接运行 bin/elasticsearch.bat 就可以了。
pip install pyes
#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,u'title': {'boost': 1.0,u'position': {'store': 'yes','type': u'integer'},u'uuid': {'boost': 1.0,'index': 'not_analyzed','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":"22222","position":1}: 文档数据
#test-index:索引名称
#test-type: 类型
#1: id 注:id可以不给,系统会自动生成
conn.index({"name":"Joe Tester","position":1},"test-index","test-type",1)

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

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读