全文检索 Elasticsearch

Elasticsearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java语言开发的,并作为Apache许可条款下的开放源码发布,是一种流行的企业级搜索引擎。Elasticsearch用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。官方客户端在Java、.NET(C#)、PHP、Python、Apache Groovy、Ruby和许多其他语言中都是可用的。
1.安装Elasticsearch
1.1 下载Elasticsearch安装包
下载地址 https://www.elastic.co/cn/downloads/elasticsearch
在这里插入图片描述
Elasticsearch是用Java语言开发的,需具有java的运行环境,确定已安装了jdk
elasticsearch-7.8.0\config\elasticsearch.yml(ES 配置文件)
2. 启动Elasticsearch
解压后,进入elasticsearch-7.8.0\bin目录下
在这里插入图片描述
启动成功后,就可以操作了(以python为例)
3. 操作Elasticsearch
python安装elasticsearch的依赖
python -m pip install elasticsearch

代码示例

from datetime import datetime
from elasticsearch import Elasticsearch
es = Elasticsearch(["127.0.0.1:9200"])

doc = {
    'author': 'kangkang',
    'text': 'Elasticsearch: cool. bonsai cool.',
    'timestamp': datetime.now(),
}

res = es.index(index="test-index2", id=2, body=doc)
print(res["result"])

res = es.get(index="test-index2", id=2)
print(res['_source'])

# es.indices.refresh(index="test-index")

res = es.search(index="test-index2", body={"query": {"match_all": {}}})

print("Got %d Hits:" % res['hits']['total']['value'])
for hit in res['hits']['hits']:
    print("%(timestamp)s %(author)s: %(text)s" % hit["_source"])

执行后的结果
在这里插入图片描述
需要更为详细的说明,可参考
https://elasticsearch-py.readthedocs.io/en/master/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值