文章目录
一、什么是Elasticsearch ?
想查数据就免不了搜索,搜索就离不开搜索引擎,百度、谷歌都是一个非常庞大复杂的搜索引擎,他们几乎索引了互联网上开放的所有网页和数据。然而对于我们自己的业务数据来说,肯定就没必要用这么复杂的技术了,如果我们想实现自己的搜索引擎,方便存储和检索,Elasticsearch 就是不二选择,它是一个全文搜索引擎,可以快速地储存、搜索和分析海量数据。
二、为什么使用Elasticsearch ?
Elasticsearch 是一个开源的搜索引擎,建立在一个全文搜索引擎库 Apache Lucene™ 基础之上。 那 Lucene 又是什么?Lucene 可能是目前存在的,不论开源还是私有的,拥有最先进,高性能和全功能搜索引擎功能的库,但也仅仅只是一个库。要用上 Lucene,我们需要编写 Java 并引用 Lucene 包才可以,而且我们需要对信息检索有一定程度的理解才能明白 Lucene 是怎么工作的,反正用起来没那么简单。 那么为了解决这个问题,Elasticsearch 就诞生了。Elasticsearch 也是使用 Java 编写的,它的内部使用 Lucene 做索引与搜索,但是它的目标是使全文检索变得简单,相当于 Lucene 的一层封装,它提供了一套简单一致的 RESTful API 来帮助我们实现存储和检索。 所以 Elasticsearch 仅仅就是一个简易版的 Lucene 封装吗?那就大错特错了,Elasticsearch 不仅仅是 Lucene,并且也不仅仅只是一个全文搜索引擎。 它可以被下面这样准确的形容:
- 一个分布式的实时文档存储,每个字段可以被索引与搜索
- 一个分布式实时分析搜索引擎
- 能胜任上百个服务节点的扩展,并支持 PB 级别的结构化或者非结构化数据
三、Windows如何安装Elasticsearch
这里比较推荐的是使用docker desktop 几行命令,即可安装成功,相比于下载压缩包再解压之类方便不少。
检验Docker desktop是否安装成功
先打开cmd命令终端输入:
docker --version
检验是否真正的安装了docker,如果安装了但是在cmd输入命令没有显示,大概率是没有配置docker的环境变量
Docker安装 ES
拉取 ES的镜像
docker pull elasticsearch:6.6.2
使用以下命令在后台启动Elasticsearch容器,并将虚拟机的9200端口映射到Elasticsearch的9200端口
docker run -d -p 9200:9200 --name=es elasticsearch:6.6.2
使用以下命令拉取Elasticsearch-head镜像:
docker pull mobz/elasticsearch-head:5
使用以下命令在后台启动Elasticsearch-head容器,并将容器的9100端口映射到主机的9100端口:
docker run -d -p 9100:9100 --name=es-head mobz/elasticsearch-head:5
检验ES是否安装成功
在浏览器网址中输入
localhost:9100
点击连接检测是否成功,如果点击没有反应的话
请打开Docker Desktop
需要修改配置的文件路径:
/usr/share/elasticsearch/config/elasticsearch.yml
编辑该文件,在最后加入:
http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-headers: "*"
镜像重启后刷新网页是否成功
四、如何使用Elasticsearch
Python 测试Elasticsearch是否可以连接成功
pip install elasticsearch
from elasticsearch import Elasticsearch
# 创建一个连接到本地ElasticSearch实例的客户端
es = Elasticsearch([{'host': 'localhost', 'port': 9200}]) # es = Elasticsearch()
print(es.ping()) # True 是连接成功的
如果连接失败,返回的是False,请根据以下方法进行改正:
pip show elasticsearch
展示ES的版本号,如果版本大于7.14之类的,重新下载ES的Py包,具体原因还未知
pip install elastcisearch==7.13.0
索引一个文档
doc = {
'author': 'Min Guang',
'text': 'Elasticsearch: cool. bonsai cool.',
'timestamp': '2024-07-13'
}
将文档索引到名为 “test-index” 的索引中,并指定文档ID为 1
res = es.index(index="test-index", id=1, document=doc)
print(res['result'])
根据文档ID检索文档
res = es.get(index="test-index", id=1)
print(res['_source'])
更新文档
update_doc = {
'doc': {
'text': 'Elasticsearch: amazing. bonsai amazing.'
}
}
res = es.update(index="test-index", id=1, body=update_doc)
print(res['result'])
删除文档
res = es.delete(index="test-index", id=1)
print(res['result'])
批量索引多个文档
from elasticsearch import helpers
actions = [
{
'_index': 'test-index',
'_id': 2,
'_source': {
'author': 'Jane Doe',
'text': 'Elasticsearch: fantastic.',
'timestamp': '2024-07-13'
}
},
{
'_index': 'test-index',
'_id': 3,
'_source': {
'author': 'Mike Smith',
'text': 'Elasticsearch: superb.',
'timestamp': '2024-07-13'
}
}
]
helpers.bulk(es, actions)
基本搜索查询
query = {
'query': {
'match': {
'text': 'Elasticsearch'
}
}
}
res = es.search(index="test-index", body=query)
for hit in res['hits']['hits']:
print(hit['_source'])
基本聚合查询
query = {
'size': 0,
'aggs': {
'all_authors': {
'terms': {
'field': 'author.keyword'
}
}
}
}
res = es.search(index="test-index", body=query)
print(res['aggregations']['all_authors']['buckets'])
创建一个新索引
index_settings = {
'settings': {
'number_of_shards': 1,
'number_of_replicas': 0
},
'mappings': {
'properties': {
'author': {'type': 'text'},
'text': {'type': 'text'},
'timestamp': {'type': 'date'}
}
}
}
res = es.indices.create(index='test-index', body=index_settings)
print(res['acknowledged'])
删除一个索引
res = es.indices.delete(index='test-index')
print(res['acknowledged'])
获取索引信息
res = es.indices.get(index='test-index')
print(res)
在与 ElasticSearch 交互时,可能会遇到各种错误。例如,尝试检索不存在的文档或索引会引发异常。可以使用 try-except 块来处理这些错误:
from elasticsearch.exceptions import NotFoundError
try:
res = es.get(index="test-index", id=1)
print(res['_source'])
except NotFoundError:
print("Document not found.")