Kibana+ElasticSearch实现索引数据的几种查询方式

1.match_all搜索,直接返回所有文档


GET /school/_search
{
"query": {
"match_all": {

}
}
}

 返回结果大致如下:

{
"took": 13,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 23,
"max_score": 1,
"hits": [
{
"_index": "school",
"_type": "student",
"_id": "b3ffcWIB-npqvsX5SmVm",
"_score": 1,
"_source": {
"aggs": {
"group_by_word_count": {
"terms": {
"field": "word_count"
}
}
}
}
},
{
"_index": "school",
"_type": "student",
"_id": "c3fjcWIB-npqvsX5G2Wh",
"_score": 1,
"_source": {
"aggs": {
"grades_word_count": {
"stats": {
"field": "word_count"
}
}
}
}
},
{
"_index": "school",
"_type": "student",
"_id": "dHfjcWIB-npqvsX5uWWr",
"_score": 1,
"_source": {
"aggs": {
"grades_word_count": {
"min": {
"field": "word_count"
}
}
}
}
},
{
"_index": "school",
"_type": "student",
"_id": "dXfkcWIB-npqvsX5hmWx",
"_score": 1,
"_source": {
"query": {
"match": {
"name": "海哥"
}
}
}
},
{
"_index": "school",
"_type": "student",
"_id": "dnflcWIB-npqvsX5S2V0",
"_score": 1,
"_source": {
"query": {
"multi_match": {
"query": "海哥",
"fields": [
"name",
"address"
]
}
}
}
},
{
"_index": "school",
"_type": "student",
"_id": "fXfqcWIB-npqvsX5yGXf",
"_score": 1,
"_source": {
"query": {
"bool": {
"filter": {
"term": {
"word_count": 2000
}
}
}
}
}
},
{
"_index": "school",
"_type": "student",
"_id": "fnfrcWIB-npqvsX5mGXq",
"_score": 1,
"_source": {
"query": {
"constant_score": {
"filter": {
"match": {
"title": "ElasticSearch"
}
},
"boost": 2
}
}
}
},
{
"_index": "school",
"_type": "student",
"_id": "gHfucWIB-npqvsX5HWUB",
"_score": 1,
"_source": {
"query": {
"bool": {
"must_not": [
{
"term": {
"word_count": "2000"
}
}
]
}
}
}
},
{
"_index": "school",
"_type": "student",
"_id": "HYVJOGIBUtf8tEPshwDC",
"_score": 1,
"_source": {
"name": "张小花",
"address": "山东烟台",
"age": 24,
"date": "1996-07-24"
}
},
{
"_index": "school",
"_type": "student",
"_id": "cHffcWIB-npqvsX59mXN",
"_score": 1,
"_source": {
"aggs": {
"group_by_word_count": {
"terms": {
"field": "word_count"
}
}
}
}
}
]
}
}

 参数大致解释:
took: 执行搜索耗时,毫秒为单位
time_out: 搜索是否超时
_shards: 多少分片被搜索,成功多少,失败多少
hits: 搜索结果展示
hits.total: 匹配条件的文档总数
hits.hits: 返回结果展示,默认返回十个
hits.max_score:最大匹配得分
hits._score: 返回文档的匹配得分(得分越高,匹配程度越高,越靠前)
_index _type _id 作为剥层定位到特定的文档
_source 文档源

 2.执行查询
   2.1 只显示name和address

POST /school/_search
{
"query": { "match_all": {} },
"_source": ["name", "address"]
}
  查询结果:


{
"took": 313,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 23,
"max_score": 1,
"hits": [
{
"_index": "school",
"_type": "student",
"_id": "b3ffcWIB-npqvsX5SmVm",
"_score": 1,
"_source": {}
},
{
"_index": "school",
"_type": "student",
"_id": "c3fjcWIB-npqvsX5G2Wh",
"_score": 1,
"_source": {}
},
{
"_index": "school",
"_type": "student",
"_id": "dHfjcWIB-npqvsX5uWWr",
"_score": 1,
"_source": {}
},
{
"_index": "school",
"_type": "student",
"_id": "dXfkcWIB-npqvsX5hmWx",
"_score": 1,
"_source": {}
},
{
"_index": "school",
"_type": "student",
"_id": "dnflcWIB-npqvsX5S2V0",
"_score": 1,
"_source": {}
},
{
"_index": "school",
"_type": "student",
"_id": "fXfqcWIB-npqvsX5yGXf",
"_score": 1,
"_source": {}
},
{
"_index": "school",
"_type": "student",
"_id": "fnfrcWIB-npqvsX5mGXq",
"_score": 1,
"_source": {}
},
{
"_index": "school",
"_type": "student",
"_id": "gHfucWIB-npqvsX5HWUB",
"_score": 1,
"_source": {}
},
{
"_index": "school",
"_type": "student",
"_id": "HYVJOGIBUtf8tEPshwDC",
"_score": 1,
"_source": {
"address": "山东烟台",
"name": "张小花"
}
},
{
"_index": "school",
"_type": "student",
"_id": "cHffcWIB-npqvsX59mXN",
"_score": 1,
"_source": {}
}
]
}
}

 2.2 返回name为haige的document

