9.1.2-elasticsearch复合查询之constant_score/dis_max/function_score查询

本文详细介绍了Elasticsearch的三种复合查询方式:constant_score查询,不计算相似度分;dis_max查询,用于处理多个查询子句,打破分数相同的情况;以及function_score查询,允许根据函数自定义文档分数。特别是function_score查询,包括script_score、weight、random_score和field_value_factor等函数的使用方法和参数配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、constant_score查询

内部包装了过滤查询,故而不会计算相似度分,该查询返回的相似度分与字段上指定boost参数值相同

//请求参数
GET bank/_search
{
   
   
  "query": {
   
   
    "constant_score": {
   
   
      "filter": {
   
   
        "term": {
   
   
          "city.keyword": "Fredericktown"
        }
      },
      "boost": 1.3
    }
  }
}

//结果返回
{
   
   
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
   
   
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
   
   
    "total" : {
   
   
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.3,
    "hits" : [
      {
   
   
        "_index" : "bank",
        "_type" : "_doc",
        "_id" : "97",
        "_score" : 1.3,
        "_source" : {
   
   
          "account_number" : 97,
          "balance" : 49671,
          "firstname" : "Karen",
          "lastname" : "Trujillo",
          "age" : 40,
          "gender" : "F",
          "address" : "512 Cumberland Walk",
          "employer" : "Tsunamia",
          "email" : "[email protected]",
          "city" : "Fredericktown",
          "state" : "MO"
        }
      }
    ]
  }
}

constant_score查询的顶层参数

序号 参数 描述
1 filter 必须,查询对象,指定希望执行的过滤查询,任何返回的文档都必须匹配这个查询;过滤查询不会计算相似度分,为提升性能,ES会对使用频率高的过滤查询的结果进行缓存
2 boost 可选,浮点数,该值作为匹配了以上filter的文档的相似度分,默认为1.0
2、dis_max查询

最大析取(disjunction max)
返回的文档必须要满足多个查询子句中的一项条件;
若一个文档能匹配多个查询子句时,则dis_max查询将为能匹配上查询子句条件的项增加额外分,即针对多个子句文档有一项满足就针对满足的那一项分配更高分,这也能打破在多个文档都匹配某一个或多个条件时分数相同的情况;

//查询city为Brogan与gender为M的银行职员,能匹配任一项在返回结果中就存在
//tie_breaker计算最高分数因子
//在dis_max查询时针对多个查询字段指定不同的boost因子,此处指定city与gender不同的因子,分数也会因此不同

//请求参数
GET bank/_search
{
   
   
  "size": 5, 
  "query": {
   
   
    "dis_max": {
   
   
      "tie_breaker": 0.7,
      "queries": [
        {
   
   
          "term": {
   
   
            "city.keyword": {
   
   
              "boost": 2, 
              "value": "Brogan"
            }
          }
        },
        {
   
   
          "term": {
   
   
            "gender.keyword": {
   
   
              "boost": 1.0, 
              "value": "M"
            }
          }
        }
      ]
    }
  }
}

//返回结果
{
   
   
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
   
   
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
   
   
    "total" : {
   
   
      "value" : 507,
      "relation" : "eq"
    },
    "max_score" : 13.48206,
    "hits" : [
      {
   
   
        "_index" : "bank",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 13.48206,
        "_source" : {
   
   
          "account_number" : 1,
          "balance" : 39225,
          "firstname" : "Amber",
          "lastname" : "Duke",
          "age" : 32,
          "gender" : "M",
          "address" : "880 Holmes Lane",
          "employer" : "Pyrami",
          "email" : "[email protected]",
          "city" : "Brogan",
          "state" : "IL"
        }
      },
      {
   
   
        "_index" : "bank",
        "_type" : "_doc",
        "_id" : "6",
        "_score" : 0.679258,
        "_source" : {
   
   
          "account_number" : 6,
          "balance" : 5686,
          "firstname" : "Hattie",
          "lastname" : "Bond",
          "age" : 36,
          "gender" : "M",
          "address" : "671 Bristol Street",
          "employer" : "Netagy",
          "email" : "[email protected]",
          "city" : "Dante",
          "state" : "TN"
        }
      },
      {
   
   
        "_index" : "bank"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值