-
Notifications
You must be signed in to change notification settings - Fork 72
/
HybridQueryBuilderTests.java
862 lines (772 loc) · 39.7 KB
/
HybridQueryBuilderTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
package org.opensearch.neuralsearch.query;
import static org.hamcrest.Matchers.containsString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.opensearch.core.xcontent.ToXContent.EMPTY_PARAMS;
import static org.opensearch.index.query.AbstractQueryBuilder.BOOST_FIELD;
import static org.opensearch.index.query.AbstractQueryBuilder.DEFAULT_BOOST;
import static org.opensearch.knn.index.query.KNNQueryBuilder.FILTER_FIELD;
import static org.opensearch.neuralsearch.util.TestUtils.xContentBuilderToMap;
import static org.opensearch.neuralsearch.query.NeuralQueryBuilder.K_FIELD;
import static org.opensearch.neuralsearch.query.NeuralQueryBuilder.MODEL_ID_FIELD;
import static org.opensearch.neuralsearch.query.NeuralQueryBuilder.QUERY_TEXT_FIELD;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;
import java.util.Optional;
import java.util.Set;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import org.apache.lucene.search.MatchNoDocsQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.opensearch.Version;
import org.opensearch.cluster.service.ClusterService;
import org.opensearch.common.io.stream.BytesStreamOutput;
import org.opensearch.common.settings.ClusterSettings;
import org.opensearch.common.settings.Setting;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.core.ParseField;
import org.opensearch.core.common.ParsingException;
import org.opensearch.core.common.bytes.BytesReference;
import org.opensearch.core.common.io.stream.FilterStreamInput;
import org.opensearch.core.common.io.stream.NamedWriteableAwareStreamInput;
import org.opensearch.core.common.io.stream.NamedWriteableRegistry;
import org.opensearch.core.index.Index;
import org.opensearch.core.xcontent.NamedXContentRegistry;
import org.opensearch.core.xcontent.ToXContent;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.core.xcontent.XContentParser;
import org.opensearch.index.mapper.MappedFieldType;
import org.opensearch.index.mapper.TextFieldMapper;
import org.opensearch.index.query.MatchAllQueryBuilder;
import org.opensearch.index.query.QueryBuilder;
import org.opensearch.index.query.QueryBuilders;
import org.opensearch.index.query.QueryShardContext;
import org.opensearch.index.query.TermQueryBuilder;
import org.opensearch.knn.index.KNNSettings;
import org.opensearch.knn.index.SpaceType;
import org.opensearch.knn.index.VectorDataType;
import org.opensearch.knn.index.engine.KNNEngine;
import org.opensearch.knn.index.engine.KNNMethodContext;
import org.opensearch.knn.index.engine.MethodComponentContext;
import org.opensearch.knn.index.mapper.KNNMappingConfig;
import org.opensearch.knn.index.mapper.KNNVectorFieldType;
import org.opensearch.knn.index.query.KNNQuery;
import org.opensearch.knn.index.query.KNNQueryBuilder;
import org.opensearch.knn.index.query.nativelib.NativeEngineKnnVectorQuery;
import org.opensearch.neuralsearch.util.NeuralSearchClusterTestUtils;
import org.opensearch.neuralsearch.util.NeuralSearchClusterUtil;
import com.carrotsearch.randomizedtesting.RandomizedTest;
import lombok.SneakyThrows;
public class HybridQueryBuilderTests extends OpenSearchQueryTestCase {
static final String VECTOR_FIELD_NAME = "vectorField";
static final String TEXT_FIELD_NAME = "field";
static final String QUERY_TEXT = "Hello world!";
static final String TERM_QUERY_TEXT = "keyword";
static final String MODEL_ID = "mfgfgdsfgfdgsde";
static final int K = 10;
static final float BOOST = 1.8f;
static final Supplier<float[]> TEST_VECTOR_SUPPLIER = () -> new float[4];
static final QueryBuilder TEST_FILTER = new MatchAllQueryBuilder();
@Mock
private ClusterService clusterService;
private AutoCloseable openMocks;
@Override
public void setUp() throws Exception {
super.setUp();
openMocks = MockitoAnnotations.openMocks(this);
// This is required to make sure that before every test we are initializing the KNNSettings. Not doing this
// leads to failures of unit tests cases when a unit test is run separately. Try running this test:
// ./gradlew ':test' --tests "org.opensearch.knn.training.TrainingJobTests.testRun_success" and see it fails
// but if run along with other tests this test passes.
initKNNSettings();
}
@Override
public void tearDown() throws Exception {
super.tearDown();
openMocks.close();
}
@SneakyThrows
public void testDoToQuery_whenNoSubqueries_thenBuildSuccessfully() {
HybridQueryBuilder queryBuilder = new HybridQueryBuilder();
Index dummyIndex = new Index("dummy", "dummy");
QueryShardContext mockQueryShardContext = mock(QueryShardContext.class);
when(mockQueryShardContext.index()).thenReturn(dummyIndex);
Query queryNoSubQueries = queryBuilder.doToQuery(mockQueryShardContext);
assertTrue(queryNoSubQueries instanceof MatchNoDocsQuery);
}
@SneakyThrows
public void testDoToQuery_whenOneSubquery_thenBuildSuccessfully() {
HybridQueryBuilder queryBuilder = new HybridQueryBuilder();
Index dummyIndex = new Index("dummy", "dummy");
QueryShardContext mockQueryShardContext = mock(QueryShardContext.class);
KNNVectorFieldType mockKNNVectorField = mock(KNNVectorFieldType.class);
KNNMappingConfig mockKNNMappingConfig = mock(KNNMappingConfig.class);
KNNMethodContext knnMethodContext = new KNNMethodContext(KNNEngine.FAISS, SpaceType.L2, MethodComponentContext.EMPTY);
when(mockKNNVectorField.getKnnMappingConfig()).thenReturn(mockKNNMappingConfig);
when(mockKNNMappingConfig.getKnnMethodContext()).thenReturn(Optional.of(knnMethodContext));
when(mockQueryShardContext.index()).thenReturn(dummyIndex);
when(mockKNNVectorField.getKnnMappingConfig().getDimension()).thenReturn(4);
when(mockKNNVectorField.getVectorDataType()).thenReturn(VectorDataType.FLOAT);
when(mockQueryShardContext.fieldMapper(eq(VECTOR_FIELD_NAME))).thenReturn(mockKNNVectorField);
NeuralQueryBuilder neuralQueryBuilder = new NeuralQueryBuilder().fieldName(VECTOR_FIELD_NAME)
.queryText(QUERY_TEXT)
.modelId(MODEL_ID)
.k(K)
.vectorSupplier(TEST_VECTOR_SUPPLIER);
queryBuilder.add(neuralQueryBuilder);
Query queryOnlyNeural = queryBuilder.doToQuery(mockQueryShardContext);
assertNotNull(queryOnlyNeural);
assertTrue(queryOnlyNeural instanceof HybridQuery);
assertEquals(1, ((HybridQuery) queryOnlyNeural).getSubQueries().size());
assertTrue(((HybridQuery) queryOnlyNeural).getSubQueries().iterator().next() instanceof NativeEngineKnnVectorQuery);
KNNQuery knnQuery = ((NativeEngineKnnVectorQuery) ((HybridQuery) queryOnlyNeural).getSubQueries().iterator().next()).getKnnQuery();
assertEquals(VECTOR_FIELD_NAME, knnQuery.getField());
assertEquals(K, knnQuery.getK());
assertNotNull(knnQuery.getQueryVector());
}
@SneakyThrows
public void testDoToQuery_whenMultipleSubqueries_thenBuildSuccessfully() {
HybridQueryBuilder queryBuilder = new HybridQueryBuilder();
Index dummyIndex = new Index("dummy", "dummy");
QueryShardContext mockQueryShardContext = mock(QueryShardContext.class);
KNNVectorFieldType mockKNNVectorField = mock(KNNVectorFieldType.class);
KNNMappingConfig mockKNNMappingConfig = mock(KNNMappingConfig.class);
KNNMethodContext knnMethodContext = new KNNMethodContext(KNNEngine.FAISS, SpaceType.L2, MethodComponentContext.EMPTY);
when(mockKNNVectorField.getKnnMappingConfig()).thenReturn(mockKNNMappingConfig);
when(mockKNNMappingConfig.getKnnMethodContext()).thenReturn(Optional.of(knnMethodContext));
when(mockQueryShardContext.index()).thenReturn(dummyIndex);
when(mockKNNVectorField.getKnnMappingConfig().getDimension()).thenReturn(4);
when(mockKNNVectorField.getVectorDataType()).thenReturn(VectorDataType.FLOAT);
when(mockQueryShardContext.fieldMapper(eq(VECTOR_FIELD_NAME))).thenReturn(mockKNNVectorField);
NeuralQueryBuilder neuralQueryBuilder = new NeuralQueryBuilder().fieldName(VECTOR_FIELD_NAME)
.queryText(QUERY_TEXT)
.modelId(MODEL_ID)
.k(K)
.vectorSupplier(TEST_VECTOR_SUPPLIER);
queryBuilder.add(neuralQueryBuilder);
TermQueryBuilder termSubQuery = QueryBuilders.termQuery(TEXT_FIELD_NAME, TERM_QUERY_TEXT);
queryBuilder.add(termSubQuery);
TextFieldMapper.TextFieldType fieldType = (TextFieldMapper.TextFieldType) createMapperService().fieldType(TEXT_FIELD_NAME);
when(mockQueryShardContext.fieldMapper(eq(TEXT_FIELD_NAME))).thenReturn(fieldType);
Query queryTwoSubQueries = queryBuilder.doToQuery(mockQueryShardContext);
assertNotNull(queryTwoSubQueries);
assertTrue(queryTwoSubQueries instanceof HybridQuery);
assertEquals(2, ((HybridQuery) queryTwoSubQueries).getSubQueries().size());
// verify knn vector query
Iterator<Query> queryIterator = ((HybridQuery) queryTwoSubQueries).getSubQueries().iterator();
Query firstQuery = queryIterator.next();
assertTrue(firstQuery instanceof NativeEngineKnnVectorQuery);
KNNQuery knnQuery = ((NativeEngineKnnVectorQuery) firstQuery).getKnnQuery();
assertEquals(VECTOR_FIELD_NAME, knnQuery.getField());
assertEquals(K, knnQuery.getK());
assertNotNull(knnQuery.getQueryVector());
// verify term query
Query secondQuery = queryIterator.next();
assertTrue(secondQuery instanceof TermQuery);
TermQuery termQuery = (TermQuery) secondQuery;
assertEquals(TEXT_FIELD_NAME, termQuery.getTerm().field());
assertEquals(TERM_QUERY_TEXT, termQuery.getTerm().text());
}
@SneakyThrows
public void testDoToQuery_whenTooManySubqueries_thenFail() {
// create query with 6 sub-queries, which is more than current max allowed
XContentBuilder xContentBuilder = XContentFactory.jsonBuilder()
.startObject()
.startArray("queries")
.startObject()
.startObject("term")
.field(TEXT_FIELD_NAME, RandomizedTest.randomAsciiAlphanumOfLength(10))
.endObject()
.endObject()
.startObject()
.startObject("term")
.field(TEXT_FIELD_NAME, RandomizedTest.randomAsciiAlphanumOfLength(10))
.endObject()
.endObject()
.startObject()
.startObject("term")
.field(TEXT_FIELD_NAME, RandomizedTest.randomAsciiAlphanumOfLength(10))
.endObject()
.endObject()
.startObject()
.startObject("term")
.field(TEXT_FIELD_NAME, RandomizedTest.randomAsciiAlphanumOfLength(10))
.endObject()
.endObject()
.startObject()
.startObject("term")
.field(TEXT_FIELD_NAME, RandomizedTest.randomAsciiAlphanumOfLength(10))
.endObject()
.endObject()
.startObject()
.startObject("term")
.field(TEXT_FIELD_NAME, TERM_QUERY_TEXT)
.endObject()
.endObject()
.endArray()
.endObject();
NamedXContentRegistry namedXContentRegistry = new NamedXContentRegistry(
List.of(
new NamedXContentRegistry.Entry(QueryBuilder.class, new ParseField(TermQueryBuilder.NAME), TermQueryBuilder::fromXContent),
new NamedXContentRegistry.Entry(
QueryBuilder.class,
new ParseField(HybridQueryBuilder.NAME),
HybridQueryBuilder::fromXContent
)
)
);
XContentParser contentParser = createParser(
namedXContentRegistry,
xContentBuilder.contentType().xContent(),
BytesReference.bytes(xContentBuilder)
);
contentParser.nextToken();
ParsingException exception = expectThrows(ParsingException.class, () -> HybridQueryBuilder.fromXContent(contentParser));
assertThat(exception.getMessage(), containsString("Number of sub-queries exceeds maximum supported"));
}
/**
* Tests basic query:
* {
* "query": {
* "hybrid": {
* "queries": [
* {
* "neural": {
* "text_knn": {
* "query_text": "Hello world",
* "model_id": "dcsdcasd",
* "k": 1
* }
* }
* },
* {
* "term": {
* "text": "keyword"
* }
* }
* ]
* }
* }
* }
*/
@SneakyThrows
public void testFromXContent_whenMultipleSubQueries_thenBuildSuccessfully() {
setUpClusterService();
XContentBuilder xContentBuilder = XContentFactory.jsonBuilder()
.startObject()
.startArray("queries")
.startObject()
.startObject(NeuralQueryBuilder.NAME)
.startObject(VECTOR_FIELD_NAME)
.field(QUERY_TEXT_FIELD.getPreferredName(), QUERY_TEXT)
.field(MODEL_ID_FIELD.getPreferredName(), MODEL_ID)
.field(K_FIELD.getPreferredName(), K)
.field(BOOST_FIELD.getPreferredName(), BOOST)
.endObject()
.endObject()
.endObject()
.startObject()
.startObject(TermQueryBuilder.NAME)
.field(TEXT_FIELD_NAME, TERM_QUERY_TEXT)
.endObject()
.endObject()
.endArray()
.endObject();
NamedXContentRegistry namedXContentRegistry = new NamedXContentRegistry(
List.of(
new NamedXContentRegistry.Entry(QueryBuilder.class, new ParseField(TermQueryBuilder.NAME), TermQueryBuilder::fromXContent),
new NamedXContentRegistry.Entry(
QueryBuilder.class,
new ParseField(NeuralQueryBuilder.NAME),
NeuralQueryBuilder::fromXContent
),
new NamedXContentRegistry.Entry(
QueryBuilder.class,
new ParseField(HybridQueryBuilder.NAME),
HybridQueryBuilder::fromXContent
)
)
);
XContentParser contentParser = createParser(
namedXContentRegistry,
xContentBuilder.contentType().xContent(),
BytesReference.bytes(xContentBuilder)
);
contentParser.nextToken();
HybridQueryBuilder queryTwoSubQueries = HybridQueryBuilder.fromXContent(contentParser);
assertEquals(2, queryTwoSubQueries.queries().size());
assertTrue(queryTwoSubQueries.queries().get(0) instanceof NeuralQueryBuilder);
assertTrue(queryTwoSubQueries.queries().get(1) instanceof TermQueryBuilder);
// verify knn vector query
NeuralQueryBuilder neuralQueryBuilder = (NeuralQueryBuilder) queryTwoSubQueries.queries().get(0);
assertEquals(VECTOR_FIELD_NAME, neuralQueryBuilder.fieldName());
assertEquals(QUERY_TEXT, neuralQueryBuilder.queryText());
assertEquals(K, (int) neuralQueryBuilder.k());
assertEquals(MODEL_ID, neuralQueryBuilder.modelId());
assertEquals(BOOST, neuralQueryBuilder.boost(), 0f);
// verify term query
TermQueryBuilder termQueryBuilder = (TermQueryBuilder) queryTwoSubQueries.queries().get(1);
assertEquals(TEXT_FIELD_NAME, termQueryBuilder.fieldName());
assertEquals(TERM_QUERY_TEXT, termQueryBuilder.value());
}
@SneakyThrows
public void testFromXContent_whenIncorrectFormat_thenFail() {
XContentBuilder unsupportedFieldXContentBuilder = XContentFactory.jsonBuilder()
.startObject()
.startArray("random_field")
.startObject()
.startObject(NeuralQueryBuilder.NAME)
.startObject(VECTOR_FIELD_NAME)
.field(QUERY_TEXT_FIELD.getPreferredName(), QUERY_TEXT)
.field(MODEL_ID_FIELD.getPreferredName(), MODEL_ID)
.field(K_FIELD.getPreferredName(), K)
.field(BOOST_FIELD.getPreferredName(), BOOST)
.endObject()
.endObject()
.endObject()
.endArray()
.endObject();
NamedXContentRegistry namedXContentRegistry = new NamedXContentRegistry(
List.of(
new NamedXContentRegistry.Entry(QueryBuilder.class, new ParseField(TermQueryBuilder.NAME), TermQueryBuilder::fromXContent),
new NamedXContentRegistry.Entry(
QueryBuilder.class,
new ParseField(NeuralQueryBuilder.NAME),
NeuralQueryBuilder::fromXContent
),
new NamedXContentRegistry.Entry(
QueryBuilder.class,
new ParseField(HybridQueryBuilder.NAME),
HybridQueryBuilder::fromXContent
)
)
);
XContentParser contentParser = createParser(
namedXContentRegistry,
unsupportedFieldXContentBuilder.contentType().xContent(),
BytesReference.bytes(unsupportedFieldXContentBuilder)
);
contentParser.nextToken();
expectThrows(ParsingException.class, () -> HybridQueryBuilder.fromXContent(contentParser));
XContentBuilder emptySubQueriesXContentBuilder = XContentFactory.jsonBuilder()
.startObject()
.startArray("queries")
.endArray()
.endObject();
XContentParser contentParser2 = createParser(
namedXContentRegistry,
unsupportedFieldXContentBuilder.contentType().xContent(),
BytesReference.bytes(emptySubQueriesXContentBuilder)
);
contentParser2.nextToken();
expectThrows(ParsingException.class, () -> HybridQueryBuilder.fromXContent(contentParser2));
}
@SneakyThrows
public void testToXContent_whenIncomingJsonIsCorrect_thenSuccessful() {
HybridQueryBuilder queryBuilder = new HybridQueryBuilder();
Index dummyIndex = new Index("dummy", "dummy");
QueryShardContext mockQueryShardContext = mock(QueryShardContext.class);
KNNVectorFieldType mockKNNVectorField = mock(KNNVectorFieldType.class);
KNNMappingConfig mockKNNMappingConfig = mock(KNNMappingConfig.class);
when(mockKNNVectorField.getKnnMappingConfig()).thenReturn(mockKNNMappingConfig);
when(mockQueryShardContext.index()).thenReturn(dummyIndex);
when(mockKNNVectorField.getKnnMappingConfig().getDimension()).thenReturn(4);
when(mockQueryShardContext.fieldMapper(eq(VECTOR_FIELD_NAME))).thenReturn(mockKNNVectorField);
NeuralQueryBuilder neuralQueryBuilder = new NeuralQueryBuilder().fieldName(VECTOR_FIELD_NAME)
.queryText(QUERY_TEXT)
.modelId(MODEL_ID)
.k(K)
.vectorSupplier(TEST_VECTOR_SUPPLIER)
.filter(TEST_FILTER);
queryBuilder.add(neuralQueryBuilder);
TermQueryBuilder termSubQuery = QueryBuilders.termQuery(TEXT_FIELD_NAME, TERM_QUERY_TEXT);
queryBuilder.add(termSubQuery);
TextFieldMapper.TextFieldType fieldType = (TextFieldMapper.TextFieldType) createMapperService().fieldType(TEXT_FIELD_NAME);
when(mockQueryShardContext.fieldMapper(eq(TEXT_FIELD_NAME))).thenReturn(fieldType);
XContentBuilder builder = XContentFactory.jsonBuilder();
builder = queryBuilder.toXContent(builder, ToXContent.EMPTY_PARAMS);
Map<String, Object> out = xContentBuilderToMap(builder);
Object outer = out.get(HybridQueryBuilder.NAME);
if (!(outer instanceof Map)) {
fail("hybrid does not map to nested object");
}
Map<String, Object> outerMap = (Map<String, Object>) outer;
assertNotNull(outerMap);
assertTrue(outerMap.containsKey("queries"));
assertTrue(outerMap.get("queries") instanceof List);
List listWithQueries = (List) outerMap.get("queries");
assertEquals(2, listWithQueries.size());
// verify neural search query
Map<String, Object> vectorFieldInnerMap = getInnerMap(listWithQueries.get(0), NeuralQueryBuilder.NAME, VECTOR_FIELD_NAME);
assertEquals(MODEL_ID, vectorFieldInnerMap.get(MODEL_ID_FIELD.getPreferredName()));
assertEquals(QUERY_TEXT, vectorFieldInnerMap.get(QUERY_TEXT_FIELD.getPreferredName()));
assertEquals(K, vectorFieldInnerMap.get(K_FIELD.getPreferredName()));
XContentBuilder xContentBuilder = XContentFactory.jsonBuilder();
assertEquals(
xContentBuilderToMap(TEST_FILTER.toXContent(xContentBuilder, EMPTY_PARAMS)),
vectorFieldInnerMap.get(FILTER_FIELD.getPreferredName())
);
// verify term query
Map<String, Object> termFieldInnerMap = getInnerMap(listWithQueries.get(1), TermQueryBuilder.NAME, TEXT_FIELD_NAME);
assertEquals(TERM_QUERY_TEXT, termFieldInnerMap.get("value"));
}
@SneakyThrows
public void testStreams_whenWrittingToStream_thenSuccessful() {
setUpClusterService();
HybridQueryBuilder original = new HybridQueryBuilder();
NeuralQueryBuilder neuralQueryBuilder = new NeuralQueryBuilder().fieldName(VECTOR_FIELD_NAME)
.queryText(QUERY_TEXT)
.modelId(MODEL_ID)
.k(K)
.vectorSupplier(TEST_VECTOR_SUPPLIER);
original.add(neuralQueryBuilder);
TermQueryBuilder termSubQuery = QueryBuilders.termQuery(TEXT_FIELD_NAME, TERM_QUERY_TEXT);
original.add(termSubQuery);
BytesStreamOutput streamOutput = new BytesStreamOutput();
original.writeTo(streamOutput);
FilterStreamInput filterStreamInput = new NamedWriteableAwareStreamInput(
streamOutput.bytes().streamInput(),
new NamedWriteableRegistry(
List.of(
new NamedWriteableRegistry.Entry(QueryBuilder.class, TermQueryBuilder.NAME, TermQueryBuilder::new),
new NamedWriteableRegistry.Entry(QueryBuilder.class, NeuralQueryBuilder.NAME, NeuralQueryBuilder::new),
new NamedWriteableRegistry.Entry(QueryBuilder.class, HybridQueryBuilder.NAME, HybridQueryBuilder::new)
)
)
);
HybridQueryBuilder copy = new HybridQueryBuilder(filterStreamInput);
assertEquals(original, copy);
}
public void testHashAndEquals_whenSameOrIdenticalObject_thenReturnEqual() {
HybridQueryBuilder hybridQueryBuilderBaseline = new HybridQueryBuilder();
hybridQueryBuilderBaseline.add(
new NeuralQueryBuilder().fieldName(VECTOR_FIELD_NAME)
.queryText(QUERY_TEXT)
.modelId(MODEL_ID)
.k(K)
.vectorSupplier(TEST_VECTOR_SUPPLIER)
.filter(TEST_FILTER)
);
hybridQueryBuilderBaseline.add(QueryBuilders.termQuery(TEXT_FIELD_NAME, TERM_QUERY_TEXT));
HybridQueryBuilder hybridQueryBuilderBaselineCopy = new HybridQueryBuilder();
hybridQueryBuilderBaselineCopy.add(
new NeuralQueryBuilder().fieldName(VECTOR_FIELD_NAME)
.queryText(QUERY_TEXT)
.modelId(MODEL_ID)
.k(K)
.vectorSupplier(TEST_VECTOR_SUPPLIER)
.filter(TEST_FILTER)
);
hybridQueryBuilderBaselineCopy.add(QueryBuilders.termQuery(TEXT_FIELD_NAME, TERM_QUERY_TEXT));
assertEquals(hybridQueryBuilderBaseline, hybridQueryBuilderBaseline);
assertEquals(hybridQueryBuilderBaseline.hashCode(), hybridQueryBuilderBaseline.hashCode());
assertEquals(hybridQueryBuilderBaselineCopy, hybridQueryBuilderBaselineCopy);
assertEquals(hybridQueryBuilderBaselineCopy.hashCode(), hybridQueryBuilderBaselineCopy.hashCode());
}
public void testHashAndEquals_whenSubQueriesDifferent_thenReturnNotEqual() {
String modelId = "testModelId";
String fieldName = "fieldTwo";
String queryText = "query text";
String termText = "another keyword";
HybridQueryBuilder hybridQueryBuilderBaseline = new HybridQueryBuilder();
hybridQueryBuilderBaseline.add(
new NeuralQueryBuilder().fieldName(VECTOR_FIELD_NAME)
.queryText(QUERY_TEXT)
.modelId(MODEL_ID)
.k(K)
.vectorSupplier(TEST_VECTOR_SUPPLIER)
.filter(TEST_FILTER)
);
hybridQueryBuilderBaseline.add(QueryBuilders.termQuery(TEXT_FIELD_NAME, TERM_QUERY_TEXT));
HybridQueryBuilder hybridQueryBuilderOnlyOneSubQuery = new HybridQueryBuilder();
hybridQueryBuilderOnlyOneSubQuery.add(
new NeuralQueryBuilder().fieldName(VECTOR_FIELD_NAME)
.queryText(QUERY_TEXT)
.modelId(MODEL_ID)
.k(K)
.vectorSupplier(TEST_VECTOR_SUPPLIER)
.filter(TEST_FILTER)
);
HybridQueryBuilder hybridQueryBuilderOnlyDifferentModelId = new HybridQueryBuilder();
hybridQueryBuilderOnlyDifferentModelId.add(
new NeuralQueryBuilder().fieldName(VECTOR_FIELD_NAME)
.queryText(QUERY_TEXT)
.modelId(modelId)
.k(K)
.vectorSupplier(TEST_VECTOR_SUPPLIER)
.filter(TEST_FILTER)
);
hybridQueryBuilderBaseline.add(QueryBuilders.termQuery(TEXT_FIELD_NAME, TERM_QUERY_TEXT));
HybridQueryBuilder hybridQueryBuilderOnlyDifferentFieldName = new HybridQueryBuilder();
hybridQueryBuilderOnlyDifferentFieldName.add(
new NeuralQueryBuilder().fieldName(fieldName)
.queryText(QUERY_TEXT)
.modelId(MODEL_ID)
.k(K)
.vectorSupplier(TEST_VECTOR_SUPPLIER)
.filter(TEST_FILTER)
);
hybridQueryBuilderOnlyDifferentFieldName.add(QueryBuilders.termQuery(TEXT_FIELD_NAME, TERM_QUERY_TEXT));
HybridQueryBuilder hybridQueryBuilderOnlyDifferentQuery = new HybridQueryBuilder();
hybridQueryBuilderOnlyDifferentQuery.add(
new NeuralQueryBuilder().fieldName(VECTOR_FIELD_NAME)
.queryText(queryText)
.modelId(MODEL_ID)
.k(K)
.vectorSupplier(TEST_VECTOR_SUPPLIER)
.filter(TEST_FILTER)
);
hybridQueryBuilderOnlyDifferentQuery.add(QueryBuilders.termQuery(TEXT_FIELD_NAME, TERM_QUERY_TEXT));
HybridQueryBuilder hybridQueryBuilderOnlyDifferentTermValue = new HybridQueryBuilder();
hybridQueryBuilderOnlyDifferentTermValue.add(
new NeuralQueryBuilder().fieldName(VECTOR_FIELD_NAME)
.queryText(QUERY_TEXT)
.modelId(MODEL_ID)
.k(K)
.vectorSupplier(TEST_VECTOR_SUPPLIER)
.filter(TEST_FILTER)
);
hybridQueryBuilderOnlyDifferentTermValue.add(QueryBuilders.termQuery(TEXT_FIELD_NAME, termText));
assertNotEquals(hybridQueryBuilderBaseline, hybridQueryBuilderOnlyOneSubQuery);
assertNotEquals(hybridQueryBuilderBaseline.hashCode(), hybridQueryBuilderOnlyOneSubQuery.hashCode());
assertNotEquals(hybridQueryBuilderBaseline, hybridQueryBuilderOnlyDifferentModelId);
assertNotEquals(hybridQueryBuilderBaseline.hashCode(), hybridQueryBuilderOnlyDifferentModelId.hashCode());
assertNotEquals(hybridQueryBuilderBaseline, hybridQueryBuilderOnlyDifferentFieldName);
assertNotEquals(hybridQueryBuilderBaseline.hashCode(), hybridQueryBuilderOnlyDifferentFieldName.hashCode());
assertNotEquals(hybridQueryBuilderBaseline, hybridQueryBuilderOnlyDifferentQuery);
assertNotEquals(hybridQueryBuilderBaseline.hashCode(), hybridQueryBuilderOnlyDifferentQuery.hashCode());
assertNotEquals(hybridQueryBuilderBaseline, hybridQueryBuilderOnlyDifferentTermValue);
assertNotEquals(hybridQueryBuilderBaseline.hashCode(), hybridQueryBuilderOnlyDifferentTermValue.hashCode());
}
@SneakyThrows
public void testRewrite_whenMultipleSubQueries_thenReturnBuilderForEachSubQuery() {
HybridQueryBuilder queryBuilder = new HybridQueryBuilder();
NeuralQueryBuilder neuralQueryBuilder = new NeuralQueryBuilder().fieldName(VECTOR_FIELD_NAME)
.queryText(QUERY_TEXT)
.modelId(MODEL_ID)
.k(K)
.vectorSupplier(TEST_VECTOR_SUPPLIER);
queryBuilder.add(neuralQueryBuilder);
TermQueryBuilder termSubQuery = QueryBuilders.termQuery(TEXT_FIELD_NAME, TERM_QUERY_TEXT);
queryBuilder.add(termSubQuery);
QueryShardContext mockQueryShardContext = mock(QueryShardContext.class);
KNNVectorFieldType mockKNNVectorField = mock(KNNVectorFieldType.class);
KNNMappingConfig mockKNNMappingConfig = mock(KNNMappingConfig.class);
when(mockKNNVectorField.getKnnMappingConfig()).thenReturn(mockKNNMappingConfig);
Index dummyIndex = new Index("dummy", "dummy");
when(mockQueryShardContext.index()).thenReturn(dummyIndex);
when(mockKNNVectorField.getKnnMappingConfig().getDimension()).thenReturn(4);
when(mockQueryShardContext.fieldMapper(eq(VECTOR_FIELD_NAME))).thenReturn(mockKNNVectorField);
TextFieldMapper.TextFieldType fieldType = (TextFieldMapper.TextFieldType) createMapperService().fieldType(TEXT_FIELD_NAME);
when(mockQueryShardContext.fieldMapper(eq(TEXT_FIELD_NAME))).thenReturn(fieldType);
QueryBuilder queryBuilderAfterRewrite = queryBuilder.doRewrite(mockQueryShardContext);
assertTrue(queryBuilderAfterRewrite instanceof HybridQueryBuilder);
HybridQueryBuilder hybridQueryBuilder = (HybridQueryBuilder) queryBuilderAfterRewrite;
assertNotNull(hybridQueryBuilder.queries());
assertEquals(2, hybridQueryBuilder.queries().size());
List<QueryBuilder> queryBuilders = hybridQueryBuilder.queries();
// verify each sub-query builder
assertTrue(queryBuilders.get(0) instanceof KNNQueryBuilder);
KNNQueryBuilder knnQueryBuilder = (KNNQueryBuilder) queryBuilders.get(0);
assertEquals(neuralQueryBuilder.fieldName(), knnQueryBuilder.fieldName());
assertEquals((int) neuralQueryBuilder.k(), knnQueryBuilder.getK());
assertTrue(queryBuilders.get(1) instanceof TermQueryBuilder);
TermQueryBuilder termQueryBuilder = (TermQueryBuilder) queryBuilders.get(1);
assertEquals(termSubQuery.fieldName(), termQueryBuilder.fieldName());
assertEquals(termSubQuery.value(), termQueryBuilder.value());
}
/**
* Tests query with boost:
* {
* "query": {
* "hybrid": {
* "queries": [
* {
* "term": {
* "text": "keyword"
* }
* },
* {
* "term": {
* "text": "keyword"
* }
* }
* ],
* "boost" : 2.0
* }
* }
* }
*/
@SneakyThrows
public void testBoost_whenNonDefaultBoostSet_thenFail() {
XContentBuilder xContentBuilderWithNonDefaultBoost = XContentFactory.jsonBuilder()
.startObject()
.startArray("queries")
.startObject()
.startObject("term")
.field(TEXT_FIELD_NAME, RandomizedTest.randomAsciiAlphanumOfLength(10))
.endObject()
.endObject()
.startObject()
.startObject("term")
.field(TEXT_FIELD_NAME, RandomizedTest.randomAsciiAlphanumOfLength(10))
.endObject()
.endObject()
.endArray()
.field("boost", 2.0f)
.endObject();
NamedXContentRegistry namedXContentRegistry = new NamedXContentRegistry(
List.of(
new NamedXContentRegistry.Entry(QueryBuilder.class, new ParseField(TermQueryBuilder.NAME), TermQueryBuilder::fromXContent),
new NamedXContentRegistry.Entry(
QueryBuilder.class,
new ParseField(HybridQueryBuilder.NAME),
HybridQueryBuilder::fromXContent
)
)
);
XContentParser contentParser = createParser(
namedXContentRegistry,
xContentBuilderWithNonDefaultBoost.contentType().xContent(),
BytesReference.bytes(xContentBuilderWithNonDefaultBoost)
);
contentParser.nextToken();
ParsingException exception = expectThrows(ParsingException.class, () -> HybridQueryBuilder.fromXContent(contentParser));
assertThat(exception.getMessage(), containsString("query does not support [boost]"));
}
@SneakyThrows
public void testBoost_whenDefaultBoostSet_thenBuildSuccessfully() {
// create query with 6 sub-queries, which is more than current max allowed
XContentBuilder xContentBuilderWithNonDefaultBoost = XContentFactory.jsonBuilder()
.startObject()
.startArray("queries")
.startObject()
.startObject("term")
.field(TEXT_FIELD_NAME, RandomizedTest.randomAsciiAlphanumOfLength(10))
.endObject()
.endObject()
.startObject()
.startObject("term")
.field(TEXT_FIELD_NAME, RandomizedTest.randomAsciiAlphanumOfLength(10))
.endObject()
.endObject()
.endArray()
.field("boost", DEFAULT_BOOST)
.endObject();
NamedXContentRegistry namedXContentRegistry = new NamedXContentRegistry(
List.of(
new NamedXContentRegistry.Entry(QueryBuilder.class, new ParseField(TermQueryBuilder.NAME), TermQueryBuilder::fromXContent),
new NamedXContentRegistry.Entry(
QueryBuilder.class,
new ParseField(HybridQueryBuilder.NAME),
HybridQueryBuilder::fromXContent
)
)
);
XContentParser contentParser = createParser(
namedXContentRegistry,
xContentBuilderWithNonDefaultBoost.contentType().xContent(),
BytesReference.bytes(xContentBuilderWithNonDefaultBoost)
);
contentParser.nextToken();
HybridQueryBuilder hybridQueryBuilder = HybridQueryBuilder.fromXContent(contentParser);
assertNotNull(hybridQueryBuilder);
}
@SneakyThrows
public void testBuild_whenValidParameters_thenCreateQuery() {
String queryText = "test query";
String modelId = "test_model";
String fieldName = "rank_features";
// Create mock context
QueryShardContext context = mock(QueryShardContext.class);
MappedFieldType fieldType = mock(MappedFieldType.class);
when(context.fieldMapper(fieldName)).thenReturn(fieldType);
when(fieldType.typeName()).thenReturn("rank_features");
// Create HybridQueryBuilder instance (no spy since it's final)
NeuralSparseQueryBuilder neuralSparseQueryBuilder = new NeuralSparseQueryBuilder();
neuralSparseQueryBuilder.fieldName(fieldName)
.queryText(queryText)
.modelId(modelId)
.queryTokensSupplier(() -> Map.of("token1", 1.0f, "token2", 0.5f));
HybridQueryBuilder builder = new HybridQueryBuilder().add(neuralSparseQueryBuilder);
// Build query
Query query = builder.toQuery(context);
// Verify
assertNotNull("Query should not be null", query);
assertTrue("Should be HybridQuery", query instanceof HybridQuery);
}
@SneakyThrows
public void testDoEquals_whenSameParameters_thenEqual() {
// Create neural queries
NeuralQueryBuilder neuralQueryBuilder1 = new NeuralQueryBuilder().queryText("test").modelId("test_model");
NeuralQueryBuilder neuralQueryBuilder2 = new NeuralQueryBuilder().queryText("test").modelId("test_model");
// Create neural sparse queries with queryTokensSupplier
NeuralSparseQueryBuilder neuralSparseQueryBuilder1 = new NeuralSparseQueryBuilder().fieldName("test_field")
.queryText("test")
.modelId("test_model")
.queryTokensSupplier(() -> Map.of("token1", 1.0f));
NeuralSparseQueryBuilder neuralSparseQueryBuilder2 = new NeuralSparseQueryBuilder().fieldName("test_field")
.queryText("test")
.modelId("test_model")
.queryTokensSupplier(() -> Map.of("token1", 1.0f));
// Create builders
HybridQueryBuilder builder1 = new HybridQueryBuilder().add(neuralQueryBuilder1).add(neuralSparseQueryBuilder1);
HybridQueryBuilder builder2 = new HybridQueryBuilder().add(neuralQueryBuilder2).add(neuralSparseQueryBuilder2);
// Verify
assertTrue("Builders should be equal", builder1.equals(builder2));
assertEquals("Hash codes should match", builder1.hashCode(), builder2.hashCode());
}
public void testValidate_whenInvalidParameters_thenThrowException() {
// Test null query builder
HybridQueryBuilder builderWithNull = new HybridQueryBuilder();
IllegalArgumentException nullException = assertThrows(IllegalArgumentException.class, () -> builderWithNull.add(null));
assertEquals("inner hybrid query clause cannot be null", nullException.getMessage());
}
public void testVisit() {
HybridQueryBuilder hybridQueryBuilder = new HybridQueryBuilder().add(new NeuralQueryBuilder()).add(new NeuralSparseQueryBuilder());
List<QueryBuilder> visitedQueries = new ArrayList<>();
hybridQueryBuilder.visit(createTestVisitor(visitedQueries));
assertEquals(3, visitedQueries.size());
}
private Map<String, Object> getInnerMap(Object innerObject, String queryName, String fieldName) {
if (!(innerObject instanceof Map)) {
fail("field name does not map to nested object");
}
Map<String, Object> secondInnerMap = (Map<String, Object>) innerObject;
assertTrue(secondInnerMap.containsKey(queryName));
assertTrue(secondInnerMap.get(queryName) instanceof Map);
Map<String, Object> neuralInnerMap = (Map<String, Object>) secondInnerMap.get(queryName);
assertTrue(neuralInnerMap.containsKey(fieldName));
assertTrue(neuralInnerMap.get(fieldName) instanceof Map);
Map<String, Object> vectorFieldInnerMap = (Map<String, Object>) neuralInnerMap.get(fieldName);
return vectorFieldInnerMap;
}
private void setUpClusterService() {
ClusterService clusterService = NeuralSearchClusterTestUtils.mockClusterService(Version.CURRENT);
NeuralSearchClusterUtil.instance().initialize(clusterService);
}
private void initKNNSettings() {
Set<Setting<?>> defaultClusterSettings = new HashSet<>(ClusterSettings.BUILT_IN_CLUSTER_SETTINGS);
defaultClusterSettings.addAll(
KNNSettings.state()
.getSettings()
.stream()
.filter(s -> s.getProperties().contains(Setting.Property.NodeScope))
.collect(Collectors.toList())
);
when(clusterService.getClusterSettings()).thenReturn(new ClusterSettings(Settings.EMPTY, defaultClusterSettings));
KNNSettings.state().setClusterService(clusterService);
}
}