class EagleSearch::Index

Attributes

alias_name[R]
settings[R]

Public Class Methods

new(klass, settings) click to toggle source
# File lib/eagle_search/index.rb, line 8
def initialize(klass, settings)
  @klass = klass
  @settings = settings
end

Public Instance Methods

create() click to toggle source
# File lib/eagle_search/index.rb, line 13
def create
  EagleSearch.client.indices.create index: name, body: body
end
delete() click to toggle source
# File lib/eagle_search/index.rb, line 17
def delete
  EagleSearch.client.indices.delete index: alias_name
end
has_synonyms?() click to toggle source
# File lib/eagle_search/index.rb, line 81
def has_synonyms?
  @settings[:synonyms]
end
info() click to toggle source
# File lib/eagle_search/index.rb, line 25
def info
  EagleSearch.client.indices.get index: alias_name
end
mappings() click to toggle source
# File lib/eagle_search/index.rb, line 63
def mappings
  if @settings[:mappings]
    @settings[:mappings]
  else
    base_mappings = {
      type_name => {
        properties: {}
      }
    }

    columns.each do |column|
      base_mappings[type_name][:properties][column.name] = EagleSearch::Field.new(self, column).mapping
    end

    base_mappings
  end
end
name() click to toggle source
# File lib/eagle_search/index.rb, line 29
def name
  @name ||= @settings[:index_name] || "#{ alias_name }_#{ DateTime.now.strftime('%Q') }"
end
refresh() click to toggle source
# File lib/eagle_search/index.rb, line 21
def refresh
  EagleSearch.client.indices.refresh index: alias_name
end
reindex() click to toggle source
# File lib/eagle_search/index.rb, line 45
def reindex
  client = EagleSearch.client
  begin
    aliases = client.indices.get_alias name: alias_name
    client.indices.delete index: aliases.keys.join(",")
  rescue
    #do something
  ensure
    create
    bulk = []
    @klass.all.each do |record|
      bulk << { index: { _index: alias_name, _type: type_name, _id: record.id } }
      bulk << record.index_data
    end
    client.bulk body: bulk
  end
end
type_name() click to toggle source
# File lib/eagle_search/index.rb, line 37
def type_name
  if @settings[:mappings]
    @settings[:mappings].keys.first.downcase
  else
    @klass.model_name.param_key
  end
end

Private Instance Methods

analysis_settings() click to toggle source
# File lib/eagle_search/index.rb, line 98
def analysis_settings
  default_analysis = {
    filter: {
      eagle_search_shingle_filter: {
        type: "shingle",
        min_shingle_size: 2,
        max_shingle_size: 2,
        output_unigrams: false
      },
      eagle_search_autocomplete_filter: {
        type: "edge_ngram",
        min_gram: 1,
        max_gram: 20
      }
    },
    analyzer: {
      eagle_search_shingle_analyzer: {
        type: "custom",
        tokenizer: "standard",
        filter: [
          "lowercase",
          "eagle_search_shingle_filter"
        ]
      },
      eagle_search_autocomplete_analyzer: {
        tokenizer: "standard",
        filter: ["lowercase", "eagle_search_autocomplete_filter"]
      }
    }
  }
  merge_synonyms!(default_analysis)
  default_analysis
end
body() click to toggle source
# File lib/eagle_search/index.rb, line 86
def body
  body = {
    mappings: mappings,
    aliases: { alias_name => {} },
    settings: {
      analysis: analysis_settings
    }
  }
  body[:settings][:number_of_shards] = 1 if EagleSearch.env == "test" || EagleSearch.env == "development"
  body
end
klass() click to toggle source
# File lib/eagle_search/index.rb, line 163
def klass
  @klass
end
merge_synonyms!(default_analysis) click to toggle source
# File lib/eagle_search/index.rb, line 132
def merge_synonyms!(default_analysis)
  if has_synonyms?
    default_analysis[:filter]["eagle_search_synonym_filter"] = {}
    default_analysis[:analyzer].merge!(
      {
        eagle_search_synonym_analyzer: {
          tokenizer: "standard",
          filter: ["lowercase", "eagle_search_synonym_filter"]
        }
      }
    )

    default_analysis[:filter]["eagle_search_synonym_filter"].merge!(
      synonym_payload(@settings[:synonyms])
    )
  end
end
synonym_payload(synonyms) click to toggle source
# File lib/eagle_search/index.rb, line 150
def synonym_payload(synonyms)
  if synonyms.is_a?(Array)
    {
      type: "synonym",
      synonyms: synonyms
    }
  else
    {
      type: "synonym"
    }.merge(synonyms)
  end
end