/
SearchSortIT.java
767 lines (648 loc) · 30.1 KB
/
SearchSortIT.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
/*
* Hibernate Search, full-text search for your domain model
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.search.integrationtest.backend.tck.search.sort;
import static org.hibernate.search.util.impl.integrationtest.common.assertion.SearchResultAssert.assertThat;
import static org.hibernate.search.util.impl.integrationtest.common.stub.mapper.StubMapperUtils.referenceProvider;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import org.hibernate.search.engine.backend.common.DocumentReference;
import org.hibernate.search.engine.backend.document.DocumentElement;
import org.hibernate.search.engine.backend.document.IndexFieldReference;
import org.hibernate.search.engine.backend.document.IndexObjectFieldReference;
import org.hibernate.search.engine.backend.document.model.dsl.IndexSchemaElement;
import org.hibernate.search.engine.backend.document.model.dsl.IndexSchemaObjectField;
import org.hibernate.search.engine.backend.document.model.dsl.ObjectFieldStorage;
import org.hibernate.search.engine.backend.types.Sortable;
import org.hibernate.search.engine.backend.work.execution.spi.IndexIndexingPlan;
import org.hibernate.search.engine.search.predicate.SearchPredicate;
import org.hibernate.search.engine.search.query.SearchQuery;
import org.hibernate.search.engine.search.query.SearchResult;
import org.hibernate.search.engine.search.sort.SearchSort;
import org.hibernate.search.engine.search.sort.dsl.SearchSortFactory;
import org.hibernate.search.engine.search.sort.dsl.SearchSortFactoryExtension;
import org.hibernate.search.engine.search.sort.dsl.SortFinalStep;
import org.hibernate.search.engine.search.sort.dsl.spi.DelegatingSearchSortFactory;
import org.hibernate.search.engine.search.sort.dsl.spi.SearchSortDslContext;
import org.hibernate.search.engine.spatial.GeoPoint;
import org.hibernate.search.integrationtest.backend.tck.testsupport.configuration.DefaultAnalysisDefinitions;
import org.hibernate.search.integrationtest.backend.tck.testsupport.util.rule.SearchSetupHelper;
import org.hibernate.search.util.common.SearchException;
import org.hibernate.search.util.impl.integrationtest.common.stub.mapper.StubMappingIndexManager;
import org.hibernate.search.util.impl.integrationtest.common.stub.mapper.StubMappingScope;
import org.hibernate.search.util.impl.test.SubTest;
import org.hibernate.search.util.impl.test.annotation.TestForIssue;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.assertj.core.api.Assertions;
/**
* Generic tests for sorts. More specific tests can be found in other classes, such as {@link FieldSearchSortIT}.
*/
public class SearchSortIT {
private static final String INDEX_NAME = "IndexName";
private static final String ANOTHER_INDEX_NAME = "AnotherIndexName";
private static final int INDEX_ORDER_CHECKS = 10;
private static final String FIRST_ID = "1";
private static final String SECOND_ID = "2";
private static final String THIRD_ID = "3";
private static final String EMPTY_ID = "empty";
@Rule
public SearchSetupHelper setupHelper = new SearchSetupHelper();
@Rule
public ExpectedException thrown = ExpectedException.none();
private IndexMapping indexMapping;
private StubMappingIndexManager indexManager;
private StubMappingIndexManager anotherIndexManager;
@Before
public void setup() {
setupHelper.start()
.withIndex(
INDEX_NAME,
ctx -> this.indexMapping = new IndexMapping( ctx.getSchemaElement() ),
indexManager -> this.indexManager = indexManager
)
.withIndex(
ANOTHER_INDEX_NAME,
// Using the same mapping here. But a different mapping would work the same.
// What matters here is that is a different index.
ctx -> new IndexMapping( ctx.getSchemaElement() ),
indexManager -> this.anotherIndexManager = indexManager
)
.setup();
initData();
}
private SearchQuery<DocumentReference> simpleQuery(
Function<? super SearchSortFactory, ? extends SortFinalStep> sortContributor) {
StubMappingScope scope = indexManager.createScope();
return scope.query()
.predicate( f -> f.matchAll() )
.sort( sortContributor )
.toQuery();
}
@Test
public void byIndexOrder() {
/*
* We don't really know in advance what the index order is, but we want it to be consistent.
* Thus we just test that the order stays the same over several calls.
*/
SearchQuery<DocumentReference> query = simpleQuery( b -> b.indexOrder() );
SearchResult<DocumentReference> firstCallResult = query.fetchAll();
assertThat( firstCallResult ).fromQuery( query )
.hasDocRefHitsAnyOrder( INDEX_NAME, FIRST_ID, SECOND_ID, THIRD_ID, EMPTY_ID );
List<DocumentReference> firstCallHits = firstCallResult.getHits();
for ( int i = 0; i < INDEX_ORDER_CHECKS; ++i ) {
// Rebuild the query to bypass any cache in the query object
query = simpleQuery( b -> b.indexOrder() );
assertThat( query ).hasHitsExactOrder( firstCallHits );
}
}
@Test
public void byField_flattened() {
SearchQuery<DocumentReference> query;
query = simpleQuery( b -> b.field( "flattenedObject.string" )
.asc().missing().last() );
assertThat( query ).hasDocRefHitsExactOrder( INDEX_NAME, FIRST_ID, SECOND_ID, THIRD_ID, EMPTY_ID );
query = simpleQuery( b -> b.field( "flattenedObject.string" )
.desc().missing().last() );
assertThat( query ).hasDocRefHitsExactOrder( INDEX_NAME, THIRD_ID, SECOND_ID, FIRST_ID, EMPTY_ID );
query = simpleQuery( b -> b.field( "flattenedObject.integer" )
.asc().missing().last() );
assertThat( query ).hasDocRefHitsExactOrder( INDEX_NAME, FIRST_ID, SECOND_ID, THIRD_ID, EMPTY_ID );
query = simpleQuery( b -> b.field( "flattenedObject.integer" )
.desc().missing().last() );
assertThat( query ).hasDocRefHitsExactOrder( INDEX_NAME, THIRD_ID, SECOND_ID, FIRST_ID, EMPTY_ID );
}
@Test
@TestForIssue( jiraKey = "HSEARCH-2254" )
public void byField_nested() {
SearchQuery<DocumentReference> query;
query = simpleQuery( b -> b.field( "nestedObject.string" )
.asc().missing().last() );
assertThat( query ).hasDocRefHitsExactOrder( INDEX_NAME, FIRST_ID, SECOND_ID, THIRD_ID, EMPTY_ID );
query = simpleQuery( b -> b.field( "nestedObject.string" )
.desc().missing().last() );
assertThat( query ).hasDocRefHitsExactOrder( INDEX_NAME, THIRD_ID, SECOND_ID, FIRST_ID, EMPTY_ID );
query = simpleQuery( b -> b.field( "nestedObject.string" )
.asc().missing().first() );
assertThat( query ).hasDocRefHitsExactOrder( INDEX_NAME, EMPTY_ID, FIRST_ID, SECOND_ID, THIRD_ID );
query = simpleQuery( b -> b.field( "nestedObject.string" )
.desc().missing().first() );
assertThat( query ).hasDocRefHitsExactOrder( INDEX_NAME, EMPTY_ID, THIRD_ID, SECOND_ID, FIRST_ID );
query = simpleQuery( b -> b.field( "nestedObject.integer" )
.asc().missing().last() );
assertThat( query ).hasDocRefHitsExactOrder( INDEX_NAME, FIRST_ID, SECOND_ID, THIRD_ID, EMPTY_ID );
query = simpleQuery( b -> b.field( "nestedObject.integer" )
.desc().missing().last() );
assertThat( query ).hasDocRefHitsExactOrder( INDEX_NAME, THIRD_ID, SECOND_ID, FIRST_ID, EMPTY_ID );
query = simpleQuery( b -> b.field( "nestedObject.integer" )
.asc().missing().first() );
assertThat( query ).hasDocRefHitsExactOrder( INDEX_NAME, EMPTY_ID, FIRST_ID, SECOND_ID, THIRD_ID );
query = simpleQuery( b -> b.field( "nestedObject.integer" )
.desc().missing().first() );
assertThat( query ).hasDocRefHitsExactOrder( INDEX_NAME, EMPTY_ID, THIRD_ID, SECOND_ID, FIRST_ID );
}
@Test
@TestForIssue( jiraKey = "HSEARCH-2254" )
public void byField_nested_x2() {
SearchQuery<DocumentReference> query;
query = simpleQuery( b -> b.field( "nestedObject.nestedObject.string" )
.asc().missing().last() );
assertThat( query ).hasDocRefHitsExactOrder( INDEX_NAME, FIRST_ID, SECOND_ID, THIRD_ID, EMPTY_ID );
query = simpleQuery( b -> b.field( "nestedObject.nestedObject.string" )
.desc().missing().last() );
assertThat( query ).hasDocRefHitsExactOrder( INDEX_NAME, THIRD_ID, SECOND_ID, FIRST_ID, EMPTY_ID );
query = simpleQuery( b -> b.field( "nestedObject.nestedObject.string" )
.asc().missing().first() );
assertThat( query ).hasDocRefHitsExactOrder( INDEX_NAME, EMPTY_ID, FIRST_ID, SECOND_ID, THIRD_ID );
query = simpleQuery( b -> b.field( "nestedObject.nestedObject.string" )
.desc().missing().first() );
assertThat( query ).hasDocRefHitsExactOrder( INDEX_NAME, EMPTY_ID, THIRD_ID, SECOND_ID, FIRST_ID );
query = simpleQuery( b -> b.field( "nestedObject.nestedObject.integer" )
.asc().missing().last() );
assertThat( query ).hasDocRefHitsExactOrder( INDEX_NAME, FIRST_ID, SECOND_ID, THIRD_ID, EMPTY_ID );
query = simpleQuery( b -> b.field( "nestedObject.nestedObject.integer" )
.desc().missing().last() );
assertThat( query ).hasDocRefHitsExactOrder( INDEX_NAME, THIRD_ID, SECOND_ID, FIRST_ID, EMPTY_ID );
query = simpleQuery( b -> b.field( "nestedObject.nestedObject.integer" )
.asc().missing().first() );
assertThat( query ).hasDocRefHitsExactOrder( INDEX_NAME, EMPTY_ID, FIRST_ID, SECOND_ID, THIRD_ID );
query = simpleQuery( b -> b.field( "nestedObject.nestedObject.integer" )
.desc().missing().first() );
assertThat( query ).hasDocRefHitsExactOrder( INDEX_NAME, EMPTY_ID, THIRD_ID, SECOND_ID, FIRST_ID );
}
@Test
public void byScore() {
StubMappingScope scope = indexManager.createScope();
SearchQuery<DocumentReference> query;
SearchPredicate predicate = scope.predicate()
.match().field( "string_analyzed_forScore" ).matching( "hooray" ).toPredicate();
query = scope.query()
.predicate( predicate )
.sort( f -> f.score() )
.toQuery();
assertThat( query )
.hasDocRefHitsExactOrder( INDEX_NAME, FIRST_ID, SECOND_ID, THIRD_ID );
query = scope.query()
.predicate( predicate )
.sort( f -> f.score().desc() )
.toQuery();
assertThat( query )
.hasDocRefHitsExactOrder( INDEX_NAME, FIRST_ID, SECOND_ID, THIRD_ID );
query = scope.query()
.predicate( predicate )
.sort( f -> f.score().asc() )
.toQuery();
assertThat( query )
.hasDocRefHitsExactOrder( INDEX_NAME, THIRD_ID, SECOND_ID, FIRST_ID );
}
@Test
public void separateSort() {
StubMappingScope scope = indexManager.createScope();
SearchQuery<DocumentReference> query;
SearchSort sortAsc = scope.sort()
.field( "string" ).asc().missing().last()
.toSort();
query = scope.query()
.predicate( f -> f.matchAll() )
.sort( sortAsc )
.toQuery();
assertThat( query )
.hasDocRefHitsExactOrder( INDEX_NAME, FIRST_ID, SECOND_ID, THIRD_ID, EMPTY_ID );
SearchSort sortDesc = scope.sort()
.field( "string" ).desc().missing().last()
.toSort();
query = scope.query()
.predicate( f -> f.matchAll() )
.sort( sortDesc )
.toQuery();
assertThat( query )
.hasDocRefHitsExactOrder( INDEX_NAME, THIRD_ID, SECOND_ID, FIRST_ID, EMPTY_ID );
}
@Test
public void reuseSortInstance_onScopeTargetingSameIndexes() {
StubMappingScope scope = indexManager.createScope();
SearchSort sort = scope
.sort().field( "string" ).asc().missing().last().toSort();
SearchQuery<DocumentReference> query = scope.query()
.predicate( f -> f.matchAll() )
.sort( sort )
.toQuery();
assertThat( query ).hasDocRefHitsExactOrder( INDEX_NAME, FIRST_ID, SECOND_ID, THIRD_ID, EMPTY_ID );
// reuse the same sort instance on the same scope
query = scope.query()
.predicate( f -> f.matchAll() )
.sort( sort )
.toQuery();
assertThat( query ).hasDocRefHitsExactOrder( INDEX_NAME, FIRST_ID, SECOND_ID, THIRD_ID, EMPTY_ID );
// reuse the same sort instance on a different scope,
// targeting the same index
query = indexManager.createScope().query()
.predicate( f -> f.matchAll() )
.sort( sort )
.toQuery();
assertThat( query ).hasDocRefHitsExactOrder( INDEX_NAME, FIRST_ID, SECOND_ID, THIRD_ID, EMPTY_ID );
sort = indexManager.createScope( anotherIndexManager )
.sort().field( "string" ).asc().missing().last().toSort();
// reuse the same sort instance on a different scope,
// targeting same indexes
query = anotherIndexManager.createScope( indexManager ).query()
.predicate( f -> f.matchAll() )
.sort( sort )
.toQuery();
assertThat( query ).hasDocRefHitsExactOrder( INDEX_NAME, FIRST_ID, SECOND_ID, THIRD_ID, EMPTY_ID );
}
@Test
public void reuseSortInstance_onScopeTargetingDifferentIndexes() {
StubMappingScope scope = indexManager.createScope();
SearchSort sort = scope
.sort().field( "string" ).asc().missing().last().toSort();
// reuse the same sort instance on a different scope,
// targeting a different index
SubTest.expectException( () ->
anotherIndexManager.createScope().query()
.predicate( f -> f.matchAll() )
.sort( sort )
.toQuery() )
.assertThrown()
.isInstanceOf( SearchException.class )
.hasMessageContaining( "scope targeting different indexes" )
.hasMessageContaining( INDEX_NAME )
.hasMessageContaining( ANOTHER_INDEX_NAME );
// reuse the same sort instance on a different scope,
// targeting different indexes
SubTest.expectException( () ->
indexManager.createScope( anotherIndexManager ).query()
.predicate( f -> f.matchAll() )
.sort( sort )
.toQuery() )
.assertThrown()
.isInstanceOf( SearchException.class )
.hasMessageContaining( "scope targeting different indexes" )
.hasMessageContaining( INDEX_NAME )
.hasMessageContaining( ANOTHER_INDEX_NAME );
}
@Test
public void byDistance_asc() {
SearchQuery<DocumentReference> query = simpleQuery( b -> b.distance( "geoPoint", GeoPoint.of( 45.757864, 4.834496 ) ) );
assertThat( query )
.hasDocRefHitsExactOrder( INDEX_NAME, FIRST_ID, THIRD_ID, SECOND_ID, EMPTY_ID );
query = simpleQuery( b -> b.distance( "geoPoint", 45.757864, 4.834496 ) );
assertThat( query )
.hasDocRefHitsExactOrder( INDEX_NAME, FIRST_ID, THIRD_ID, SECOND_ID, EMPTY_ID );
query = simpleQuery( b -> b.distance( "geoPoint", 45.757864, 4.834496 ).asc() );
assertThat( query )
.hasDocRefHitsExactOrder( INDEX_NAME, FIRST_ID, THIRD_ID, SECOND_ID, EMPTY_ID );
}
@Test
public void byDistance_desc() {
SearchQuery<DocumentReference> query = simpleQuery(
b -> b.distance( "geoPoint", GeoPoint.of( 45.757864, 4.834496 ) ).desc()
);
assertThat( query )
.hasDocRefHitsExactOrder( INDEX_NAME, EMPTY_ID, SECOND_ID, THIRD_ID, FIRST_ID );
query = simpleQuery( b -> b.distance( "geoPoint", 45.757864, 4.834496 ).desc() );
assertThat( query )
.hasDocRefHitsExactOrder( INDEX_NAME, EMPTY_ID, SECOND_ID, THIRD_ID, FIRST_ID );
}
@Test
public void distanceSort_invalidType() {
thrown.expect( SearchException.class );
thrown.expectMessage( "Distance related operations are not supported" );
thrown.expectMessage( "string" );
simpleQuery(
b -> b.distance( "string", GeoPoint.of( 45.757864, 4.834496 ) ).desc()
);
}
@Test
public void byDistance_asc_flattened() {
SearchQuery<DocumentReference> query = simpleQuery( b -> b.distance( "flattenedObject.geoPoint", GeoPoint.of( 45.757864, 4.834496 ) ) );
assertThat( query )
.hasDocRefHitsExactOrder( INDEX_NAME, FIRST_ID, THIRD_ID, SECOND_ID, EMPTY_ID );
query = simpleQuery( b -> b.distance( "flattenedObject.geoPoint", 45.757864, 4.834496 ) );
assertThat( query )
.hasDocRefHitsExactOrder( INDEX_NAME, FIRST_ID, THIRD_ID, SECOND_ID, EMPTY_ID );
query = simpleQuery( b -> b.distance( "flattenedObject.geoPoint", 45.757864, 4.834496 ).asc() );
assertThat( query )
.hasDocRefHitsExactOrder( INDEX_NAME, FIRST_ID, THIRD_ID, SECOND_ID, EMPTY_ID );
}
@Test
public void byDistance_desc_flattened() {
SearchQuery<DocumentReference> query = simpleQuery(
b -> b.distance( "flattenedObject.geoPoint", GeoPoint.of( 45.757864, 4.834496 ) ).desc()
);
assertThat( query )
.hasDocRefHitsExactOrder( INDEX_NAME, EMPTY_ID, SECOND_ID, THIRD_ID, FIRST_ID );
query = simpleQuery( b -> b.distance( "flattenedObject.geoPoint", 45.757864, 4.834496 ).desc() );
assertThat( query )
.hasDocRefHitsExactOrder( INDEX_NAME, EMPTY_ID, SECOND_ID, THIRD_ID, FIRST_ID );
}
@Test
public void byDistance_asc_nested() {
SearchQuery<DocumentReference> query = simpleQuery( b -> b.distance( "nestedObject.geoPoint", GeoPoint.of( 45.757864, 4.834496 ) ) );
assertThat( query )
.hasDocRefHitsExactOrder( INDEX_NAME, FIRST_ID, THIRD_ID, SECOND_ID, EMPTY_ID );
query = simpleQuery( b -> b.distance( "nestedObject.geoPoint", 45.757864, 4.834496 ) );
assertThat( query )
.hasDocRefHitsExactOrder( INDEX_NAME, FIRST_ID, THIRD_ID, SECOND_ID, EMPTY_ID );
query = simpleQuery( b -> b.distance( "nestedObject.geoPoint", 45.757864, 4.834496 ).asc() );
assertThat( query )
.hasDocRefHitsExactOrder( INDEX_NAME, FIRST_ID, THIRD_ID, SECOND_ID, EMPTY_ID );
}
@Test
public void byDistance_desc_nested() {
SearchQuery<DocumentReference> query = simpleQuery(
b -> b.distance( "nestedObject.geoPoint", GeoPoint.of( 45.757864, 4.834496 ) ).desc()
);
assertThat( query )
.hasDocRefHitsExactOrder( INDEX_NAME, EMPTY_ID, SECOND_ID, THIRD_ID, FIRST_ID );
query = simpleQuery( b -> b.distance( "nestedObject.geoPoint", 45.757864, 4.834496 ).desc() );
assertThat( query )
.hasDocRefHitsExactOrder( INDEX_NAME, EMPTY_ID, SECOND_ID, THIRD_ID, FIRST_ID );
}
@Test
public void byDistance_asc_nested_x2() {
SearchQuery<DocumentReference> query = simpleQuery( b -> b.distance( "nestedObject.nestedObject.geoPoint", GeoPoint.of( 45.757864, 4.834496 ) ) );
assertThat( query )
.hasDocRefHitsExactOrder( INDEX_NAME, FIRST_ID, THIRD_ID, SECOND_ID, EMPTY_ID );
query = simpleQuery( b -> b.distance( "nestedObject.nestedObject.geoPoint", 45.757864, 4.834496 ) );
assertThat( query )
.hasDocRefHitsExactOrder( INDEX_NAME, FIRST_ID, THIRD_ID, SECOND_ID, EMPTY_ID );
query = simpleQuery( b -> b.distance( "nestedObject.nestedObject.geoPoint", 45.757864, 4.834496 ).asc() );
assertThat( query )
.hasDocRefHitsExactOrder( INDEX_NAME, FIRST_ID, THIRD_ID, SECOND_ID, EMPTY_ID );
}
@Test
public void byDistance_desc_nested_x2() {
SearchQuery<DocumentReference> query = simpleQuery(
b -> b.distance( "nestedObject.nestedObject.geoPoint", GeoPoint.of( 45.757864, 4.834496 ) ).desc()
);
assertThat( query )
.hasDocRefHitsExactOrder( INDEX_NAME, EMPTY_ID, SECOND_ID, THIRD_ID, FIRST_ID );
query = simpleQuery( b -> b.distance( "nestedObject.nestedObject.geoPoint", 45.757864, 4.834496 ).desc() );
assertThat( query )
.hasDocRefHitsExactOrder( INDEX_NAME, EMPTY_ID, SECOND_ID, THIRD_ID, FIRST_ID );
}
@Test
public void extension() {
SearchQuery<DocumentReference> query;
// Mandatory extension, supported
query = simpleQuery( c -> c
.extension( new SupportedExtension() ).field( "string" ).missing().last()
);
assertThat( query )
.hasDocRefHitsAnyOrder( INDEX_NAME, FIRST_ID, SECOND_ID, THIRD_ID, EMPTY_ID );
query = simpleQuery( b -> b
.extension( new SupportedExtension() ).field( "string" ).desc().missing().last()
);
assertThat( query )
.hasDocRefHitsAnyOrder( INDEX_NAME, THIRD_ID, SECOND_ID, FIRST_ID, EMPTY_ID );
// Mandatory extension, unsupported
SubTest.expectException(
() -> indexManager.createScope().sort().extension( new UnSupportedExtension() )
)
.assertThrown()
.isInstanceOf( SearchException.class );
// Conditional extensions with orElse - two, both supported
query = simpleQuery( b -> b
.extension()
.ifSupported(
new SupportedExtension(),
c -> c.field( "string" ).missing().last()
)
.ifSupported(
new SupportedExtension(),
ignored -> Assertions.fail( "This should not be called" )
)
.orElseFail()
);
assertThat( query )
.hasDocRefHitsAnyOrder( INDEX_NAME, FIRST_ID, SECOND_ID, THIRD_ID, EMPTY_ID );
query = simpleQuery( b -> b
.extension()
.ifSupported(
new SupportedExtension(),
c -> c.field( "string" ).desc().missing().last()
)
.ifSupported(
new SupportedExtension(),
ignored -> Assertions.fail( "This should not be called" )
)
.orElseFail()
);
assertThat( query )
.hasDocRefHitsAnyOrder( INDEX_NAME, THIRD_ID, SECOND_ID, FIRST_ID, EMPTY_ID );
// Conditional extensions with orElse - two, second supported
query = simpleQuery( b -> b
.extension()
.ifSupported(
new UnSupportedExtension(),
ignored -> Assertions.fail( "This should not be called" )
)
.ifSupported(
new SupportedExtension(),
c -> c.field( "string" ).missing().last()
)
.orElse( ignored -> Assertions.fail( "This should not be called" ) )
);
assertThat( query )
.hasDocRefHitsAnyOrder( INDEX_NAME, FIRST_ID, SECOND_ID, THIRD_ID, EMPTY_ID );
query = simpleQuery( b -> b
.extension()
.ifSupported(
new UnSupportedExtension(),
ignored -> Assertions.fail( "This should not be called" )
)
.ifSupported(
new SupportedExtension(),
c -> c.field( "string" ).desc().missing().last()
)
.orElse( ignored -> Assertions.fail( "This should not be called" ) )
);
assertThat( query )
.hasDocRefHitsAnyOrder( INDEX_NAME, THIRD_ID, SECOND_ID, FIRST_ID, EMPTY_ID );
// Conditional extensions with orElse - two, both unsupported
query = simpleQuery( b -> b
.extension()
.ifSupported(
new UnSupportedExtension(),
ignored -> Assertions.fail( "This should not be called" )
)
.ifSupported(
new UnSupportedExtension(),
ignored -> Assertions.fail( "This should not be called" )
)
.orElse(
c -> c.field( "string" ).missing().last()
)
);
assertThat( query )
.hasDocRefHitsAnyOrder( INDEX_NAME, FIRST_ID, SECOND_ID, THIRD_ID, EMPTY_ID );
query = simpleQuery( b -> b
.extension()
.ifSupported(
new UnSupportedExtension(),
ignored -> Assertions.fail( "This should not be called" )
)
.ifSupported(
new UnSupportedExtension(),
ignored -> Assertions.fail( "This should not be called" )
)
.orElse(
c -> c.field( "string" ).desc().missing().last()
)
);
}
private void initData() {
IndexIndexingPlan<? extends DocumentElement> plan = indexManager.createIndexingPlan();
// Important: do not index the documents in the expected order after sorts
plan.add( referenceProvider( SECOND_ID ), document -> {
GeoPoint geoPoint = GeoPoint.of( 45.7705687, 4.835233 );
document.addValue( indexMapping.string, "george" );
document.addValue( indexMapping.geoPoint, geoPoint );
document.addValue( indexMapping.string_analyzed_forScore, "Hooray Hooray" );
document.addValue( indexMapping.unsortable, "george" );
// Note: this object must be single-valued for these tests
DocumentElement flattenedObject = document.addObject( indexMapping.flattenedObject.self );
flattenedObject.addValue( indexMapping.flattenedObject.string, "george" );
flattenedObject.addValue( indexMapping.flattenedObject.integer, 2 );
flattenedObject.addValue( indexMapping.flattenedObject.geoPoint, geoPoint );
// Note: this object must be single-valued for these tests
DocumentElement nestedObject = document.addObject( indexMapping.nestedObject.self );
nestedObject.addValue( indexMapping.nestedObject.string, "george" );
nestedObject.addValue( indexMapping.nestedObject.integer, 2 );
nestedObject.addValue( indexMapping.nestedObject.geoPoint, geoPoint );
// Note: this object must be single-valued for these tests
DocumentElement nestedX2Object = nestedObject.addObject( indexMapping.nestedX2Object.self );
nestedX2Object.addValue( indexMapping.nestedX2Object.string, "george" );
nestedX2Object.addValue( indexMapping.nestedX2Object.integer, 2 );
nestedX2Object.addValue( indexMapping.nestedX2Object.geoPoint, geoPoint );
} );
plan.add( referenceProvider( FIRST_ID ), document -> {
GeoPoint geoPoint = GeoPoint.of( 45.7541719, 4.8386221 );
document.addValue( indexMapping.string, "aaron" );
document.addValue( indexMapping.geoPoint, geoPoint );
document.addValue( indexMapping.string_analyzed_forScore, "Hooray Hooray Hooray" );
document.addValue( indexMapping.unsortable, "aaron" );
// Note: this object must be single-valued for these tests
DocumentElement flattenedObject = document.addObject( indexMapping.flattenedObject.self );
flattenedObject.addValue( indexMapping.flattenedObject.string, "aaron" );
flattenedObject.addValue( indexMapping.flattenedObject.integer, 1 );
flattenedObject.addValue( indexMapping.flattenedObject.geoPoint, geoPoint );
// Note: this object must be single-valued for these tests
DocumentElement nestedObject = document.addObject( indexMapping.nestedObject.self );
nestedObject.addValue( indexMapping.nestedObject.string, "aaron" );
nestedObject.addValue( indexMapping.nestedObject.integer, 1 );
nestedObject.addValue( indexMapping.nestedObject.geoPoint, geoPoint );
// Note: this object must be single-valued for these tests
DocumentElement nestedX2Object = nestedObject.addObject( indexMapping.nestedX2Object.self );
nestedX2Object.addValue( indexMapping.nestedX2Object.string, "aaron" );
nestedX2Object.addValue( indexMapping.nestedX2Object.integer, 1 );
nestedX2Object.addValue( indexMapping.nestedX2Object.geoPoint, geoPoint );
} );
plan.add( referenceProvider( THIRD_ID ), document -> {
GeoPoint geoPoint = GeoPoint.of( 45.7530374, 4.8510299 );
document.addValue( indexMapping.string, "zach" );
document.addValue( indexMapping.geoPoint, geoPoint );
document.addValue( indexMapping.string_analyzed_forScore, "Hooray" );
document.addValue( indexMapping.unsortable, "zach" );
// Note: this object must be single-valued for these tests
DocumentElement flattenedObject = document.addObject( indexMapping.flattenedObject.self );
flattenedObject.addValue( indexMapping.flattenedObject.string, "zach" );
flattenedObject.addValue( indexMapping.flattenedObject.integer, 3 );
flattenedObject.addValue( indexMapping.flattenedObject.geoPoint, geoPoint );
// Note: this object must be single-valued for these tests
DocumentElement nestedObject = document.addObject( indexMapping.nestedObject.self );
nestedObject.addValue( indexMapping.nestedObject.string, "zach" );
nestedObject.addValue( indexMapping.nestedObject.integer, 3 );
nestedObject.addValue( indexMapping.nestedObject.geoPoint, geoPoint );
// Note: this object must be single-valued for these tests
DocumentElement nestedX2Object = nestedObject.addObject( indexMapping.nestedX2Object.self );
nestedX2Object.addValue( indexMapping.nestedX2Object.string, "zach" );
nestedX2Object.addValue( indexMapping.nestedX2Object.integer, 3 );
nestedX2Object.addValue( indexMapping.nestedX2Object.geoPoint, geoPoint );
} );
plan.add( referenceProvider( EMPTY_ID ), document -> { } );
plan.execute().join();
// Check that all documents are searchable
StubMappingScope scope = indexManager.createScope();
SearchQuery<DocumentReference> query = scope.query()
.predicate( f -> f.matchAll() )
.toQuery();
assertThat( query ).hasDocRefHitsAnyOrder( INDEX_NAME, FIRST_ID, SECOND_ID, THIRD_ID, EMPTY_ID );
}
private static class IndexMapping {
final IndexFieldReference<String> string;
final IndexFieldReference<GeoPoint> geoPoint;
final IndexFieldReference<String> string_analyzed_forScore;
final IndexFieldReference<String> unsortable;
final ObjectMapping flattenedObject;
final ObjectMapping nestedObject;
final ObjectMapping nestedX2Object;
IndexMapping(IndexSchemaElement root) {
string = root.field( "string", f -> f.asString().sortable( Sortable.YES ) )
.toReference();
geoPoint = root.field( "geoPoint", f -> f.asGeoPoint().sortable( Sortable.YES ) )
.toReference();
string_analyzed_forScore = root.field(
"string_analyzed_forScore" ,
f -> f.asString()
.analyzer( DefaultAnalysisDefinitions.ANALYZER_STANDARD_ENGLISH.name )
)
.toReference();
unsortable = root.field( "unsortable", f -> f.asString().sortable( Sortable.NO ) )
.toReference();
IndexSchemaObjectField flattenedObjectField = root.objectField( "flattenedObject", ObjectFieldStorage.FLATTENED );
flattenedObject = new ObjectMapping( flattenedObjectField );
IndexSchemaObjectField nestedObjectField = root.objectField( "nestedObject", ObjectFieldStorage.NESTED );
nestedObject = new ObjectMapping( nestedObjectField );
IndexSchemaObjectField nestedX2ObjectField = nestedObjectField.objectField( "nestedObject", ObjectFieldStorage.NESTED );
nestedX2Object = new ObjectMapping( nestedX2ObjectField );
}
}
private static class ObjectMapping {
final IndexObjectFieldReference self;
final IndexFieldReference<Integer> integer;
final IndexFieldReference<String> string;
final IndexFieldReference<GeoPoint> geoPoint;
ObjectMapping(IndexSchemaObjectField objectField) {
self = objectField.toReference();
string = objectField.field( "string", f -> f.asString().sortable( Sortable.YES ) )
.toReference();
integer = objectField.field( "integer", f -> f.asInteger().sortable( Sortable.YES ) )
.toReference();
geoPoint = objectField.field( "geoPoint", f -> f.asGeoPoint().sortable( Sortable.YES ) )
.toReference();
}
}
private static class SupportedExtension implements SearchSortFactoryExtension<MyExtendedFactory> {
@Override
public Optional<MyExtendedFactory> extendOptional(SearchSortFactory original,
SearchSortDslContext<?, ?> dslContext) {
Assertions.assertThat( original ).isNotNull();
Assertions.assertThat( dslContext ).isNotNull();
Assertions.assertThat( dslContext.getBuilderFactory() ).isNotNull();
return Optional.of( new MyExtendedFactory( original ) );
}
}
private static class UnSupportedExtension implements SearchSortFactoryExtension<MyExtendedFactory> {
@Override
public Optional<MyExtendedFactory> extendOptional(SearchSortFactory original,
SearchSortDslContext<?, ?> dslContext) {
Assertions.assertThat( original ).isNotNull();
Assertions.assertThat( dslContext ).isNotNull();
Assertions.assertThat( dslContext.getBuilderFactory() ).isNotNull();
return Optional.empty();
}
}
private static class MyExtendedFactory extends DelegatingSearchSortFactory {
MyExtendedFactory(SearchSortFactory delegate) {
super( delegate );
}
}
}