-
-
Save julianalucena/5aee5bbb8fb4fe4acdd4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module BookSearchable | |
# (...) | |
module ClassMethods | |
def search_fulltext(term, opts = {}) | |
options = { | |
page: 1, | |
size: Rails.configuration.results_count, | |
} | |
options.merge!(opts) | |
options[:page] = options[:page].to_i | |
options[:size] = options[:size].to_i | |
page = options[:page] | |
page = 1 if page == 0 | |
options[:order] ||= {} | |
default_facet_filter = [] | |
default_facet_filter << BookSearchable.inactives?(false) | |
default_facet_filter << BookSearchable.country(options[:country]) | |
price_filter = [BookSearchable.price(options[:price])] | |
sellers_filter = [BookSearchable.publishers(options[:publishers])] | |
category_filter = [BookSearchable.category_id(options[:category_id])] | |
category_facet_filter = \ | |
default_facet_filter | sellers_filter | price_filter | |
publishers_facet_filter = \ | |
default_facet_filter | category_filter | price_filter | |
price_statistics_facet_filter = \ | |
default_facet_filter | sellers_filter | category_filter | |
s = Tire.search(Offer.index_name, | |
query: { | |
bool: { | |
should: [ | |
{ match: { category: { query: term, operator: "AND", boost: 20 } } }, | |
{ match: { name: { query: term, operator: "AND", boost: 5 } } }, | |
{ match: { 'name.partial' => { query: term, operator: 'AND', boost: 4 } } }, | |
{ match: { 'name.partial_middle' => { query: term, operator: 'AND', boost: 2 } } }, | |
{ match: { 'name.partial_back' => { query: term, operator: 'AND', boost: 4 } } } | |
] | |
} | |
}, | |
filter: { | |
and: [ | |
BookSearchable.inactives?(false), | |
BookSearchable.country(options[:country]), | |
BookSearchable.category_id(options[:category_id]), | |
BookSearchable.publishers(options[:publishers]), | |
BookSearchable.price(options[:price]), | |
] | |
}, | |
facets: { | |
category_id: { | |
facet_filter: { and: category_facet_filter }, | |
terms: { field: "categories_ids", size: 100, all_terms: false } | |
}, | |
publisher: { | |
facet_filter: { and: publishers_facet_filter }, | |
terms: { field: "seller_name", size: 10, all_terms: false } | |
}, | |
price_statistics: { | |
facet_filter: { and: price_statistics_facet_filter }, | |
statistical: { field: "price" } | |
} | |
}, | |
size: options[:size], | |
from: (page.to_i - 1) * options[:size], | |
sort: options[:order] | |
) | |
s.results | |
end | |
def inactives?(status) | |
{ term: { inactive: status } } | |
end | |
def country(country) | |
{ term: { country: country } } | |
end | |
def category_id(category_id) | |
if category_id.present? | |
{ term: { categories_ids: category_id } } | |
else | |
{} | |
end | |
end | |
def publishers(publishers) | |
if publishers.present? | |
{ terms: { publisher: publishers } } | |
else | |
{} | |
end | |
end | |
def price(price) | |
if price.present? | |
if price[:min].present? and price[:max].present? | |
{ range: { price: { gte: price[:min], lte: price[:max] } } } | |
elsif price[:min].present? | |
{ range: { price: { gte: price[:min] } } } | |
elsif price[:max].present? | |
{ range: { price: { lte: price[:max] } } } | |
else | |
{} | |
end | |
else | |
{} | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment