Elasticsearch入门教程
环境安装
Java
下载地址
- 推荐安装Java 1.8,安装路径不要带中文字符,空格。
Java Archive Downloads - Java SE 8u211 and later | Oracle 中国(windows)
How To Install Java with Apt-Get on Debian 8 | DigitalOcean(debian)
配置环境变量
参考Java 开发环境配置 | 菜鸟教程 (runoob.com)
Elasticsearch
简介
开源的全文搜索引擎,底层是开源库Lucene。对搜索引擎的操作封装成了RESTful的API,通过http请求就能对其进行操作。
与关系库对比
| 数据库 | ES |
|---|---|
| 库(database) | 索引(index) |
| 表(table) | 类型(type) |
| 记录(record) | 文档(document,使用json格式) |
| 字段(column) | 属性(property) |
下载地址
Download Elasticsearch | Elastic
注:如需使用ik,根据ik最新版本下载es。详见本文中Elasticsearch入门教程
验证安装成功
- 启动ELASTICSEARCH
# 以windows为例,在bin目录下执行
elasticsearch.bat
- 在浏览器中输入http://localhost:9200
- 返回以下信息则成功:
{
"name" : "EIYA",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "RGPJxZnBRlu4Y09pkE0exQ",
"version" : {
"number" : "8.7.1",
"build_flavor" : "default",
"build_type" : "zip",
"build_hash" : "f229ed3f893a515d590d0f39b05f68913e2d9b53",
"build_date" : "2023-04-27T04:33:42.127815583Z",
"build_snapshot" : false,
"lucene_version" : "9.5.0",
"minimum_wire_compatibility_version" : "7.17.0",
"minimum_index_compatibility_version" : "7.0.0"
},
"tagline" : "You Know, for Search"
}
- 地址访问不了:原因是是因为开启了ssl认证。 可在config/elasticsearch.yml中关闭该配置。(本教程为测试环境,在生产运行环境下应考虑安全性,下同。)
xpack.security.transport.ssl:
enabled: false
- 需要密码验证:可在config/elasticsearch.yml中关闭该配置。
# Enable security features
xpack.security.enabled: false
kibana
简介
Kibana 是一个对 Elasticsearch 数据进行可视化的用户界面。详见本文中Elasticsearch入门教程
下载地址
Download Kibana Free | Get Started Now | Elastic
验证安装成功:
- 启动kibana
# 以windows为例,在bin目录下执行
kibana.bat
- 在浏览器输入http://localhost:5601
- 页面转到Home - Elastic安装成功
ik
简介
ik分词器是一个中文分词插件包。
下载地址
Release v8.7.0 · medcl/elasticsearch-analysis-ik (github.com)
- 下载release版本。注意ik与es版本号,需完全一致。
- 安装目录:解压在elasticsearch/plugins/下
验证安装成功
- 在kibana dev tools页面输入
GET /_analyze
{
"analyzer": "ik_max_word",
"text":"我爱你中国"
}
- 正确中文分词,则成功
{
"tokens": [
{
"token": "我爱你",
"start_offset": 0,
"end_offset": 3,
"type": "CN_WORD",
"position": 0
},
{
"token": "爱你",
"start_offset": 1,
"end_offset": 3,
"type": "CN_WORD",
"position": 1
},
{
"token": "中国",
"start_offset": 3,
"end_offset": 5,
"type": "CN_WORD",
"position": 2
}
]
}
基本用法
可在kibana里练习。
增加记录
PUT test1/_doc/1
{
"name":"kitty",
"age":12
}
查找某一条记录
GET test1/_doc/
结果如下:
{
"_index": "test1",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1
}
查找所有记录
GET test01/_search
{
"query": {
"match_all": {}
}
}
更新记录
语法同增加记录
PUT test1/_doc/1
{
"name":"kitty",
"age":13
}
查看更新结果
GET test1/_doc/
对比观察插入和更新结果的"result"和"_version"属性
{
"_index": "test1",
"_id": "1",
"_version": 2,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 1,
"_primary_term": 1
}
查看所有索引
GET _cat/indices?v
删除索引
DELETE /test1
Python操作
安装elasticsearch包
在命令行输入
pip install elasticsearch
示例程序
from elasticsearch import Elasticsearch
# 连接
# 参数是列表,可连接多个地址
es = Elasticsearch(['http://127.0.0.1:9200'])
# 添加索引
es.indices.create(index="test01")
# 删除索引
es.indices.delete(index="test01")
# 增加记录
# 原始插入,相当于
# PUT test1/_doc/1
# {
# "name":"kitty",
# "age":12
# }
ret = es.index(index="test01", id=1, document={"name": "kitty", "age": 12})
print(ret['result'])
# 不指定id生成uuid,相当于POST,而不是PUT
ret = es.index(index="test01", document={"name": "tom", "age": 5})
print(ret['result'])
# 查找一条记录
# 相当于GET test1/_doc/1
ret = es.get(index="test01", id=1)
print(ret)
结果如下:
{
'_index': 'test01',
'_id': '1',
'_version': 3,
'_seq_no': 2,
'_primary_term': 1,
'found': True,
'_source':{
'name': 'kitty',
'age': 12}
}
扩展阅读
Python Elasticsearch Client ‒ Python Elasticsearch client 8.7.1 documentation

浙公网安备 33010602011771号