【资料图】
Elasticsearch是一个基于Lucene的搜索引擎。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful 的API接口。Elasticsearch是用Java语言开发的,并作为Apache许可条款下的开放源码发布,是非常流行的企业级搜索引擎。官方支持的客户端语言包括Java、.NET(C#)、PHP、Python、Apache Groovy、Ruby等。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr,而Solr也是基于Lucene开发的。
Elasticsearch的安装方式有许多,官方也特别希望能够在公有云上部署。本文选择最简单的方式,直接在自己掌握的主机(ip:172.29.30.155)上安装。其安装过程如下所述:
# 这个安装过程也有可能非常慢。wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo gpg --dearmor -o /usr/share/keyrings/elasticsearch-keyring.gpgsudo apt-get install apt-transport-httpsecho "deb [signed-by=/usr/share/keyrings/elasticsearch-keyring.gpg] https://artifacts.elastic.co/packages/8.x/apt stable main" | sudo tee /etc/apt/sources.list.d/elastic-8.x.listsudo apt-get update && sudo apt-get install -y elasticsearch
另一个简单的办法就是直接下载安装包。从官网上下载:
# 在ubuntu bionic目标机的终端下wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-8.3.2-amd64.debsudo dpkg -i elasticsearch-8.3.2-amd64.deb
这种方式的好处是可以复制deb文件以多个计算机上,从而节省下载时间。需要安装的目标计算机越多,这种方式越合算。
在ubuntu bionic下,可以使用systemd对其进行管理。相关命令如下:
sudo /bin/systemctl daemon-reload# 自动启动sudo /bin/systemctl enable elasticsearch# 启动sudo systemctl start elasticsearch# 查看状态sudo systemctl status elasticsearch# 如果出现错误,可以查看日志。journalctl -fjournalctl -u elasticsearch# 停止sudo systemctl stop elasticsearch# 重置口令,人工指定/usr/share/elasticsearch/bin/elasticsearch-reset-password -u elastic -i# 重置口令,自动生成/usr/share/elasticsearch/bin/elasticsearch-reset-password -u elastic# 测试之curl --cacert /etc/elasticsearch/certs/http_ca.crt -u elastic https://localhost:9200curl --cacert /etc/elasticsearch/certs/http_ca.crt -u elastic https://172.29.30.155:9200
获得的响应类似下列信息:
{ "name" : "dbservers", "cluster_name" : "elasticsearch", "cluster_uuid" : "LFs6cpSHTSqLqbx6lRgkvw", "version" : { "number" : "8.3.2", "build_type" : "deb", "build_hash" : "8b0b1f23fbebecc3c88e4464319dea8989f374fd", "build_date" : "2022-07-06T15:15:15.901688194Z", "build_snapshot" : false, "lucene_version" : "9.2.0", "minimum_wire_compatibility_version" : "7.17.0", "minimum_index_compatibility_version" : "7.0.0" }, "tagline" : "You Know, for Search"}
Elasticsearch的功能非常复杂,需要下功夫学习,本文只从python的角度来使用这个工具。官方推荐的模块安装如下:
pip install elasticsearch# 为了能够完成安全验证,需要下载相关的证书到本地scp root@172.29.30.155:/etc/elasticsearch/certs/http_ca.crt .
完成后,以下代码简单示例了如何插入记录:
from elasticsearch import Elasticsearchfrom datetime import datetimeserverip = "172.29.30.155"cafile = r"d:\http_ca.crt"ELASTIC_PASSWORD = "88488848"indexname = "poetry"index = 0def connect(): client = Elasticsearch( f"https://{serverip}:9200", ca_certs=cafile, basic_auth=("elastic", ELASTIC_PASSWORD)) return clientdef docgen(author, content): doc = {"author": author, "text": content, "timestamp": datetime.now(), } return docdef insert(con, id, doc): resp = con.index(index=indexname, id=id, document=doc) return resp["result"]def getbyindex(con, id): resp = con.get(index=indexname, id=id) return resp["_source"]def list(con): resp = con.search(index=indexname, query={"match_all": {}}) print("Got %d Hits:" % resp["hits"]["total"]["value"]) for hit in resp["hits"]["hits"]: print("%(timestamp)s %(author)s: %(text)s" % hit["_source"])def search(con, str): resp = con.search(index=indexname, query={"match": {"text": str}}) print("Got %d Hits:" % resp["hits"]["total"]["value"]) for hit in resp["hits"]["hits"]: print("%(timestamp)s %(author)s: %(text)s" % hit["_source"])# 连接con = connect()# 插入记录index += 1doc = docgen("李白", "天生我才必有用")print(insert(con, index, doc))index += 1doc = docgen("杜甫", "功盖三分国,名成八阵图,江流石不转,遗恨失吞吴")print(insert(con, index, doc))# 准确获得记录print(getbyindex(con, 1))# 列出所有记录list(con)# 使用搜索功能,找到相关记录search(con, "天生")
上述代码只是简单地插入了2条记录。真正要发挥作用搜索引擎的能力,必须要将大量的信息导入,同时也要建设集群系统,这部分的内容请阅读官网相关资料,本文不再重复。
关键词:
Python工具箱系列(三十二)
2023-05-15每日热议!2023年5月15日LME锌库存报49050吨
2023-05-15瑞安市湖岭镇中心幼儿园_关于瑞安市湖岭镇中心幼儿园介绍 天天观天下
2023-05-15买车刷的信用卡后悔了?是真的吗?
2023-05-15天天讯息:全面恢复口岸快捷通关 出入境证件可“全国通办”
2023-05-15天天热议:音乐剧如何更好地走进小学课堂?杭州这场活动寻找答案
2023-05-15中粮科技:阿洛酮糖获批成为食品加工用酶制剂
2023-05-15m4a格式怎么转换成mp3_怎样将m4a转换成mp3-环球播资讯
2023-05-15自动变速器启动的步骤和操作方法-天天精选
2023-05-15合肥南二环计划向西延伸10公里 力争对接上方兴大道 当前热点
2023-05-15Python工具箱系列(三十二)
2023-05-15每日热议!2023年5月15日LME锌库存报49050吨
2023-05-15瑞安市湖岭镇中心幼儿园_关于瑞安市湖岭镇中心幼儿园介绍 天天观天下
2023-05-15买车刷的信用卡后悔了?是真的吗?
2023-05-15天天讯息:全面恢复口岸快捷通关 出入境证件可“全国通办”
2023-05-15天天热议:音乐剧如何更好地走进小学课堂?杭州这场活动寻找答案
2023-05-15中粮科技:阿洛酮糖获批成为食品加工用酶制剂
2023-05-15m4a格式怎么转换成mp3_怎样将m4a转换成mp3-环球播资讯
2023-05-15自动变速器启动的步骤和操作方法-天天精选
2023-05-15合肥南二环计划向西延伸10公里 力争对接上方兴大道 当前热点
2023-05-15Copyright 2015-2022 南方超市网版权所有 备案号:粤ICP备18023326号-21 联系邮箱:855 729 8@qq.com