Skip to content

Commit

Permalink
added match_phrase support
Browse files Browse the repository at this point in the history
  • Loading branch information
bharathkontham committed Nov 6, 2018
1 parent 74cc62a commit 6feafe0
Showing 1 changed file with 31 additions and 21 deletions.
52 changes: 31 additions & 21 deletions lib/esConnector.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ ESConnector.prototype.connect = function (callback) {
} else {
self.db = new elasticsearch.Client(self.getClientConfig());
self.db.ping({
requestTimeout: 30000,
requestTimeout: 10000,
}, function (error) {
if (error) {
throw callback(error);
Expand Down Expand Up @@ -453,14 +453,12 @@ ESConnector.prototype.buildFilter = function (modelName, idName, criteria, size,
} else if (criteria.native) {
filter.body = criteria.native; // assume that the developer has provided ES compatible DSL
} else if (_.keys(criteria).length === 0) {
/*filter.body = {
'query': {
'match_all': {}
}
};*/
filter.body = {
'query': {
'bool': {
'must': {
'match_all': {}
},
'filter': [{
'term': {
'docType': modelName
Expand Down Expand Up @@ -558,12 +556,13 @@ ESConnector.prototype.buildWhere = function (model, idName, where) {
if (body && body.query && body.query.bool && _.isEmpty(body.query.bool)) {
delete body.query['bool']; // jshint ignore:line
}

if (body && body.query && _.isEmpty(body.query)) {
/*body.query = {
match_all: {}
}*/
body.query = {
'bool': {
'must': {
'match_all': {}
},
'filter': [{
'term': {
'docType': model
Expand All @@ -581,10 +580,12 @@ ESConnector.prototype.buildNestedQueries = function (body, model, idName, where,
* @example {where: {}}
*/
if (_.keys(where).length === 0) {
// body.match_all = {};
body = {
'query': {
'bool': {
'must': {
'match_all': {}
},
'filter': [{
'term': {
'docType': model
Expand All @@ -598,16 +599,17 @@ ESConnector.prototype.buildNestedQueries = function (body, model, idName, where,
}
var rootPath = body.query;
ESConnector.prototype.buildDeepNestedQueries(true, idName, where, body, rootPath, model, nestedFields);
var docTypeQuery = _.find(rootPath.bool.must, function (v) {
return !!v.match && !!v.match.docType;
var docTypeQuery = _.find(rootPath.bool.filter, function (v) {
return v.term && v.term.docType;
});
var addedDocTypeToRootPath = false;
if (typeof docTypeQuery != 'undefined') {
docTypeQuery.match.docType = model;
addedDocTypeToRootPath = true;
docTypeQuery.term.docType = model;
} else {
addedDocTypeToRootPath = true;
rootPath.bool.must.push({
'match': {
rootPath.bool.filter.push({
'term': {
'docType': model
}
});
Expand Down Expand Up @@ -962,12 +964,20 @@ ESConnector.prototype.buildDeepNestedQueries = function (root, idName, where, bo

}
} else {
var nestedQuery = {
match: {}
};
// var nestedQuery = {query: { match: {}}};
nestedQuery.match[key] = value;

var nestedQuery = {};
if (typeof value === 'string') {
value = value.trim();
if (value.indexOf(' ') > -1) {
nestedQuery.match_phrase = {};
nestedQuery.match_phrase[key] = value;
} else {
nestedQuery.match = {};
nestedQuery.match[key] = value;
}
} else {
nestedQuery.match = {};
nestedQuery.match[key] = value;
}
// Additional handling for nested Objects
if (isNestedKey) {
nestedQuery = {
Expand Down

0 comments on commit 6feafe0

Please sign in to comment.