ElasticSearch7 基本操作

7 篇文章 0 订阅

相关内容:
ElasticSearch7 实现全文检索、关键词高亮

1. 基础操作;

1.1 索引创建;

# 非结构化创建:直接创建索引名称,mappings 为 {}

# 结构化创建:
# type 类型为 text:1. 会分词,然后进行索引,用于全文搜索 2. 支持模糊、精确查询 3. 不支持聚合
# type 类型为 keyword:1. 不进行分词,直接索引,用于关键词搜索 2. 支持模糊、精确查询 3. 支持聚合
curl -H 'Content-Type: application/json' \
-XPUT "http://localhost:9201/book/?pretty" -d '{
	"settings": {
		"refresh_interval": "20s",
		"number_of_shards": 1,
		"number_of_replicas": 0
	},
	"mappings": {
		"properties": {
			"title": {"type": "text"},
			"author": {"type": "keyword"},
			"word_count": {"type": "integer"},
			"publish_date": {"type": "date", "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"}
			"type": {"type": "text"}
		}
	}
}'

1.2 插入;

# 指定文档 id 插入
curl -H 'Content-Type: application/json' \
-XPUT "http://localhost:9201/book/_doc/1?pretty" -d '{
	"title": "xxx",
	"author": "unknown",
	"publish_date": "2019-12-13",
	"word_count": 10000
}'

# 自动产生文档 id 插入
curl -H 'Content-Type: application/json' \
-XPOST "http://localhost:9201/book/_doc?pretty" -d '{
	"title": "yyy",
	"author": "unknown",
	"publish_date": "2019-12-13",
	"word_count": 20000
}'

1.3 修改;

# 直接覆盖:同插入

# 修改指定文档
curl -H 'Content-Type: application/json' \
-XPOST "http://localhost:9201/book/_doc/1/_update?pretty" -d '{
	"doc" : {"title": "xxx1"}
}'

# 脚本修改文档
# 修改关键字:script
# 指定脚本语言 lang:内置 painless / js / python
# 指定脚本内容:inline
# es 上下文:ctx
# es 当前文档:_source
curl -H 'Content-Type: application/json' \
-XPOST 'http://localhost:9200/book/_doc/_update_by_query' -d '{
	"query": {
		"match": { "title": "xxx" }
	},
	"script": {
		"lang": "painless",
		// "inline": "ctx._source.word_count += 10",
		"inline": "ctx._source.word_count = params.word_count",
		"params": { "word_count" : 100000 }
	}
}'

1.4 删除;

# 删除文档
curl -H 'Content-Type: application/json' \
-XDELETE 'http://localhost:9200/book/_doc/1'

# 删除索引
curl -H 'Content-Type: application/json' \
-XDELETE 'http://localhost:9200/book'

# 删除字段
curl -H 'Content-Type: application/json' -u elastic -XPOST \ 'http://localhost:9200/book/_doc/_update_by_query?pretty' -d '{"script":{"lang":"painless","inline":"ctx._source.remove(\"word_count\")"}}

2. 查询;

2.1 简单查询、条件查询、聚合查询;

# 1. 简单查询
# 1.1 检索信息
curl -H 'Content-Type: application/json' \
-XGET 'http://localhost:9200/book/_doc/1?pretty'

# 1.2 普通查询、查询关键字
# 结尾使用关键字 _search 来取代原来的文档ID。
# 响应内容的 hits 数组中包含了我们所有的三个文档
# 默认情况下搜索会返回前 10 个结果
# 关键字查询、将查询语句传递给参数 q=
curl -H 'Content-Type: application/json' \
-XGET 'http://localhost:9200/book/_doc/_search?q=title:xxx&pretty'

# 2. 条件查询
# 使用 DSL 语句查询
# 2.1 查询全部数据
# size 返回几条数据
curl -H 'Content-Type: application/json' \
-XPOST 'http://localhost:9200/book/_doc/_search?pretty' -d '{
	"query": {
		"match_all": {}
	},
	"from": 1,	
	"size": 1
}'

