elasticsearch-查询

时间:2023-03-08 20:29:57

使用如下语句创建一个名字为:user_document,别名为user的索引

PUT:http://localhost:9200/user_document

{
"settings" : {
"number_of_shards" : 1,
"number_of_replicas" : 0
},
"mappings" : {
"user_1" : {
"properties" : {
"Identity" :
{
"type" : "string",
"index" : "not_analyzed"
},
"Name" :
{
"type" : "string"
},
"Age" :
{
"type" : "integer"
},
"Birthday" :
{
"format": "dateOptionalTime",
"type": "date"
},
"Sex" :
{
"type" : "boolean"
}
}
}
},
"aliases" : {
"user" : {}
}
}

并新增10条测试数据

1、Match All 查询

GET _search
{
"query":
{
"match_all": {}
}
}

匹配索引中的全部文档,默认得分为1.0

2、Match 查询

GET _search
{
"query":
{
"match":
{
"Name":"陈楚生"
}
}
}

结果

{
"took": 3,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 1.8677558,
"hits": [
{
"_index": "user_document",
"_type": "user_1",
"_id": "AVYDbtj0OpxK9K1OdHXF",
"_score": 1.8677558,
"_source": {
"Identity": "360501199001016210",
"Name": "陈楚生",
"Age": 35,
"Birthday": "1981-07-25T14:12:12",
"Sex": false
}
},
{
"_index": "user_document",
"_type": "user_1",
"_id": "AVYDcPTKOpxK9K1OdHXH",
"_score": 0.6856395,
"_source": {
"Identity": "360501199001016212",
"Name": "陈楚河",
"Age": 40,
"Birthday": "1978-05-09T14:12:12",
"Sex": false
}
},
{
"_index": "user_document",
"_type": "user_1",
"_id": "AVYDcnK7OpxK9K1OdHXL",
"_score": 0.14631948,
"_source": {
"Identity": "360501199001016214",
"Name": "陈乔恩",
"Age": 36,
"Birthday": "1980-05-09T14:12:12",
"Sex": false
}
}
]
}
}

Match查询字符串时,会将文本内容分词并做Or操作

3、Multi Match 查询

GET _search
{
"query":
{
"multi_match": {
"query": "陈楚生",
"fields": ["Name","Identity"]
}
}
}

同Match查询,但是可以同时查多个字段

4、Query String 查询

GET _search
{
"query":
{
"query_string": {
"default_field": "Name",
"query": "陈楚生"
}
}
}

结果

{
"took": 3,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 1.8677558,
"hits": [
{
"_index": "user_document",
"_type": "user_1",
"_id": "AVYDbtj0OpxK9K1OdHXF",
"_score": 1.8677558,
"_source": {
"Identity": "360501199001016210",
"Name": "陈楚生",
"Age": 35,
"Birthday": "1981-07-25T14:12:12",
"Sex": false
}
},
{
"_index": "user_document",
"_type": "user_1",
"_id": "AVYDcPTKOpxK9K1OdHXH",
"_score": 0.6856395,
"_source": {
"Identity": "360501199001016212",
"Name": "陈楚河",
"Age": 40,
"Birthday": "1978-05-09T14:12:12",
"Sex": false
}
},
{
"_index": "user_document",
"_type": "user_1",
"_id": "AVYDcnK7OpxK9K1OdHXL",
"_score": 0.14631948,
"_source": {
"Identity": "360501199001016214",
"Name": "陈乔恩",
"Age": 36,
"Birthday": "1980-05-09T14:12:12",
"Sex": false
}
}
]
}
}

分词查询,也可以想同时查询多个字段

5、Term 查询

GET _search
{
"query":
{
"term": {
"Identity": {
"value": "360501199001016210",
"boost": 2
}
}
}
}

结果

{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 2.5040774,
"hits": [
{
"_index": "user_document",
"_type": "user_1",
"_id": "AVYDbtj0OpxK9K1OdHXF",
"_score": 2.5040774,
"_source": {
"Identity": "360501199001016210",
"Name": "陈楚生",
"Age": 35,
"Birthday": "1981-07-25T14:12:12",
"Sex": false
}
}
]
}
}

Term查询一般用来做精确查询,即查询关键字需要和被查询字段值完全一致才会被查询出来。

所以如果当被查询的字段是一个String类型时,该类型需要被显示标记为not_analyzed,

否则字段内容会先被分词器分成许多个词条,导致term查询不出结果。

可以使用Match 或Query_String查询会被分词的字段。

6、Terms 查询

GET _search
{
"query":
{
"terms": {
"Age": [
35,30
]
}
}
}

结果

{
"took": 2,
"timed_out": false,
"_shards": {
"total": 2,
"successful": 2,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0.8853251,
"hits": [
{
"_index": "user_document",
"_type": "user_1",
"_id": "AVYDbtj0OpxK9K1OdHXF",
"_score": 0.8853251,
"_source": {
"Identity": "360501199001016210",
"Name": "陈楚生",
"Age": 35,
"Birthday": "1981-07-25T14:12:12",
"Sex": false
}
},
{
"_index": "user_document",
"_type": "user_1",
"_id": "AVYDc4xqOpxK9K1OdHXN",
"_score": 0.8853251,
"_source": {
"Identity": "360501199001016214",
"Name": "唐嫣",
"Age": 30,
"Birthday": "1986-05-09T14:12:12",
"Sex": true
}
}
]
}
}

查询一个值数组范围内的所有文档

7、Range 查询

GET _search
{
"query":
{
"range": {
"Age": {
"from": 25,
"to": 35
}
}
}
}

