相关内容:
ElasticSearch7 实现全文检索、关键词高亮
# 非结构化创建:直接创建索引名称,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"}
}
}
}'
# 指定文档 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
}'
# 直接覆盖:同插入
# 修改指定文档
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 }
}
}'
# 删除文档
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\")"}}
# 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"
}
}
}
}'
特定字段查询所指特定值,分为 Query context 和 Filter context
# 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 只是做数据过滤,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
}
}
}
}
}'
以一定的逻辑组合子条件查询
# 模糊匹配
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"
}
}]
}