Skip to content

Commit

Permalink
Es template search (#92)
Browse files Browse the repository at this point in the history
* deal with templated search

* add a check to see if a query is template call

Co-authored-by: epugh@opensourceconnections.com <>
  • Loading branch information
epugh authored Aug 16, 2021
1 parent 26f6cac commit bee82d7
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 3 deletions.
28 changes: 25 additions & 3 deletions factories/esSearcherFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
Searcher.prototype.explainOther = explainOther;
Searcher.prototype.explain = explain;
Searcher.prototype.majorVersion = majorVersion;
Searcher.prototype.isTemplateCall = isTemplateCall;


function addDocToGroup (groupedBy, group, solrDoc) {
Expand Down Expand Up @@ -117,7 +118,9 @@
apiMethod = 'bulk';
}

if (apiMethod === 'get' ) {
// Using templates assumes that the _source field is defined
// in the template, not passed in
if (apiMethod === 'get' && !esUrlSvc.isTemplateCall(uri)) {
var fieldList = (self.fieldList === '*') ? '*' : self.fieldList.join(',');

if ( 5 <= self.majorVersion() ) {
Expand All @@ -137,8 +140,21 @@

var queryDslWithPagerArgs = angular.copy(self.queryDsl);
if (self.pagerArgs) {
queryDslWithPagerArgs.from = self.pagerArgs.from;
queryDslWithPagerArgs.size = self.pagerArgs.size;
if (esUrlSvc.isTemplateCall(uri)) {
queryDslWithPagerArgs.params.from = self.pagerArgs.from;
queryDslWithPagerArgs.params.size = self.pagerArgs.size;
}
else {
queryDslWithPagerArgs.from = self.pagerArgs.from;
queryDslWithPagerArgs.size = self.pagerArgs.size;
}
}

if (esUrlSvc.isTemplateCall(uri)) {
console.log("DUDE:");
console.log(queryDslWithPagerArgs);
delete queryDslWithPagerArgs._source;
delete queryDslWithPagerArgs.highlight;
}

self.inError = false;
Expand Down Expand Up @@ -351,6 +367,12 @@
}
}

function isTemplateCall() {
var self = this;

return esUrlSvc.isTemplateCall(esUrlSvc.parseUrl(self.url));
}

// Return factory object
return Searcher;
}
Expand Down
5 changes: 5 additions & 0 deletions services/esUrlSvc.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ angular.module('o19s.splainer-search')
self.setParams = setParams;
self.getHeaders = getHeaders;
self.isBulkCall = isBulkCall;
self.isTemplateCall = isTemplateCall;

/**
*
Expand Down Expand Up @@ -156,5 +157,9 @@ angular.module('o19s.splainer-search')
function isBulkCall (uri) {
return uri.pathname.endsWith('_msearch');
}

function isTemplateCall (uri) {
return uri.pathname.endsWith('_search/template');
}
}
]);
51 changes: 51 additions & 0 deletions test/spec/esSearchSvc.js
Original file line number Diff line number Diff line change
Expand Up @@ -1477,6 +1477,57 @@ describe('Service: searchSvc: ElasticSearch', function() {
called++;
});

$httpBackend.flush();
$httpBackend.verifyNoOutstandingExpectation();
expect(called).toEqual(1);
});
});
describe('templated search', function() {
beforeEach(inject(function () {

var mockEsParams = {
id: 'tmdb-title-search-template',
params: {
search_query: 'star'
}
};

searcher = searchSvc.createSearcher(
mockFieldSpec,
mockEsUrl + '/template',
mockEsParams,
mockQueryText,
{ },
'es'
);
}));

it('returns docs, and removes _source and highlight query params', function() {
$httpBackend.expectPOST(mockEsUrl + '/template', function verifyParamsStripped(data) {
var esQuery = angular.fromJson(data);
return (
(esQuery.id === 'tmdb-title-search-template') &&
(angular.isDefined(esQuery.highlight) == false) &&
(angular.isDefined(esQuery._source) == false) &&
(angular.isDefined(esQuery.from) == false) &&
(angular.isDefined(esQuery.size) == false) &&
(esQuery.params.from === 0) &&
(esQuery.params.size === 10)
);
}).
respond(200, mockES7Results);

var called = 0;

searcher.search()
.then(function() {
var docs = searcher.docs;
expect(docs.length === 2);

expect(searcher.numFound).toEqual(2);
called++;
});

$httpBackend.flush();
$httpBackend.verifyNoOutstandingExpectation();
expect(called).toEqual(1);
Expand Down
22 changes: 22 additions & 0 deletions test/spec/esUrlSvc.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ describe('Service: esUrlSvc', function () {
expect(uri.protocol).toBe('https');
expect(uri.host).toBe('es.quepid.com');
expect(uri.pathname).toBe('/tmdb/_search');

url = 'https://es.quepid.com/tmdb/_search/template';
uri = esUrlSvc.parseUrl(url);

expect(uri.protocol).toBe('https');
expect(uri.host).toBe('es.quepid.com');
expect(uri.pathname).toBe('/tmdb/_search/template');
});

it('adds http if the protocol is missing', function() {
Expand Down Expand Up @@ -75,6 +82,21 @@ describe('Service: esUrlSvc', function () {
uri = esUrlSvc.parseUrl(url);
expect(esUrlSvc.isBulkCall(uri)).toBe(true);
});

it('understands when template endpoint used', function() {
var url = 'http://es.quepid.com/tmdb/_search';
var uri = esUrlSvc.parseUrl(url);
expect(esUrlSvc.isTemplateCall(uri)).toBe(false);

url = 'http://es.quepid.com/tmdb/_search/template';
uri = esUrlSvc.parseUrl(url);
expect(esUrlSvc.isTemplateCall(uri)).toBe(true);

url = 'http://es.quepid.com/tmdb/_search/template/';
uri = esUrlSvc.parseUrl(url);
expect(esUrlSvc.isTemplateCall(uri)).toBe(true);
});

});

describe('build doc URL', function() {
Expand Down

0 comments on commit bee82d7

Please sign in to comment.