|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const { Query } = require('../../core'); |
| 4 | + |
| 5 | +/** |
| 6 | + * The sparse vector query executes a query consisting of sparse vectors, such as built by a learned sparse retrieval model, |
| 7 | + * NOTE: Only available in Elasticsearch v8.15+ |
| 8 | + * |
| 9 | + * [Elasticsearch reference](https://www.elastic.co/docs/reference/query-languages/query-dsl/query-dsl-sparse-vector-query) |
| 10 | + * |
| 11 | + * @example |
| 12 | + * const qry = esb.sparseVector().field('ml_tokens').inferenceId('model_id').query('my query'); |
| 13 | + * |
| 14 | + * @extends Query |
| 15 | + */ |
| 16 | +class SparseVectorQuery extends Query { |
| 17 | + // eslint-disable-next-line require-jsdoc |
| 18 | + constructor() { |
| 19 | + super('sparse_vector'); |
| 20 | + } |
| 21 | + |
| 22 | + /** |
| 23 | + * Sets the field to query |
| 24 | + * |
| 25 | + * @param {string} field the field for the query |
| 26 | + * @returns {SparseVectorQuery} |
| 27 | + */ |
| 28 | + field(field) { |
| 29 | + this._queryOpts.field = field; |
| 30 | + return this; |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Set model inference id |
| 35 | + * |
| 36 | + * @param {string} inferenceId The model inference ID |
| 37 | + * @returns {SparseVectorQuery} |
| 38 | + */ |
| 39 | + inferenceId(inferenceId) { |
| 40 | + this._queryOpts.inference_id = inferenceId; |
| 41 | + return this; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Sets the input query. |
| 46 | + * You should set either query or query vector, but not both |
| 47 | + * |
| 48 | + * @param {string} query The input query |
| 49 | + * @returns {SparseVectorQuery} |
| 50 | + */ |
| 51 | + query(query) { |
| 52 | + this._queryOpts.query = query; |
| 53 | + return this; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Set a query vector to the query to run. if you don't use inference |
| 58 | + * You should set either query or query vector, but not both |
| 59 | + * |
| 60 | + * @param {Object} queryVector |
| 61 | + * @returns {SparseVectorQuery} |
| 62 | + */ |
| 63 | + queryVector(queryVector) { |
| 64 | + this._queryOpts.query_vector = queryVector; |
| 65 | + return this; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Enable pruning |
| 70 | + * |
| 71 | + * NOTE: Only available in Elasticsearch v9.0+ |
| 72 | + * |
| 73 | + * @param {boolean} prune |
| 74 | + * @returns {SparseVectorQuery} returns `this` so that calls can be chained. |
| 75 | + */ |
| 76 | + prune(prune) { |
| 77 | + this._queryOpts.prune = prune; |
| 78 | + return this; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Set pruning config tokens_freq_ratio_threshold |
| 83 | + * |
| 84 | + * NOTE: Only available in Elasticsearch v9.0+ |
| 85 | + * |
| 86 | + * @param {number} tokensFreqRatioThreshold |
| 87 | + * @returns {SparseVectorQuery} returns `this` so that calls can be chained. |
| 88 | + */ |
| 89 | + tokensFreqRatioThreshold(tokensFreqRatioThreshold) { |
| 90 | + if (!this._queryOpts.pruning_config) { |
| 91 | + this._queryOpts.pruning_config = {}; |
| 92 | + } |
| 93 | + this._queryOpts.pruning_config.tokens_freq_ratio_threshold = tokensFreqRatioThreshold; |
| 94 | + return this; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Set pruning config tokens_weight_threshold |
| 99 | + * |
| 100 | + * NOTE: Only available in Elasticsearch v9.0+ |
| 101 | + * |
| 102 | + * @param {number} tokensWeightThreshold |
| 103 | + * @returns {SparseVectorQuery} returns `this` so that calls can be chained. |
| 104 | + */ |
| 105 | + tokensWeightThreshold(tokensWeightThreshold) { |
| 106 | + if (!this._queryOpts.pruning_config) { |
| 107 | + this._queryOpts.pruning_config = {}; |
| 108 | + } |
| 109 | + this._queryOpts.pruning_config.tokens_weight_threshold = tokensWeightThreshold; |
| 110 | + return this; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Set pruning config only_score_pruned_tokens |
| 115 | + * |
| 116 | + * NOTE: Only available in Elasticsearch v9.0+ |
| 117 | + * |
| 118 | + * @param {boolean} onlyScorePrunedTokens |
| 119 | + * @returns {SparseVectorQuery} returns `this` so that calls can be chained. |
| 120 | + */ |
| 121 | + onlyScorePrunedTokens(onlyScorePrunedTokens) { |
| 122 | + if (!this._queryOpts.pruning_config) { |
| 123 | + this._queryOpts.pruning_config = {}; |
| 124 | + } |
| 125 | + this._queryOpts.pruning_config.only_score_pruned_tokens = onlyScorePrunedTokens; |
| 126 | + return this; |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +module.exports = SparseVectorQuery; |
0 commit comments