首页 文章

带有过滤器的Elasticsearch bool查询

提问于
浏览
1

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

更具体地说,我所做的查询在这里:

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

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

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

如何按顺序重新编写此查询:1)获取attr1和类别精确的所有行2)然后使用should查询这些结果

有什么好主意吗?

1 回答

  • 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"
                }
              }
            ]
          }
        }
      }
    }
    

相关问题