如何过滤这些Elasticsearch聚合?

时间:2015-05-17 20:50:26

标签: ruby-on-rails elasticsearch

我正在使用这个rails gem:

https://github.com/elastic/elasticsearch-rails

我正在尝试构建一个分面导航,用户可以按颜色,大小等进行过滤。

我正在设置我的aggs(这是代码的相关代码段):

    aggs: {
      key: {
        terms: {
          field: "features.key",
          exclude: options[:key] # These are excluded because I want all the aggs that have not been selected, so I can display the remaining aggs for the user.
        },
        aggs: {
          value: {
            terms: {
              field: "features.value",
              exclude: options[:value]
            }
          }
        }
      }
    }
  }

  # These are filtering what the user selects, to return products matching these feature key/value pairs
  __set_filters.(:features, { term: { "key" => options[:key] } })
  __set_filters.(:features, { term: { "value" => options[:value] } })

我这样搜索:

Product.search("", {key: :size, value: :small}

这是产生宝石的查询对象:

=> #<Elasticsearch::Model::Response::Response:0x007fbf38d01ea8
 @klass=
  [PROXY] Product(id: integer, name: string, sku: string, description: text, tenant_id: integer, created_at: datetime, updated_at: datetime, meta_keywords: string, meta_description: string, base_price_cents: integer, base_price_currency: string, discount_price_cents: integer, discount_price_currency: string, retail_price_cents: integer, retail_price_currency: string, quantity: integer, parent_id: integer),
 @search=
  #<Elasticsearch::Model::Searching::SearchRequest:0x007fbf38c81c08
   @definition=
    {:index=>"swift_platform_application_development",
     :type=>"product",
     :body=>
      {:query=>{:bool=>{:should=>[{:multi_match=>{:query=>"Movies", :fields=>["name^10", "features^2", "categories", "description"], :operator=>"and"}}]}},
       :filter=>{:and=>[{:term=>{"key"=>:size}}, {:term=>{"value"=>:small}}, {:term=>{"tenant_id"=>1}}]},
       :aggs=>{:key=>{:terms=>{:field=>"features.key", :exclude=>:size}, :aggs=>{:value=>{:terms=>{:field=>"features.value", :exclude=>:small}}}}}}},
   @klass=
    [PROXY] Product(id: integer, name: string, sku: string, description: text, tenant_id: integer, created_at: datetime, updated_at: datetime, meta_keywords: string, meta_description: string, base_price_cents: integer, base_price_currency: string, discount_price_cents: integer, discount_price_currency: string, retail_price_cents: integer, retail_price_currency: string, quantity: integer, parent_id: integer),
   @options={}>>

以下是实际回复

[313] pry(main)> a.response["aggregations"]
=> {"key"=>
  {"doc_count_error_upper_bound"=>0,
   "sum_other_doc_count"=>0,
   "buckets"=>
    [{"key"=>"shape",
      "doc_count"=>41,
      "value"=>
       {"doc_count_error_upper_bound"=>0,
        "sum_other_doc_count"=>0,
        "buckets"=>
         [{"key"=>"square", "doc_count"=>17},
          {"key"=>"oval", "doc_count"=>14},
          {"key"=>"medium", "doc_count"=>11},
          {"key"=>"green", "doc_count"=>10},
          {"key"=>"large", "doc_count"=>10},
          {"key"=>"round", "doc_count"=>10},
          {"key"=>"blue", "doc_count"=>9},
          {"key"=>"orange", "doc_count"=>7},
          {"key"=>"red", "doc_count"=>6}]}},
     {"key"=>"color",
      "doc_count"=>37,
      "value"=>
       {"doc_count_error_upper_bound"=>0,
        "sum_other_doc_count"=>0,
        "buckets"=>
         [{"key"=>"square", "doc_count"=>14},
          {"key"=>"blue", "doc_count"=>12},
          {"key"=>"oval", "doc_count"=>12},
          {"key"=>"green", "doc_count"=>11},
          {"key"=>"medium", "doc_count"=>10},
          {"key"=>"large", "doc_count"=>8},
          {"key"=>"orange", "doc_count"=>8},
          {"key"=>"red", "doc_count"=>6},
          {"key"=>"round", "doc_count"=>6}]}}]}}

所以“关键”aggs shapecolor具有相同的“价值”aggs。但我希望shape仅包含square, oval, round, etc.color,仅包含red, blue, orange, etc.

我做错了什么?

谢谢!

0 个答案:

没有答案