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

在python中使用elasticsearch做为搜索引擎,第1张

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

下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。

内存溢出小编现在分享给大家,也给大家做个参考。

一直想找一个快速全文搜索的工具,目前找到的有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-8import pyesconn = pyes.ES(['127.0.0.1:9200'])#连接esconn.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-typeconn.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","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

以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

总结

以上是内存溢出为你收集整理的在python中使用elasticsearch做为搜索引擎全部内容,希望文章能够帮你解决在python中使用elasticsearch做为搜索引擎所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1199055.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存