Class: Estella::Query

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Query

Returns a new instance of Query.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/estella/query.rb', line 7

def initialize(params)
  @params = params
  @query = {
    _source: false,
    query: {},
    filter: {
      bool: { must: [], must_not: [] }
    },
    aggregations: {}
  }
  add_query
  add_filters
  add_pagination
  add_aggregations if params[:aggregations]
  add_sort
end

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



5
6
7
# File 'lib/estella/query.rb', line 5

def params
  @params
end

#queryObject

Constructs a search query for ES



4
5
6
# File 'lib/estella/query.rb', line 4

def query
  @query
end

Instance Method Details

#add_aggregationsObject

override if needed



25
# File 'lib/estella/query.rb', line 25

def add_aggregations; end

#add_field_boostObject



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/estella/query.rb', line 72

def add_field_boost
  if params[:boost]
    query[:query][:function_score][:field_value_factor] = {
      field: params[:boost][:field],
      modifier: params[:boost][:modifier],
      factor: params[:boost][:factor]
    }

    if params[:boost][:max]
      query[:query][:function_score][:max_boost] = params[:boost][:max]
    end
  end
end

#add_filtersObject



105
106
107
108
109
110
111
# File 'lib/estella/query.rb', line 105

def add_filters
  if params[:indexed_fields]
    params[:indexed_fields].each do |field, opts|
      must term: { field => params[field] } if opts[:filter] && params[field]
    end
  end
end

#add_paginationObject



38
39
40
41
# File 'lib/estella/query.rb', line 38

def add_pagination
  query[:size] = params[:size] if params[:size]
  query[:from] = params[:from] if params[:from]
end

#add_queryObject



43
44
45
46
47
48
49
# File 'lib/estella/query.rb', line 43

def add_query
  if params[:term] && params[:indexed_fields]
    add_term_query
  else
    query[:query] = { match_all: {} }
  end
end

#add_sortObject

override if needed



28
# File 'lib/estella/query.rb', line 28

def add_sort; end

#add_term_queryObject

fulltext search across all string fields



52
53
54
55
56
57
58
59
60
# File 'lib/estella/query.rb', line 52

def add_term_query
  query[:query] = {
    function_score: {
      query: query_definition
    }
  }

  add_field_boost
end

#exclude(filter) ⇒ Object



34
35
36
# File 'lib/estella/query.rb', line 34

def exclude(filter)
  query[:filter][:bool][:must_not] << filter
end

#field_factorsObject



86
87
88
# File 'lib/estella/query.rb', line 86

def field_factors
  Estella::Analysis::DEFAULT_FIELD_FACTORS
end

#must(filter) ⇒ Object



30
31
32
# File 'lib/estella/query.rb', line 30

def must(filter)
  query[:filter][:bool][:must] << filter
end

#query_definitionObject



62
63
64
65
66
67
68
69
70
# File 'lib/estella/query.rb', line 62

def query_definition
  {
    multi_match: {
      type: 'most_fields',
      fields: term_search_fields,
      query: params[:term]
    }
  }
end

#term_search_fieldsObject

search all analysed string fields by default boost them by factor if provided



92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/estella/query.rb', line 92

def term_search_fields
  params[:indexed_fields]
    .select { |_, opts| opts[:type].to_s == 'string' }
    .reject { |_, opts| opts[:analysis].nil? }
    .map do |field, opts|
      opts[:analysis].map do |analyzer|
        factor = field_factors[analyzer] * opts.fetch(:factor, 1.0)
        "#{field}.#{analyzer}^#{factor}"
      end
    end
    .flatten
end