POST /school/_search
{
"query": { "match": { "name": "张小花" } }
}
  查询结果:

{
"took": 439,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 2.634553,
"hits": [
{
"_index": "school",
"_type": "student",
"_id": "bXfXcWIB-npqvsX5w2Vc",
"_score": 2.634553,
"_source": {
"name": "张小花",
"address": "山东烟台",
"age": 24,
"date": "1996-07-24"
}
},
{
"_index": "school",
"_type": "student",
"_id": "HYVJOGIBUtf8tEPshwDC",
"_score": 0.8630463,
"_source": {
"name": "张小花",
"address": "山东烟台",
"age": 24,
"date": "1996-07-24"
}
}
]
}
}

 2.3 返回name包含"海"的所有document

POST /school/_search
{
"query": { "match": { "name": "海" } }
}

  查询结果:

{
"took": 68,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1.0417081,
"hits": [
{
"_index": "school",
"_type": "student",
"_id": "3",
"_score": 1.0417081,
"_source": {
"name": "海哥",
"address": "山东济宁",
"age": 27,
"date": "1998-03-16"
}
}
]
}
}

 2.4 返回name中包含term "海" 或 "花" 的所有document

POST /school/_search
{
"query": { "match": { "name": "海 花" } }
}

{
"took": 26,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 1.0417081,
"hits": [
{
"_index": "school",
"_type": "student",
"_id": "3",
"_score": 1.0417081,
"_source": {
"name": "海哥",
"address": "山东济宁",
"age": 27,
"date": "1998-03-16"
}
},
{
"_index": "school",
"_type": "student",
"_id": "bXfXcWIB-npqvsX5w2Vc",
"_score": 0.8781843,
"_source": {
"name": "张小花",
"address": "山东烟台",
"age": 24,
"date": "1996-07-24"
}
},
{
"_index": "school",
"_type": "student",
"_id": "HYVJOGIBUtf8tEPshwDC",
"_score": 0.2876821,
"_source": {
"name": "张小花",
"address": "山东烟台",
"age": 24,
"date": "1996-07-24"
}
}
]
}
}


 2.5 匹配phrase "海 花"

POST /school/_search
{
"query": { "match_phrase": { "name": "海 花" }}
}

  查询结果:

{
"took": 5,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 0,
"max_score": null,
"hits": []
}
}

 2.6 返回name中包含"海"和"哥"的所有账户(AND)

POST /school/_search
{
"query": {
"bool": {
"must": [
{ "match": { "name": "海" } },
{ "match": { "name": "哥" } }
]
}
}
}

  查询结果:

{
"took": 208,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 2.0834162,
"hits": [
{
"_index": "school",
"_type": "student",
"_id": "3",
"_score": 2.0834162,
"_source": {
"name": "海哥",
"address": "山东济宁",
"age": 27,
"date": "1998-03-16"
}
}
]
}
}

 2.7 返回name中包含"海"或"花"的所有document

POST /school/_search
{
"query": {
"bool": {
"should": [
{ "match": { "name": "海" } },
{ "match": { "name": "花" } }
]
}
}
}

 查询结果:

{
"took": 19,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 1.0417081,
"hits": [
{
"_index": "school",
"_type": "student",
"_id": "3",
"_score": 1.0417081,
"_source": {
"name": "海哥",
"address": "山东济宁",
"age": 27,
"date": "1998-03-16"
}
},
{
"_index": "school",
"_type": "student",
"_id": "bXfXcWIB-npqvsX5w2Vc",
"_score": 0.8781843,
"_source": {
"name": "张小花",
"address": "山东烟台",
"age": 24,
"date": "1996-07-24"
}
},
{
"_index": "school",
"_type": "student",
"_id": "HYVJOGIBUtf8tEPshwDC",
"_score": 0.2876821,
"_source": {
"name": "张小花",
"address": "山东烟台",
"age": 24,
"date": "1996-07-24"
}
}
]
}
}

  2.8 查询name中既不包含"海",也不包含"哥"的所有document

