Module: Gummi::DbLayer::Index::ClassMethods

Defined in:
lib/gummi/db_layer/index.rb

Instance Method Summary collapse

Instance Method Details

#clientObject



147
148
149
# File 'lib/gummi/db_layer/index.rb', line 147

def client
  Gummi.client
end

#default_settingsObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/gummi/db_layer/index.rb', line 48

def default_settings
  {
    index: {
      # Main Settings
      number_of_shards: '1',
      number_of_replicas: (Gummi.env == 'production' ? '2' : '0'),
      refresh_interval: '1s',
      store: { type: (Gummi.env == 'test' ? :memory : :niofs) },
      mapper: { dynamic: false },

      analysis: {

        # Tokenizers are just some sort of "tool" or "module"  that can be applied to analyzers.
        tokenizer: {
          # This one is a little bit more general and is able to chop any word into all of its components.
          ngram_tokenizer: {
            type: 'nGram',
            min_gram: 1,
            max_gram: 7,
            token_chars: [ 'letter', 'digit' ],
          }

        },

        # Now we are ready to use our tokenizers.
        # Let's create the most important thing: Analyzers.
        analyzer: {

          path_hierarchy_analyzer: {
            type: 'custom',
            tokenizer: 'path_hierarchy',
          },
          # When adding long text to Elastic, we most likely are going to use this
          # analyzer. This is commonly used for titles and descriptions.
          text_index_analyzer: {
            type: 'custom',
            tokenizer: 'ngram_tokenizer',  # Chopping every word up into tokens
            filter: {
              0 => 'standard',       # Some default transformations
              1 => 'lowercase',      # Make everything lowercase
              2 => 'word_delimiter', # E.g. "O'Neil" -> "O Neil", "Victoria's" -> "Victoria"
              3 => 'asciifolding',   # Transform everything into ASCII
            },
          },

          # For smaller texts, such as the city "stockholm", we don't want any
          # tokenizing. It's enough to explicitly save the word as it is.
          # As a matter of fact, if we would tokenize the city, then the facets
          # would report that we have Transports in "st" "sto" "stoc" etc.
          string_index_analyzer: {
            type: 'custom',
            tokenizer: 'standard',
            filter: {
              # The filters, however, are identical to the other analyzer.
              0 => 'standard',
              1 => 'lowercase',
              2 => 'word_delimiter',
              3 => 'asciifolding',
            },
          },

          # For finding Slugs
          keyword_index_analyzer: {
            type: 'custom',
            tokenizer: 'keyword',
            filter: {
              0 => 'lowercase',
              1 => 'asciifolding',
            },
          },

          # This is an analyzer that we apply to the search query itself.
          text_search_analyzer: {
            type: 'custom',
            tokenizer: 'standard',
            filter: {
              0 => 'standard',
              1 => 'lowercase',
              2 => 'word_delimiter',
              3 => 'asciifolding',
            },
          },

          # This is an analyzer that we apply to the search query itself.
          keyword_search_analyzer: {
            type: 'custom',
            tokenizer: 'keyword',
            filter: {
              0 => 'lowercase',
              1 => 'asciifolding',
            },
          },

        }
      }
    }
  }
end

#nameObject



33
34
35
# File 'lib/gummi/db_layer/index.rb', line 33

def name
  raise "Implement me"
end

#refreshObject



37
38
39
40
41
42
# File 'lib/gummi/db_layer/index.rb', line 37

def refresh
  ActiveSupport::Notifications.instrument "search.elasticsearch", name: "Index(#{self.name}).refresh", search: {} do
    client.indices.refresh
    client.cluster.health wait_for_status: :yellow
  end
end

#settingsObject



44
45
46
# File 'lib/gummi/db_layer/index.rb', line 44

def settings
  default_settings
end

#setupObject

Return true if created or false if already created.



10
11
12
13
14
15
16
# File 'lib/gummi/db_layer/index.rb', line 10

def setup
  created_settings = client.indices.create index: name, body: { settings: settings }
  created_settings.present?
  refresh
rescue ::Elasticsearch::Transport::Transport::Errors::BadRequest => exception
  false
end

#teardownObject

Return true if successful or already teared down.

Raises NotImplementedError in production.



22
23
24
25
26
27
28
29
30
31
# File 'lib/gummi/db_layer/index.rb', line 22

def teardown
  raise NotImplementedError if Gummi.env == 'production'

  response = ActiveSupport::Notifications.instrument "search.elasticsearch", name: "Index(#{self.name}).teardown", search: {} do
    client.indices.delete index: name
  end
  response.present?
rescue ::Elasticsearch::Transport::Transport::Errors::NotFound
  true
end