async getExistingDoc(collection, id) { try { const doc = await this.esClient.search({ index: collection.index, type: collection.type, q: `_id:${id}`, size: 1, version: Boolean(collection.versionType), }); return { parentId: doc.hits.hits[0]._parent || null, version: doc.hits.hits[0]._version || null, } } catch(err) { logger.error(`cannot find item of type ${collection.type} with id ${id}`); } }
test('should add documents with random ids', async (t) => { const index = 'load_random_index'; const loader = esFixtures.bootstrap(index, type); const data = [{ name: 'Jotaro', standName: 'Star Platinum' }, { name: 'Jolyne', standName: 'Stone Free' }]; await loader.load(data); // check it was inserted correctly const searchResult = (await loader.client.search({ index: index })).hits.hits; const result1 = searchResult.some(result => result._source.name === 'Jotaro'); const result2 = searchResult.some(result => result._source.name === 'Jolyne'); t.is(searchResult.length, 2); t.true(result1); t.true(result2); });
function GetAllTodos(req, res) { var start = monitor(); client.search({ index:'todo', type: 'todo', q: '*', _sourceInclude: 'todo_id, todo, completed, tags, author, completeddate, duedate' }, function(error, response){ if(error){ res.end(JSON.stringify(error)); } else { var results = []; results = response.hits.hits.map(function(hit){ return hit._source }); res.header('Content-Type', 'application/json'); res.end(JSON.stringify(results)); monitor(start, 'GetAllTodos'); } }); }
function testSearchAll() { ESClient.search({ index: "imdb", body: { size: 10, from: 0, query: { match_all: {} } } }) .then(results => { console.log("|Test SearchAll|"); console.log("-----------------------------------"); console.log("Response Body: "); console.log(results); console.log(`found ${results.hits.total} items in ${results.took}ms`); console.log("returned 10 imdb titles:"); results.hits.hits.forEach((hit, index) => console.log(`\t${index} - ${hit._source.title}`) ); }) .catch(console.error); }
async function testSearch2 () { const result = await client.search({ index, type, body: { query: { match: { 'Text': 'opium den sherlock' } }, highlight: { fields: { Text: {} } } } }) console.log('testSearch2', result) for (let hit of result.hits.hits) { console.log(hit._source.Text) console.log(hit.highlight.Text) } }
body: query }).then(function(body) { if (body.hits.hits.length === 0) { return res.badRequest({error: 'Nothing Found'}); 'count': body.hits.hits.length, 'total_edits': body.aggregations.totalChanges.value, 'total_contributors': body.aggregations.userTotal.buckets.length, 'total_results': body.hits.total, 'next_results': '?' + underscore(params) .map(function(val, key) { for (var i = 0; i < body.hits.hits.length; i++) { var es_results = body.hits.hits[i]._source; for (var j = 0; j < sails.config.osm.fields_to_remove.length; j++) { delete es_results[sails.config.osm.fields_to_remove[j]];
for (let i = 0; i < Math.ceil(searchResponse.hits.total / this.bulkSize); i++) { const bulkDelete = []; const dumpDocs = searchResponse.hits.hits; for (let j = 0; j < dumpDocs.length; j++) { bulkDelete.push({ logger.info(`${collectionName} delete progress: ${numDeleted}/${searchResponse.hits.total}`); searchResponse = await this.esClient.scroll({ scrollId: searchResponse._scroll_id,
test('should add documents with id specified inside doc', async (t) => { const index = 'load_assigned_id_index'; const loader = esFixtures.bootstrap(index, type); const data = [{ _id: 1, name: 'Jotaro', standName: 'Star Platinum' }, { _id: 2, name: 'Jolyne', standName: 'Stone Free' }]; await loader.load(data); // check it was inserted correctly const searchResult = (await loader.client.search({ index: index })).hits.hits; const result1 = searchResult.find(result => result._id === '1'); const result2 = searchResult.find(result => result._id === '2'); t.is(result1._source.name, 'Jotaro'); t.is(result2._source.name, 'Jolyne'); });
test('should add documents with incremental ids', async (t) => { const index = 'load_incremental_index'; const loader = esFixtures.bootstrap(index, type); const data = [{ name: 'Jotaro', standName: 'Star Platinum' }, { name: 'Jolyne', standName: 'Stone Free' }]; const options = { incremental: true }; await loader.load(data, options); // check it was inserted correctly const searchResult = (await loader.client.search({ index: index })).hits.hits; const result1 = searchResult.find(result => result._id === '1'); const result2 = searchResult.find(result => result._id === '2'); t.is(result1._source.name, 'Jotaro'); t.is(result2._source.name, 'Jolyne'); });