Module: Workarea::Search::QuerySuggestions

Included in:
ProductSearch
Defined in:
app/queries/workarea/search/query_suggestions.rb

Instance Method Summary collapse

Instance Method Details

#query_suggestion_responseObject



28
29
30
31
32
33
34
# File 'app/queries/workarea/search/query_suggestions.rb', line 28

def query_suggestion_response
  @query_suggestion_response ||=
    begin
      new_query = self.class.new(q: query_suggestions.first).body
      self.class.document.new.search(new_query)
    end
end

#query_suggestionsObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/queries/workarea/search/query_suggestions.rb', line 4

def query_suggestions
  @query_suggestions ||=
    begin
      return [] unless response['suggest'].present?

      phrase = response['suggest']['spelling_correction'].first
      original_query = QueryString.new(params[:q])

      if phrase.present?
        phrase['options']
          .select { |o| o['collate_match'] }
          .map { |o| o['text'] }
          .reject { |o| QueryString.new(o).id == original_query.id }
          .take(3)
      else
        []
      end
    end
end

#sanitized_queryObject



36
37
38
# File 'app/queries/workarea/search/query_suggestions.rb', line 36

def sanitized_query
  @sanitized_query ||= QueryString.new(params[:q]).sanitized
end

#should_use_suggestion?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'app/queries/workarea/search/query_suggestions.rb', line 24

def should_use_suggestion?
  total.zero? && query_suggestions.any?
end

#suggestObject

TODO: for v4, rework query to remove dependency on ProductDisplayRules Currently preventing non-product searches (i.e. content) from using query suggestions without stubbing #product_display_query_clauses.



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
# File 'app/queries/workarea/search/query_suggestions.rb', line 43

def suggest
  {
    spelling_correction: {
      text: query_string.sanitized,
      phrase: {
        field: 'suggestion_content',
        direct_generator: [
          {
            field: 'suggestion_content',
            min_doc_freq: Workarea.config.search_suggestion_min_doc_freq
          }
        ],
        collate: {
          prune: true,
          query: {
            bool: {
              must: product_display_query_clauses + [
                { match_phrase: { suggestion_content: '{{suggestion}}' } }
              ]
            }
          }.to_json
        }
      }
    }
  }
end