@@ -174,7 +174,7 @@ def analytics_query(statement, options = Options::Analytics::DEFAULT)
174174 #
175175 # @param [String] index_name the name of the search index
176176 # @param [SearchQuery] query the query tree
177- # @param [Options::Search] options the query tree
177+ # @param [Options::Search] options the custom options for this search query
178178 #
179179 # @example Return first 10 results of "hop beer" query and request highlighting
180180 # cluster.search_query("beer_index", Cluster::SearchQuery.match_phrase("hop beer"),
@@ -187,90 +187,23 @@ def analytics_query(statement, options = Options::Analytics::DEFAULT)
187187 #
188188 # @return [SearchResult]
189189 def search_query ( index_name , query , options = Options ::Search ::DEFAULT )
190- resp = @backend . document_search ( index_name , JSON . generate ( query ) , options . to_backend )
190+ resp = @backend . document_search ( index_name , JSON . generate ( query ) , { } , options . to_backend )
191+ convert_search_result ( resp , options )
192+ end
191193
192- SearchResult . new do |res |
193- res . meta_data = SearchMetaData . new do |meta |
194- meta . metrics . max_score = resp [ :meta_data ] [ :metrics ] [ :max_score ]
195- meta . metrics . error_partition_count = resp [ :meta_data ] [ :metrics ] [ :error_partition_count ]
196- meta . metrics . success_partition_count = resp [ :meta_data ] [ :metrics ] [ :success_partition_count ]
197- meta . metrics . took = resp [ :meta_data ] [ :metrics ] [ :took ]
198- meta . metrics . total_rows = resp [ :meta_data ] [ :metrics ] [ :total_rows ]
199- meta . errors = resp [ :meta_data ] [ :errors ]
200- end
201- res . rows = resp [ :rows ] . map do |r |
202- SearchRow . new do |row |
203- row . transcoder = options . transcoder
204- row . index = r [ :index ]
205- row . id = r [ :id ]
206- row . score = r [ :score ]
207- row . fragments = r [ :fragments ]
208- unless r [ :locations ] . empty?
209- row . locations = SearchRowLocations . new (
210- r [ :locations ] . map do |loc |
211- SearchRowLocation . new do |location |
212- location . field = loc [ :field ]
213- location . term = loc [ :term ]
214- location . position = loc [ :position ]
215- location . start_offset = loc [ :start_offset ]
216- location . end_offset = loc [ :end_offset ]
217- location . array_positions = loc [ :array_positions ]
218- end
219- end
220- )
221- end
222- row . instance_variable_set ( :@fields , r [ :fields ] )
223- row . explanation = JSON . parse ( r [ :explanation ] ) if r [ :explanation ]
224- end
225- end
226- if resp [ :facets ]
227- res . facets = resp [ :facets ] . each_with_object ( { } ) do |( k , v ) , o |
228- facet = case options . facets [ k ]
229- when SearchFacet ::SearchFacetTerm
230- SearchFacetResult ::TermFacetResult . new do |f |
231- f . terms =
232- if v [ :terms ]
233- v [ :terms ] . map do |t |
234- SearchFacetResult ::TermFacetResult ::TermFacet . new ( t [ :term ] , t [ :count ] )
235- end
236- else
237- [ ]
238- end
239- end
240- when SearchFacet ::SearchFacetDateRange
241- SearchFacetResult ::DateRangeFacetResult . new do |f |
242- f . date_ranges =
243- if v [ :date_ranges ]
244- v [ :date_ranges ] . map do |r |
245- SearchFacetResult ::DateRangeFacetResult ::DateRangeFacet . new ( r [ :name ] , r [ :count ] , r [ :start_time ] , r [ :end_time ] )
246- end
247- else
248- [ ]
249- end
250- end
251- when SearchFacet ::SearchFacetNumericRange
252- SearchFacetResult ::NumericRangeFacetResult . new do |f |
253- f . numeric_ranges =
254- if v [ :numeric_ranges ]
255- v [ :numeric_ranges ] . map do |r |
256- SearchFacetResult ::NumericRangeFacetResult ::NumericRangeFacet . new ( r [ :name ] , r [ :count ] , r [ :min ] , r [ :max ] )
257- end
258- else
259- [ ]
260- end
261- end
262- else
263- next # ignore unknown facet result
264- end
265- facet . name = v [ :name ]
266- facet . field = v [ :field ]
267- facet . total = v [ :total ]
268- facet . missing = v [ :missing ]
269- facet . other = v [ :other ]
270- o [ k ] = facet
271- end
272- end
273- end
194+ # Performs a request against the Full Text Search (FTS) service.
195+ #
196+ # @api volatile
197+ #
198+ # @param [String] index_name the name of the search index
199+ # @param [SearchRequest] search_request the request
200+ # @param [Options::Search] options the custom options for this search request
201+ #
202+ # @return [SearchResult]
203+ def search ( index_name , search_request , options = Options ::Search ::DEFAULT )
204+ encoded_query , encoded_req = search_request . to_backend
205+ resp = @backend . document_search ( index_name , encoded_query , encoded_req , options . to_backend ( show_request : false ) )
206+ convert_search_result ( resp , options )
274207 end
275208
276209 # @return [Management::UserManager]
@@ -427,6 +360,92 @@ def initialize(connection_string, *args)
427360 @backend . open ( connection_string , credentials , open_options )
428361 end
429362
363+ # @api private
364+ def convert_search_result ( resp , options )
365+ SearchResult . new do |res |
366+ res . meta_data = SearchMetaData . new do |meta |
367+ meta . metrics . max_score = resp [ :meta_data ] [ :metrics ] [ :max_score ]
368+ meta . metrics . error_partition_count = resp [ :meta_data ] [ :metrics ] [ :error_partition_count ]
369+ meta . metrics . success_partition_count = resp [ :meta_data ] [ :metrics ] [ :success_partition_count ]
370+ meta . metrics . took = resp [ :meta_data ] [ :metrics ] [ :took ]
371+ meta . metrics . total_rows = resp [ :meta_data ] [ :metrics ] [ :total_rows ]
372+ meta . errors = resp [ :meta_data ] [ :errors ]
373+ end
374+ res . rows = resp [ :rows ] . map do |r |
375+ SearchRow . new do |row |
376+ row . transcoder = options . transcoder
377+ row . index = r [ :index ]
378+ row . id = r [ :id ]
379+ row . score = r [ :score ]
380+ row . fragments = r [ :fragments ]
381+ unless r [ :locations ] . empty?
382+ row . locations = SearchRowLocations . new (
383+ r [ :locations ] . map do |loc |
384+ SearchRowLocation . new do |location |
385+ location . field = loc [ :field ]
386+ location . term = loc [ :term ]
387+ location . position = loc [ :position ]
388+ location . start_offset = loc [ :start_offset ]
389+ location . end_offset = loc [ :end_offset ]
390+ location . array_positions = loc [ :array_positions ]
391+ end
392+ end
393+ )
394+ end
395+ row . instance_variable_set ( :@fields , r [ :fields ] )
396+ row . explanation = JSON . parse ( r [ :explanation ] ) if r [ :explanation ]
397+ end
398+ end
399+ if resp [ :facets ]
400+ res . facets = resp [ :facets ] . each_with_object ( { } ) do |( k , v ) , o |
401+ facet = case options . facets [ k ]
402+ when SearchFacet ::SearchFacetTerm
403+ SearchFacetResult ::TermFacetResult . new do |f |
404+ f . terms =
405+ if v [ :terms ]
406+ v [ :terms ] . map do |t |
407+ SearchFacetResult ::TermFacetResult ::TermFacet . new ( t [ :term ] , t [ :count ] )
408+ end
409+ else
410+ [ ]
411+ end
412+ end
413+ when SearchFacet ::SearchFacetDateRange
414+ SearchFacetResult ::DateRangeFacetResult . new do |f |
415+ f . date_ranges =
416+ if v [ :date_ranges ]
417+ v [ :date_ranges ] . map do |r |
418+ SearchFacetResult ::DateRangeFacetResult ::DateRangeFacet . new ( r [ :name ] , r [ :count ] , r [ :start_time ] , r [ :end_time ] )
419+ end
420+ else
421+ [ ]
422+ end
423+ end
424+ when SearchFacet ::SearchFacetNumericRange
425+ SearchFacetResult ::NumericRangeFacetResult . new do |f |
426+ f . numeric_ranges =
427+ if v [ :numeric_ranges ]
428+ v [ :numeric_ranges ] . map do |r |
429+ SearchFacetResult ::NumericRangeFacetResult ::NumericRangeFacet . new ( r [ :name ] , r [ :count ] , r [ :min ] , r [ :max ] )
430+ end
431+ else
432+ [ ]
433+ end
434+ end
435+ else
436+ next # ignore unknown facet result
437+ end
438+ facet . name = v [ :name ]
439+ facet . field = v [ :field ]
440+ facet . total = v [ :total ]
441+ facet . missing = v [ :missing ]
442+ facet . other = v [ :other ]
443+ o [ k ] = facet
444+ end
445+ end
446+ end
447+ end
448+
430449 # @api private
431450 ClusterOptions = ::Couchbase ::Options ::Cluster
432451 # @api private
0 commit comments