简体   繁体   中英

ElasticSearch / tire histogram usage, how to return certain values from basket?

I'm having trouble figuring out how to use histogram. I have a price field in my model and I want to group results with some interval for example in 400 units. Page should have links: 400, 800, 1200, 1600. On clicking link results should be filtered with this price group.(Same thing with dates)
In model I have:

def self.search params
      tire.search(page: params[:page], per_page: 20) do
        query do
          boolean do
           ...
            must { term :price, params[:price] } if params[:price].present?
            must { term :date_from, params[:dates]   } if params[:dates].present?
          end
        end


        facet("prices")       {  histogram :price, interval: 400, order: 'key'  }
        facet('dates')       {  date :date_from, interval: '3w', order: 'time'}
      end
    end

It gives me right facets:

{"_type"=>"histogram", "entries"=>[{"key"=>600, "count"=>20}, {"key"=>1800, "count"=>30}]}

but how can I use it with params? My implementation gives me only one field with exact match. I'll be grateful for any advice.

After making some research (a lot), I realized that the only way of getting range of values is "range" with gt/gte/lt/lte ( http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-range-query.html )

So answer on my own question is:

must { range :price, gte: params[:price], lt: params[:price].to_i + 400  } if params[:price].present?
must { range :date_from, gte: params[:dates], lt: Time.at(params[:dates].to_i / 1000) + 3.weeks   } if params[:dates].present?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM