Skip to content

Commit

Permalink
Add .msearch() method
Browse files Browse the repository at this point in the history
 🐿 v2.8.0
  • Loading branch information
i-like-robots committed Mar 14, 2019
1 parent 4926174 commit 3fdb823
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,21 @@ client.count({
});
```

### `.msearch(options[, timeout][, dataHandler])`

Perform multiple search queries with one request. By default returns an array of query responses including total matches and content sources for the items that were found but allows an optional custom data handler function.

#### Example

```js
client.msearch({
queries: [
{ query: { term: { 'annotations.id': 'cce58e8e-158c-11e7-80f4-13e067d5072c' } } },
{ query: { term: { 'annotations.id': '0615fc8c-1558-11e7-80f4-13e067d5072c' } } }
]
})
```

### `.concept(uuid[, timeout])`

Get a single concept by UUID. Returns an object or `undefined` if no matches were found.
Expand Down
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ module.exports = {
get: require('./lib/get'),
tag: require('./lib/tag'),
concept: require('./lib/concept'),
mapping: require('./lib/mapping')
mapping: require('./lib/mapping'),
msearch: require('./lib/msearch')
};
48 changes: 48 additions & 0 deletions lib/msearch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const signedFetch = require('signed-aws-es-fetch');
const agent = require('./helpers/https-agent');
const handleResponse = require('./helpers/handle-response');
const extractSource = require('./helpers/extract-source');

const DEFAULT_QUERY = {
from: 0,
size: 10,
sort: { publishedDate: 'desc' },
_source: true
};

const DEFAULTS = {
queries: []
};

function handleData ({ responses }) {
return responses.map((data) => {
return {
total: data.hits.total,
hits: data.hits.hits.map(extractSource)
};
});
}

function search (options = {}, timeout = 3000, dataHandler = handleData) {
const params = Object.assign({}, DEFAULTS, options);

const body = params.queries.reduce((accumulator, query) => {
accumulator.push({});
accumulator.push(Object.assign({}, DEFAULT_QUERY, query));
return accumulator;
}, []);

return signedFetch('https://next-elastic.ft.com/content/item/_msearch', {
body: body.map(JSON.stringify).join('\n'),
agent,
timeout,
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
})
.then(handleResponse)
.then(dataHandler);
};

module.exports = search;

0 comments on commit 3fdb823

Please sign in to comment.