2

我来自 Solr 背景,并试图在 Elasticsearch中找到“标记”和“排除”的等价物。

在以下示例中,如何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"
      }
    }
  }
}
4

2 回答 2

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" } }
  }
}
于 2013-03-27T00:02:22.387 回答
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

并且有一个global选项可以避免通过查询过滤器进行过滤(elasticsearch.org/guide/en/elasticsearch/reference/current/search-facets.html#_scope)。

于 2014-06-04T03:22:04.037 回答