Class: Workarea::Search::Storefront::CategoryQuery

Inherits:
Object
  • Object
show all
Defined in:
app/models/workarea/search/storefront/category_query.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(category) ⇒ CategoryQuery

Returns a new instance of CategoryQuery.



79
80
81
# File 'app/models/workarea/search/storefront/category_query.rb', line 79

def initialize(category)
  @category = category
end

Instance Attribute Details

#categoryObject (readonly)

Returns the value of attribute category.



8
9
10
# File 'app/models/workarea/search/storefront/category_query.rb', line 8

def category
  @category
end

Class Method Details

.find!(options) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/models/workarea/search/storefront/category_query.rb', line 26

def find!(options)
  results = Storefront.current_index.search(
    size: Workarea.config.product_categories_by_rules_max_count,
    query: {
      percolate: options.merge(
        field: 'query',
        index: Storefront.current_index.name,
        type: Storefront.type,
        document_type: 'category'
      )
    },
    post_filter: if Release.current.blank?
      {
        bool: {
          minimum_should_match: 1,
          should: [
            { term: { release_id: 'live' } },
            { bool: { must_not: { exists: { field: 'release_id' } } } } # for upgrade compatiblity
          ]
        }
      }
    else
      {
        bool: {
          minimum_should_match: 1,
          should: [
            { term: { release_id: Release.current.id } },
            {
              bool: {
                must_not: [{ term: { changeset_release_ids: Release.current.id } }],
                must: [
                  {
                    bool: {
                      minimum_should_match: 1,
                      should: [
                        { term: { release_id: 'live' } },
                        { bool: { must_not: { exists: { field: 'release_id' } } } } # for upgrade compatiblity
                      ]
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    end
  )

  results['hits']['hits'].map { |h| h['_id'].split('-').first }
end

.find_by_product(product) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/models/workarea/search/storefront/category_query.rb', line 11

def find_by_product(product)
  product = product.in_release(Release.current) if product.persisted?
  search_model = Product.new(product, skip_categorization: true)

  begin
    find!(id: search_model.id)
  rescue
    begin
      find!(document: search_model.as_document)
    rescue ::Elasticsearch::Transport::Transport::ServerError
      []
    end
  end
end

Instance Method Details

#createObject



88
89
90
91
92
93
# File 'app/models/workarea/search/storefront/category_query.rb', line 88

def create
  I18n.for_each_locale do
    create_live
    create_releases
  end
end

#deleteObject



95
96
97
98
99
100
101
102
103
# File 'app/models/workarea/search/storefront/category_query.rb', line 95

def delete
  I18n.for_each_locale do
    begin
      Storefront.current_index.delete(category.id, type: 'category')
    rescue ::Elasticsearch::Transport::Transport::Errors::NotFound
      # doesn't matter we want it deleted
    end
  end
end

#updateObject



83
84
85
86
# File 'app/models/workarea/search/storefront/category_query.rb', line 83

def update
  delete
  create unless category.product_rules.blank?
end