结果

{
"took": 3,
"timed_out": false,
"_shards": {
"total": 2,
"successful": 2,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 1,
"hits": [
{
"_index": "user_document",
"_type": "user_1",
"_id": "AVYDbtj0OpxK9K1OdHXF",
"_score": 1,
"_source": {
"Identity": "360501199001016210",
"Name": "陈楚生",
"Age": 35,
"Birthday": "1981-07-25T14:12:12",
"Sex": false
}
},
{
"_index": "user_document",
"_type": "user_1",
"_id": "AVYDch_LOpxK9K1OdHXK",
"_score": 1,
"_source": {
"Identity": "360501199001016214",
"Name": "赵默笙",
"Age": 28,
"Birthday": "1988-05-09T14:12:12",
"Sex": false
}
},
{
"_index": "user_document",
"_type": "user_1",
"_id": "AVYDc4xqOpxK9K1OdHXN",
"_score": 1,
"_source": {
"Identity": "360501199001016214",
"Name": "唐嫣",
"Age": 30,
"Birthday": "1986-05-09T14:12:12",
"Sex": true
}
}
]
}
}

范围查询

8、filter 过滤器

GET _search
{
"query": {
"filtered": {
"query": {
"bool": {
"should": [
{
"bool": {
"must":
{
"query_string": {
"default_field": "Name",
"query": "陈楚生"
}
},
"must_not":
{
"term": {
"Age": {
"value": 40
}
}
}
}
},
{
"bool": {
"should":
{
"term": {
"Identity": {
"value": "360501199001016211"
}
}
}
}
}
]
}
},
"filter": {
"range": {
"Age": {
"from": 30,
"to": 35
}
}
}
}
}
}

结果

{
"took": 3,
"timed_out": false,
"_shards": {
"total": 2,
"successful": 2,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.7757143,
"hits": [
{
"_index": "user_document",
"_type": "user_1",
"_id": "AVYDbtj0OpxK9K1OdHXF",
"_score": 0.7757143,
"_source": {
"Identity": "360501199001016210",
"Name": "陈楚生",
"Age": 35,
"Birthday": "1981-07-25T14:12:12",
"Sex": false
}
}
]
}
}

filter过滤器只会用于过滤文档,不会影响文档的得分情况

9、Constant Score 查询

GET _search
{
"query": {
"constant_score": {
"filter": {
"query": {
"query_string": {
"default_field": "Name",
"query": "陈楚生"
}
}
},
"boost": 1.2
}
}
}

结果

{
"took": 3,
"timed_out": false,
"_shards": {
"total": 2,
"successful": 2,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 1,
"hits": [
{
"_index": "user_document",
"_type": "user_1",
"_id": "AVYDbtj0OpxK9K1OdHXF",
"_score": 1,
"_source": {
"Identity": "360501199001016210",
"Name": "陈楚生",
"Age": 35,
"Birthday": "1981-07-25T14:12:12",
"Sex": false
}
},
{
"_index": "user_document",
"_type": "user_1",
"_id": "AVYDcPTKOpxK9K1OdHXH",
"_score": 1,
"_source": {
"Identity": "360501199001016212",
"Name": "陈楚河",
"Age": 40,
"Birthday": "1978-05-09T14:12:12",
"Sex": false
}
},
{
"_index": "user_document",
"_type": "user_1",
"_id": "AVYDcnK7OpxK9K1OdHXL",
"_score": 1,
"_source": {
"Identity": "360501199001016214",
"Name": "陈乔恩",
"Age": 36,
"Birthday": "1980-05-09T14:12:12",
"Sex": false
}
}
]
}
}

该查询出来的文档得分是一个恒定的分数1

10、Bool 查询

GET _search
{
"query": {
"bool": {
"should": [
{
"bool": {
"must": [
{
"query_string": {
"default_field": "Name",
"query": "陈楚生"
}
}
],
"must_not": [
{
"term": {
"Age": {
"value": 40
}
}
}
]
}
},
{
"bool": {
"should": [
{
"term": {
"Identity": {
"value": "360501199001016211"
}
}
}
]
}
}
]
}
}
}

结果

{
"took": 2,
"timed_out": false,
"_shards": {
"total": 2,
"successful": 2,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 0.7757143,
"hits": [
{
"_index": "user_document",
"_type": "user_1",
"_id": "AVYDbtj0OpxK9K1OdHXF",
"_score": 0.7757143,
"_source": {
"Identity": "360501199001016210",
"Name": "陈楚生",
"Age": 35,
"Birthday": "1981-07-25T14:12:12",
"Sex": false
}
},
{
"_index": "user_document",
"_type": "user_1",
"_id": "AVYDcHILOpxK9K1OdHXG",
"_score": 0.6971515,
"_source": {
"Identity": "360501199001016211",
"Name": "何家劲",
"Age": 52,
"Birthday": "1964-07-25T14:12:12",
"Sex": false
}
},
{
"_index": "user_document",
"_type": "user_1",
"_id": "AVYDcnK7OpxK9K1OdHXL",
"_score": 0.060769252,
"_source": {
"Identity": "360501199001016214",
"Name": "陈乔恩",
"Age": 36,
"Birthday": "1980-05-09T14:12:12",
"Sex": false
}
}
]
}
}

bool 查询用于组合不同的查询条件,其中可以有以下参数:must、must_not、should、filter

must:相当于and,Must中的查询条件最终会做一个and操作

must_not:对每一个查询子句做取反操作。

should:相当于or,should中的所有的子句做or操作。