class EagleSearch::Interpreter::Query

Public Class Methods

new(index, query, options) click to toggle source
# File lib/eagle_search/interpreter/query.rb, line 3
def initialize(index, query, options)
  @index = index
  @query = query
  @options = options
end

Public Instance Methods

payload() click to toggle source
# File lib/eagle_search/interpreter/query.rb, line 9
def payload
  case @query
  when String
    if @query == "*"
      { match_all: {} }
    else
      query_payload
    end
  end
end

Private Instance Methods

analyzed_properties() click to toggle source
# File lib/eagle_search/interpreter/query.rb, line 25
def analyzed_properties
  properties.select { |field_name, field_hash| field_hash[:type] == "string" && field_hash[:index] == "analyzed" }
end
build_match_queries() click to toggle source
# File lib/eagle_search/interpreter/query.rb, line 59
def build_match_queries
  return unless analyzed_properties

  match_queries = []
  analyzed_properties.keys.each do |field_name|
    match_queries << {
      match: {
        "#{ field_name }.shingle" => {
          query: @query
        }
      }
    }

    match_queries << {
      match: {
        "#{ field_name }.synonym" => {
          query: @query
        }
      }
    } if @index.has_synonyms?

    match_queries << {
      match: {
        "#{ field_name }.autocomplete" => {
          query: @query
        }
      }
    } if @options[:autocomplete].nil? || @options[:autocomplete]

    match_queries << {
      match: {
        "#{ field_name }" => {
          query: @query,
          fuzziness: "AUTO"
        }
      }
    }
  end

  payload = {
    bool: {
      should: match_queries
    }
  }

  @query_payload[:bool] ? @query_payload[:bool][:should] << payload : @query_payload[:bool][:should] = payload
end
build_multi_match_query() click to toggle source
# File lib/eagle_search/interpreter/query.rb, line 41
def build_multi_match_query
  if analyzed_properties
    @query_payload = {
      bool: {
        should: []
      }
    }

    @query_payload[:bool][:should] << {
      multi_match: {
        query: @query,
        fields: @options[:fields] || analyzed_properties.keys,
        tie_breaker: 0.3
      }
    }
  end
end
build_term_queries() click to toggle source
# File lib/eagle_search/interpreter/query.rb, line 107
def build_term_queries
  return if not_analyzed_properties.empty?

  term_queries = []
  not_analyzed_properties.keys.each do |field_name|
    term_queries << {
      term: {
        field_name => {
          value: @query,
          boost: 3
        }
      }
    }
  end

  payload = {
    bool: {
      should: term_queries
    }
  }

  if @query_payload[:bool]
    @query_payload[:bool][:should] << payload
  else
    @query_payload = payload
  end
end
not_analyzed_properties() click to toggle source
# File lib/eagle_search/interpreter/query.rb, line 29
def not_analyzed_properties
  properties.select { |field_name, field_hash| field_hash[:type] == "string" && field_hash[:index] == "not_analyzed" }
end
properties() click to toggle source
# File lib/eagle_search/interpreter/query.rb, line 21
def properties
  @index.mappings[@index.type_name][:properties]
end
query_payload() click to toggle source
# File lib/eagle_search/interpreter/query.rb, line 33
def query_payload
  @query_payload = {}
  build_multi_match_query
  build_match_queries
  build_term_queries
  @query_payload
end