Skip to content

Commit

Permalink
feat: Add method PercolateQuery.documents (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
austince authored and sudo-suhas committed Mar 14, 2019
1 parent c52ee2c commit 9f0027f
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 14 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,6 @@ _build
browser/
docs/assets/
docs/index.html

# ides
.idea
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,8 @@
"ejs",
"dsl"
],
"author": "Suhas Karanth <sudo.suhas@gmail.com>"
"author": "Suhas Karanth <sudo.suhas@gmail.com>",
"contributors": [
"austin ce <austin.cawley@gmail.com>"
]
}
19 changes: 14 additions & 5 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3047,14 +3047,23 @@ declare namespace esb {
documentType(docType: string): this;

/**
* Sets the source of the document being percolated.
* Instead of specifying the source of the document being percolated,
* Appends given source document to the list of source documents being percolated.
* Instead of specifying the source document being percolated,
* the source can also be retrieved from an already stored document.
*
* @param {object} doc The source of the document being percolated.
* @param {Object} doc The source document being percolated.
*/
document(doc: object): this;

/**
* Appends given source documents to the list of source documents being percolated.
* Instead of specifying the source documents being percolated,
* the source can also be retrieved from already stored documents.
*
* @param {Object[]} docs The source documents being percolated.
*/
documents(docs: object[]): this;

/**
* Sets the index the document resides in. This is a required parameter if `document`
* is not specified.
Expand Down Expand Up @@ -8265,7 +8274,7 @@ declare namespace esb {
*/
export class Sort {
constructor(field?: string, order?: string);

/**
* Set order for sorting. The order defaults to `desc` when sorting on the `_score`,
* and defaults to `asc` when sorting on anything else.
Expand Down Expand Up @@ -8312,7 +8321,7 @@ declare namespace esb {
*
* Note: This method has been deprecated in elasticsearch 6.1. From 6.1 and
* later, use `nested` method instead.
*
*
* @param {Query} filterQuery
* @throws {TypeError} If filter query is not an instance of `Query`
*/
Expand Down
37 changes: 32 additions & 5 deletions src/queries/specialized-queries/percolate-query.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

const isNil = require('lodash.isnil');

const { Query } = require('../../core');
const {
util: { checkType },
Query
} = require('../../core');

/**
* The `percolate` query can be used to match queries stored in an index.
Expand Down Expand Up @@ -32,6 +35,7 @@ class PercolateQuery extends Query {
// eslint-disable-next-line require-jsdoc
constructor(field, docType) {
super('percolate');
this._queryOpts.documents = [];

if (!isNil(field)) this._queryOpts.field = field;
// Delegate this to method:
Expand Down Expand Up @@ -64,15 +68,38 @@ class PercolateQuery extends Query {
}

/**
* Sets the source of the document being percolated.
* Instead of specifying the source of the document being percolated,
* Appends given source document to the list of source documents being percolated.
* Instead of specifying the source document being percolated,
* the source can also be retrieved from an already stored document.
*
* @param {Object} doc The source of the document being percolated.
* @example
*const qry = esb.percolateQuery('query', 'people')
* .document({ name: 'Will Smith' });
*
* @param {Object} doc The source document being percolated.
* @returns {PercolateQuery} returns `this` so that calls can be chained.
*/
document(doc) {
this._queryOpts.document = doc;
this._queryOpts.documents.push(doc);
return this;
}

/**
* Appends given source documents to the list of source documents being percolated.
* Instead of specifying the source documents being percolated,
* the source can also be retrieved from already stored documents.
*
* @example
*const qry = esb.percolateQuery('query', 'people')
* .documents([{ name: 'Will Smith' }, { name: 'Willow Smith' }]);
*
* @param {Object[]} docs The source documents being percolated.
* @returns {PercolateQuery} returns `this` so that calls can be chained.
*/
documents(docs) {
checkType(docs, Array);

this._queryOpts.documents = this._queryOpts.documents.concat(docs);
return this;
}

Expand Down
34 changes: 31 additions & 3 deletions test/queries-test/percolate-query.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@ import { nameExpectStrategy, makeSetsOptionMacro } from '../_macros';

const setsOption = makeSetsOptionMacro(
percolateQuery,
nameExpectStrategy('percolate')
nameExpectStrategy('percolate', { documents: [] })
);

test(setsOption, 'field', { param: 'query' });
test(setsOption, 'documentType', { param: 'doctype' });
test(setsOption, 'document', {
param: { message: 'A new bonsai tree in the office' }
param: { message: 'A new bonsai tree in the office' },
propValue: [{ message: 'A new bonsai tree in the office' }],
keyName: 'documents'
});
test(setsOption, 'documents', {
param: [{ message: 'A new bonsai tree in the office' }],
spread: false
});
test(setsOption, 'index', { param: 'my-index' });
test(setsOption, 'type', { param: 'message' });
Expand All @@ -24,7 +30,29 @@ test('constructor sets arguments', t => {
const expected = {
percolate: {
field: 'query',
document_type: 'doctype'
document_type: 'doctype',
documents: []
}
};
t.deepEqual(value, expected);
});

test('set document after documents', t => {
const docA = { param: { message: 'a bonsai' } };
const docB = { param: { message: 'another bonsai' } };
const field = 'query';
const docType = 'docType';
const query = new PercolateQuery(field, docType);
const value = query
.document(docA)
.documents([docB])
.toJSON();

const expected = {
percolate: {
field,
document_type: docType,
documents: [docA, docB]
}
};
t.deepEqual(value, expected);
Expand Down
10 changes: 10 additions & 0 deletions test/typedef.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ esb
.minimumShouldMatch('30%')
);

// Percolate Query
esb
.requestBodySearch()
.query(
esb
.percolateQuery('query', 'people')
.document({ name: 'Will Smith' })
.documents([{ name: 'Willow Smith'}, { name: 'Jaden Smith' }])
);

// Aggregation
esb.requestBodySearch().size(0).agg(esb.termsAggregation('popular_colors', 'color'));

Expand Down

0 comments on commit 9f0027f

Please sign in to comment.