如何从构面中排除过滤器?

gjb*_*gjb 2 elasticsearch

我来自Solr背景,我试图在Elasticsearch中找到相当于"标记"和"排除"的东西.

在以下示例中,如何pricepricesfacet 的计算中排除过滤器?换句话说,pricesfacet应该考虑除了之外的所有过滤器price.

{
  query : {
    "filtered" : {
      "query" : {
        "match_all" : {}
      },
      "filter" : {
        "and" : [
          {
            "term" : {
              "colour" : "Red"
            }
          },
          {
            "term" : {
              "feature" : "Square"
            }
          },
          {
            "term" : {
              "feature" : "Shiny"
            }
          },
          {
            "range" : {
              "price" : { 
                "from" : "10",
                "to" : "20"
              }
            }
          }
        ]
      }
    }
  },
  "facets" : {
    "colours" : {
      "terms" : {
        "field" : "colour"
      }
    },
    "features" : {
      "terms" : {
        "field" : "feature"
      }
    },
    "prices" : {
      "statistical" : {
        "field" : "price"
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

imo*_*tov 5

您可以将价格过滤器应用为查询的顶级过滤器,并将其添加到所有方面,期望价格为facet_filter:

{
  query : {
    "filtered" : {
      "query" : {
        "match_all" : {}
      },
      "filter" : {
        "and" : [
          {
            "term" : {
              "colour" : "Red"
            }
          },
          {
            "term" : {
              "feature" : "Square"
            }
          },
          {
            "term" : {
              "feature" : "Shiny"
            }
          }
        ]
      }
    }
  },
  "facets" : {
    "colours" : {
      "terms" : {
        "field" : "colour"
      },
      "facet_filter" : {
        "range" : { "price" : {  "from" : "10", "to" : "20" } }
      }
    },
    "features" : {
      "terms" : {
        "field" : "feature"
      },
      "facet_filter" : {
        "range" : { "price" : {  "from" : "10", "to" : "20" } }
      }
    },
    "prices" : {
      "statistical" : {
        "field" : "price"
      }
    }
  },
  "filter": {
    "range" : { "price" : {  "from" : "10", "to" : "20" } }
  }
}
Run Code Online (Sandbox Code Playgroud)