# 2.2 关键词查询
# match 查询的字段匹配
# sort 根据字段自定义排序(指定排序规则,返回的 _score 为 null)
curl -H 'Content-Type: application/json' \
-XPOST 'http://localhost:9200/book/_doc/_search?pretty' -d '{
	"query": {
		"match": { "title" : "xxx" }
	},
	"sort": [
		{ "publish_date": { "order" : "desc" } }
	]
}'

# 3. 聚合查询
# 3.1 聚合名字自定义:group_by_word_count,可以多个分组聚合
# field 后面为聚合字段
curl -H 'Content-Type: application/json' \
-XPOST 'http://localhost:9200/book/_doc/_search?pretty' -d '{
	"aggs": {
		"group_by_word_count": {
			"term": {
				"field": "word_count"
			}
		},
		"group_by_publish_date" :{
			"term": {
				"field": "publish_date"
			}
		}
	}
}'

# 3.2 计算
# stats 关键字表示计算
curl -H 'Content-Type: application/json' \
-XPOST 'http://localhost:9200/book/_doc/_search?pretty' -d '{
	"aggs": {
		"grades_word_count": {
			"stats": {
				"field": "word_count"
			}
		}
	}
}'

2.2 高级查询;

2.2.1 子条件查询;

特定字段查询所指特定值,分为 Query context 和 Filter context

  • Query context:在查询过程中,除了判断文档是否满足查询条件外,ElasticSearch 还会计算一个 _score 来标识匹配的程度,旨在判断目标文档和查询条件匹配有多好。常用查询为:
    • 全文本查询:针对文本类型数据
    • 字段级别查询:针对结构化数据,如数字、日期等
# 1. 全文本查询,有模糊匹配、短语匹配、多个字段匹配查询、语法查询
# 1.1 模糊匹配,关键词 match
# 关键词 highlight,高亮
curl -H 'Content-Type: application/json' \
-XPOST 'http://localhost:9200/book/_doc/_search?pretty' -d '{
	"query": {
		"match": { "title": "xxx" }
	},
	"highlight": {
        "fields" : {
            "title" : {}
        }
    }
}'

# 1.2 短语匹配,关键词 match_phrase
curl -H 'Content-Type: application/json' \
-XPOST 'http://localhost:9200/book/_doc/_search?pretty' -d '{
	"query": {
		"match_phrase": { "title": "xxx" }
	}
}'

# 1.3 多个字段匹配查询,关键词 multi_match
curl -H 'Content-Type: application/json' \
-XPOST 'http://localhost:9200/book/_doc/_search?pretty' -d '{
	"query": {
		"multi_match": {
			"query": "xxx",
			"fields": ["author", "title"]
		}
	}
}'

# 1.4 语法查询:是根据一定的语法规则进行的查询,
# 一般做数据搜索用,支持通配符、范围查询、布尔查询、正则表达式
curl -H 'Content-Type: application/json' \
-XPOST 'http://localhost:9200/book/_doc/_search?pretty' -d '{
	"query": {
		"query_string": {
			// 1.4.1
			"query": "xxx AND yyy"
			// 1.4.2
			// "query": "(xxx AND yyy) OR zzz"
			// 1.4.3
			// "query": "xxx OR zzz"
			// "fields": ["author", "title"]
		}
	}
}'

# 2. 字段级别查询
# 2.1 
curl -H 'Content-Type: application/json' \
-XPOST 'http://localhost:9200/book/_doc/_search?pretty' -d '{
	"query": {
		"term": {
			"word_count": 1000,
		}
	}
}'

# 2.2 范围级别查询
curl -H 'Content-Type: application/json' \
-XPOST 'http://localhost:9200/book/_doc/_search?pretty' -d '{
	"query": {
		"range": {
			"word_count": {
				"gte" : 1000,
				"lte" : 2000
				// "gte" : "2019-12-01",
				// "lte" : "now"
			}
		}
	}
}'
  • Filter context:在查询过程中,只判断该文档是否满足条件,只有 YES 或 NO
# filter 只是做数据过滤,ElasticSearch 会对其做结果缓存
# 相对 query 快一些,要和 bool 一起使用
curl -H 'Content-Type: application/json' \
-XPOST 'http://localhost:9200/book/_doc/_search?pretty' -d '{
	"query": {
		"bool": {
			"filter": {
				"term": {
					"word_count": 1000
				}
			}
		}
	}
}'

2.2.2 复合条件查询;

