From 9f0027f32e76dfb9f260f7b50be59fb1cd9c4605 Mon Sep 17 00:00:00 2001 From: Austin Cawley-Edwards Date: Wed, 13 Mar 2019 23:10:30 -0400 Subject: [PATCH] feat: Add method PercolateQuery.documents (#87) --- .gitignore | 3 ++ package.json | 5 ++- src/index.d.ts | 19 +++++++--- .../specialized-queries/percolate-query.js | 37 ++++++++++++++++--- test/queries-test/percolate-query.test.js | 34 +++++++++++++++-- test/typedef.test.ts | 10 +++++ 6 files changed, 94 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index 45bfe55f..2c3372ce 100644 --- a/.gitignore +++ b/.gitignore @@ -49,3 +49,6 @@ _build browser/ docs/assets/ docs/index.html + +# ides +.idea diff --git a/package.json b/package.json index d409dad9..f0600f39 100644 --- a/package.json +++ b/package.json @@ -97,5 +97,8 @@ "ejs", "dsl" ], - "author": "Suhas Karanth " + "author": "Suhas Karanth ", + "contributors": [ + "austin ce " + ] } diff --git a/src/index.d.ts b/src/index.d.ts index 3d451fbe..5a749b31 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -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. @@ -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. @@ -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` */ diff --git a/src/queries/specialized-queries/percolate-query.js b/src/queries/specialized-queries/percolate-query.js index bbb4a3fb..229e694a 100644 --- a/src/queries/specialized-queries/percolate-query.js +++ b/src/queries/specialized-queries/percolate-query.js @@ -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. @@ -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: @@ -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; } diff --git a/test/queries-test/percolate-query.test.js b/test/queries-test/percolate-query.test.js index 8b091f34..0ad3d61c 100644 --- a/test/queries-test/percolate-query.test.js +++ b/test/queries-test/percolate-query.test.js @@ -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' }); @@ -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); diff --git a/test/typedef.test.ts b/test/typedef.test.ts index 4db00b60..a4584de8 100644 --- a/test/typedef.test.ts +++ b/test/typedef.test.ts @@ -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'));