前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >在python中使用elasticsearch做为搜索引擎

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

原创
作者头像
好派笔记
修改2021-11-02 13:48:44
5740
修改2021-11-02 13:48:44
举报
文章被收录于专栏:好派笔记好派笔记

一直想找一个快速全文搜索的工具,目前找到的有Sphinx,xapian,Lucene,solr, elasticsearch ,whoosh,hyper estraier等,原本一直不太喜欢用java系的,内存大户伤不起啊。尝试了sphinx,xapian,hyper estraier,其中xapian资料太少,hyper estraier虽然比较简单,但资料也少。sphinx到是有一个中文化的分支coreseek,然后看到文档里面提到sphinx支持一元切分,但根 据查询的例子去查的结果不是我想要的,不知道是不是我的查询语句用错了。而且因为我是在windows上测试的,而我的python又是2.7的版本,无 法在 coreseek 上直接使用,应该需要重新编译。后来看到 elasticsearch ,真是亮瞎老夫的狗眼啊,这货直接可以用restful json操作又有pyes,pyelasticsearch这些已经封装好的操作库。 elasticsearch 还是支持分布式,扩展也方便了。由于是java开发的,跨平台也无问题,默认单机尝试的时候无须改配置,直接运行 bin/elasticsearch.bat 就可以了。

安装pyes

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,

'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

#查询name中包含 百度 的数据

q = pyes.StringQuery(u"百 度",'name')

results = conn.search(q)

for r in results:

print

#查询name中包含 百度 或着 中度 的数据

q = pyes.StringQuery(u"百 度 OR 中 度",'name')

results = conn.search(q)

for r in results:

print r

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
Elasticsearch Service
腾讯云 Elasticsearch Service(ES)是云端全托管海量数据检索分析服务,拥有高性能自研内核,集成X-Pack。ES 支持通过自治索引、存算分离、集群巡检等特性轻松管理集群,也支持免运维、自动弹性、按需使用的 Serverless 模式。使用 ES 您可以高效构建信息检索、日志分析、运维监控等服务,它独特的向量检索还可助您构建基于语义、图像的AI深度应用。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档