Class: MarkLogic::Queries::NearQuery

Inherits:
BaseQuery
  • Object
show all
Defined in:
lib/marklogic/queries/near_query.rb

Instance Method Summary collapse

Methods inherited from BaseQuery

#add_sub_query, #query_value

Constructor Details

#initialize(queries, distance = 10, distance_weight = 1.0, options = {}) ⇒ NearQuery

Returns a new instance of NearQuery.


4
5
6
7
8
9
# File 'lib/marklogic/queries/near_query.rb', line 4

def initialize(queries, distance = 10, distance_weight = 1.0, options = {})
  @queries = queries
  @distance = distance
  @distance_weight = distance_weight
  @ordered = options.delete(:ordered)
end

Instance Method Details

#to_jsonObject


11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/marklogic/queries/near_query.rb', line 11

def to_json
  json = {
    "near-query" => {
      "queries" => @queries.map { |q| q.to_json }
    }
  }

  json["near-query"]["queries"].push({ "distance" => @distance }) if @distance
  json["near-query"]["queries"].push({ "distance-weight" => @distance_weight }) if @distance_weight
  json["near-query"]["queries"].push({ "ordered" => @ordered })
  json
end

#to_xqyObject


24
25
26
27
28
# File 'lib/marklogic/queries/near_query.rb', line 24

def to_xqy
  queries = @queries.map { |q| q.to_xqy }.join(',')
  ordered = (@ordered == true ? %Q{"ordered"} : %Q{"unordered"}) if !@ordered.nil?
  %Q{cts:near-query((#{queries}),#{@distance},(#{ordered}),#{@distance_weight})}
end