1
0
mirror of https://github.com/bigchaindb/js-bigchaindb-driver.git synced 2024-11-22 09:46:58 +01:00

Merge branch 'search-metadata'

This commit is contained in:
manolodewiner 2017-11-27 15:09:34 +01:00
commit ad03590347
5 changed files with 135 additions and 39 deletions

View File

@ -18,7 +18,7 @@ before_install:
-e BIGCHAINDB_KEYPAIR_PRIVATE=5C5Cknco7YxBRP9AgB1cbUVTL4FAcooxErLygw1DeG2D
-e BIGCHAINDB_DATABASE_BACKEND=mongodb
-e BIGCHAINDB_DATABASE_HOST=172.17.0.1
bigchaindb/bigchaindb:1.0.0
bigchaindb/bigchaindb:1.3.0
start
- gem install cowsay
- npm install -g codecov

View File

@ -228,6 +228,10 @@ Querying for Assets
BigchainDB allows you to query for assets using simple text search. This search is applied to all the strings inside the asset payload and returns all the assets that match a given text search string.
BigchainDB also allows you to query for metadata, but there are some differences. The response of the text search call, beside retrieving the asset or metadata in each case, it consist of:
- In the assets search the call returns the asset id which is the same id of the transaction that created the asset.
- In the metadata search the call returns the transaction id that contains this metadata.
Lets assume that we created 3 assets that look like this:
.. code-block:: js
@ -265,7 +269,49 @@ Which leads to following result:
]
This call returns all the assets that match the string 'Bicycle Inc.', sorted by text score, as well as the asset id. This is the same id of the transaction that created the asset.
This call returns all the assets that match the string 'Bicycle Inc.', sorted by text score, as well as the asset id.
Querying for Metadata
-------------------
Similar as querying for assets, in BigchainDB you can query for metadata using simple text search.
This search is applied to all the strings inside the metadata payload and returns all the metadata payloads that match a given text search string.
Having 3 metadata objets that look like this:
.. code-block:: js
metadata = [
{'state': {'price': 145, 'eur/us': '1.32'}},
{'state': {'price': 236, 'eur/us': '1.15'}},
{'state': {'price': 102, 'eur/us': '1.32'}},
]
Lets perform a text search for all metadata that contains the word '1.32':
.. code-block:: js
conn.searchMetadata('Bicycle Inc.')
.then(assets => console.log('Found assets with serial number Bicycle Inc.:', assets))
Which leads to following result:
.. code-block:: js
[
{
'metadata': {'state': {'price': 145, 'eur/us': '1.32'}},
'id': '14045a0e27ea971f8ac88762d2d74518d3a21f3f0fcd9d8a9a3b644b689cf3eb'
},
{
'metadata': {'state': {'price': 102, 'eur/us': '1.32'}},
'id': '6dd91f4700b3f66c55c50be009018e96f026d37f565d042d1aedfb322623d17d'
}
]
This call returns all the metadata objects that match the string '1.32', sorted by text score, as well as the transaction id corresponding to each metadata object.

View File

@ -25,6 +25,7 @@ export default class Connection {
'transactions': 'transactions',
'transactionsDetail': 'transactions/%(transactionId)s',
'assets': 'assets',
'metadata': 'metadata',
'votes': 'votes'
}[endpoint]
}
@ -167,7 +168,6 @@ export default class Connection {
})
}
/**
* @public
* @param search
@ -179,4 +179,16 @@ export default class Connection {
}
})
}
/**
* @public
* @param search
*/
searchMetadata(search) {
return this._req(this.getApiUrls('metadata'), {
query: {
search
}
})
}
}

View File

@ -219,3 +219,18 @@ test('Get asset for text', t => {
{ query: { search } }
))
})
test('Get metadata for text', t => {
const expectedPath = 'path'
const search = 'abc'
conn._req = sinon.spy()
conn.getApiUrls = sinon.stub().returns(expectedPath)
conn.searchMetadata(search)
t.truthy(conn._req.calledWith(
expectedPath,
{ query: { search } }
))
})

View File

@ -264,6 +264,29 @@ test('Search for an asset', t => {
})
test('Search for metadata', t => {
const conn = new Connection(API_PATH)
const createTx = Transaction.makeCreateTransaction(
asset(),
metaData,
[aliceOutput],
alice.publicKey
)
const createTxSigned = Transaction.signTransaction(
createTx,
alice.privateKey
)
return conn.postTransaction(createTxSigned)
.then(({ id }) => conn.pollStatusAndFetchTransaction(id))
.then(() => conn.searchMetadata(createTxSigned.metadata.message))
.then(assets => t.truthy(
assets.pop(),
createTxSigned.metadata.message
))
})
test('Search blocks containing a transaction', t => {
const conn = new Connection(API_PATH)