以一定的逻辑组合子条件查询

  • 固定分数查询
# 模糊匹配
curl -H 'Content-Type: application/json' \
-XPOST 'http://localhost:9200/book/_doc/_search?pretty' -d '{
	"query": {
		"match": { "title": "xxx" }
	}
}'

# 固定分数查询
# 不支持 match,只支持 filter
curl -H 'Content-Type: application/json' \
-XPOST 'http://localhost:9200/book/_doc/_search?pretty' -d '{
	"query": {
		"constant_score": {
			"filter": {
				"match": { "title": "xxx" }
			},
			"boost": 2
		}
	}
}'

  • 布尔查询
# 关键词 should,“或”的关系,满足其中一个即可 
curl -H 'Content-Type: application/json' \
-XPOST 'http://localhost:9200/book/_doc/_search?pretty' -d '{
	"query": {
		"bool": {
			"should": [
				{ "match": { "author": "xxx" } },
				{ "match": { "title": "yyy" } }
			]
		}
	}
}'

# 关键词 must,满足所有条件
# 可以和 filter 组合
curl -H 'Content-Type: application/json' \
-XPOST 'http://localhost:9200/book/_doc/_search?pretty' -d '{
	"query": {
		"bool": {
			"must": [
				{ "match": { "author": "xxx" } },
				{ "match": { "title": "yyy" } }	
			],
			"filter": [
				{ "term": { "word_count": 1000 } }
			]
		}
	}
}'

# 关键词 must_not
curl -H 'Content-Type: application/json' \
-XPOST 'http://localhost:9200/book/_doc/_search?pretty' -d '{
	"query": {
		"bool": {
			"must_not": {
				"term": { "author": "xxx" }
			}
		}
	}
}'

# 混合
curl -H 'Content-Type: application/json' \
-XPOST 'http://localhost:9200/book/_doc/_search?pretty' -d '{
	"query": {
		"bool": {
			"must": [
				{ "match": { "author": "xxx" } }, 
				{ "match": { "title": "yyy" } }
			],
			"filter": [{
				"range": {
					"publish_date": {
						"gte": 1577203200,
						"lte": 1577203203
					}
				}
			}]
		}
	},
	"_source": "publish_date",
	"size": 1000,
	"sort": [{
		"publish_date": {
			"order": "asc"
		}
	}]
}
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Elasticsearch是一个开源的分布式搜索和分析引擎,它可以用于存储、搜索和分析大量的数据。以下是一些Elasticsearch基本操作: 1. 安装和启动Elasticsearch:首先,你需要从Elasticsearch官方网站下载和安装Elasticsearch。安装完成后,你可以使用命令行或者图形界面来启动Elasticsearch。 2. 创建索引:在Elasticsearch中,数据存储在索引中。你可以使用PUT请求来创建一个新的索引。例如,使用curl命令可以发送以下请求来创建一个名为"my_index"的索引: ``` curl -XPUT 'localhost:9200/my_index' ``` 3. 添加文档:一旦索引创建好了,你可以使用POST请求来向索引中添加文档。文档是以JSON格式表示的数据。以下是向名为"my_index"的索引添加一个文档的示例请求: ``` curl -XPOST 'localhost:9200/my_index/_doc' -d ' { "title": "Elasticsearch Basics", "content": "This is a basic introduction to Elasticsearch" }' ``` 4. 搜索文档:你可以使用GET请求来搜索索引中的文档。以下是一个搜索名为"my_index"的索引中包含关键字"elasticsearch"的文档的示例请求: ``` curl -XGET 'localhost:9200/my_index/_search?q=elasticsearch' ``` 5. 更新文档:使用POST请求可以更新索引中的文档。以下是更新名为"my_index"的索引中ID为1的文档的示例请求: ``` curl -XPOST 'localhost:9200/my_index/_doc/1/_update' -d ' { "doc": { "content": "This is an updated content" } }' ``` 6. 删除文档:使用DELETE请求可以删除索引中的文档。以下是删除名为"my_index"的索引中ID为1的文档的示例请求: ``` curl -XDELETE 'localhost:9200/my_index/_doc/1' ``` 这些是Elasticsearch的一些基本操作。你可以根据需要进一步探索和学习更多高级功能和API。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值