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

Parameters:

  • analyzer (String)

    Type of analyzer to be used (Analyzer provided here must be defined in the index definition first)

  • query (String)

    input data

  • key (String)

    field to be searched

  • alias_key (String) (defaults to: nil)

    alias of the field to be searched

  • boost (Float) (defaults to: 1)

    to influence relevance score

  • options (Hash) (defaults to: {})

    options to be provided for search (eg. 0, prefix_length: 1, max_expansions: 20, operator: “and”)

Returns:

  • (Hash)


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

Parameters:

  • analyzer (String)

    Type of analyzer to be used

  • field_name (String)

    field to be searched

  • query (String)

    input data

  • opts (Hash)

    options to be provided for search (eg. 0, prefix_length: 1, max_expansions: 20, operator: “and”)

Returns:

  • (Hash)


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

Parameters:

  • analyzer (String)

    Type of analyzer to be used

  • query (String)

    input data

  • opts (Hash)

    options to be provided for search (eg. 0, prefix_length: 1, max_expansions: 20, operator: “and”)

  • keys (Array) (defaults to: [])

    fields to be queried

  • boost (Float) (defaults to: 0)

    to influence the relevance score

Returns:

  • (Hash)


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

Parameters:

  • filters (Array) (defaults to: [])

    filters to be included

  • boost (Float) (defaults to: 0)

    to influence the relevance score

Returns:

  • (Hash)


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