【问题标题】:Elasticsearch query + filterElasticsearch 查询 + 过滤器
【发布时间】:2017-01-24 17:02:34
【问题描述】:

这是我的原始查询 dsl,总点击量为 8,981。

GET /{index}/{document}/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "title": {
              "query": "blue shoes",
              "boost": 2
            }
          }
        },
        {
          "match": {
            "description": {
              "query": "blue shoes",
              "operator": "and",
              "boost": 1
            }
          }
        }
      ]
    }
  }
}

我想为此查询添加过滤器。

GET /{index}/{document}/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "title": {
              "query": "blue shoes",
              "boost": 2
            }
          }
        },
        {
          "match": {
            "description": {
              "query": "blue shoes",
              "operator": "and",
              "boost": 1
            }
          }
        }
      ],
      "filter": {
        "terms": {
          "store.id": [ "store_a.com", "store_b.com" ]
        }
      }
    }
  }
}

现在它的点击总数为 15,989(increased)。 我在 asc 中按分数对结果进行排序(我不知道为什么它是 asc 而不是 desc),有些文档得分为 0。

我认为没有更多的查询过滤,因为它已经被过滤了。

我可以从结果中删除 0 个评分的文档吗?

【问题讨论】:

  • 出于好奇,我的回答有帮助还是我错过了什么?
  • @nikoshr 对我迟到的回复感到抱歉。我通过将匹配项之一(在本例中为“标题”)从“应该”移动到“必须”来解决了这个问题。

标签: elasticsearch


【解决方案1】:

要添加过滤器,请在布尔查询中使用 must clause 来添加强制值。试试看:

GET /{index}/{document}/_search
{
    "query": {
        "bool": {
            "must": [
                "terms": {
                    "store.id": [ "store_a.com", "store_b.com" ]
                }
            ],
            "should": [
                {
                    "match": {
                        "title": {
                            "query": "blue shoes",
                            "boost": 2
                        }
                    }
                },
                {
                    "match": {
                        "description": {
                            "query": "blue shoes",
                            "operator": "and",
                            "boost": 1
                        }
                    }
                }
            ]
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-17
    • 1970-01-01
    • 2020-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多