From 8920fec4017449d79500ce1890e225f855d55de2 Mon Sep 17 00:00:00 2001 From: Niklas Donath Date: Wed, 31 Mar 2021 12:26:12 +0200 Subject: [PATCH 1/2] added fq() for passing in an object or multiple filters --- lib/query.js | 28 ++++++++++++++++++++++++++++ test/core-query-test.js | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/lib/query.js b/lib/query.js index b97e3a5b..d098eab0 100644 --- a/lib/query.js +++ b/lib/query.js @@ -267,6 +267,34 @@ Query.prototype.matchFilter = function (field, value) { return self; }; +/** + * wrapper function for matchFilter, accepting either an object with `field` and `value` properties + * or an array containing such objects to be mapped on matchFilter + * + * @param {Object|Array} filters - Object or an array of objects with `field` and `value` properties + * + * @return {Query} + * + * @throws {Error} + * @api public + * + * @example + * var query = client.createQuery(); + * query.q({ '*' : '*' }).fq({field: 'id', value: 100}) + * query.q({ '*' : '*' }).fq([{field: 'id', value: 100}, {field: 'name', value: 'John'}]) + */ + Query.prototype.fq = function (filters) { + const self = this + if (filters instanceof Array) { + filters.map(f => this.matchFilter(f.field, f.value)) + return self + } + if (filters instanceof Object) return this.matchFilter(filters.field, filters.value) + else { + throw new Error('unknown type for filter in fq()') + } +} + /** * Specify a set of fields to return. * diff --git a/test/core-query-test.js b/test/core-query-test.js index 1a9c8f46..6b4f07dd 100644 --- a/test/core-query-test.js +++ b/test/core-query-test.js @@ -266,5 +266,43 @@ describe('Client#createQuery', function () { done(); }); }); + + it('query with multiple match-filters', function (done) { + const query = client + .createQuery() + .q('*:*') + .fq([{field: 'id', value: '19700506.173.85'}, {field: 'title', value: 'testvalue'}]) + .debugQuery(); + + client.search(query, function (err, data) { + sassert.ok(err, data); + assert.deepEqual(data.responseHeader.params, { + debugQuery: 'true', + q: '*:*', + fq: ['id:19700506.173.85', 'title:testvalue'], + wt: 'json', + }); + done(); + }); + }); + + it('query with object match-filter', function (done) { + const query = client + .createQuery() + .q('*:*') + .fq({field: 'id', value: '19700506.173.85'}) + .debugQuery(); + + client.search(query, function (err, data) { + sassert.ok(err, data); + assert.deepEqual(data.responseHeader.params, { + debugQuery: 'true', + q: '*:*', + fq: 'id:19700506.173.85', + wt: 'json', + }); + done(); + }); + }); }); }); From bc8f7eb726ad8e38fb17abe0d72dc7bf742fd11f Mon Sep 17 00:00:00 2001 From: Niklas Donath Date: Mon, 26 Apr 2021 23:58:15 +0200 Subject: [PATCH 2/2] changed instanceof to Array.isArray() check --- lib/query.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/query.js b/lib/query.js index d098eab0..2d7cc602 100644 --- a/lib/query.js +++ b/lib/query.js @@ -285,7 +285,7 @@ Query.prototype.matchFilter = function (field, value) { */ Query.prototype.fq = function (filters) { const self = this - if (filters instanceof Array) { + if (Array.isArray(filters)) { filters.map(f => this.matchFilter(f.field, f.value)) return self }