原文:http://tianyalong.icu/content.html?id=41
简介
elasticsearch是一个全文搜索引擎,提供大数据搜索、高亮显示等功能。
概念
DB | Elasticsearch |
---|---|
数据库(database) | 索引(index) |
表(tables) | 类型(types) |
行(rows) | 文档(documents) |
列(columns) | 字段(fields) |
docker安装es
- 拉取镜像
docker pull elasticsearch:7.16.3
- 创建容器
docker run --name elasticsearch -p 9200:9200 -p 9300:9300
-e "discovery.type=single-node" -e ES_JAVA_OPTS="-Xms64m -Xmx128m"
-v /home/tyl/elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
-v /home/tyl/elasticsearch/data:/usr/share/elasticsearch/data
-v /home/tyl/elasticsearch/plugins:/usr/share/elasticsearch/plugins
-itd elasticsearch:7.16.3
基本操作
索引
- 创建索引(PUT保证幂等性,POST不保证幂等性,只能使用PUT)
PUT http://8.142.97.150:9200/article
- 获取索引信息
GET http://8.142.97.150:9200/article
- 获取所有索引
GET http://8.142.97.150:9200/_cat/indices?v
- 删除索引
DELETE http://8.142.97.150:9200/article
- 索引应映射
PUT http://8.142.97.150:9200/article
{
"properties":{
"name":{
"type":"text", //可以分词查询
"index":true //建立索引
},
"sex":{
"type":"keyword", //不可以分词
"index":true
},
"tel":{
"type":"keyword",
"index":false //不支持索引查询
}
}
}
文章
- 插入文档(不能使用PUT,只能使用POST,除非自己指定1d)
POST http://8.142.97.150:9200/article/_doc
POST http://8.142.97.150:9200/article/_create
POST http://8.142.97.150:9200/article/_doc/1001(添加id 1001,可以使用PUT)
raw:
{
"title":"go语言高级编程》",
"start_time":"2022-1-10",
"ID":40
}
- 查询文档(请求里不能有body等其他数据)
GET http://8.142.97.150:9200/article/_doc/1001
- 查询所有文档
GET http://8.142.97.150:9200/article/_search
- 覆盖修改
PUT http://8.142.97.150:9200/article/_doc/1001
raw
{
"title":"go语言高级编程》",
"start_time":"2022-1-10",
"ID":41
}
- 部分修改(只能POST)
POST http://8.142.97.150:9200/article/_update/1001
{
"doc":{
"ID":40
}
}
- 删除文档
DELETE http://8.142.97.150:9200/article/_doc/1001
条件查询
- 精准匹配
GET http://8.142.97.150:9200/article/_search
{
"query":{
"match":{
"ID": 40
}
}
}
- 搜索全部
GET http://8.142.97.150:9200/article/_search
{
"query":{
"match_all":{
}
}
}
- 分页、筛选数据,并排序
GET http://8.142.97.150:9200/article/_search
{
"query":{
"match_all":{
}
},
"from":0,
"size":1,
"_source":["title"],
"sort":{
"price":{
"order":"desc"
}
}
}
- 交集and
GET http://8.142.97.150:9200/article/_search
{
"query":{
"bool":{
"must":[
{
"match":{
"title":"docker"
}
},
{
"match":{
"price":19999.00
}
}
]
}
}
}
- 并集or
{
GET http://8.142.97.150:9200/article/_search
{
"query":{
"bool":{
"should":[
{
"match":{
"title":"docker"
}
},
{
"match":{
"ID":40
}
}
]
}
}
}
- 范围查询(price[HTML_REMOVED]5000)
{
GET http://8.142.97.150:9200/article/_search
{
"query":{
"bool":{
"should":[
{
"match":{
"title":"docker"
}
},
{
"match":{
"ID":40
}
}
],
"filter":{
"range":{
"price":{
"gt":5000
}
}
}
}
}
}
- 全文检索
GET http://8.142.97.150:9200/article/_search
{
"query":{
"match":{
"title":"go语言"
}
}
}
- 完全匹配
GET http://8.142.97.150:9200/article/_search
{
"query":{
"match_phrase":{
"title":"go语言"
}
}
}
- 高亮显示
GET http://8.142.97.150:9200/article/_search
{
"query":{
"match":{
"title":"go语言"
}
},
"highlight":{
"fields":{
"title":{}
}
}
}
- 聚合查询
GET http://8.142.97.150:9200/article/_search
{
"aggs":{
"price_group":{ //求分组
"terms":{
"field":"price" 将价格一样的数据进行聚合,显示数量
}
}
},
"size":0 //不显示原始数据
}
{
"aggs":{
"price_avg":{ //求平均值
"terms":{
"field":"price" 将价格一样的数据进行聚合,显示数量
}
}
},
"size":0 //不显示原始数据
}