2012-10-25 49 views
0

我想爲我的Flower Store實現簡單的搜索請求,但由於我是Tire和ElasticSearch的新手,我無法得到如何操作。使用寶石輪胎和ElasticSearch構建正確的搜索請求

我有可搜索的模型Product和habtm模型CategoryColour,Flower。我要的是每個關聯複選框,生產要求類同此:

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,但因爲英文不是我的母語,所以我不能理解什麼是什麼以及如何使用它們。

回答

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 
+1

你應該接受你雁 – concept47

相關問題