4.5 ElasticSearch搜索之Bool Query

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
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值