Skip to content

Enhanced support for adding words, filter operator. #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 27 additions & 11 deletions lib/esqbuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* Copyright (c) 2014 Leonard Wu <[email protected]>
* https://github.com/leonardw/elasticsearch-query-builder
* MIT Licensed
*
* Customized: https://github.com/prasad83/elasticsearch-query-builder
*/
(function () {
function hasWildcard(str) {
Expand All @@ -17,55 +19,69 @@
return str.replace(/([\+\-\&\|\!\(\)\{\}\[\]\^\"\~\:\/\\])/g, '\\$1');
}

function addCriteria(qobj, criteria) {
function addCriteria(qobj, criteria, filterop) {
if (typeof filterop == 'undefined') filterop = 'and';

if (!qobj.filter) {
qobj.filter = criteria;
} else if (!qobj.filter.and) {
} else if (!qobj.filter[filterop]) {
var arr = [];
arr.push(qobj.filter);
arr.push(criteria);
qobj.filter = {and : arr};
qobj.filter = {}; qobj.filter[filterop] = arr;
} else {
qobj.filter.and.push(criteria);
qobj.filter[filterop].push(criteria);
}
}

function stringCriteria(src, qobj, prop) {
function stringCriteria(src, qobj, prop, filterop) {
if (src) {
var input = src.trim(),
len = input.length;
if (len > 0) {
if (hasWildcard(src)) {
var wildcard = {};
wildcard[prop] = esEscape(src);
addCriteria(qobj, {query: {wildcard: wildcard}});
addCriteria(qobj, {query: {wildcard: wildcard}}, filterop);
} else {
var term = {};
term[prop] = unescWildcard(src);
addCriteria(qobj, {term: term});
addCriteria(qobj, {term: term}, filterop);
}
}
}
}

function selectCriteria(src, qobj, prop) {
function wordsCriteria(words, qobj, prop, filterop) {
if (words.length) {
for (var i = 0, l = words.length; i < l; i++) {
words[i]= hasWildcard(words[i])? esEscape(words[i]) : unescWildcard(words[i]);
}
var term = {};
term[prop] = words;
addCriteria(qobj, {terms: term}, filterop);
}
}

function selectCriteria(src, qobj, prop, filterop) {
var r = [];
for ( var key in src) {
if (src.hasOwnProperty(key) && src[key] === true) r.push(key);
}
var term = {};
if (r.length === 1) {
term[prop] = r[0];
addCriteria(qobj, {term: term});
addCriteria(qobj, {term: term}, filterop);
} else if (r.length > 1) {
term[prop] = r.sort();
addCriteria(qobj, {terms: term});
addCriteria(qobj, {terms: term}, filterop);
}
}

var _me = {
stringCriteria: stringCriteria,
selectCriteria: selectCriteria
selectCriteria: selectCriteria,
wordsCriteria : wordsCriteria
};

if (typeof module !== 'undefined' && module.exports) {
Expand Down