Skip to content

Commit

Permalink
Merge pull request rufuspollock-okfn#12 from kielni/exists-missing-fi…
Browse files Browse the repository at this point in the history
…lters

Exists and missing filters
  • Loading branch information
rufuspollock committed Apr 9, 2014
2 parents 84a482e + c224c51 commit 1c5c625
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 2 deletions.
10 changes: 8 additions & 2 deletions elasticsearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,12 @@ var ES = {};
} else if (filter.type == 'type') {
// type filter: http://www.elasticsearch.org/guide/reference/query-dsl/type-filter/
out.type = { value : filter.value };
} else if (filter.type == 'exists') {
// exists filter: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-exists-filter.html
out.exists = { field : filter.field };
} else if (filter.type == 'missing') {
// missing filter: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-missing-filter.html
out.missing = { field : filter.field };
}
if (filter.not) {
out = { not: JSON.parse(JSON.stringify(out)) };
Expand All @@ -223,8 +229,8 @@ var ES = {};
var url = this.endpoint + '/_search';
var jqxhr = makeRequest({
url: url,
data: data,
dataType: this.options.dataType
type: 'POST',
data: JSON.stringify(esQuery)
});
return jqxhr;
};
Expand Down
76 changes: 76 additions & 0 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,82 @@ test("queryNormalize", function() {
};
deepEqual(out, exp);

// exists
var in_ = emptyQuery();
in_.filters = [
{
type: 'exists',
field: 'xyz'
}
];
var out = backend._normalizeQuery(in_);
var exp = {
filtered: {
filter: {
and: [
{
exists: {
field: 'xyz'
}
}
]
}
}
};
deepEqual(out, exp);

// missing
var in_ = emptyQuery();
in_.filters = [
{
type: 'missing',
field: 'xyz'
}
];
var out = backend._normalizeQuery(in_);
var exp = {
filtered: {
filter: {
and: [
{
missing: {
field: 'xyz'
}
}
]
}
}
};
deepEqual(out, exp);

// not
var in_ = emptyQuery();
in_.filters = [
{
not: true,
type: 'term',
field: 'xyz',
term: 'one'
}
];
var out = backend._normalizeQuery(in_);
var exp = {
filtered: {
filter: {
and: [
{
'not' : {
term: {
xyz: 'one'
}
}
}
]
}
}
};
deepEqual(out, exp);

// ids query
var in_ = emptyQuery();
in_.ids = [ 1, 2, 3, 4 ];
Expand Down

0 comments on commit 1c5c625

Please sign in to comment.