带有过滤器的Elasticsearch bool查询

时间:2016-01-16 17:45:26

标签: elasticsearch

我正在尝试编写2部分的Elasticsearch bool查询。 我想要两个条件为“必须”,两个条件为“应该”。问题是我想只为“应该”得分。我试过“过滤器”但没有成功。

更具体地说,我所做的查询是:

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "attr1": "on"
          }
        },
        {
          "match": {
            "category": "7eb84c804a8c824fd608e05f78d42f10"
          }
        }
      ],
      "should": [
        {
          "term": {
            "attr2": "on"
          }
        },
        {
          "term": {
            "attr3": "on"
          }
        }
      ],
      "minimum_should_match": "10%"
    }
  }
}

更新 这是失败的查询,我想找到错误!:

  {
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "attr1": "on"
          }
        },
        {
          "term": {
            "category": "7eb84c804a8c824fd608e05f78d42f10"
          }
        }
      ],
      "should": [
        {
          "term": {
            "attr2": "on"
          }
        },
        {
          "term": {
            "attr3": "on"
          }
        }
      ],
      "minimum_should_match": "10%"
    }
  }
}

如何按顺序重写此查询: 1)获取attr1和类别的所有行 2)然后用

查询这些结果

请问好吗?

1 个答案:

答案 0 :(得分:3)

好像你不在Elasticsearch 2.x上。

对于Elasticsearch 1.x,请使用FilteredQuery:https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-filtered-query.html

{
  "filtered": {
    "query": {
      "bool": {
        "should": [
          {
            "term": {
              "attr2": "on"
            }
          },
          {
            "term": {
              "attr3": "on"
            }
          }
        ]
      }
    },
    "filter": {
      "bool": {
        "must": [
          {
            "match": {
              "attr1": "on"
            }
          },
          {
            "match": {
              "category": "7eb84c804a8c824fd608e05f78d42f10"
            }
          }
        ]
      }
    }
  }
}
相关问题