【问题标题】:Building the right search request with gem Tire and ElasticSearch使用 gem Tire 和 ElasticSearch 构建正确的搜索请求
【发布时间】:2012-10-30 02:56:46
【问题描述】:

我正在尝试为我的 Flower Store 实现简单的搜索请求,但由于我是轮胎和 ElasticSearch 的新手,所以我不知道该怎么做。

我有可搜索的模型 Product,以及 habtm 模型 CategoryColourFlower。我想要的是每个关联的复选框,产生与此类似的请求:

http://example.com/search?q=some%20query&category_names[]=bouquet&category_names[]=marriage&colour_names[]=red&colour_names[]=yellow&colour_names[]=white&flower_names[]=rose&flower_names[]=tulip&price_min=100&price_max=1000

但我只需要展示以下产品:

a) prestent in any of the requested categories (OR);
b) have all of the requested colours (AND);
c) have all of the requested flowers (AND);
d) enter the price range between price_min and price_max.

q 参数是搜索任何文本,输入到文本字段中,在关联的名称中(比如说,red roses bouquet),并用上面的标准显示它。

目前我只有

class Product
  include Mongoid::Document

  include Tire::Model::Search
  include Tire::Model::Callbacks

  has_and_belongs_to_many :categories
  has_and_belongs_to_many :colours
  has_and_belongs_to_many :flowers

  def self.search(params)
    tire.search(load: true, page: params[:page], per_page: 8) do |search|
      search.query  { string params[:query] } if params[:query].present?
    end
  end

  def to_indexed_json
    self.to_json(methods: [:category_names, :colour_names])
  end

  def category_names
    self.categories.collect { |c| c.name }
  end

  def colour_names
    self.colours.collect { |c| c.name }
  end
end

不知道怎么配置。我读到了filtersfacets,但由于英语不是我的母语,我无法理解它们是什么以及如何使用它们。

【问题讨论】:

    标签: ruby-on-rails elasticsearch tire


    【解决方案1】:

    我这样做了:

    def self.search(params)
      tire.search(load: true, page: params[:page], per_page: 8) do
        query do
          boolean do
            must { string params[:query], default_operator: "AND" } if params[:query].present?
            must { string params[:category_names] } if params[:category_names].present?
            must { string params[:colour_names], default_operator: "AND" } if params[:colour_names].present?
            must { range :price, { gte: params[:price_min], lte: params[:price_max] } } if params[:price_min].present? && params[:price_max].present?
          end
        end
    
        # raise to_curl
      end
    end
    

    【讨论】:

      猜你喜欢
      • 2012-06-10
      • 1970-01-01
      • 2012-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-09
      • 2012-04-25
      • 1970-01-01
      相关资源
      最近更新 更多