一、DSL 查询
Elasticsearch提供丰富且灵活的的查询语言叫做 DSL查询(Query DSL),它允许你构建更加复杂的查询,强大的查询。
DSL(Domain Specific Language 特定领域语言)以JSON请求体的形式出现。
示例:查询age=20岁的用户
原始数据展示
# 请求参数
POST http://localhost:9200/haoke/user/_search
{
"query":{
"match":{ #match只是查询的一种
"age":20
}
}
}
#返回结果
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 2,
"successful": 2,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "haoke",
"_type": "user",
"_id": "LwPYcnEBK3PERGtNAOmn",
"_score": 1,
"_source": {
"id": 1002,
"name": "张三",
"age": 23,
"sex": "男"
}
}
]
}
}
#math: 只能作为 单条件查询,如果 有多个条件,则会报错,例如:
{
"query":{
"match":{ # 两个条件时则会报错
"age":23,
"sex":"女"
}
}
}
# 报错信息
{
"error": {
"root_cause": [
{
"type": "parsing_exception",
"reason": "[match] query doesn't support multiple fields, found [age] and [sex]",
"line": 5,
"col": 10
}
],
"type": "parsing_exception",
"reason": "[match] query doesn't support multiple fields, found [age] and [sex]",
"line": 5,
"col": 10
},
"status": 400
}
示例:查询年龄(age)>20岁的用户
# 请求参数
POST http://localhost:9200/haoke/user/_search
{
"query": {
"bool": {
"filter": {
"range": {
"age": {
"gt": 20
}
}
},
"must": {
"match": {
"sex": "男"
}
}
}
}
}
# 返回结果
{
"took": 5,
"timed_out": false,
"_shards": {
"total": 2,
"successful": 2,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 0.6931472,
"hits": [
{
"_index": "haoke",
"_type": "user",
"_id": "LwPYcnEBK3PERGtNAOmn",
"_score": 0.6931472,
"_source": {
"id": 1002,
"name": "张三",
"age": 23,
"sex": "男"
}
}
]
}
}
二、 全文索搜
# 传入参数
POST http://localhost:9200/haoke/user/_search
{
"query": {
"match":{
"name":"张三 王五"
}
}
}
# 返回结果
{
"took": 41,
"timed_out": false,
"_shards": {
"total": 2,
"successful": 2,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 1.3862944,
"hits": [
{
"_index": "haoke",
"_type": "user",
"_id": "LgOccnEBK3PERGtN9-n7",
"_score": 1.3862944,
"_source": {
"name": "王五",
"age": 20,
"sex": "女"
}
},
{
"_index": "haoke",
"_type": "user",
"_id": "LwPYcnEBK3PERGtNAOmn",
"_score": 1.3862944,
"_source": {
"id": 1002,
"name": "张三",
"age": 23,
"sex": "男"
}
}
]
}
}
三、高亮显示
# 传入参数
POST http://localhost:9200/haoke/user/_search
{
"query": {
"match": {
"name": "张三 李四"
}
},
"highlight": {
"fields": {
"name": {}
}
}
}
# 返回结果
{
"took": 164,
"timed_out": false,
"_shards": {
"total": 2,
"successful": 2,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 1.3862944,
"hits": [
{
"_index": "haoke",
"_type": "user",
"_id": "LwPYcnEBK3PERGtNAOmn",
"_score": 1.3862944,
"_source": {
"id": 1002,
"name": "张三",
"age": 23,
"sex": "男"
},
"highlight": {
"name": [
"<em>张</em><em>三</em>" #高亮显示
]
}
},
{
"_index": "haoke",
"_type": "user",
"_id": "MAPYcnEBK3PERGtNx-k7",
"_score": 0.5753642,
"_source": {
"id": 1003,
"name": "李四",
"age": 20,
"sex": "女"
},
"highlight": {
"name": [
"<em>李</em><em>四</em>" #高亮显示
]
}
}
]
}
}
四、 聚合查询
在 Elasticsearch中,支持聚合操作,类似SQL中的group by 操作
# 请求参数
POST http://localhost:9200/haoke/user/_search
{
"aggs":{
"all_interests":{
"terms":{
"field":"age"
}
}
}
}
# 返回结果
{
"took": 61,
"timed_out": false,
"_shards": {
"total": 2,
"successful": 2,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 3,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "haoke",
"_type": "user",
"_id": "LgOccnEBK3PERGtN9-n7",
"_score": 1,
"_source": {
"name": "王五",
"age": 20,
"sex": "女"
}
},
{
"_index": "haoke",
"_type": "user",
"_id": "LwPYcnEBK3PERGtNAOmn",
"_score": 1,
"_source": {
"id": 1002,
"name": "张三",
"age": 23,
"sex": "男"
}
},
{
"_index": "haoke",
"_type": "user",
"_id": "MAPYcnEBK3PERGtNx-k7",
"_score": 1,
"_source": {
"id": 1003,
"name": "李四",
"age": 20,
"sex": "女"
}
}
]
},
"aggregations": {
"all_interests": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 20,
"doc_count": 2 # 可以看出,年龄为20的用户有2个
},
{
"key": 23,
"doc_count": 1 # 可以看出,年龄为23的用户有1个
}
]
}
}
}