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"
        }
      }
    ]
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值