1.简介
布尔查询由一个或者多个布尔子句组成,其类型和基本语法如下。
| 类型 | 功能 |
|---|---|
| filter | 只过滤符合条件的文档,不计算相关性得分 |
| must | 文档必须符合must中的所有条件,会影响相关性得分 |
| must_not | 文档必须不符合must_not中的所有条件,会影响相关性得分 |
| should | 文档可以符合should中的条件,会影响相关性得分 |
POST /index_name/_search
{
"query": {
"bool": {
"filter": [{}, {}],
"must": [{}, {}],
"must_not": [{}, {}],
"should": [{}, {}]
}
}
}
2.filter
(1).简介
filter查询只过滤符合条件的文档,不会进行相关性算分。elasticsearch针对filter会有智能缓存,因此其执行效率很高。当做简单匹配查询且不考虑算分时,推荐使用filter代替query。
POST /index_name/_search
{
"query": {
"bool": {
"filter": [{
"match": {
"field_name": "query_clause"
}
}]
}
}
}
(2).query
查询name字段中包含Mike的文档。
POST /people/_search
{
"query": {
"bool": {
"filter": [{
"match": {
"name": "Mike"
}
}]
}
}
}
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 0.0,
"hits" : [
{
"_index" : "people",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.0,
"_source" : {
"name" : "Mike Steven",
"country" : "China",
"age" : 26,
"birthday" : "1995-01-01",
"description" : "I am Mike Steven."
}
},
{
"_index" : "people",
"_type" : "_doc",
"_id" : "mom6gXsBEsHOdz1YRcov",
"_score" : 0.0,
"_source" : {
"name" : "Mike Sherry",
"country" : "China",
"age" : 23,
"birthday" : "1998-06-01",
"description" : "I am Mike Sherry."
}
},
{
"_index" : "people",
"_type" : "_doc",
"_id" : "nonCgXsBEsHOdz1YDcoP",
"_score" : 0.0,
"_source" : {
"name" : "Mike Owen",
"country" : "Englend",
"age" : 34,
"birthday" : "1987-03-12",
"description" : "Are you?"
}
}
]
}
}
3.must
must查询必须符合所有的条件,会影响相关性得分。如查询name字段中包含Mike并且country字段包含China的文档。
POST /people/_search
{
"query": {
"bool": {
"must": [{
"match": {
"name": "Mike"
}
},
{
"match": {
"country": "China"
}
}
]
}
}
}
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 0.46203545,
"hits" : [
{
"_index" : "people",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.46203545,
"_source" : {
"name" : "Mike Steven",
"country" : "China",
"age" : 26,
"birthday" : "1995-01-01",
"description" : "I am Mike Steven."
}
},
{
"_index" : "people",
"_type" : "_doc",
"_id" : "mom6gXsBEsHOdz1YRcov",
"_score" : 0.46203545,
"_source" : {
"name" : "Mike Sherry",
"country" : "China",
"age" : 23,
"birthday" : "1998-06-01",
"description" : "I am Mike Sherry."
}
}
]
}
}
2个match query文档最终得分为两个查询得分之和。
4.must_not
must_not查询必须不符合其中的条件,会影响相关性得分。如查询name字段中不包含Sherry并且country字段包含China的文档。
POST /people/_search
{
"query": {
"bool": {
"must": [{
"match": {
"country": "China"
}
}],
"must_not": [{
"match": {
"name": "Sherry"
}
}]
}
}
}
{
"took" : 5,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.35667494,
"hits" : [
{
"_index" : "people",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.35667494,
"_source" : {
"name" : "Mike Steven",
"country" : "China",
"age" : 26,
"birthday" : "1995-01-01",
"description" : "I am Mike Steven."
}
}
]
}
}
5.should
(1).bool查询中只包含should,不包含must查询
只包含should时,文档必须满足至少一个条件,另外可以通过设置参数minimum_should_match来控制最少满足的条件个数。如查询name字段中包含Sherry、country字段包含China的文档。
POST /people/_search
{
"query": {
"bool": {
"should": [{
"match": {
"name": "Mike"
}
},
{
"match": {
"country": "China"
}
}
],
"minimum_should_match": 2
}
}
}
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 0.46203545,
"hits" : [
{
"_index" : "people",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.46203545,
"_source" : {
"name" : "Mike Steven",
"country" : "China",
"age" : 26,
"birthday" : "1995-01-01",
"description" : "I am Mike Steven."
}
},
{
"_index" : "people",
"_type" : "_doc",
"_id" : "mom6gXsBEsHOdz1YRcov",
"_score" : 0.46203545,
"_source" : {
"name" : "Mike Sherry",
"country" : "China",
"age" : 23,
"birthday" : "1998-06-01",
"description" : "I am Mike Sherry."
}
}
]
}
}
(2).bool查询中同时包含should和must查询
同时包含should和must时,文档不必满足should中的条件,如果满足会增加相关性得分。如查询name字段中包含Sherry、country字段包含China的文档。
POST /people/_search
{
"query": {
"bool": {
"must": [{
"match": {
"name": "Mike"
}
}],
"should": [{
"match": {
"country": "China"
}
}]
}
}
}
{
"took" : 4,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 0.46203545,
"hits" : [
{
"_index" : "people",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.46203545,
"_source" : {
"name" : "Mike Steven",
"country" : "China",
"age" : 26,
"birthday" : "1995-01-01",
"description" : "I am Mike Steven."
}
},
{
"_index" : "people",
"_type" : "_doc",
"_id" : "mom6gXsBEsHOdz1YRcov",
"_score" : 0.46203545,
"_source" : {
"name" : "Mike Sherry",
"country" : "China",
"age" : 23,
"birthday" : "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,
"birthday" : "1987-03-12",
"description" : "Are you?"
}
}
]
}
}
6.Query Context和Filter Context的区别
当一个陈述句语句位于query或filter上下文时,elasticsearch执行的结果会不同,对比如下。
| 上下文类型 | 执行类型 | 使用方式 |
|---|---|---|
| query | 查找与查询语句最匹配的文档,对所有的文档进行相关性算分并排序 | query或者bool中的must和should |
| filter | 查找与查询语句最匹配的文档 | bool中的filter与must_not或者constant_score中的filter |
2281

被折叠的 条评论
为什么被折叠?



