If a suitable hash index exists, then /_api/simple/by-example will use this index to execute a query-by-example.
POST /_api/index?collection=collection-identifier
Creates a hash index for the collection collection-identifier, if it does not already exist. The call expects an object containing the index details.
type
: must be equal to "hash"
.fields
: A list of attribute paths.unique
: If true
, then create a unique index.If the index does not already exists and could be created, then a HTTP 201
is returned. If the index already exists, then a HTTP 200
is returned.
If the collection-identifier is unknown, then a HTTP 404
is returned. It is possible to specify a name instead of an identifier.
If the collection already contains documents and you try to create a unique hash index in such a way that there are documents violating the uniqueness, then a HTTP 400
is returned.
Examples
Creating an unique constraint:
> curl --data @- -X POST --dump - http://localhost:8529/_api/index?collection=102901008 { "type" : "hash", "unique" : true, "fields" : [ "a", "b" ] } HTTP/1.1 201 Created content-type: application/json { "code": 201, "fields": [ "a", "b" ], "id": "102901008/168054969", "type": "hash", "isNewlyCreated": true, "unique": true, "error": false }
Creating a hash index:
> curl --data @- -X POST --dump - http://localhost:8529/_api/index?collection=105981200 { "type" : "hash", "unique" : false, "fields" : [ "a", "b" ] } HTTP/1.1 201 Created content-type: application/json { "code": 201, "fields": [ "a", "b" ], "id": "105981200/171069625", "type": "hash", "isNewlyCreated": true, "unique": false, "error": false }
PUT /_api/simple/by-example
This will find all documents matching a given example.
The call expects a JSON hash array as body with the following attributes:
collection
: The identifier or name of the collection to query.example
: The example.skip
: The documents to skip in the query. (optional)limit
: The maximal amount of documents to return. (optional)Returns a cursor containing the result, see HTTP Interface for AQL Query Cursors for details.
Examples
Matching an attribute:
> curl --data @- -X PUT --dump - http://localhost:8529/_api/simple/by-example { "collection" : "3179705695", "example" : [ { "i" : 1 } ] } HTTP/1.1 201 Created content-type: application/json { "result": [ { "a": { "k": 2, "j": 2 }, "i": 1, "_rev": 3181802847, "_id": "3179705695/3181802847" }, { "a": { "j": 1 }, "i": 1, "_rev": 3181475167, "_id": "3179705695/3181475167" }, { "a": { "k": 1, "j": 1 }, "i": 1, "_rev": 3181737311, "_id": "3179705695/3181737311" }, { "i": 1, "_rev": 3181147487, "_id": "3179705695/3181147487" } ], "count": 4, "error": false, "hasMore": false, "code": 201 }
Matching an attribute which is a sub-document:
> curl --data @- -X PUT --dump - http://localhost:8529/_api/simple/by-example { "collection" : "3179705695", "example" : [ { "a" : { "j" : 1 } } ] } HTTP/1.1 201 Created content-type: application/json { "result": [ { "a": { "j": 1 }, "i": 1, "_rev": 3181475167, "_id": "3179705695/3181475167" } ], "count": 1, "error": false, "hasMore": false, "code": 201 }
Matching an attribute within a sub-document:
> curl --data @- -X PUT --dump - http://localhost:8529/_api/simple/by-example { "collection" : "3179705695", "example" : [ "a.j", 1 ] } HTTP/1.1 201 Created content-type: application/json { "result": [ { "a": { "j": 1 }, "i": 1, "_rev": 3181475167, "_id": "3179705695/3181475167" }, { "a": { "k": 1, "j": 1 }, "i": 1, "_rev": 3181737311, "_id": "3179705695/3181737311" } ], "count": 2, "error": false, "hasMore": false, "code": 201 }
PUT /_api/simple/first-example
This will return the first document matching a given example.
The call expects a JSON hash array as body with the following attributes:
collection
: The identifier or name of the collection to query.example
: The example.Returns a result containing the document or HTTP 404
if no document matched the example.
Examples
If a matching document was found:
> curl --data @- -X PUT --dump - http://localhost:8529/_api/simple/first-example { "collection" : "666351134", "example" : [ "a.j", 1, "a.k", 1 ] } HTTP/1.1 200 OK content-type: application/json { "error": false, "code": 200, "document": { "_rev": 668382750, "_id": "666351134/668382750", "a": { "k": 1, "j": 1, "l" : 10 }, "i": 1 } }
If no document was found:
> curl --data @- -X PUT --dump - http://localhost:8529/_api/simple/first-example { "collection" : "666351134", "example" : [ "a.j", 1, "a.k", 2 ] } HTTP/1.1 404 Not Found content-type: application/json { "errorMessage": "no match", "error": true, "code": 404, "errorNum": 404 }