Class: TokenQueryBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/token_query_builder.rb

Class Method Summary collapse

Class Method Details

.constant_score_match_query(analyzer, query, key, alias_key = nil, boost = 1, options = {}) ⇒ Hash

constructs a constant_score wrapped match query based on the analyzer provided



72
73
74
# File 'lib/token_query_builder.rb', line 72

def self.constant_score_match_query(analyzer, query, key, alias_key = nil, boost = 1, options = {})
  cs_with_multiple_filter(analyzer, query, options, [key, alias_key], boost: boost)
end

.construct_match_filter(analyzer, field_name, query, opts) ⇒ Hash

constructs match filter based on the field_name and analyzer provided



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/token_query_builder.rb', line 10

def self.construct_match_filter(analyzer, field_name, query, opts)
  default_options = {fuzziness: 0, prefix_length: 1, max_expansions: 20, operator: "or"}
  opts = default_options.merge(opts)
  field = analyzer.present? ? "#{field_name}.#{analyzer}": "#{field_name}"
  {
    match: {
      field => {
        query: query,
        operator: opts[:operator],
        fuzziness: opts[:fuzziness],
        prefix_length: opts[:prefix_length]
      }
    }
  }
end

.cs_with_multiple_filter(analyzer, query, opts, keys = [], boost: 0) ⇒ Hash

constructs match filter based on keys array



50
51
52
53
54
55
56
57
# File 'lib/token_query_builder.rb', line 50

def self.cs_with_multiple_filter(analyzer, query, opts, keys = [], boost: 0)
  filters = []
  (keys.compact).each do |key|
    filter = construct_match_filter(analyzer, key, query, opts)
    filters.push(filter)
  end
  wrap_constant_score_query(filters: filters, boost: boost)
end

.cs_with_single_filter(query, key = "") ⇒ Object

constructs simgle term query



60
61
62
# File 'lib/token_query_builder.rb', line 60

def self.cs_with_single_filter(query, key = "")
  ElasticSearchQuery.get_term_filter_query key, query, true
end

.wrap_constant_score_query(filters: [], boost: 0) ⇒ Hash

wraps the query in the constant_score structure



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/token_query_builder.rb', line 30

def self.wrap_constant_score_query(filters: [], boost: 0)
  {
    constant_score: {
      boost: boost,
        filter: {
          bool: {
            should: filters
        }
      }
  }
}
end