1+ # frozen_string_literal: true
2+
3+ class HotelSearch
4+ attr_accessor :name , :title , :description , :country , :city , :state
5+
6+ def initialize ( attributes )
7+ @name = attributes [ 'name' ]
8+ @title = attributes [ 'title' ]
9+ @description = attributes [ 'description' ]
10+ @country = attributes [ 'country' ]
11+ @city = attributes [ 'city' ]
12+ @state = attributes [ 'state' ]
13+ end
14+
15+ def self . search_name ( name )
16+ request = Couchbase ::SearchRequest . new (
17+ Couchbase ::SearchQuery . match ( name ) { |obj | obj . field = "name" }
18+ )
19+ options = Couchbase ::Options ::Search . new
20+ options . limit = 50
21+ options . fields = [ "name" ]
22+ result = INVENTORY_SCOPE . search ( INDEX_NAME , request , options )
23+ result . rows . map do |row |
24+ row . fields [ "name" ]
25+ end
26+ end
27+
28+ def self . filter ( hotel_search , offset , limit )
29+ query = Couchbase ::SearchQuery . conjuncts ( )
30+
31+ query . and_also (
32+ Couchbase ::SearchQuery . term ( hotel_search . name ) { |obj | obj . field = "name_keyword" }
33+ ) if hotel_search . name
34+
35+ query . and_also (
36+ Couchbase ::SearchQuery . match ( hotel_search . title ) { |obj | obj . field = "title" }
37+ ) if hotel_search . title
38+
39+ query . and_also (
40+ Couchbase ::SearchQuery . match ( hotel_search . description ) { |obj | obj . field = "description" }
41+ ) if hotel_search . description
42+
43+ query . and_also (
44+ Couchbase ::SearchQuery . match ( hotel_search . country ) { |obj | obj . field = "country" }
45+ ) if hotel_search . country
46+
47+ query . and_also (
48+ Couchbase ::SearchQuery . match ( hotel_search . state ) { |obj | obj . field = "state" }
49+ ) if hotel_search . state
50+
51+ query . and_also (
52+ Couchbase ::SearchQuery . match ( hotel_search . city ) { |obj | obj . field = "city" }
53+ ) if hotel_search . city
54+
55+ request = Couchbase ::SearchRequest . new ( query )
56+
57+ options = Couchbase ::Options ::Search . new
58+ options . skip = 0
59+ options . limit = 50
60+ options . skip = offset if offset
61+ options . limit = limit if limit
62+ options . fields = [ "*" ]
63+ options . sort = [ "-_score" , "name_keyword" ]
64+
65+ result = INVENTORY_SCOPE . search ( INDEX_NAME , request , options )
66+ result . rows . map do |row |
67+ new ( row . fields )
68+ end
69+ end
70+ end
71+
0 commit comments