Class: Couchbase::Protostellar::ResponseConverter::Search Private

Inherits:
Object
  • Object
show all
Defined in:
lib/couchbase/protostellar/response_converter/search.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Class Method Summary collapse

Class Method Details

.convert_facet_result(proto_facet_res) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/couchbase/protostellar/response_converter/search.rb', line 70

def self.convert_facet_result(proto_facet_res)
  facet_type = proto_facet_res.search_facet
  case facet_type
  when :term_facet
    res = proto_facet_res.term_facet
    Couchbase::Cluster::SearchFacetResult::TermFacetResult.new do |f|
      f.name = res.name
      f.field = res.field
      f.total = res.total
      f.missing = res.missing
      f.other = res.other
      f.terms = res.terms.map do |term_res|
        Couchbase::Cluster::SearchFacetResult::TermFacetResult::TermFacet.new(term_res.name, term_res.size)
      end
    end
  when :date_range_facet
    res = proto_facet_res.date_range_facet
    Couchbase::Cluster::SearchFacetResult::DateRangeFacetResult.new do |f|
      f.name = res.name
      f.field = res.field
      f.total = res.total
      f.missing = res.missing
      f.other = res.other
      f.date_ranges = res.date_ranges.map do |date_range|
        start_time = Time.at(date_range.start.seconds).strftime("%Y-%m-%d")
        end_time = Time.at(date_range.end.seconds).strftime("%Y-%m-%d")
        Couchbase::Cluster::SearchFacetResult::DateRangeFacetResult::DateRangeFacet.new(
          date_range.name, date_range.size, start_time, end_time
        )
      end
    end
  when :numeric_range_facet
    res = proto_facet_res.numeric_range_facet
    Couchbase::Cluster::SearchFacetResult::NumericRangeFacetResult.new do |f|
      f.name = res.name
      f.field = res.field
      f.total = res.total
      f.missing = res.missing
      f.other = res.other
      f.numeric_ranges = res.numeric_ranges.map do |numeric_range|
        Couchbase::Cluster::NumericRangeResult::NumericRangeFacetResult::NumericRangeFacet.new(
          numeric_range.name, numeric_range.size, numeric_range.min, numeric_range.max
        )
      end
    end
  else
    raise ProtostellarError, "Unrecognised facet type"
  end
end

.convert_fragments(proto_fragments) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



55
56
57
# File 'lib/couchbase/protostellar/response_converter/search.rb', line 55

def self.convert_fragments(proto_fragments)
  proto_fragments.to_h.transform_values { |f| f.content.to_a }
end

.convert_meta_data(proto_meta_data) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/couchbase/protostellar/response_converter/search.rb', line 120

def self.()
  Couchbase::Cluster::SearchMetaData.new do |meta|
    proto_metrics = .metrics
    dur = proto_metrics.execution_time
    meta.metrics.took = (dur.seconds * 1000) + (dur.nanos / 1000.0).round # `took` is in milliseconds
    meta.metrics.total_rows = proto_metrics.total_rows
    meta.metrics.max_score = proto_metrics.max_score
    meta.metrics.success_partition_count = proto_metrics.success_partition_count
    meta.metrics.error_partition_count = proto_metrics.error_partition_count

    meta.errors = .errors.to_h
  end
end

.convert_search_row(proto_row, options) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/couchbase/protostellar/response_converter/search.rb', line 39

def self.convert_search_row(proto_row, options)
  Couchbase::Cluster::SearchRow.new do |r|
    r.instance_variable_set(:@fields, (proto_row.fields.to_h.transform_values { |v| JSON.parse(v) }).to_json)
    r.transcoder = options.transcoder
    r.index = proto_row.index
    r.id = proto_row.id
    r.score = proto_row.score.to_f
    unless proto_row.locations.empty?
      r.locations = Couchbase::Cluster::SearchRowLocations.new(
        proto_row.locations.map { |loc| convert_search_row_location(loc) }
      )
    end
    r.fragments = convert_fragments(proto_row.fragments)
  end
end

.convert_search_row_location(proto_location) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



59
60
61
62
63
64
65
66
67
68
# File 'lib/couchbase/protostellar/response_converter/search.rb', line 59

def self.convert_search_row_location(proto_location)
  Couchbase::Cluster::SearchRowLocation.new do |loc|
    loc.field = proto_location.field
    loc.term = proto_location.term
    loc.position = proto_location.position
    loc.start_offset = proto_location.start
    loc.end_offset = proto_location.end
    loc.array_positions = proto_location.array_positions.to_a
  end
end

.to_search_result(resps, options) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/couchbase/protostellar/response_converter/search.rb', line 23

def self.to_search_result(resps, options)
  Couchbase::Cluster::SearchResult.new do |res|
    res.rows = []
    res.facets = {}
    resps.each do |resp|
      resp.hits.each do |hit|
        res.rows.append(convert_search_row(hit, options))
      end
      resp.facets.each do |k, facet_res|
        res.facets[k] = convert_facet_result(facet_res)
      end
      res. = (resp.) if resp.has_meta_data?
    end
  end
end