Elasticsearch 查询之Constant Score Query

前言

Constant Score Query 也是属于 ES 复合查询中的一种,可以将一个 query 的结果的 score 评分给设定成一个常量值,并将结果集缓存起来,与 Boolean filter query 的区别,就在于 filter 的结果评分是 0,不支持设置,除此之外没有区别了,两者的性能是一样的

Constant Score Query查询示例

GET test01/_search?
{
  "query": {
        "constant_score" : {
            "filter" : {
                "match" : {
                    "title": "Collecting"
                }
            },
        "boost": 1.2
        }
  }
}

返回的数据结果如下:

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 6,
    "successful" : 6,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 3,
    "max_score" : 1.2,
    "hits" : [
      {
        "_index" : "test01",
        "_type" : "doc",
        "_id" : "3",
        "_score" : 1.2,
        "_source" : {
          "title" : "Collecting  Service",
          "content" : "FLume"
        }
      },
      {
        "_index" : "test01",
        "_type" : "doc",
        "_id" : "2",
        "_score" : 1.2,
        "_source" : {
          "title" : "Collecting  Service",
          "content" : "Beats"
        }
      },
      {
        "_index" : "test01",
        "_type" : "doc",
        "_id" : "1",
        "_score" : 1.2,
        "_source" : {
          "title" : "Collecting  Service",
          "content" : "Logstash"
        }
      }
    ]
  }
}

注意:constant score query 不支持直接写多 filter 的条件,如果需要可以内嵌一个 bool query 支持:

GET test01/_search?
{
	"query": {
		"constant_score": {
			"filter": {
				"bool": {
					"filter": [{
							"match": {
								"title": "Collecting"
							}
						},
						{
							"match": {
								"content": "Logstash"
							}
						}
					]
				}
			},
			"boost": 1.2
		}
	}
}

Boolean Filter Query对比演示

GET test01/_search?
{
  "query": {
        "bool": {
            "filter": [
                {"match" : {
                    "title": "Collecting"
                }},
                {"match": {
                    "content": "FLume"
                }}
                ]
            }
          
        }
}

返回的结果可以看到都是 0

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 6,
    "successful" : 6,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 0.0,
    "hits" : [
      {
        "_index" : "test01",
        "_type" : "doc",
        "_id" : "3",
        "_score" : 0.0,
        "_source" : {
          "title" : "Collecting  Service",
          "content" : "FLume"
        }
      }
    ]
  }
}

你可能感兴趣的:(ElasticSearch,elasticsearch,java,大数据)