searchkick高级搜索无法正常工作

时间:2015-01-16 15:44:57

标签: ruby-on-rails elasticsearch searchkick

class Product < ActiveRecord::Base
belongs_to :city
has_and_belongs_to_many :categories
before_destroy { categories.clear }
searchkick locations: ["location"]

def search_data
    {
        istatus: i_status,  
        name: name,
        price: price,
        city_id: city_id,
        value: value,
        discount: discount,
        expiry_date: expiry_date,
        created_at: created_at,
        products_sold: products_sold, 
        city: city.name,
        deal_type: deal_type,
        country: city.country.name,
        category_id: categories.map(&:id),
        location: [latitude, longitude]
    }
end

def self.apply_filters(request)
    # @product = Product.search "Tex-Mex", limit:10  #=>this works
    @product = Product.search body: {match: {name: "Tex-Mex"}},limit: 10 #=>does not work, the limit part work
    end
end

当我使用身体进行高级搜索时...它没有返回所需的结果,尽管限制:10部分我们正在工作,因为它确实只返回10个结果

3 个答案:

答案 0 :(得分:0)

我相信文档中缺少一些信息。

这里是对基于SearchKick中编写的测试工作的正文查询的引用: https://github.com/ankane/searchkick/blob/c8f8dc65df2e85b97ea508e14ded299bb8111942/test/index_test.rb#L47

要使高级搜索起作用,应该采用的方式是:

@product = Product.search body: { query: { match: {name: "Tex-Mex"}}},limit: 10

query后面需要body键。

答案 1 :(得分:0)

// conditions = {}

query = Product.search params[:query], execute: false, where : conditions

query.body[:query] =  { match: {name: "Tex-Mex"} }

query.body[:size] = 10

query.execute

答案 2 :(得分:0)

您需要使用Elasticsearch DSL构建查询。具体而言,使用sizematch

Product.search body: { query: { match: {name: "Tex-Mex"} }, size: 10 }

使用高级搜索时,Searchkick会忽略正文哈希之外的参数。身体哈希允许您使用完整的ES DSL。