1.简介
查询elasticsearch中的数据时,通常使用具有完备的查询语法query dsl进行(基于json定义的查询语言),主要包括两种类型。
(1).简单类查询
如match query(全文检索)、term query(词)、range query(范围)等针对某一个字段进行的查询。

(2).复合类查询
如constant score query、bool query等针对一个或者多个字段进行的复合查询。

(3).文档准备
打开kibana Dev Tools,分别添加如下文档记录。

POST /people/_doc
{
"name": "Mike Steven",
"country": "China",
"age": 26,
"date": "1995-01-01",
"description":"I am Mike Steven."
}
{
"name": "Mike Sherry",
"country": "China",
"age": 23,
"date": "1998-06-01",
"description":"I am Mike Sherry."
}
{
"name": "Deron Williams",
"country": "Englend",
"age": 32,
"date": "1989-01-01",
"description":"How are you!"
}
{
"name": "LeBron James",
"country": "USA",
"age": 37,
"date": "1984-06-26",
"description":"How old are you?"
}
{
"name": "Mike Owen",
"country": "Englend",
"age": 34,
"date": "1987-03-12",
"description":"Are you?"
}

2.Match Query
(1).query
查询姓名包含Mike的文档。

POST /people/_search
{
"query": {
"match": {
"name": "Mike"
}
}
}
{
"took" : 1084,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 0.105360515,
"hits" : [
{
"_index" : "people",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.105360515,
"_source" : {
"name" : "Mike Steven",
"country" : "China",
"age" : 26,
"date" : "1995-01-01",
"description" : "I am Mike Steven."
}
},
{
"_index" : "people",
"_type" : "_doc",
"_id" : "mom6gXsBEsHOdz1YRcov",
"_score" : 0.105360515,
"_source" : {
"name" : "Mike Sherry",
"country" : "China",
"age" : 23,
"date" : "1998-06-01",
"description" : "I am Mike Sherry."
}
},
{
"_index" : "people",
"_type" : "_doc",
"_id" : "nonCgXsBEsHOdz1YDcoP",
"_score" : 0.105360515,
"_source" : {
"name" : "Mike Owen",
"country" : "Englend",
"age" : 34,
"date" : "1987-03-12",
"description" : "Are you?"
}
}
]
}
}

(2).operator
通过operator参数可以控制单词间的匹配关系,可选项为or和and,默认是or,如待查询语句为“How are you”,operator为“and”,那么“Are you?”这样的文本不会被匹配。

POST /people/_search
{
"query": {
"match": {
"description": {
"query": "How are you",
"operator": "and"
}
}
}
}
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 0.8630463,
"hits" : [
{
"_index" : "people",
"_type" : "_doc",
"_id" : "nYnBgXsBEsHOdz1YcspL",
"_score" : 0.8630463,
"_source" : {
"name" : "LeBron James",
"country" : "USA",
"age" : 37,
"date" : "1984-06-26",
"description" : "How old are you?"
}
},
{
"_index" : "people",
"_type" : "_doc",
"_id" : "nIm-gXsBEsHOdz1Yrcq7",
"_score" : 0.8630463,
"_source" : {
"name" : "Deron Williams",
"country" : "Englend",
"age" : 32,
"date" : "1989-01-01",
"description" : "How are you!"
}
}
]
}
}

(3).minimum_should_match
通过minimum_should_match参数可以控制需要匹配的单词个数,如待查询语句为“How are you”,minimum_should_match为2,那么“How are you!”和“Are you?”以及“How old are you!”这样的文本都会被匹配。

POST /people/_search
{
"query": {
"match": {
"description": {
"query": "How are you",
"minimum_should_match": 2
}
}
}
}
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 2.919871,
"hits" : [
{
"_index" : "people",
"_type" : "_doc",
"_id" : "nonCgXsBEsHOdz1YDcoP",
"_score" : 2.919871,
"_source" : {
"name" : "Mike Owen",
"country" : "Englend",
"age" : 34,
"date" : "1987-03-12",
"description" : "Are you?"
}
},
{
"_index" : "people",
"_type" : "_doc",
"_id" : "nYnBgXsBEsHOdz1YcspL",
"_score" : 0.8630463,
"_source" : {
"name" : "LeBron James",
"country" : "USA",
"age" : 37,
"date" : "1984-06-26",
"description" : "How old are you?"
}
},
{
"_index" : "people",
"_type" : "_doc",
"_id" : "nIm-gXsBEsHOdz1Yrcq7",
"_score" : 0.8630463,
"_source" : {
"name" : "Deron Williams",
"country" : "Englend",
"age" : 32,
"date" : "1989-01-01",
"description" : "How are you!"
}
}
]
}
}

3.String Query
(1).query
类似于参数查询,单词间的查询连词用大写的AND、OR和NOT表示。如查询条件为“how AND you”,则表示将检索包含how以及you的文档。

POST /people/_search
{
"query": {
"query_string": {
"default_field": "description",
"query": "how AND you"
}
}
}
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 0.5753642,
"hits" : [
{
"_index" : "people",
"_type" : "_doc",
"_id" : "nYnBgXsBEsHOdz1YcspL",
"_score" : 0.5753642,
"_source" : {
"name" : "LeBron James",
"country" : "USA",
"age" : 37,
"date" : "1984-06-26",
"description" : "How old are you?"
}
},
{
"_index" : "people",
"_type" : "_doc",
"_id" : "nIm-gXsBEsHOdz1Yrcq7",
"_score" : 0.5753642,
"_source" : {
"name" : "Deron Williams",
"country" : "Englend",
"age" : 32,
"date" : "1989-01-01",
"description" : "How are you!"
}
}
]
}
}

(2).fields
通过fields参数控制查询的字段个数。如下面查询语句表示,显示name或country字段包含Steven的文档。

POST /people/_search
{
"query": {
"query_string": {
"fields": ["name", "country"],
"query": "Steven"
}
}
}
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.6931472,
"hits" : [
{
"_index" : "people",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.6931472,
"_source" : {
"name" : "Mike Steven",
"country" : "China",
"age" : 26,
"date" : "1995-01-01",
"description" : "I am Mike Steven."
}
}
]
}
}