从 Elasticsearch 中的 Nested 和 Aggregation 中移除选定的过滤器以执行过滤搜索

Remove selected filters from Nested and Aggregation in Elasticsearh to perform filtered search

我正在尝试使用 elasticsearch(版本 7.8.0)执行分面搜索,并且已经开始工作。但是,我想从返回的聚合中删除选定的过滤器。例如。如果我有一家卖衣服的商店,我按颜色过滤产品:红色然后我不希望颜色:红色出现在我的聚合中,因为已经选择了它。

为了执行我的过滤器,我在我的示例产品中使用了以下嵌套字段:

"search_filters" : {
    "type" : "nested",
    "properties" : {
        "key" : {
          "type" : "keyword"
        },
        "value" : {
          "type" : "keyword"
        }
     }
 },

所以如果我有这样的数据

 "search_filters" : [
     {
         "key" : "colour",
         "value" : "red"
     }
 ]

然后我执行如下搜索:

  {
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "search_filters",
            "query": {
              "bool": {
                "must": [
                  {
                    "match": {
                      "search_filters.key": "colour"
                    }
                  },
                  {
                    "match": {
                      "search_filters.value": "red"
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  },
  "aggs": {
    "filters": {
      "nested": {
        "path": "search_filters"
      },
      "aggs": {
        "search_keys": {
          "terms": {
            "field": "search_filters.key"
          },
          "aggs": {
            "search_values": {
              "terms": {
                "field": "search_filters.value"
              }
            }
          }
        }
      }
    }
  }
}

这给了我正确的文件,但我的聚合显示颜色:红色例如

{
  ...
  "filters": {
    "doc_count": 31,
    "search_keys": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "colour",
          "doc_count": 31,
          "search_values": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": "red",
                "doc_count": 31
              }
              ...
            ]
          }
        }
        ...
      ]
    }
  }
}

有没有办法将它们从 elasticsearch 中排除,或者我是否必须手动解析并 ignore/remove 我选择的过滤器?

我在请求的聚合字段部分看到了过滤器的使用,但我无法让它与我的过滤器字段一起使用;特别是当我应用多个过滤器时,例如颜色和尺寸。

您可以使用过滤器聚合,排除那些与 "search_filters.key": "colour" "search_filters.value": "red" 匹配的结果。这可以包含额外过滤器的额外必需品。

{
  "query": {
    "nested": {
      "path": "search_filters",
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "search_filters.key": "colour"
              }
            },
            {
              "match": {
                "search_filters.value": "red"
              }
            }
          ]
        }
      }
    }
  },
  "aggs": {
    "filters": {
      "nested": {
        "path": "search_filters"
      },
      "aggs": {
        "filterResult": {
          "filter": {
            "bool": {
              "must_not": {
                {
                  "bool": {
                    "must": [
                      {
                        "match": {
                          "search_filters.key": "colour"
                        }
                      },
                      {
                        "match": {
                          "search_filters.value": "red"
                      }
                    }
                  ]
                }
              }
            }
          },
          "aggs": {
            "search_keys": {
              "terms": {
                "field": "search_filters.key"
              },
              "aggs": {
                "search_values": {
                  "terms": {
                    "field": "search_filters.value"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

搜索结果将是

"hits": {
    "total": {
      "value": 1,
      "relation": "eq"
    },
    "max_score": 0.8754687,
    "hits": [
      {
        "_index": "66222818",
        "_type": "_doc",
        "_id": "1",
        "_score": 0.8754687,
        "_source": {
          "search_filters": [
            {
              "key": "colour",
              "value": "red"                 // note this
            }
          ]
        }
      }
    ]
  },
  "aggregations": {
    "filters": {
      "doc_count": 1,
      "filterResult": {
        "doc_count": 0,
        "search_keys": {
          "doc_count_error_upper_bound": 0,
          "sum_other_doc_count": 0,
          "buckets": []                                // note this
        } 
      }
    }
  }