Skip to content

add post_filter function #95

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 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
dist/docs
node_modules/
.idea/
65 changes: 49 additions & 16 deletions dist/elastic.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*! elastic.js - v1.2.0 - 2014-10-13
/*! elastic.js - v1.2.0 - 2016-02-22
* https://github.com/fullscale/elastic.js
* Copyright (c) 2014 FullScale Labs, LLC; Licensed MIT */
* Copyright (c) 2016 FullScale Labs, LLC; Licensed MIT */

/**
@namespace
Expand Down Expand Up @@ -10960,6 +10960,19 @@
return this;
},

/**
* https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-inner-hits.html
* q should be a json
*/
inner_hits: function(q) {
if (q == null) {
return query.has_child.query;
}

query.has_child.inner_hits = q;
return this;
},

/**
Sets the child document type to search against

Expand Down Expand Up @@ -12087,23 +12100,14 @@
@param {String} likeText The text to find documents like it.

*/
ejs.MoreLikeThisQuery = function (fields, likeText) {
ejs.MoreLikeThisQuery = function (likeText) {

var
_common = ejs.QueryMixin('mlt'),
query = _common.toJSON();

query.mlt.like_text = likeText;
query.mlt.fields = [];

if (isString(fields)) {
query.mlt.fields.push(fields);
} else if (isArray(fields)) {
query.mlt.fields = fields;
} else {
throw new TypeError('Argument must be string or array');
}

return extend(_common, {

/**
Expand All @@ -12117,20 +12121,29 @@
*/
fields: function (f) {
if (f == null) {
return query.mlt.fields;
return this;
}

if (isString(f)) {
query.mlt.fields.push(f);
query.mlt.fields = [f];
} else if (isArray(f)) {
query.mlt.fields = f;
} else {
throw new TypeError('Argument must be a string or array');
throw new TypeError('Must pass a field or an array of fields');
}

return this;
},


docs: function(doc) {
if (isArray(doc)) {
query.mlt.docs = doc;
} else {
throw new TypeError('Must pass an array of docs as argument');
}
return this;
},

/**
The text to find documents like

Expand Down Expand Up @@ -16350,6 +16363,26 @@
return this;
},

/**
Allows you to set a specified post_filter on this request object.

@member ejs.Request
@param {Object} filter Any valid <code>Filter</code> object.
@returns {Object} returns <code>this</code> so that calls can be chained.
*/
post_filter: function (filter) {
if (filter == null) {
return query.filter;
}

if (!isFilter(filter)) {
throw new TypeError('Argument must be a Filter');
}

query.post_filter = filter.toJSON();
return this;
},

/**
Performs highlighting based on the <code>Highlight</code>
settings.
Expand Down
12 changes: 6 additions & 6 deletions dist/elastic.min.js

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions src/query/HasChildQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,19 @@
return this;
},

/**
* https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-inner-hits.html
* q should be a json
*/
inner_hits: function(q) {
if (q == null) {
return query.has_child.query;
}

query.has_child.inner_hits = q;
return this;
},

/**
Sets the child document type to search against

Expand Down
28 changes: 14 additions & 14 deletions src/query/MoreLikeThisQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,14 @@
@param {String} likeText The text to find documents like it.

*/
ejs.MoreLikeThisQuery = function (fields, likeText) {
ejs.MoreLikeThisQuery = function (likeText) {

var
_common = ejs.QueryMixin('mlt'),
query = _common.toJSON();

query.mlt.like_text = likeText;
query.mlt.fields = [];

if (isString(fields)) {
query.mlt.fields.push(fields);
} else if (isArray(fields)) {
query.mlt.fields = fields;
} else {
throw new TypeError('Argument must be string or array');
}

return extend(_common, {

/**
Expand All @@ -46,20 +37,29 @@
*/
fields: function (f) {
if (f == null) {
return query.mlt.fields;
return this;
}

if (isString(f)) {
query.mlt.fields.push(f);
query.mlt.fields = [f];
} else if (isArray(f)) {
query.mlt.fields = f;
} else {
throw new TypeError('Argument must be a string or array');
throw new TypeError('Must pass a field or an array of fields');
}

return this;
},


docs: function(doc) {
if (isArray(doc)) {
query.mlt.docs = doc;
} else {
throw new TypeError('Must pass an array of docs as argument');
}
return this;
},

/**
The text to find documents like

Expand Down
20 changes: 20 additions & 0 deletions src/search/Request.js
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,26 @@
return this;
},

/**
Allows you to set a specified post_filter on this request object.

@member ejs.Request
@param {Object} filter Any valid <code>Filter</code> object.
@returns {Object} returns <code>this</code> so that calls can be chained.
*/
post_filter: function (filter) {
if (filter == null) {
return query.filter;
}

if (!isFilter(filter)) {
throw new TypeError('Argument must be a Filter');
}

query.post_filter = filter.toJSON();
return this;
},

/**
Performs highlighting based on the <code>Highlight</code>
settings.
Expand Down
14 changes: 4 additions & 10 deletions tests/query_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1213,9 +1213,9 @@ exports.queries = {
test.done();
},
MoreLikeThisQuery: function (test) {
test.expect(22);
test.expect(21);

var mltQuery = ejs.MoreLikeThisQuery(['f', 'f2'], 'like text'),
var mltQuery = ejs.MoreLikeThisQuery('like text'),
expected,
doTest = function () {
test.deepEqual(mltQuery.toJSON(), expected);
Expand All @@ -1224,24 +1224,23 @@ exports.queries = {
expected = {
mlt: {
like_text: 'like text',
fields: ['f', 'f2']
}
};

test.ok(mltQuery, 'MoreLikeThisQuery exists');
test.ok(mltQuery.toJSON(), 'toJSON() works');
doTest();

mltQuery = ejs.MoreLikeThisQuery('f', 'like text');
mltQuery = ejs.MoreLikeThisQuery('like text');
expected = {
mlt: {
like_text: 'like text',
fields: ['f']
}
};
doTest();

mltQuery.fields('f2');
expected.mlt.fields = [];
expected.mlt.fields.push('f2');
doTest();

Expand Down Expand Up @@ -1303,11 +1302,6 @@ exports.queries = {

test.strictEqual(mltQuery._type(), 'query');


test.throws(function () {
ejs.MoreLikeThisQuery(9, 'like');
}, TypeError);

test.throws(function () {
mltQuery.fields(3);
}, TypeError);
Expand Down