Skip to content

Commit

Permalink
Merge pull request #141 from social-native/fix/es710-results
Browse files Browse the repository at this point in the history
Fix bug of not saving results from ES710
  • Loading branch information
markrsocialnative authored May 6, 2021
2 parents 31b3db6 + 82e9ff7 commit f45b3ef
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "elastic-composer",
"version": "4.6.0",
"version": "4.6.1",
"description": "",
"main": "dist/index.cjs.js",
"module": "dist/index.es.js",
Expand Down
39 changes: 39 additions & 0 deletions src/__tests__/manager.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import Manager from '../manager';

describe('Manager', () => {
describe('#_saveQueryResults', () => {
it('saves ES 7.10 query results', () => {
const fakeES710QueryResults = {
took: 2,
timed_out: false,
_shards: {total: 1, successful: 1, skipped: 0, failed: 0},
hits: {
total: {value: 10, relation: 'eq'},
max_score: 1,
hits: [
{
_index: 'crm_index',
_type: '_doc',
_id: '_aLNPnkBrs-zuIFb2nyl',
_score: 1,
_source: {id: 234},
sort: [1, 0]
},
{
_index: 'crm_index',
_type: '_doc',
_id: '_qKLP3kBrs-zuIFbcXxC',
_score: 1,
_source: {id: 2344, 'user_profile.birth_date': 712652400000},
sort: [1, 1]
}
]
}
};
// @ts-ignore
const manager = new Manager();
manager._saveQueryResults(fakeES710QueryResults);
expect(manager.results).toEqual(fakeES710QueryResults.hits.hits)
});
});
});
10 changes: 6 additions & 4 deletions src/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ import {
IFilters,
ISuggestions,
ManagerOptions,
EffectKinds
EffectKinds,
ES710Response,
} from './types';
import {objKeys} from './utils';
import {decorate, observable, runInAction, reaction, toJS, computed} from 'mobx';
Expand Down Expand Up @@ -129,7 +130,7 @@ class Manager<
public suggestions: ISuggestions;

public results: Array<ESHit<ESDocSource>>;
public rawESResponse?: ESResponse<ESDocSource>;
public rawESResponse?: ESResponse<ESDocSource> | ES710Response<ESDocSource>;

public _sideEffectQueue: Array<EffectRequest<EffectKinds>>;
public isSideEffectRunning: boolean;
Expand Down Expand Up @@ -761,8 +762,9 @@ class Manager<
* Save the results
* The results contain the documents found in the query that match the filters
*/
public _saveQueryResults = (response: ESResponse<ESDocSource>) => {
if (response.timed_out === false && response.hits.total >= 0) {
public _saveQueryResults = (response: ESResponse<ESDocSource> | ES710Response<ESDocSource>) => {
const totalHits = typeof response.hits.total === 'number' ? response.hits.total : response.hits.total.value;
if (response.timed_out === false && totalHits >= 0) {
runInAction(() => {
if (response && response.hits && response.hits.hits) {
this.results = response.hits.hits;
Expand Down
12 changes: 11 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {FuzzySuggestion, BaseSuggestion} from './suggestions';
* ES Request
* ***********************************
*/
export type ESRequestSortField = Array<object | string>;
export type ESRequestSortField = Array<object | string | number>;

export type ESRequest = {
query: {
Expand Down Expand Up @@ -63,6 +63,16 @@ export type ESResponse<Source extends object = object> = {
};
};

export type ES710Response<Source extends object = object> = {
took: number;
timed_out: boolean;
_shards: {total: number; successful: number; skipped: number; failed: number};
hits: {total: { value: number, relation: string}; max_score: number; hits: Array<ESHit<Source>>};
aggregations?: {
[boundary: string]: AggregationResults;
};
};

export type ESHit<Source extends object = object> = {
_index: string;
_type: string;
Expand Down

0 comments on commit f45b3ef

Please sign in to comment.