ElasticSearch 6.x 学习笔记:18.复合查询


原文: https://blog.csdn.net/chengyuqiang/article/details/79101416

https://www.elastic.co/guide/en/elasticsearch/reference/6.1/compound-queries.html

复合查询包装其他复合或term查询,可以将其结果和分数相结合,改变他们的行为,或者从查询切换到过滤器上下文。

18.1 constant_score 查询

包含另一个查询但在过滤器上下文中执行的查询。所有匹配的文档都给出相同的“常量” _score。

GET /_search
{
    "query": {
        "constant_score" : {
            "filter" : {
                "term" : { "user" : "jane"}
            },
            "boost" : 1.2
        }
    }
}
{
  "took": 4,
  "timed_out": false,
  "_shards": {
    "total": 19,
    "successful": 19,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 1.2,
    "hits": [
      {
        "_index": "my-index",
        "_type": "doc",
        "_id": "4",
        "_score": 1.2,
        "_source": {
          "user": [
            "jane",
            null
          ]
        }
      },
      {
        "_index": "my-index",
        "_type": "doc",
        "_id": "1",
        "_score": 1.2,
        "_source": {
          "user": "jane"
        }
      }
    ]
  }
}

18.2 bool查询(逻辑查询)

用于组合多个子查询,作为默认查询 must,should,must_not,或filter条款。在must和should 条款有他们的分数相结合-更匹配的条款,更好的-而must_not和filter条款在过滤器上下文中执行。

  • must 子句(查询)必须出现在匹配的文档中,并有助于得分。
  • filter 子句(查询)必须出现在匹配的文档中。但是不同于 must 查询的分数将会被忽略。过滤器子句在过滤器上下文中执行,这意味着忽略评分并考虑使用字句进行高速缓存。
  • should 子句(查询)应该出现在匹配的文档中。如果 bool 查询位于查询上下文中并且有must or filter 子句,则bool 即使没有任何should 查询匹配,文档也将与查询匹配。在这种情况下,这些条款仅用于影响分数。如果bool 查询时过滤器上下文,或者既没有must 或者filter 至少有一个should 查询必须匹配文档以使其与bool查询匹配。可以通过设置minimum_should_match 参数来明确控制此行为。
  • must 子句(查询)不得出现在匹配的文档中。子句在过滤器上下文中执行,这意味着忽略评分并考虑使用子句进行高速缓存。由于忽略了评分,因此0会返回所有文档的分数。
POST /_search
{
  "query": {
    "bool" : {
      "must" : {
        "term" : { "title" : "centos" }
      },
      "filter": {
        "term" : { "abstract" : "centos" }
      },
      "must_not" : {
        "range" : {
          "age" : { "gte" : 10, "lte" : 20 }
        }
      },
      "should" : [
        { "term" : { "abstract" : "yum" } },
        { "term" : { "abstract" : "elasticsearch" } }
      ],
      "minimum_should_match" : 1,
      "boost" : 1.0
    }
  }
}
{
  "took": 9,
  "timed_out": false,
  "_shards": {
    "total": 19,
    "successful": 19,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1.9753511,
    "hits": [
      {
        "_index": "website",
        "_type": "blog",
        "_id": "6",
        "_score": 1.9753511,
        "_source": {
          "title": "CentOS更换国内yum源",
          "author": "程裕强",
          "postdate": "2016-12-30",
          "abstract": "CentOS更换国内yum源",
          "url": "http://url.cn/53946911"
        }
      }
    ]
  }
}

18.3 dis_max 查询

一个接受多个查询的查询,并返回与任何查询子句匹配的任何文档。虽然bool查询组合了所有匹配查询的分数,但dis_max查询使用单个最佳匹配查询子句的分数。

GET /_search
{
    "query": {
        "dis_max" : {
            "tie_breaker" : 0.1,
            "boost" : 1.2,
            "queries" : [
                {
                    "match" : { "title" : "centos yum" }
                },
                {
                    "term" : { "age" : 29 }
                }
            ]
        }
    }
}
{
  "took": 18,
  "timed_out": false,
  "_shards": {
    "total": 19,
    "successful": 19,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 2.2146094,
    "hits": [
      {
        "_index": "website",
        "_type": "blog",
        "_id": "6",
        "_score": 2.2146094,
        "_source": {
          "title": "CentOS更换国内yum源",
          "author": "程裕强",
          "postdate": "2016-12-30",
          "abstract": "CentOS更换国内yum源",
          "url": "http://url.cn/53946911"
        }
      },
      {
        "_index": "my-index",
        "_type": "doc",
        "_id": "5",
        "_score": 1.2,
        "_source": {
          "age": 29
        }
      },
      {
        "_index": "website",
        "_type": "blog",
        "_id": "3",
        "_score": 0.3452185,
        "_source": {
          "title": "CentOS升级gcc",
          "author": "程裕强",
          "postdate": "2016-12-25",
          "abstract": "CentOS升级gcc",
          "url": "http://url.cn/53868915"
        }
      }
    ]
  }
}

18.4 fucntion_score 查询

使用函数修改主查询返回的分数,以考虑使用脚本实现的流行度,新近度,距离或自定义算法等因素。

18.5 boosting 查询

返回与positive 查询匹配的文档,但减少与nagative 查询匹配的文档的分数

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值