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

时间:2013-03-26 23:16:45

标签: elasticsearch

我来自Solr背景,我试图在Elasticsearch中找到相当于"tagging" and "excluding"的内容。

在以下示例中,如何从price方面的计算中排除prices过滤器?换句话说,prices方面应考虑除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"
      }
    }
  }
}

2 个答案:

答案 0 :(得分: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" } }
  }
}

答案 1 :(得分:0)

顺便说一下,自ES 1.0.0以来的重大变化。顶级过滤器已重命名为post_filter(http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/_search_requests.html#_search_requests)。使用过滤后的查询仍然是首选,如下所述:http://elasticsearch-users.115913.n3.nabble.com/Filters-vs-Queries-td3219558.html

对于facet,global选项可以避免按查询过滤器进行过滤(elasticsearch.org/guide/en/elasticsearch/reference/current/search-facets.html#_scope)。