极客ES11-Query与Filter实现多字段查询

Query与Filter实现多字段查询

Query和Filter

在这里插入图片描述

过滤和查询的区别

  • 过滤不计算得分,查询计算得分
  • 过滤可以使用缓存,查询不会使用缓冲
  • 综上2点,过滤的性能好于查询的性能

Bool查询

在这里插入图片描述
当遇到多种条件组和时,我们需要用到bool查询,其中mustshould属于Query Context,贡献得分,而must_notfilter属于Filter Context,不贡献得分。

注意:当Bool查询中不含有must时,此时的should查询必须至少匹配其中一条

Bool查询的语法

在这里插入图片描述

解决结构化查询中出现的Term是“包含”而不是“相等”的問題

在这里插入图片描述在这里插入图片描述

算分影响

在这里插入图片描述
在这里插入图片描述

Bool嵌套

在这里插入图片描述

使用查询结构修改相关度

https://www.elastic.co/guide/cn/elasticsearch/guide/current/query-scoring.html

在这里插入图片描述
在以上查询语句中,redbrown是近义词,但是这个查询语句的逻辑是:
brown OR red OR quick OR dog
也因此, brown OR red OR quick 和 brown OR dog OR quick 得到的相关度是一样的,而这显然是不合理的,我们希望后者能有更高的相关度,因为redbrown是近义词。
因此我们将bool嵌套,使 brown OR red 和dog处于同一级别。嵌套后的Bool逻辑是:
quick OR dog OR (brown OR red)

使用Boost控制查询相关度

https://www.elastic.co/guide/cn/elasticsearch/guide/current/query-time-boosting.html在这里插入图片描述

Not Quite Not

https://www.elastic.co/guide/cn/elasticsearch/guide/current/not-quite-not.html

  • 插入数据
DELETE news
POST /news/_bulk
{ "index": { "_id": 1 }}
{ "content":"Apple Mac" }
{ "index": { "_id": 2 }}
{ "content":"Apple iPad" }
{ "index": { "_id": 3 }}
{ "content":"Apple employee like Apple Pie and Apple Juice" }
  • 搜索只与苹果公司产品相关的结果
DELETE news
POST /news/_bulk
{ "index": { "_id": 1 }}
{ "content":"Apple Mac" }
{ "index": { "_id": 2 }}
{ "content":"Apple iPad" }
{ "index": { "_id": 3 }}
{ "content":"Apple employee like Apple Pie and Apple Juice" }

返回结果

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.16786805,
    "hits" : [
      {
        "_index" : "news",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.16786805,
        "_source" : {
          "content" : "Apple Mac"
        }
      },
      {
        "_index" : "news",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.16786805,
        "_source" : {
          "content" : "Apple iPad"
        }
      }
    ]
  }
}
  • 但是我们并不想去除掉那些包含Apple但是不与苹果公司产品相关的结果,而只是想让他们的排名靠后,这时我们要使用到,boosting查询
POST news/_search
{
  "query": {
    "boosting": {
      "positive": {
        "match": {
          "content": "apple"
        }
      },
      "negative": {
        "match": {
          "content": "pie"
        }
      },
      "negative_boost": 0.5
    }
  }
}

boosting查询会将那些除positive外同时匹配到negative的文档的权重乘以negative_boost(必须在0-1之间),以此来降低这些文档的权重,但又不将至排除

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值