Skip to content

Commit eab7ee8

Browse files
committed
fix(improved_timeout_handling): remove dependency on hits.total property
1 parent aeb2b2f commit eab7ee8

File tree

4 files changed

+19
-72
lines changed

4 files changed

+19
-72
lines changed

controller/search.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,17 @@ function setup( apiConfig, esclient, query, should_execute ){
6060
const initialTime = debugLog.beginTimer(req, `Attempt ${currentAttempt}`);
6161
// query elasticsearch
6262
searchService( esclient, cmd, function( err, docs, meta, data ){
63+
64+
// keep tally of hit counts - compatible with new/old versions of ES
65+
let totalHits = 0;
66+
if( _.has(data, 'hits.total') ) {
67+
totalHits = _.isPlainObject(data.hits.total) ? data.hits.total.value : data.hits.total;
68+
}
69+
6370
const message = {
6471
controller: 'search',
6572
queryType: renderedQuery.type,
66-
es_hits: _.get(data, 'hits.total'),
73+
es_hits: totalHits,
6774
result_count: (docs || []).length,
6875
es_took: _.get(data, 'took', undefined),
6976
response_time: _.get(data, 'response_time', undefined),

helper/type_mapping_discovery.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,18 @@ const DISCOVERY_QUERY = {
3737
module.exports = (tm, done) => {
3838
const esclient = elasticsearch.Client(_.extend({}, peliasConfig.esclient));
3939
esclient.search(DISCOVERY_QUERY, (err, res) => {
40+
41+
// keep tally of hit counts - compatible with new/old versions of ES
42+
let totalHits = 0;
43+
if( _.has(res, 'hits.total') ) {
44+
totalHits = _.isPlainObject(res.hits.total) ? res.hits.total.value : res.hits.total;
45+
}
4046

4147
// query error
4248
if( err ){ logger.error( err ); }
4349

4450
// invalid response
45-
else if( !res || !res.hits || !res.hits.total ){
51+
else if ( totalHits < 1 ){
4652
logger.error( 'no hits for aggregation' );
4753
}
4854

@@ -60,7 +66,7 @@ module.exports = (tm, done) => {
6066

6167
// update type mapping from aggregation data
6268
if( !!Object.keys( layersBySource ).length ){
63-
logger.info( 'total hits', res.hits.total );
69+
logger.info( 'total hits', totalHits );
6470
logger.info( 'total sources', sources.length );
6571
logger.info( 'successfully discovered type mapping from elasticsearch' );
6672
tm.setLayersBySource( layersBySource );

service/search.js

+3-6
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,9 @@ function service( esclient, cmd, cb ){
4040
scores: []
4141
};
4242

43-
if (
44-
_.has(data, 'hits') &&
45-
_.get(data, 'hits.total', 0) > 0 &&
46-
_.isArray(data.hits.hits)
47-
){
48-
docs = data.hits.hits.map( function( hit ){
43+
const hits = _.get(data, 'hits.hits');
44+
if( _.isArray( hits ) && hits.length > 0 ){
45+
docs = hits.map(hit => {
4946

5047
meta.scores.push(hit._score);
5148

test/unit/service/search.js

-63
Original file line numberDiff line numberDiff line change
@@ -278,69 +278,6 @@ module.exports.tests.success_conditions = (test, common) => {
278278

279279
});
280280

281-
test('esclient.search returning falsy data.hits.total should return empty docs and meta', (t) => {
282-
const errorMessages = [];
283-
284-
const service = proxyquire('../../../service/search', {
285-
'pelias-logger': {
286-
get: () => {
287-
return {
288-
error: (msg) => {
289-
errorMessages.push(msg);
290-
}
291-
};
292-
}
293-
}
294-
});
295-
296-
const esclient = {
297-
search: (cmd, callback) => {
298-
t.deepEquals(cmd, 'this is the query');
299-
300-
const data = {
301-
hits: {
302-
hits: [
303-
{
304-
_score: 'score 1',
305-
_id: 'doc id 1',
306-
matched_queries: 'matched_queries 1',
307-
_source: {
308-
random_key: 'value 1'
309-
}
310-
},
311-
{
312-
_score: 'score 2',
313-
_id: 'doc id 2',
314-
matched_queries: 'matched_queries 2',
315-
_source: {
316-
random_key: 'value 2'
317-
}
318-
}
319-
]
320-
}
321-
};
322-
323-
callback(undefined, data);
324-
325-
}
326-
};
327-
328-
const expectedDocs = [];
329-
const expectedMeta = { scores: [] };
330-
331-
const next = (err, docs, meta) => {
332-
t.equals(err, null);
333-
t.deepEquals(docs, expectedDocs);
334-
t.deepEquals(meta, expectedMeta);
335-
336-
t.equals(errorMessages.length, 0, 'no errors should have been logged');
337-
t.end();
338-
};
339-
340-
service(esclient, 'this is the query', next);
341-
342-
});
343-
344281
test('esclient.search returning non-array data.hits.hits should return empty docs and meta', (t) => {
345282
const errorMessages = [];
346283

0 commit comments

Comments
 (0)