0
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

when i use advanced search using body.. it does not return the desired results although the limit:10 part us working as it does return 10 results only

Jamil Khan
  • 83
  • 1
  • 7

3 Answers3

0

I believe there is some missing information in the documentation.

Here's a reference to a body query that works based on the tests written in SearchKick: https://github.com/ankane/searchkick/blob/c8f8dc65df2e85b97ea508e14ded299bb8111942/test/index_test.rb#L47

For advanced search to work, the way it should be written is:

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

You need a query key following the body.

Jon
  • 241
  • 2
  • 7
0

// conditions = {}

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

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

query.body[:size] = 10

query.execute
Dias
  • 862
  • 8
  • 17
0

You would need to build your query using the Elasticsearch DSL. Specifically, using size and match.

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

When using Advanced search, Searchkick ignores parameters outside the body hash. While the body hash allows you to use the full ES DSL.

wanyama_man
  • 476
  • 4
  • 5