Class: EagleSearch::Index

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, settings) ⇒ Index

Returns a new instance of Index.



8
9
10
11
# File 'lib/eagle_search/index.rb', line 8

def initialize(klass, settings)
  @klass = klass
  @settings = settings
end

Instance Attribute Details

#alias_nameObject (readonly)

Returns the value of attribute alias_name.



5
6
7
# File 'lib/eagle_search/index.rb', line 5

def alias_name
  @alias_name
end

#settingsObject (readonly)

Returns the value of attribute settings.



5
6
7
# File 'lib/eagle_search/index.rb', line 5

def settings
  @settings
end

Instance Method Details

#createObject



13
14
15
# File 'lib/eagle_search/index.rb', line 13

def create
  EagleSearch.client.indices.create index: name, body: body
end

#deleteObject



17
18
19
# File 'lib/eagle_search/index.rb', line 17

def delete
  EagleSearch.client.indices.delete index: alias_name
end

#has_synonyms?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/eagle_search/index.rb', line 81

def has_synonyms?
  @settings[:synonyms]
end

#infoObject



25
26
27
# File 'lib/eagle_search/index.rb', line 25

def info
  EagleSearch.client.indices.get index: alias_name
end

#mappingsObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# 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

#nameObject



29
30
31
# File 'lib/eagle_search/index.rb', line 29

def name
  @name ||= @settings[:index_name] || "#{ alias_name }_#{ DateTime.now.strftime('%Q') }"
end

#refreshObject



21
22
23
# File 'lib/eagle_search/index.rb', line 21

def refresh
  EagleSearch.client.indices.refresh index: alias_name
end

#reindexObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# 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_nameObject



37
38
39
40
41
42
43
# 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