ElasticSearch7 基本操作

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

1. 基础操作;

1.1 更新;

curl -H 'Content-Type: application/json' \
-XPOST 'http://localhost:9200/book/_doc/_update_by_query'
'{
	"query": {
		"match": { "title": "xxx" }
	},
	"script": {
		"lang": "painless",
		"inline": "ctx._source.word_count = params.word_count",
		"params": { "word_count" : 100000 }
	}
}'

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"
		}
	}]
}
发布了119 篇原创文章 · 获赞 12 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/hualaoshuan/article/details/103524417