From 98a84530bda48b0d2d2f2e6737efd724fafaa2d9 Mon Sep 17 00:00:00 2001 From: Blake McCool Date: Mon, 6 Dec 2021 17:00:32 -0700 Subject: [PATCH 1/2] feat: Add type option to queryStringQuery --- CONTRIBUTING.md | 2 +- src/core/consts.js | 9 ++++ .../full-text-queries/query-string-query.js | 44 ++++++++++++++++++- test/queries-test/query-string-query.test.js | 11 ++++- 4 files changed, 63 insertions(+), 3 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index d7c2f1d9..32eae7a2 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -53,7 +53,7 @@ This is the last step! Make sure your PR is aimed to merge with the `master` branch. You should also write a good PR message with information on why this feature or -fix is necesary or a good idea. For features, be sure to include information on +fix is necessary or a good idea. For features, be sure to include information on how to use the feature; and for bugs, information on how to reproduce the bug is helpful! diff --git a/src/core/consts.js b/src/core/consts.js index 642a07f1..318a578c 100644 --- a/src/core/consts.js +++ b/src/core/consts.js @@ -108,6 +108,15 @@ exports.GEO_RELATION_SET = new Set([ 'INTERSECTS' ]); +exports.QUERY_STRING_TYPE = new Set([ + 'best_fields', + 'most_fields', + 'cross_fields', + 'phrase', + 'phrase_prefix', + 'bool_prefix' +]); + exports.SUGGEST_MODE_SET = new Set(['missing', 'popular', 'always']); exports.STRING_DISTANCE_SET = new Set([ diff --git a/src/queries/full-text-queries/query-string-query.js b/src/queries/full-text-queries/query-string-query.js index 3e9cd7c7..d726ac57 100644 --- a/src/queries/full-text-queries/query-string-query.js +++ b/src/queries/full-text-queries/query-string-query.js @@ -2,10 +2,16 @@ const QueryStringQueryBase = require('./query-string-query-base'); const { validateRewiteMethod } = require('../helper'); - +const isNil = require('lodash.isnil'); +const { + util: { invalidParam }, + consts: { QUERY_STRING_TYPE } +} = require('../../core'); const ES_REF_URL = 'https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html'; +const invalidTypeParam = invalidParam(ES_REF_URL, 'type', QUERY_STRING_TYPE); + /** * A query that uses a query parser in order to parse its content. * @@ -303,6 +309,42 @@ class QueryStringQuery extends QueryStringQueryBase { this._queryOpts.escape = enable; return this; } + + /** + * Sets the type of query string query. Valid values are: + * - `best_fields` - (default) Finds documents which match any field, + * but uses the `_score` from the best field. + * + * - `most_fields` - Finds documents which match any field and combines + * the `_score` from each field. + * + * - `cross_fields` - Treats fields with the same `analyzer` as though + * they were one big field. Looks for each word in *any* field + * + * - `phrase` - Runs a `match_phrase` query on each field and combines + * the `_score` from each field. + * + * - `phrase_prefix` - Runs a `match_phrase_prefix` query on each field + * and combines the `_score` from each field. + * + * - `bool_prefix` - Creates a match_bool_prefix query on each field and + * combines the _score from each field. + * + * + * @param {string} type Can be one of `best_fields`, `most_fields`, + * `cross_fields`, `phrase`, `phrase_prefix` and `bool_prefix`. Default is + * `best_fields`. + * @returns {QueryStringQuery} returns `this` so that calls can be chained. + */ + type(type) { + if (isNil(type)) invalidTypeParam(type); + + const typeLower = type.toLowerCase(); + if (!QUERY_STRING_TYPE.has(typeLower)) invalidTypeParam(type); + + this._queryOpts.type = typeLower; + return this; + } } module.exports = QueryStringQuery; diff --git a/test/queries-test/query-string-query.test.js b/test/queries-test/query-string-query.test.js index 71272db1..1b02aaa2 100644 --- a/test/queries-test/query-string-query.test.js +++ b/test/queries-test/query-string-query.test.js @@ -21,7 +21,15 @@ const validRewrites = [ 'top_terms_boost_23', 'top_terms_15' ]; - +const validType = [ + 'best_fields', + 'most_fields', + 'cross_fields', + 'phrase', + 'phrase_prefix', + 'bool_prefix' +]; +test(validatedCorrectly, getInstance, 'type', validType, false); test(validatedCorrectly, getInstance, 'rewrite', validRewrites, false); test(validatedCorrectly, getInstance, 'fuzzyRewrite', validRewrites, false); test(setsOption, 'defaultField', { param: 'my_field' }); @@ -41,3 +49,4 @@ test(setsOption, 'useDisMax', { param: true }); test(setsOption, 'tieBreaker', { param: 0.3 }); test(setsOption, 'quoteAnalyzer', { param: 'my_analyzer' }); test(setsOption, 'escape', { param: true }); +test(setsOption, 'type', { param: 'best_fields' }); From 726cd25cb428ee9bc5bfecd45608e5b69bd994e4 Mon Sep 17 00:00:00 2001 From: Blake McCool Date: Thu, 23 Dec 2021 10:28:13 -0700 Subject: [PATCH 2/2] add notes about support for type in query string query --- src/queries/full-text-queries/query-string-query.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/queries/full-text-queries/query-string-query.js b/src/queries/full-text-queries/query-string-query.js index d726ac57..12cf102a 100644 --- a/src/queries/full-text-queries/query-string-query.js +++ b/src/queries/full-text-queries/query-string-query.js @@ -311,7 +311,7 @@ class QueryStringQuery extends QueryStringQueryBase { } /** - * Sets the type of query string query. Valid values are: + * Sets the type of query string query (Added in v6.0). Valid values are: * - `best_fields` - (default) Finds documents which match any field, * but uses the `_score` from the best field. * @@ -327,7 +327,7 @@ class QueryStringQuery extends QueryStringQueryBase { * - `phrase_prefix` - Runs a `match_phrase_prefix` query on each field * and combines the `_score` from each field. * - * - `bool_prefix` - Creates a match_bool_prefix query on each field and + * - `bool_prefix` - (added in v7.2) Creates a match_bool_prefix query on each field and * combines the _score from each field. * *