Skip to content

Commit 8030449

Browse files
spinscaleAlexander Reelsen
andauthored
feat: Allow fieldname as parameter (#215)
Co-authored-by: Alexander Reelsen <[email protected]>
1 parent 28532fa commit 8030449

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/queries/vector-queries/sparse-vector-query.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
const { Query } = require('../../core');
4+
const { isNil } = require('lodash');
45

56
/**
67
* The sparse vector query executes a query consisting of sparse vectors, such as built by a learned sparse retrieval model,
@@ -15,8 +16,10 @@ const { Query } = require('../../core');
1516
*/
1617
class SparseVectorQuery extends Query {
1718
// eslint-disable-next-line require-jsdoc
18-
constructor() {
19+
20+
constructor(field) {
1921
super('sparse_vector');
22+
if (!isNil(field)) this._queryOpts.field = field;
2023
}
2124

2225
/**

test/queries-test/sparse-vector-query.test.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import test from 'ava';
2-
import { SparseVectorQuery } from '../../src';
2+
import esb, { SparseVectorQuery } from '../../src';
33

44
test('with inference id and query', t => {
55
const q = new SparseVectorQuery();
@@ -136,3 +136,15 @@ test('with pruning config for multiple elements', t => {
136136
};
137137
t.deepEqual(q.toJSON(), expected);
138138
});
139+
140+
test('call sparse vector query via esb', t => {
141+
const q = esb.sparseVectorQuery('my_field').query('my query');
142+
143+
const expected = {
144+
sparse_vector: {
145+
field: 'my_field',
146+
query: 'my query'
147+
}
148+
};
149+
t.deepEqual(q.toJSON(), expected);
150+
});

0 commit comments

Comments
 (0)