Class: Nazrin::SearchClient

Inherits:
Object
  • Object
show all
Defined in:
lib/nazrin/search_client.rb

Constant Summary collapse

CLOUD_SEARCH_MAX_LIMIT =
10_000

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = Nazrin.config) ⇒ SearchClient

Returns a new instance of SearchClient.



11
12
13
14
15
16
17
18
19
20
# File 'lib/nazrin/search_client.rb', line 11

def initialize(config=Nazrin.config)
  # @see http://docs.aws.amazon.com/sdkforruby/api/Aws/CloudSearchDomain/Client.html aws-sdk
  @client = Aws::CloudSearchDomain::Client.new(
    endpoint: config.search_endpoint,
    region: config.region,
    access_key_id: config.access_key_id,
    secret_access_key: config.secret_access_key,
    logger: config.logger)
  @parameters = {}
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



8
9
10
# File 'lib/nazrin/search_client.rb', line 8

def client
  @client
end

#data_accessorObject

Returns the value of attribute data_accessor.



7
8
9
# File 'lib/nazrin/search_client.rb', line 7

def data_accessor
  @data_accessor
end

#parametersObject (readonly)

Returns the value of attribute parameters.



9
10
11
# File 'lib/nazrin/search_client.rb', line 9

def parameters
  @parameters
end

Instance Method Details

#cursor(cursor) ⇒ Object

set the cursor

Parameters:

  • cursor (String)

    cursor



59
60
61
62
# File 'lib/nazrin/search_client.rb', line 59

def cursor(cursor)
  parameters[:cursor] = cursor
  self
end

#executeObject



119
120
121
122
123
124
125
126
# File 'lib/nazrin/search_client.rb', line 119

def execute
  return fake_response if Nazrin.config.mode == 'sandbox'
  if data_accessor
    data_accessor.results(self)
  else
    search
  end
end

#expr(expr) ⇒ Object

define any expression

Parameters:

  • expr (String)

    ex) “‘EXPRESSIONNAME’:‘EXPRESSION’”



108
109
110
111
# File 'lib/nazrin/search_client.rb', line 108

def expr(expr)
  parameters[:expr] = expr
  self
end

#facet(facet) ⇒ Object

facet

Parameters:

  • facet (String)

    ex) “‘year’:{‘sort’:‘bucket’}”



101
102
103
104
# File 'lib/nazrin/search_client.rb', line 101

def facet(facet)
  parameters[:facet] = facet
  self
end

#filter_query(filter_query) ⇒ Object

query filtering

Parameters:

  • filter_query (String)

    “tags:‘aaa’”



80
81
82
83
# File 'lib/nazrin/search_client.rb', line 80

def filter_query(filter_query)
  parameters[:filter_query] = filter_query
  self
end

#highlight(highlight) ⇒ Object

highlight

Parameters:

  • highlight (String)

    “‘tags’:{}”



94
95
96
97
# File 'lib/nazrin/search_client.rb', line 94

def highlight(highlight)
  parameters[:highlight] = highlight
  self
end

#partial(partial) ⇒ Object

partial

Parameters:

  • partial (Boolean)

    true or false



73
74
75
76
# File 'lib/nazrin/search_client.rb', line 73

def partial(partial)
  parameters[:partial] = partial
  self
end

#query(query) ⇒ Object

query

Parameters:

  • query (String)

    query string



24
25
26
27
# File 'lib/nazrin/search_client.rb', line 24

def query(query)
  parameters[:query] = query
  self
end

#query_options(query_options) ⇒ Object

query options

Parameters:

  • query_options (String)

    ex) target field “'title'”



87
88
89
90
# File 'lib/nazrin/search_client.rb', line 87

def query_options(query_options)
  parameters[:query_options] = query_options
  self
end

#query_parser(query_parser) ⇒ Object

set the parser to be used

Parameters:

  • parser (String)

    ‘simple’, ‘structured’, ‘lucene’, dismax’



45
46
47
48
# File 'lib/nazrin/search_client.rb', line 45

def query_parser(query_parser)
  parameters[:query_parser] = query_parser
  self
end

#return(fields) ⇒ Object

return fields

Parameters:

  • fields (Array<String>)

    ex) [‘title’]



31
32
33
34
# File 'lib/nazrin/search_client.rb', line 31

def return(fields)
  parameters[:return] = fields.join(',')
  self
end

#searchObject



113
114
115
116
117
# File 'lib/nazrin/search_client.rb', line 113

def search
  return fake_response if Nazrin.config.mode == 'sandbox'
  fail SearchClientError if deep_page?
  client.search(parameters)
end

#size(size) ⇒ Object

set the number to get

Parameters:

  • size (Integer)

    the number to get



38
39
40
41
# File 'lib/nazrin/search_client.rb', line 38

def size(size)
  parameters[:size] = size
  self
end

#sort(sorts) ⇒ Object

sort

Parameters:

  • sorts (Array<String>)

    ex) [‘year desc’]



66
67
68
69
# File 'lib/nazrin/search_client.rb', line 66

def sort(sorts)
  parameters[:sort] = sorts.join(',')
  self
end

#start(start) ⇒ Object

set the search start position

Parameters:

  • start (Integer)

    start position



52
53
54
55
# File 'lib/nazrin/search_client.rb', line 52

def start(start)
  parameters[:start] = start
  self
end