Module: S3Search::API::Searches

Included in:
Client
Defined in:
lib/s3search/api/searches.rb

Instance Method Summary collapse

Instance Method Details

#search(options) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/s3search/api/searches.rb', line 17

def search options
  raise 'where: {field: "value"} is a required option' unless options[:where]
  query_terms = options.delete(:where).map do |field, value|
    {
      query_string: {
        default_field: field,
        query: value,
        default_operator: 'AND'
      }
    }
  end
  query = {
    query: {
      bool: {
        must: query_terms
      }
    }
  }

  page = options.delete(:page) || 1
  per_page = options.delete(:per_page) || 25

  query.merge!(options)
  response = _post '/v1/searches.json', {
    search: query,
    page: page,
    per_page: per_page
  }
  S3Search::ResultPage.new(response, self)
end

#simple_search(search_term, options = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/s3search/api/searches.rb', line 7

def simple_search search_term, options={}
  if search_term.include?(':')
    field, value = search_term.split(':')[0..1]
    options.merge!({where: { field => value }})
  else
    options.merge!({where: {_all: search_term }})
  end
  search options
end