POST /school/_search
{
"query": {
"bool": {
"must_not": [
{ "match": { "name": "海" } },
{ "match": { "name": "哥" } }
]
}
}
}

 查询结果:

{
"took": 264,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 22,
"max_score": 1,
"hits": [
{
"_index": "school",
"_type": "student",
"_id": "b3ffcWIB-npqvsX5SmVm",
"_score": 1,
"_source": {
"aggs": {
"group_by_word_count": {
"terms": {
"field": "word_count"
}
}
}
}
},
{
"_index": "school",
"_type": "student",
"_id": "c3fjcWIB-npqvsX5G2Wh",
"_score": 1,
"_source": {
"aggs": {
"grades_word_count": {
"stats": {
"field": "word_count"
}
}
}
}
},
{
"_index": "school",
"_type": "student",
"_id": "dHfjcWIB-npqvsX5uWWr",
"_score": 1,
"_source": {
"aggs": {
"grades_word_count": {
"min": {
"field": "word_count"
}
}
}
}
},
{
"_index": "school",
"_type": "student",
"_id": "dXfkcWIB-npqvsX5hmWx",
"_score": 1,
"_source": {
"query": {
"match": {
"name": "海哥"
}
}
}
},
{
"_index": "school",
"_type": "student",
"_id": "dnflcWIB-npqvsX5S2V0",
"_score": 1,
"_source": {
"query": {
"multi_match": {
"query": "海哥",
"fields": [
"name",
"address"
]
}
}
}
},
{
"_index": "school",
"_type": "student",
"_id": "fXfqcWIB-npqvsX5yGXf",
"_score": 1,
"_source": {
"query": {
"bool": {
"filter": {
"term": {
"word_count": 2000
}
}
}
}
}
},
{
"_index": "school",
"_type": "student",
"_id": "fnfrcWIB-npqvsX5mGXq",
"_score": 1,
"_source": {
"query": {
"constant_score": {
"filter": {
"match": {
"title": "ElasticSearch"
}
},
"boost": 2
}
}
}
},
{
"_index": "school",
"_type": "student",
"_id": "gHfucWIB-npqvsX5HWUB",
"_score": 1,
"_source": {
"query": {
"bool": {
"must_not": [
{
"term": {
"word_count": "2000"
}
}
]
}
}
}
},
{
"_index": "school",
"_type": "student",
"_id": "HYVJOGIBUtf8tEPshwDC",
"_score": 1,
"_source": {
"name": "张小花",
"address": "山东烟台",
"age": 24,
"date": "1996-07-24"
}
},
{
"_index": "school",
"_type": "student",
"_id": "cHffcWIB-npqvsX59mXN",
"_score": 1,
"_source": {
"aggs": {
"group_by_word_count": {
"terms": {
"field": "word_count"
}
}
}
}
}
]
}
}

 2.9 返回name中包含"海",且地址不是"山东烟台"的所有document

POST /school/_search
{
"query": {
"bool": {
"must": [
{ "match": { "name": "海" } }
],
"must_not": [
{ "match": { "address": "山东烟台" } }
]
}
}
}

 查询结果:
{
"took": 73,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1.0417081,
"hits": [
{
"_index": "school",
"_type": "student",
"_id": "3",
"_score": 1.0417081,
"_source": {
"name": "海哥",
"address": "山东济宁",
"age": 27,
"date": "1998-03-16"
}
}
]
}
}

 3. 过滤查询
     3.1 在所有document中寻找age在0-25岁之间(闭区间)的学生


POST /school/_search
{
"query": {
"filtered": {
"query": { "match_all": {} },
"filter": {
"range": {
"age": {
"gte": 0,
"lte": 25
}
}
}
}
}
}

4.谈论query和filter的效率
   一般认为filter的速度快于query的速度 
   - filter不会计算相关度得分,效率高 
   - filter的结果可以缓存到内存中,方便再用
————————————————
版权声明:本文为CSDN博主「潇潇雨歇_」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/linhaiyun_ytdx/article/details/79762926

原文地址:https://www.cnblogs.com/xzlive/p/12409722.html