Elasticsearch查询+过滤器

时间:2017-01-24 07:55:24

标签: elasticsearch

这是我原来的查询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的分数对结果进行排序(我不知道为什么它不是desc),有文件得分为0。

我认为查询不再进行过滤,因为它已经过滤了。

我可以从结果中删除0个得分文件吗?

1 个答案:

答案 0 :(得分:0)

要添加过滤器,请在bool查询中使用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
                        }
                    }
                }
            ]
        }
    }
}
相关问题