目录
2. 安装 ElasticSearch-Head 主要是为了可视化我们存储的数据
4. 安装python对接ElasticSearch对接的工具包(前提是你得有个正常的python环境)
注意: 在安装ElasticSearch之前 得有一个Java的环境, NodeJs环境。这里的JDK要求1.8以上
一: 软件安装
1. 安装ElasticSearch
https://www.elastic.co/cn/downloads/elasticsearch 点开链接 下载对应版本的软件。我现在使用的是7.8.0版本
解压以后,进入ElasticSearch/bin/ 然后点击elasticsearch.bat启动 启动之后你可以在浏览器上访问
http://localhost:9200/ 看到以下东西 说明目前为止 环境都没有问题
2. 安装 ElasticSearch-Head 主要是为了可视化看到我们存储的数据
如果有git 可以直接git clone https://github.com/mobz/elasticsearch-head/ 没有git的,直接去下载压缩包
下载完解压 就是上面的样子,然后在当前目录下启动dos(命令行) 输入以下命令npm install 进行安装 , 安装完成后 使用npm run start 启动
这里会设计一个跨域的问题,也就是你的 elasticsearch-head连不上你的elasticsearch. 解决方式
在elasticsearch/config/elasticsearch.yml 中的最后面加上
http.cors.enabled: true
http.cors.allow-origin: "*"
重新启动就好了。 这里需要启动elasticsearch和elasticsearch-head。
访问: http://localhost:9100/ 即可看到以下画面
3. 安装Kibana
注意: 这里的Kibana和ElasticSearch的版本一定要对上 不然会出错。
https://www.elastic.co/cn/downloads/kibana 点开链接 下载对应的版本即可。
启动Kibana: 同理 解压 然后点击Kibana/bin/kibana.bat文件 即可启动。
然后在浏览器访问http://localhost:5601 即可看到以下界面 有人想汉化的话 可以修改Kibana的配置文件
4. 安装python对接ElasticSearch对接的工具包(前提是你得有个正常的python环境)
pip install elasticsearch # 命令行直接输入 即可安装
5. 安装IK分词器插件 后面会用到
https://github.com/medcl/elasticsearch-analysis-ik/releases 点击链接 直接下载,这里也得注意版本对应哦
下载完后, 解压 然后放到ElasticSearch/plugins下。这里需重新建一个ik文件夹 然后把所有文件放进去。
二: python对接ElasticSearch案例
以下的操作 每执行一步 都可以打开ElasticSearch-head去查看(浏览器输入对应端口)
1. 创建索引
from elasticsearch import Elasticsearch
import json
# 创建索引index
es = Elasticsearch()
result = es.indices.create(index='news', ignore=400)
print(result)
# {'acknowledged': True, 'shards_acknowledged': True, 'index': 'news'}
# ignore标记成400 的意思是我们忽略状态码为400的错误
# acknowledged字段显示True 表示索引创建成功
2. 删除索引
# 删除索引index
es = Elasticsearch()
result = es.indices.delete(index='news', ignore=[400, 404])
print(result)
3. 插入数据
# 插入数据
from elasticsearch import Elasticsearch
es = Elasticsearch()
es.indices.create(index='news', ignore=400) # 创建索引
data = {
'title': '美国留给伊拉克的是个烂摊子吗',
'url': 'http://view.news.qq.com/zt2011/usa_iraq/index.htm'
}
result = es.create(index='news', doc_type='politics', id=1, body=data) # 插入数据
print(result)
4. 更新数据
# 更新数据
from elasticsearch import Elasticsearch
es = Elasticsearch()
data = {
'title': '美国留给伊拉克的是个烂摊子吗',
'url': 'http://view.news.qq.com/zt2001/usa_iraq/index.htm',
'date': '2011-12-16'
}
es.index(index='news', doc_type='politics', body=data, id=1)
5. 删除数据
# 删除数据
from elasticsearch import Elasticsearch
es = Elasticsearch()
result = es.delete(index='news', doc_type='politics', id=1)
print(result)
6. 查询数据
这里需要安装ik分词器 所以 务必把前面的安装步骤走完
首先mapping 指定分词器。 然后删除数据 最后在查询
from elasticsearch import Elasticsearch
es = Elasticsearch()
mapping = {
'properties': {
'title': {
'type': 'text',
'analyzer': 'ik_max_word',
'search_analyzer': 'ik_max_word'
}
}
}
# 删除索引
# es.indices.delete(index='news', ignore=[400, 404])
# 创建索引
es.indices.create(index='news', ignore=400)
result = es.indices.put_mapping(index='news', doc_type='politics', body=mapping, include_type_name=True)
datas = [
{
'title': '美国留给伊拉克的是个烂摊子吗',
'url': 'http://view.news.qq.com/zt2011/usa_iraq/index.htm',
'date': '2011-12-16'
},
{
'title': '公安部:各地校车将享最高路权',
'url': 'http://www.chinanews.com/gn/2011/12-16/3536077.shtml',
'date': '2011-12-16'
},
{
'title': '中韩渔警冲突调查:韩警平均每天扣1艘中国渔船',
'url': 'https://news.qq.com/a/20111216/001044.htm',
'date': '2011-12-17'
},
{
'title': '中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首',
'url': 'http://news.ifeng.com/world/detail_2011_12/16/11372558_0.shtml',
'date': '2011-12-18'
}
]
for data in datas:
es.index(index='news', doc_type='politics', body=data)
# 这里我们指定了四条数据,都带有 title、url、date 字段,然后通过 index() 方法将其插入 Elasticsearch 中,索引名称为 news,类型为 politics。
# 查询一下
from elasticsearch import Elasticsearch
es = Elasticsearch()
result = es.search(index='news', doc_type='politics')
print(result)
# 可以看到返回结果会出现在 hits 字段里面,然后其中有 total 字段标明了查询的结果条目数,还有 max_score 代表了最大匹配分数。
# 另外我们还可以进行全文检索, 这才体现ElasticSearch搜索引擎特性的地方
# 使用 Elasticsearch 支持的 DSL 语句来进行查询, 使用 match 指定全文检索,检索的字段是 title
dsl = {
'query': {
'match': {
'title': '中国 领事馆'
}
}
}
es = Elasticsearch()
result = es.search(index='news', doc_type='politics', body=dsl)
print(json.dumps(result, indent=2, ensure_ascii=False))
参考文献:
Elasticsearch 基本介绍及其与 Python 的对接实现 https://blog.csdn.net/devcloud/article/details/91446259
狂神说 https://www.bilibili.com/video/BV17a4y1x7zq?from=search&seid=1857410452811541954