Last active
April 21, 2019 02:48
-
-
Save renatosousafilho/9aa412f2adb05d43a7d5b7038a8b84e7 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
class ProductsSearch | |
include ActiveData::Model | |
attribute :query, type: String | |
attribute :tags, mode: :arrayed, type: String | |
attribute :brand, type: String | |
attribute :main_brand, type: String | |
attribute :gender, type: String | |
attribute :shipping_type, type: String | |
attribute :condition, type: String | |
attribute :size, type: String | |
attribute :min_price, type: Float | |
attribute :max_price, type: Float | |
attribute :category, type: String | |
attribute :main_category, type: String | |
attribute :ordering_field, type: String, enum: %w(title price), default: 'price' | |
attribute :ordering_sort, type: String, enum: %w(asc desc), default: 'desc' | |
def index | |
ProductsIndex | |
end | |
def search | |
result = [query_string, store_status_filter, status_filter, tags_string, category_filter, gender_filter, condition_filter, brand_filter, shipping_type_filter, size_filter, sorting].compact.reduce(:merge) | |
end | |
def query_string | |
index.query({multi_match: {query: query,type: "phrase_prefix",fields: ["title^10"], operator: "and"}}) if query? | |
end | |
def tags_string | |
index.filter(term: {tags: tags}) if tags? | |
end | |
def brand_filter | |
return build_filter(term: {brand: main_brand}) if main_brand? | |
build_filter(term: {brand: brand}) if brand? | |
end | |
def gender_filter | |
build_filter(term: {gender: gender}) if gender? | |
end | |
def status_filter | |
build_filter(term: {status: 'active'}) | |
end | |
def store_status_filter | |
build_filter(term: {'store.status': 'active'}) | |
end | |
def shipping_type_filter | |
build_filter(term: {shipping_type: shipping_type}) if shipping_type? | |
end | |
def condition_filter | |
build_filter(term: {condition: condition}) if condition? | |
end | |
def size_filter | |
build_filter(term: {size: size}) if size? | |
end | |
def price_filter | |
body = {}.tap do |body| | |
body.merge!(gte: min_price) if min_price? | |
body.merge!(lte: max_price) if max_price? | |
end | |
build_filter(range: {price: body}) if body.present? | |
end | |
def category_filter | |
return build_filter(term: {'category.slug': main_category}) if main_category? | |
build_filter(term: {'category.slug': category}) if category? | |
end | |
def sorting | |
index.order({"#{ordering_field}.sorted": ordering_sort}) | |
end | |
private | |
def build_filter(filtering) | |
index.filter(filtering) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment