Module: ProxES::Services::Search

Extended by:
ES
Defined in:
lib/proxes/services/search.rb

Class Method Summary collapse

Methods included from ES

client, conn, ssl_store

Class Method Details

.fields(index: '_all', names_only: false) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/proxes/services/search.rb', line 17

def fields(index: '_all', names_only: false)
  fields = {}
  client.indices.get_mapping(index: index).each do |_idx, index_map|
    index_map['mappings'].each do |_type, type_map|
      next if type_map['properties'].nil?

      type_map['properties'].each do |name, details|
        if details['type'] != 'keyword' && details['fields'] && (names_only == false)
          keyword = details['fields'].find do |v|
            v[1]['type'] == 'keyword'
          end
          fields["#{name}.#{keyword[0]}"] ||= keyword[1]['type'] if keyword
        end
        fields[name] ||= details['type'] unless details['type'].nil?
      end
    end.to_h
  end
  fields
end

.indicesObject



13
14
15
# File 'lib/proxes/services/search.rb', line 13

def indices
  client.indices.get_mapping(index: '_all', ignore: 404).keys
end

.search(qs, options = {}) ⇒ Object



42
43
44
# File 'lib/proxes/services/search.rb', line 42

def search(qs, options = {})
  client.search options.merge(q: qs) # , explain: true
end

.values(field, index = '_all', size = 25) ⇒ Object



37
38
39
40
# File 'lib/proxes/services/search.rb', line 37

def values(field, index = '_all', size = 25)
  result = client.search index: index, body: { size: 0, aggs: { values: { terms: { field: field, size: size } } } }
  result['aggregations']['values']['buckets'].map { |e| e['key'] }
end