Class: Ashikawa::Core::Query

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/ashikawa-core/query.rb

Overview

Formulate a Query on a collection or on a database

Constant Summary collapse

ALLOWED_KEYS_FOR_PATH =

For each simple query define the allowed attributes for filtering

{
  'simple/all'           => [:limit, :skip, :collection],
  'simple/by-example'    => [:limit, :skip, :example, :collection],
  'simple/near'          => [:latitude, :longitude, :distance, :skip, :limit, :geo, :collection],
  'simple/within'        => [:latitude, :longitude, :radius, :distance, :skip, :limit, :geo, :collection],
  'simple/range'         => [:attribute, :left, :right, :closed, :limit, :skip, :collection],
  'cursor'               => [:query, :count, :batch_size, :collection, :bind_vars],
  'query'                => [:query],
  'simple/first-example' => [:example, :collection]
}

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ Query

Initializes a Query

Examples:

Create a new query object

collection = Ashikawa::Core::Collection.new(database, raw_collection)
query = Ashikawa::Core::Query.new(collection)

Parameters:


37
38
39
# File 'lib/ashikawa-core/query.rb', line 37

def initialize(connection)
  @connection = connection
end

Instance Method Details

#all(options = {}) ⇒ Cursor

Note:

It is advised to NOT use this method due to possible HUGE data amounts requested

Retrieves all documents for a collection

Examples:

Get an array with all documents

query = Ashikawa::Core::Query.new(collection)
query.all # => #<Cursor id=33>

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :limit (Fixnum)

    limit the maximum number of queried and returned elements.

  • :skip (Fixnum)

    skip the first <n> documents of the query.

Returns:

Raises:


52
53
54
# File 'lib/ashikawa-core/query.rb', line 52

def all(options = {})
  simple_query_request('simple/all', options)
end

#by_example(example = {}, options = {}) ⇒ Cursor

Looks for documents in a collection which match the given criteria

Examples:

Find all documents in a collection that are red

query = Ashikawa::Core::Query.new(collection)
query.by_example({ 'color' => 'red' }, { :limit => 1 }) #=> #<Cursor id=2444>

Parameters:

  • example (Hash) (defaults to: {})

    a customizable set of options

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (example):

  • a (Hash)

    Hash with data matching the documents you are looking for.

Options Hash (options):

  • a (Hash)

    Hash with additional settings for the query.

  • :limit (Fixnum)

    limit the maximum number of queried and returned elements.

  • :skip (Fixnum)

    skip the first <n> documents of the query.

Returns:

Raises:


68
69
70
# File 'lib/ashikawa-core/query.rb', line 68

def by_example(example = {}, options = {})
  simple_query_request('simple/by-example', { example: example }.merge(options))
end

#execute(query, options = {}) ⇒ Cursor

Send an AQL query to the database

Examples:

Send an AQL query to the database

query = Ashikawa::Core::Query.new(collection)
query.execute('FOR u IN users LIMIT 2') # => #<Cursor id=33>

Usage of bind variables

db = Ashikawa::Core::Database.new(){|conf| conf.url="http://127.0.0.1:8529"}
query = 'FOR t IN TRAVERSAL(imdb_vertices, imdb_edges, "imdb_vertices/759", "outbound", {maxDepth: 2})' +
 'FILTER t.vertex.genre == @foo RETURN t'
db.query.execute(query, bind_vars: {'foo' => 'Comedy'}).to_a

Parameters:

  • query (String)
  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :count (Fixnum)

    Should the number of results be counted?

  • :batch_size (Fixnum)

    Set the number of results returned at once

Returns:


157
158
159
# File 'lib/ashikawa-core/query.rb', line 157

def execute(query, options = {})
  wrapped_request('cursor', :post, options.merge({ query: query }))
end

#first_example(example = {}) ⇒ Document

Looks for one document in a collection which matches the given criteria

Examples:

Find one document in a collection that is red

query = Ashikawa::Core::Query.new(collection)
query.first_example({ 'color' => 'red'}) # => #<Document id=2444 color="red">

Parameters:

  • example (Hash) (defaults to: {})

    a Hash with data matching the document you are looking for.

Returns:

Raises:


81
82
83
84
85
# File 'lib/ashikawa-core/query.rb', line 81

def first_example(example = {})
  request = prepare_request('simple/first-example', { example: example, collection: collection.name })
  response = send_request('simple/first-example', { put: request })
  Document.new(database, response['document'])
end

#in_range(options = {}) ⇒ Cursor

Looks for documents in a collection with an attribute between two values

Examples:

Find all documents within a radius of 100 to Infinite Loop

query = Ashikawa::Core::Query.new(collection)
query.within(:latitude => 37.331693, :longitude => -122.030468, :radius => 100)

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :attribute (Fixnum)

    The attribute path to check.

  • :left (Fixnum)

    The lower bound

  • :right (Fixnum)

    The upper bound

  • :closed (Fixnum)

    If true, the interval includes right

  • :skip (Fixnum)

    The documents to skip in the query (optional).

  • :limit (Fixnum)

    The maximal amount of documents to return (optional).

Returns:

Raises:


138
139
140
# File 'lib/ashikawa-core/query.rb', line 138

def in_range(options = {})
  simple_query_request('simple/range', options)
end

#near(options = {}) ⇒ Cursor

Looks for documents in a collection based on location

Examples:

Find all documents at Infinite Loop

query = Ashikawa::Core::Query.new(collection)
query.near(:latitude => 37.331693, :longitude => -122.030468)

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :latitude (Fixnum)

    Latitude location for your search.

  • :longitude (Fixnum)

    Longitude location for your search.

  • :skip (Fixnum)

    The documents to skip in the query.

  • :distance (Fixnum)

    If given, the attribute key used to store the distance.

  • :limit (Fixnum)

    The maximal amount of documents to return (default: 100).

  • :geo (Fixnum)

    If given, the identifier of the geo-index to use.

Returns:

Raises:


101
102
103
# File 'lib/ashikawa-core/query.rb', line 101

def near(options = {})
  simple_query_request('simple/near', options)
end

#valid?(query) ⇒ Boolean

Test if an AQL query is valid

Examples:

Validate an AQL query

query = Ashikawa::Core::Query.new(collection)
query.valid?('FOR u IN users LIMIT 2') # => true

Parameters:

  • query (String)

Returns:

  • (Boolean)

169
170
171
172
173
# File 'lib/ashikawa-core/query.rb', line 169

def valid?(query)
  !!wrapped_request('query', :post, { query: query })
rescue Ashikawa::Core::BadSyntax
  false
end

#within(options = {}) ⇒ Cursor

Looks for documents in a collection within a radius

Examples:

Find all documents within a radius of 100 to Infinite Loop

query = Ashikawa::Core::Query.new(collection)
query.within(:latitude => 37.331693, :longitude => -122.030468, :radius => 100)

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :latitude (Fixnum)

    Latitude location for your search.

  • :longitude (Fixnum)

    Longitude location for your search.

  • :radius (Fixnum)

    Radius around the given location you want to search in.

  • :skip (Fixnum)

    The documents to skip in the query.

  • :distance (Fixnum)

    If given, the attribute key used to store the distance.

  • :limit (Fixnum)

    The maximal amount of documents to return (default: 100).

  • :geo (Fixnum)

    If given, the identifier of the geo-index to use.

Returns:

Raises:


120
121
122
# File 'lib/ashikawa-core/query.rb', line 120

def within(options = {})
  simple_query_request('simple/within', options)
end