This repository has been archived by the owner on Oct 30, 2020. It is now read-only.
/
TestSensei.java
1269 lines (1081 loc) · 62.9 KB
/
TestSensei.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
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package com.senseidb.test;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import junit.framework.TestCase;
import org.apache.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.browseengine.bobo.api.BrowseFacet;
import com.browseengine.bobo.api.BrowseSelection;
import com.browseengine.bobo.api.FacetAccessible;
import com.browseengine.bobo.api.FacetSpec;
import com.browseengine.bobo.api.FacetSpec.FacetSortSpec;
import com.browseengine.bobo.facets.DefaultFacetHandlerInitializerParam;
import com.senseidb.search.node.SenseiBroker;
import com.senseidb.search.req.SenseiHit;
import com.senseidb.search.req.SenseiRequest;
import com.senseidb.search.req.SenseiResult;
import com.senseidb.svc.api.SenseiService;
public class TestSensei extends TestCase {
private static final Logger logger = Logger.getLogger(TestSensei.class);
private static SenseiBroker broker;
private static SenseiService httpRestSenseiService;
static {
SenseiStarter.start("test-conf/node1","test-conf/node2");
broker = SenseiStarter.broker;
httpRestSenseiService = SenseiStarter.httpRestSenseiService;
}
public void testTotalCount() throws Exception
{
logger.info("executing test case testTotalCount");
SenseiRequest req = new SenseiRequest();
SenseiResult res = broker.browse(req);
assertEquals("wrong total number of hits" + req + res, 15000, res.getNumHits());
logger.info("request:" + req + "\nresult:" + res);
}
public void testTotalCountWithFacetSpec() throws Exception
{
logger.info("executing test case testTotalCountWithFacetSpec");
SenseiRequest req = new SenseiRequest();
FacetSpec facetSpecall = new FacetSpec();
facetSpecall.setMaxCount(1000000);
facetSpecall.setExpandSelection(true);
facetSpecall.setMinHitCount(0);
facetSpecall.setOrderBy(FacetSortSpec.OrderHitsDesc);
FacetSpec facetSpec = new FacetSpec();
facetSpec.setMaxCount(5);
setspec(req, facetSpec);
req.setCount(5);
setspec(req, facetSpecall);
SenseiResult res = broker.browse(req);
logger.info("request:" + req + "\nresult:" + res);
verifyFacetCount(res, "year", "[1993 TO 1994]", 3090);
}
public void testSelection() throws Exception
{
logger.info("executing test case testSelection");
FacetSpec facetSpecall = new FacetSpec();
facetSpecall.setMaxCount(1000000);
facetSpecall.setExpandSelection(true);
facetSpecall.setMinHitCount(0);
facetSpecall.setOrderBy(FacetSortSpec.OrderHitsDesc);
FacetSpec facetSpec = new FacetSpec();
facetSpec.setMaxCount(5);
SenseiRequest req = new SenseiRequest();
req.setCount(3);
facetSpecall.setMaxCount(3);
setspec(req, facetSpecall);
BrowseSelection sel = new BrowseSelection("year");
String selVal = "[2001 TO 2002]";
sel.addValue(selVal);
req.addSelection(sel);
SenseiResult res = broker.browse(req);
logger.info("request:" + req + "\nresult:" + res);
assertEquals(2907, res.getNumHits());
String selName = "year";
verifyFacetCount(res, selName, selVal, 2907);
verifyFacetCount(res, "year", "[1993 TO 1994]", 3090);
}
public void testSelectionDynamicTimeRange() throws Exception
{
logger.info("executing test case testSelection");
SenseiRequest req = new SenseiRequest();
DefaultFacetHandlerInitializerParam initParam = new DefaultFacetHandlerInitializerParam();
initParam.putLongParam("time", new long[]{15000L});
req.setFacetHandlerInitializerParam("timeRange", initParam);
//req.setFacetHandlerInitializerParam("timeRange_internal", new DefaultFacetHandlerInitializerParam());
req.setCount(3);
//setspec(req, facetSpecall);
BrowseSelection sel = new BrowseSelection("timeRange");
String selVal = "000000013";
sel.addValue(selVal);
req.addSelection(sel);
SenseiResult res = broker.browse(req);
logger.info("request:" + req + "\nresult:" + res);
assertEquals(12990, res.getNumHits());
}
public void testSelectionNot() throws Exception
{
logger.info("executing test case testSelectionNot");
FacetSpec facetSpecall = new FacetSpec();
facetSpecall.setMaxCount(1000000);
facetSpecall.setExpandSelection(true);
facetSpecall.setMinHitCount(0);
facetSpecall.setOrderBy(FacetSortSpec.OrderHitsDesc);
FacetSpec facetSpec = new FacetSpec();
facetSpec.setMaxCount(5);
SenseiRequest req = new SenseiRequest();
req.setCount(3);
facetSpecall.setMaxCount(3);
setspec(req, facetSpecall);
BrowseSelection sel = new BrowseSelection("year");
sel.addNotValue("[2001 TO 2002]");
req.addSelection(sel);
SenseiResult res = broker.browse(req);
logger.info("request:" + req + "\nresult:" + res);
assertEquals(12093, res.getNumHits());
verifyFacetCount(res, "year", "[1993 TO 1994]", 3090);
}
public void testGroupBy() throws Exception
{
logger.info("executing test case testGroupBy");
SenseiRequest req = new SenseiRequest();
req.setCount(1);
req.setGroupBy(new String[]{"groupid"});
SenseiResult res = broker.browse(req);
logger.info("request:" + req + "\nresult:" + res);
SenseiHit hit = res.getSenseiHits()[0];
assertTrue(hit.getGroupHitsCount() > 0);
}
public void testGroupByWithGroupedHits() throws Exception
{
logger.info("executing test case testGroupBy");
SenseiRequest req = new SenseiRequest();
req.setCount(1);
req.setGroupBy(new String[]{"groupid"});
req.setMaxPerGroup(8);
SenseiResult res = broker.browse(req);
logger.info("request:" + req + "\nresult:" + res);
SenseiHit hit = res.getSenseiHits()[0];
assertTrue(hit.getGroupHitsCount() > 0);
assertTrue(hit.getSenseiGroupHits().length > 0);
// use httpRestSenseiService
res = httpRestSenseiService.doQuery(req);
logger.info("request:" + req + "\nresult:" + res);
hit = res.getSenseiHits()[0];
assertTrue(hit.getGroupHitsCount() > 0);
assertTrue(hit.getSenseiGroupHits().length > 0);
}
public void testGroupByVirtual() throws Exception
{
logger.info("executing test case testGroupByVirtual");
SenseiRequest req = new SenseiRequest();
req.setCount(1);
req.setGroupBy(new String[]{"virtual_groupid"});
SenseiResult res = broker.browse(req);
logger.info("request:" + req + "\nresult:" + res);
SenseiHit hit = res.getSenseiHits()[0];
assertTrue(hit.getGroupHitsCount() > 0);
}
public void testGroupByVirtualWithGroupedHits() throws Exception
{
logger.info("executing test case testGroupByVirtualWithGroupedHits");
SenseiRequest req = new SenseiRequest();
req.setCount(1);
req.setGroupBy(new String[]{"virtual_groupid"});
req.setMaxPerGroup(8);
SenseiResult res = broker.browse(req);
logger.info("request:" + req + "\nresult:" + res);
SenseiHit hit = res.getSenseiHits()[0];
assertTrue(hit.getGroupHitsCount() > 0);
assertTrue(hit.getSenseiGroupHits().length > 0);
}
public void testGroupByFixedLengthLongArray() throws Exception
{
logger.info("executing test case testGroupByFixedLengthLongArray");
SenseiRequest req = new SenseiRequest();
req.setCount(1);
req.setGroupBy(new String[]{"virtual_groupid_fixedlengthlongarray"});
SenseiResult res = broker.browse(req);
logger.info("request:" + req + "\nresult:" + res);
SenseiHit hit = res.getSenseiHits()[0];
assertTrue(hit.getGroupHitsCount() > 0);
}
public void testGroupByFixedLengthLongArrayWithGroupedHits() throws Exception
{
logger.info("executing test case testGroupByFixedLengthLongArrayWithGroupedHits");
SenseiRequest req = new SenseiRequest();
req.setCount(1);
req.setGroupBy(new String[]{"virtual_groupid_fixedlengthlongarray"});
req.setMaxPerGroup(8);
SenseiResult res = broker.browse(req);
logger.info("request:" + req + "\nresult:" + res);
SenseiHit hit = res.getSenseiHits()[0];
assertTrue(hit.getGroupHitsCount() > 0);
assertTrue(hit.getSenseiGroupHits().length > 0);
}
public void testBQL1() throws Exception
{
logger.info("Executing test case testBQL1");
String req = "{\"bql\":\"select * from cars\"}";
JSONObject res = search(new JSONObject(req));
assertEquals("numhits is wrong", 15000, res.getInt("numhits"));
}
public void testBQL2() throws Exception
{
logger.info("Executing test case testBQL2");
String req = "{\"bql\":\"select * from cars where color = 'red'\"}";
JSONObject res = search(new JSONObject(req));
assertEquals("numhits is wrong", 2160, res.getInt("numhits"));
}
public void testBqlExtraFilter() throws Exception
{
logger.info("Executing test case testBqlExtraFilter");
String req = "{\"bql\":\"select * from cars where color = 'red'\", \"bql_extra_filter\":\"year < 2000\"}";
JSONObject res = search(new JSONObject(req));
assertEquals("numhits is wrong", 1534, res.getInt("numhits"));
}
public void testBqlRelevance1() throws Exception
{
logger.info("Executing test case testBqlRelevance1");
String req = "{\"bql\":\"SELECT * FROM cars USING RELEVANCE MODEL my_model (thisYear:2001, goodYear:[1996]) DEFINED AS (int thisYear, IntOpenHashSet goodYear) BEGIN if (goodYear.contains(year)) return (float)Math.exp(10d); if (year==thisYear) return 87f; return _INNER_SCORE; END\"}";
JSONObject res = search(new JSONObject(req));
JSONArray hits = res.getJSONArray("hits");
JSONObject firstHit = hits.getJSONObject(0);
JSONObject secondHit = hits.getJSONObject(1);
String firstYear = firstHit.getJSONArray("year").getString(0);
String secondYear = secondHit.getJSONArray("year").getString(0);
assertEquals("year 1996 should be on the top", true, firstYear.contains("1996"));
assertEquals("year 1996 should be on the top", true, secondYear.contains("1996"));
}
public void testSelectionTerm() throws Exception
{
logger.info("executing test case Selection term");
String req = "{\"selections\":[{\"term\":{\"color\":{\"value\":\"red\"}}}]}";
JSONObject res = search(new JSONObject(req));
assertEquals("numhits is wrong", 2160, res.getInt("numhits"));
}
public void testSelectionTerms() throws Exception
{
logger.info("executing test case Selection terms");
String req = "{\"selections\":[{\"terms\":{\"tags\":{\"values\":[\"mp3\",\"moon-roof\"],\"excludes\":[\"leather\"],\"operator\":\"or\"}}}]}";
JSONObject res = search(new JSONObject(req));
assertEquals("numhits is wrong", 4483, res.getInt("numhits"));
}
public void testSelectionDynamicTimeRangeJson() throws Exception
{
logger.info("executing test case Selection terms");
String req = "{\"selections\":[{\"term\":{\"timeRange\":{\"value\":\"000000013\"}}}]" +
", \"facetInit\":{ \"timeRange\":{\"time\" :{ \"type\" : \"long\",\"values\" : [15000] }}}" +
"}";
System.out.println(req);
JSONObject res = search(new JSONObject(req));
assertEquals("numhits is wrong", 12990, res.getInt("numhits"));
}
public void testSelectionDynamicTimeRangeJson2() throws Exception
{
// Test scalar values in facet init parameters
logger.info("executing test case Selection terms");
String req = "{\"selections\":[{\"term\":{\"timeRange\":{\"value\":\"000000013\"}}}]" +
", \"facetInit\":{ \"timeRange\":{\"time\" :{ \"type\" : \"long\",\"values\" : 15000 }}}" +
"}";
System.out.println(req);
JSONObject res = search(new JSONObject(req));
assertEquals("numhits is wrong", 12990, res.getInt("numhits"));
}
public void testSelectionRange() throws Exception
{
//2000 1548;
//2001 1443;
//2002 1464;
// [2000 TO 2002] ==> 4455
// (2000 TO 2002) ==> 1443
// (2000 TO 2002] ==> 2907
// [2000 TO 2002) ==> 2991
{
logger.info("executing test case Selection range [2000 TO 2002]");
String req = "{\"selections\":[{\"range\":{\"year\":{\"to\":\"2002\",\"include_lower\":true,\"include_upper\":true,\"from\":\"2000\"}}}]}";
JSONObject res = search(new JSONObject(req));
assertEquals("numhits is wrong", 4455, res.getInt("numhits"));
}
{
logger.info("executing test case Selection range (2000 TO 2002)");
String req = "{\"selections\":[{\"range\":{\"year\":{\"to\":\"2002\",\"include_lower\":false,\"include_upper\":false,\"from\":\"2000\"}}}]}";
JSONObject res = search(new JSONObject(req));
assertEquals("numhits is wrong", 1443, res.getInt("numhits"));
}
{
logger.info("executing test case Selection range (2000 TO 2002]");
String req = "{\"selections\":[{\"range\":{\"year\":{\"to\":\"2002\",\"include_lower\":false,\"include_upper\":true,\"from\":\"2000\"}}}]}";
JSONObject res = search(new JSONObject(req));
assertEquals("numhits is wrong", 2907, res.getInt("numhits"));
}
{
logger.info("executing test case Selection range [2000 TO 2002)");
String req = "{\"selections\":[{\"range\":{\"year\":{\"to\":\"2002\",\"include_lower\":true,\"include_upper\":false,\"from\":\"2000\"}}}]}";
JSONObject res = search(new JSONObject(req));
assertEquals("numhits is wrong", 2991, res.getInt("numhits"));
}
}
public void testMatchAllWithBoostQuery() throws Exception
{
logger.info("executing test case MatchAllQuery");
String req = "{\"query\": {\"match_all\": {\"boost\": \"1.2\"}}}";
JSONObject res = search(new JSONObject(req));
assertEquals("numhits is wrong", 15000, res.getInt("numhits"));
}
public void testQueryStringQuery() throws Exception
{
logger.info("executing test case testQueryStringQuery");
String req = "{\"query\": {\"query_string\": {\"query\": \"red AND cool\"}}}";
JSONObject res = search(new JSONObject(req));
assertEquals("numhits is wrong", 1070, res.getInt("numhits"));
}
public void testMatchAllQuery() throws Exception
{
logger.info("executing test case testMatchAllQuery");
String req = "{\"query\": {\"match_all\": {}}}";
JSONObject res = search(new JSONObject(req));
assertEquals("numhits is wrong", 15000, res.getInt("numhits"));
}
public void testUIDQuery() throws Exception
{
logger.info("executing test case testUIDQuery");
String req = "{\"query\": {\"ids\": {\"values\": [\"1\", \"4\", \"3\", \"2\", \"6\"], \"excludes\": [\"2\"]}}}";
JSONObject res = search(new JSONObject(req));
assertEquals("numhits is wrong", 4, res.getInt("numhits"));
Set<Integer> expectedIds = new HashSet(Arrays.asList(new Integer[]{1, 3, 4, 6}));
for (int i = 0; i < res.getInt("numhits"); ++i)
{
int uid = res.getJSONArray("hits").getJSONObject(i).getInt("_uid");
assertTrue("_UID " + uid + " is not expected.", expectedIds.contains(uid));
}
}
public void testTextQuery() throws Exception
{
logger.info("executing test case testTextQuery");
String req = "{\"query\": {\"text\": {\"contents\": { \"value\": \"red cool\", \"operator\": \"and\"}}}}";
JSONObject res = search(new JSONObject(req));
assertEquals("numhits is wrong", 1070, res.getInt("numhits"));
}
public void testTermQuery() throws Exception
{
logger.info("executing test case testTermQuery");
String req = "{\"query\":{\"term\":{\"color\":\"red\"}}}";
JSONObject res = search(new JSONObject(req));
assertEquals("numhits is wrong", 2160, res.getInt("numhits"));
}
public void testTermsQuery() throws Exception
{
logger.info("executing test case testTermQuery");
String req = "{\"query\":{\"terms\":{\"tags\":{\"values\":[\"leather\",\"moon-roof\"],\"excludes\":[\"hybrid\"],\"minimum_match\":0,\"operator\":\"or\"}}}}";
JSONObject res = search(new JSONObject(req));
assertEquals("numhits is wrong", 5777, res.getInt("numhits"));
}
public void testBooleanQuery() throws Exception
{
logger.info("executing test case testBooleanQuery");
String req = "{\"query\":{\"bool\":{\"must_not\":{\"term\":{\"category\":\"compact\"}},\"must\":{\"term\":{\"color\":\"red\"}}}}}";
JSONObject res = search(new JSONObject(req));
assertEquals("numhits is wrong", 1652, res.getInt("numhits"));
}
public void testDistMaxQuery() throws Exception
{
//color red ==> 2160
//color blue ==> 1104
logger.info("executing test case testDistMaxQuery");
String req = "{\"query\":{\"dis_max\":{\"tie_breaker\":0.7,\"queries\":[{\"term\":{\"color\":\"red\"}},{\"term\":{\"color\":\"blue\"}}],\"boost\":1.2}}}";
JSONObject res = search(new JSONObject(req));
assertEquals("numhits is wrong", 3264, res.getInt("numhits"));
}
public void testPathQuery() throws Exception
{
logger.info("executing test case testPathQuery");
String req = "{\"query\":{\"path\":{\"makemodel\":\"asian/acura/3.2tl\"}}}";
JSONObject res = search(new JSONObject(req));
assertEquals("numhits is wrong", 126, res.getInt("numhits"));
}
public void testPrefixQuery() throws Exception
{
//color blue ==> 1104
logger.info("executing test case testPrefixQuery");
String req = "{\"query\":{\"prefix\":{\"color\":{\"value\":\"blu\",\"boost\":2}}}}";
JSONObject res = search(new JSONObject(req));
assertEquals("numhits is wrong", 1104, res.getInt("numhits"));
}
public void testWildcardQuery() throws Exception
{
//color blue ==> 1104
logger.info("executing test case testWildcardQuery");
String req = "{\"query\":{\"wildcard\":{\"color\":{\"value\":\"bl*e\",\"boost\":2}}}}";
JSONObject res = search(new JSONObject(req));
assertEquals("numhits is wrong", 1104, res.getInt("numhits"));
}
public void testRangeQuery() throws Exception
{
logger.info("executing test case testRangeQuery");
String req = "{\"query\":{\"range\":{\"year\":{\"to\":2000,\"boost\":2,\"from\":1999,\"_noOptimize\":false}}}}";
JSONObject res = search(new JSONObject(req));
assertEquals("numhits is wrong", 3015, res.getInt("numhits"));
}
public void testRangeQuery2() throws Exception
{
logger.info("executing test case testRangeQuery2");
String req = "{\"query\":{\"range\":{\"year\":{\"to\":\"2000\",\"boost\":2,\"from\":\"1999\",\"_noOptimize\":true,\"_type\":\"int\"}}}}";
JSONObject res = search(new JSONObject(req));
assertEquals("numhits is wrong", 3015, res.getInt("numhits"));
}
public void testFilteredQuery() throws Exception
{
logger.info("executing test case testFilteredQuery");
String req ="{\"query\":{\"filtered\":{\"query\":{\"term\":{\"color\":\"red\"}},\"filter\":{\"range\":{\"year\":{\"to\":2000,\"from\":1999}}}}}}";
JSONObject res = search(new JSONObject(req));
assertEquals("numhits is wrong", 447, res.getInt("numhits"));
}
public void testSpanTermQuery() throws Exception
{
logger.info("executing test case testSpanTermQuery");
String req = "{\"query\":{\"span_term\":{\"color\":\"red\"}}}";
JSONObject res = search(new JSONObject(req));
assertEquals("numhits is wrong", 2160, res.getInt("numhits"));
}
public void testSpanOrQuery() throws Exception
{
logger.info("executing test case testSpanOrQuery");
String req = "{\"query\":{\"span_or\":{\"clauses\":[{\"span_term\":{\"color\":\"red\"}},{\"span_term\":{\"color\":\"blue\"}}]}}}";
JSONObject res = search(new JSONObject(req));
assertEquals("numhits is wrong", 3264, res.getInt("numhits"));
}
public void testSpanNotQuery() throws Exception
{
logger.info("executing test case testSpanNotQuery");
String req = "{\"query\":{\"span_not\":{\"exclude\":{\"span_term\":{\"contents\":\"red\"}},\"include\":{\"span_term\":{\"contents\":\"compact\"}}}}}";
JSONObject res = search(new JSONObject(req));
assertEquals("numhits is wrong", 4596, res.getInt("numhits"));
}
public void testSpanNearQuery1() throws Exception
{
logger.info("executing test case testSpanNearQuery1");
String req = "{\"query\":{\"span_near\":{\"in_order\":false,\"collect_payloads\":false,\"slop\":12,\"clauses\":[{\"span_term\":{\"contents\":\"red\"}},{\"span_term\":{\"contents\":\"compact\"}},{\"span_term\":{\"contents\":\"hybrid\"}}]}}}";
JSONObject res = search(new JSONObject(req));
assertEquals("numhits is wrong", 274, res.getInt("numhits"));
}
public void testSpanNearQuery2() throws Exception
{
logger.info("executing test case testSpanNearQuery2");
String req = "{\"query\":{\"span_near\":{\"in_order\":true,\"collect_payloads\":false,\"slop\":0,\"clauses\":[{\"span_term\":{\"contents\":\"red\"}},{\"span_term\":{\"contents\":\"compact\"}},{\"span_term\":{\"contents\":\"favorite\"}}]}}}";
JSONObject res = search(new JSONObject(req));
assertEquals("numhits is wrong", 63, res.getInt("numhits"));
}
public void testSpanFirstQuery() throws Exception
{
logger.info("executing test case testSpanFirstQuery");
String req = "{\"query\":{\"span_first\":{\"match\":{\"span_term\":{\"color\":\"red\"}},\"end\":2}}}";
JSONObject res = search(new JSONObject(req));
assertEquals("numhits is wrong", 2160, res.getInt("numhits"));
}
public void testNullMultiFilter() throws Exception
{
logger.info("executing test case testNullFilter");
String req = "{\"filter\":{\"isNull\":\"groupid_multi\"}}";
JSONObject res = search(new JSONObject(req));
assertEquals("numhits is wrong", 14997, res.getInt("numhits"));
}
public void testNullFilterOnSimpleColumn() throws Exception
{
logger.info("executing test case testNullFilter");
String req = "{\"filter\":{\"isNull\":\"price\"}}";
JSONObject res = search(new JSONObject(req));
assertEquals("numhits is wrong", 1, res.getInt("numhits"));
}
public void testUIDFilter() throws Exception
{
logger.info("executing test case testUIDFilter");
String req = "{\"filter\": {\"ids\": {\"values\": [\"1\", \"2\", \"3\"], \"excludes\": [\"2\"]}}}";
JSONObject res = search(new JSONObject(req));
assertEquals("numhits is wrong", 2, res.getInt("numhits"));
Set<Integer> expectedIds = new HashSet(Arrays.asList(new Integer[]{1, 3}));
for (int i = 0; i < res.getInt("numhits"); ++i)
{
int uid = res.getJSONArray("hits").getJSONObject(i).getInt("_uid");
assertTrue("_UID " + uid + " is not expected.", expectedIds.contains(uid));
}
}
public void testAndFilter() throws Exception
{
logger.info("executing test case testAndFilter");
String req = "{\"filter\":{\"and\":[{\"term\":{\"tags\":\"mp3\",\"_noOptimize\":false}},{\"term\":{\"color\":\"red\",\"_noOptimize\":false}}]}}";
JSONObject res = search(new JSONObject(req));
assertEquals("numhits is wrong", 439, res.getInt("numhits"));
}
public void testOrFilter() throws Exception
{
logger.info("executing test case testOrFilter");
String req = "{\"filter\":{\"or\":[{\"term\":{\"color\":\"blue\",\"_noOptimize\":true}},{\"term\":{\"color\":\"red\",\"_noOptimize\":true}}]}}";
JSONObject res = search(new JSONObject(req));
assertEquals("numhits is wrong", 3264, res.getInt("numhits"));
}
public void testOrFilter2() throws Exception
{
logger.info("executing test case testOrFilter2");
String req = "{\"filter\":{\"or\":[{\"term\":{\"color\":\"blue\",\"_noOptimize\":false}},{\"term\":{\"color\":\"red\",\"_noOptimize\":false}}]}}";
JSONObject res = search(new JSONObject(req));
assertEquals("numhits is wrong", 3264, res.getInt("numhits"));
}
public void testOrFilter3() throws Exception
{
logger.info("executing test case testOrFilter3");
String req = "{\"filter\":{\"or\":[{\"term\":{\"color\":\"blue\",\"_noOptimize\":true}},{\"term\":{\"color\":\"red\",\"_noOptimize\":false}}]}}";
JSONObject res = search(new JSONObject(req));
assertEquals("numhits is wrong", 3264, res.getInt("numhits"));
}
public void testBooleanFilter() throws Exception
{
logger.info("executing test case testBooleanFilter");
String req = "{\"filter\":{\"bool\":{\"must_not\":{\"term\":{\"category\":\"compact\"}},\"should\":[{\"term\":{\"color\":\"red\"}},{\"term\":{\"color\":\"green\"}}],\"must\":{\"term\":{\"color\":\"red\"}}}}}";
JSONObject res = search(new JSONObject(req));
assertEquals("numhits is wrong", 1652, res.getInt("numhits"));
}
public void testQueryFilter() throws Exception
{
logger.info("executing test case testQueryFilter");
String req = "{\"filter\": {\"query\":{\"range\":{\"year\":{\"to\":2000,\"boost\":2,\"from\":1999,\"_noOptimize\":false}}}}}";
JSONObject res = search(new JSONObject(req));
assertEquals("numhits is wrong", 3015, res.getInt("numhits"));
}
/* Need to fix the bug in bobo and kamikazi, for details see the following two test cases:*/
public void testAndFilter1() throws Exception
{
logger.info("executing test case testAndFilter1");
String req = "{\"filter\":{\"and\":[{\"term\":{\"color\":\"blue\",\"_noOptimize\":false}},{\"query\":{\"term\":{\"category\":\"compact\"}}}]}}";
JSONObject res = search(new JSONObject(req));
assertEquals("numhits is wrong", 504, res.getInt("numhits"));
}
public void testQueryFilter1() throws Exception
{
logger.info("executing test case testQueryFilter1");
String req = "{\"filter\": {\"query\":{\"term\":{\"category\":\"compact\"}}}}";
JSONObject res = search(new JSONObject(req));
assertEquals("numhits is wrong", 4169, res.getInt("numhits"));
}
/* another weird bug may exist somewhere in bobo or kamikazi.*/
/* In the following two test cases, when modifying the first one by changing "tags" to "tag", it is supposed that
* Only the first test case is not correct, but the second one also throw one NPE, which is weird.
* */
public void testAndFilter2() throws Exception
{
logger.info("executing test case testAndFilter2");
String req = "{\"filter\":{\"and\":[{\"term\":{\"tags\":\"mp3\",\"_noOptimize\":false}},{\"query\":{\"term\":{\"color\":\"red\"}}}]}}";
JSONObject res = search(new JSONObject(req));
assertEquals("numhits is wrong", 439, res.getInt("numhits"));
}
public void testOrFilter4() throws Exception
{
//color:blue ==> 1104
//color:red ==> 2160
logger.info("executing test case testOrFilter4");
String req = "{\"filter\":{\"or\":[{\"term\":{\"color\":\"blue\",\"_noOptimize\":false}},{\"query\":{\"term\":{\"color\":\"red\"}}}]}}";
JSONObject res = search(new JSONObject(req));
assertEquals("numhits is wrong", 3264, res.getInt("numhits"));
}
public void testTermFilter() throws Exception
{
logger.info("executing test case testTermFilter");
String req = "{\"filter\":{\"term\":{\"color\":\"red\"}}}";
JSONObject res = search(new JSONObject(req));
assertEquals("numhits is wrong", 2160, res.getInt("numhits"));
}
public void testTermsFilter() throws Exception
{
logger.info("executing test case testTermsFilter");
String req = "{\"filter\":{\"terms\":{\"tags\":{\"values\":[\"leather\",\"moon-roof\"],\"excludes\":[\"hybrid\"],\"minimum_match\":0,\"operator\":\"or\"}}}}";
JSONObject res = search(new JSONObject(req));
assertEquals("numhits is wrong", 5777, res.getInt("numhits"));
}
public void testRangeFilter() throws Exception
{
logger.info("executing test case testRangeFilter");
String req = "{\"filter\":{\"range\":{\"year\":{\"to\":2000,\"boost\":2,\"from\":1999,\"_noOptimize\":false}}}}";
JSONObject res = search(new JSONObject(req));
assertEquals("numhits is wrong", 3015, res.getInt("numhits"));
}
public void testRangeFilter2() throws Exception
{
logger.info("executing test case testRangeFilter2");
String req = "{\"filter\":{\"range\":{\"year\":{\"to\":\"2000\",\"boost\":2,\"from\":\"1999\",\"_noOptimize\":true,\"_type\":\"int\"}}}}";
JSONObject res = search(new JSONObject(req));
assertEquals("numhits is wrong", 3015, res.getInt("numhits"));
}
public void testRangeFilter3() throws Exception
{
logger.info("executing test case testRangeFilter3");
String req = "{\"fetchStored\":true,\"selections\":[{\"term\":{\"color\":{\"value\":\"red\"}}}],\"from\":0,\"filter\":{\"query\":{\"query_string\":{\"query\":\"cool AND moon-roof AND hybrid\"}}},\"size\":10}";
JSONObject res = search(new JSONObject(req));
//TODO Sensei returns undeterministic results for this query. Will create a Jira ticket
assertTrue("numhits is wrong", res.getInt("numhits") > 10);
}
public void testFallbackGroupBy() throws Exception
{
logger.info("executing test case testFallbackGroupBy");
String req = "{\"from\": 0,\"size\": 10,\"groupBy\": {\"columns\": [\"virtual_groupid_fixedlengthlongarray\", \"color\"],\"top\": 2}, \"sort\": [{\"color\": \"asc\"}]}";
JSONObject res = search(new JSONObject(req));
JSONArray hits = res.getJSONArray("hits");
JSONObject firstHit = hits.getJSONObject(0);
assertTrue("groupfield is wrong", "color".equals(firstHit.getString("groupfield")) || "virtual_groupid_fixedlengthlongarray".equals(firstHit.getString("groupfield")));
assertTrue("no group hits", firstHit.getJSONArray("grouphits") != null);
}
public void testGetStoreRequest() throws Exception
{
logger.info("executing test case testGetStoreRequest");
String req = "[1,2,3,5]";
JSONObject res = searchGet(new JSONArray(req));
//TODO Sensei returns undeterministic results for this query. Will create a Jira issue
assertTrue("numhits is wrong", res.length() == 4);
assertNotNull("", res.get(String.valueOf(1)));
}
public void testRelevanceMatchAll() throws Exception
{
logger.info("executing test case testRelevanceMatchAll");
String req = "{\"sort\":[\"_score\"],\"query\":{\"query_string\":{\"query\":\"\",\"relevance\":{\"model\":{\"function_params\":[\"_INNER_SCORE\",\"thisYear\",\"year\",\"goodYear\"],\"facets\":{\"int\":[\"year\",\"mileage\"],\"long\":[\"groupid\"]},\"function\":\" if(goodYear.contains(year)) return (float)Math.exp(10d); if(year==thisYear) return 87f ; return _INNER_SCORE ;\",\"variables\":{\"set_int\":[\"goodYear\"],\"int\":[\"thisYear\"]}},\"values\":{\"thisYear\":2001,\"goodYear\":[1996,1997]}}}},\"fetchStored\":false,\"from\":0,\"explain\":false,\"size\":6}";
JSONObject res = search(new JSONObject(req));
assertEquals("numhits is wrong", 15000, res.getInt("numhits"));
}
public void testRelevanceHashSet() throws Exception
{
logger.info("executing test case testRelevanceHashSet");
String req = "{\"sort\":[\"_score\"],\"query\":{\"query_string\":{\"query\":\"\",\"relevance\":{\"model\":{\"function_params\":[\"_INNER_SCORE\",\"thisYear\",\"year\",\"goodYear\"],\"facets\":{\"int\":[\"year\",\"mileage\"],\"long\":[\"groupid\"]},\"function\":\" if(goodYear.contains(year)) return (float)Math.exp(10d); if(year==thisYear) return 87f ; return _INNER_SCORE ;\",\"variables\":{\"set_int\":[\"goodYear\"],\"int\":[\"thisYear\"]}},\"values\":{\"thisYear\":2001,\"goodYear\":[1996]}}}},\"fetchStored\":false,\"from\":0,\"explain\":false,\"size\":6}";
JSONObject res = search(new JSONObject(req));
JSONArray hits = res.getJSONArray("hits");
JSONObject firstHit = hits.getJSONObject(0);
JSONObject secondHit = hits.getJSONObject(1);
String firstYear = firstHit.getJSONArray("year").getString(0);
String secondYear = secondHit.getJSONArray("year").getString(0);
assertEquals("year 1996 should be on the top", true, firstYear.contains("1996"));
assertEquals("year 1996 should be on the top", true, secondYear.contains("1996"));
}
public void testRelevanceMath() throws Exception
{
logger.info("executing test case testRelevanceMath");
String req = "{\"sort\":[\"_score\"],\"query\":{\"query_string\":{\"query\":\"\",\"relevance\":{\"model\":{\"function_params\":[\"_INNER_SCORE\",\"thisYear\",\"year\",\"goodYear\"],\"facets\":{\"int\":[\"year\",\"mileage\"],\"long\":[\"groupid\"]},\"function\":\" if(goodYear.contains(year)) return (float)Math.exp(10d); if(year==thisYear) return 87f ; return _INNER_SCORE ;\",\"variables\":{\"set_int\":[\"goodYear\"],\"int\":[\"thisYear\"]}},\"values\":{\"thisYear\":2001,\"goodYear\":[1996]}}}},\"fetchStored\":false,\"from\":0,\"explain\":false,\"size\":6}";
JSONObject res = search(new JSONObject(req));
JSONArray hits = res.getJSONArray("hits");
JSONObject firstHit = hits.getJSONObject(0);
JSONObject secondHit = hits.getJSONObject(1);
double firstScore = firstHit.getDouble("_score");
double secondScore = secondHit.getDouble("_score");
double delta1 = firstScore - Math.exp(10d);
double delta2 = secondScore - Math.exp(10d);
assertEquals("score for first is not correct. delta is: " + delta1, true, Math.abs(delta1) < 0.001 );
assertEquals("score for second is not correct. delta is: " + delta2, true, Math.abs(delta2) < 0.001);
}
public void testRelevanceInnerScore() throws Exception
{
logger.info("executing test case testRelevanceInnerScore");
String req = "{\"sort\":[\"_score\"],\"query\":{\"query_string\":{\"query\":\"\",\"relevance\":{\"model\":{\"function_params\":[\"_INNER_SCORE\",\"thisYear\",\"year\",\"goodYear\"],\"facets\":{\"int\":[\"year\",\"mileage\"],\"long\":[\"groupid\"]},\"function\":\" if(goodYear.contains(year)) return _INNER_SCORE ; return _INNER_SCORE ;\",\"variables\":{\"set_int\":[\"goodYear\"],\"int\":[\"thisYear\"]}},\"values\":{\"thisYear\":2001,\"goodYear\":[1996]}}}},\"fetchStored\":false,\"from\":0,\"explain\":false,\"size\":6}";
JSONObject res = search(new JSONObject(req));
JSONArray hits = res.getJSONArray("hits");
JSONObject firstHit = hits.getJSONObject(0);
JSONObject secondHit = hits.getJSONObject(1);
double firstScore = firstHit.getDouble("_score");
double secondScore = secondHit.getDouble("_score");
assertEquals("inner score for first is not correct." , true, firstScore == 1 );
assertEquals("inner score for second is not correct." , true, secondScore == 1);
}
public void testRelevanceHashMapInt2Float() throws Exception
{
logger.info("executing test case testRelevanceHashMapInt2Float");
String req = "{\"sort\":[\"_score\"],\"query\":{\"query_string\":{\"query\":\"\",\"relevance\":{\"model\":{\"function_params\":[\"_INNER_SCORE\",\"thisYear\",\"year\",\"goodYear\",\"mileageWeight\",\"mileage\"],\"facets\":{\"int\":[\"year\",\"mileage\"],\"long\":[\"groupid\"]},\"function\":\" if(mileageWeight.containsKey(mileage)) return 10000+mileageWeight.get(mileage); if(goodYear.contains(year)) return (float)Math.exp(2d); if(year==thisYear) return 87f ; return _INNER_SCORE;\",\"variables\":{\"map_int_float\":[\"mileageWeight\"],\"set_int\":[\"goodYear\"],\"int\":[\"thisYear\"]}},\"values\":{\"thisYear\":2001,\"mileageWeight\":{\"11400\":777.9, \"11000\":10.2},\"goodYear\":[1996,1997]}}}},\"fetchStored\":false,\"from\":0,\"explain\":false,\"size\":6}";
JSONObject res = search(new JSONObject(req));
assertEquals("numhits is wrong", 15000, res.getInt("numhits"));
//the first one should has socre 10777.900390625, and mileage: 11400;
JSONArray hits = res.getJSONArray("hits");
JSONObject firstHit = hits.getJSONObject(0);
JSONObject secondHit = hits.getJSONObject(1);
double firstScore = firstHit.getDouble("_score");
double secondScore = secondHit.getDouble("_score");
String firstMileage = firstHit.getJSONArray("mileage").getString(0);
String secondMileage = secondHit.getJSONArray("mileage").getString(0);
assertEquals("inner score for first is not correct." , true, Math.abs(firstScore - 10777.900390625) < 1 );
assertEquals("inner score for second is not correct." , true, Math.abs(secondScore - 10777.900390625) < 1 );
assertEquals("mileage for first is not correct." , true, Integer.parseInt(firstMileage)==11400 );
assertEquals("mileage for second is not correct." , true, Integer.parseInt(secondMileage)==11400 );
}
public void testRelevanceHashMapInt2String() throws Exception
{
logger.info("executing test case testRelevanceHashMapInt2String");
String req = "{\"sort\":[\"_score\"],\"query\":{\"query_string\":{\"query\":\"\",\"relevance\":{\"model\":{\"function_params\":[\"_INNER_SCORE\",\"thisYear\",\"year\",\"goodYear\",\"mileageWeight\",\"mileage\",\"color\",\"yearcolor\",\"colorweight\",\"category\",\"categorycolor\"],\"facets\":{\"int\":[\"year\",\"mileage\"],\"string\":[\"color\",\"category\"],\"long\":[\"groupid\"]},\"function\":\"if(yearcolor.containsKey(year) && yearcolor.get(year).equals(color)) return 100000f; if(goodYear.contains(year)) return (float)Math.exp(2d); if(year==thisYear) return 87f ; return _INNER_SCORE;\",\"variables\":{\"map_int_float\":[\"mileageWeight\"],\"map_int_string\":[\"yearcolor\"],\"set_int\":[\"goodYear\"],\"int\":[\"thisYear\"],\"map_string_float\":[\"colorweight\"],\"map_string_string\":[\"categorycolor\"]}},\"values\":{\"thisYear\":2001,\"yearcolor\":{\"1998\":\"red\"},\"mileageWeight\":{\"11400\":777.9, \"11000\":10.2},\"colorweight\":{\"red\":335.5},\"goodYear\":[1996,1997],\"categorycolor\":{\"compact\":\"red\"}}}}},\"fetchStored\":false,\"from\":0,\"explain\":false,\"size\":6}";
JSONObject res = search(new JSONObject(req));
assertEquals("numhits is wrong", 15000, res.getInt("numhits"));
//the first one should has socre 10777.900390625, and mileage: 11400;
JSONArray hits = res.getJSONArray("hits");
JSONObject firstHit = hits.getJSONObject(0);
JSONObject secondHit = hits.getJSONObject(1);
double firstScore = firstHit.getDouble("_score");
double secondScore = secondHit.getDouble("_score");
String firstYear = firstHit.getJSONArray("year").getString(0);
String secondYear = secondHit.getJSONArray("year").getString(0);
String firstColor = firstHit.getJSONArray("color").getString(0);
String secondColor = secondHit.getJSONArray("color").getString(0);
assertEquals("inner score for first is not correct." , true, Math.abs(firstScore - 100000) < 1 );
assertEquals("inner score for second is not correct." , true, Math.abs(secondScore - 100000) < 1 );
assertEquals("year for first is not correct." , true, Integer.parseInt(firstYear)==1998 );
assertEquals("year for second is not correct." , true, Integer.parseInt(secondYear)==1998 );
assertEquals("color for first is not correct." , true, firstColor.equals("red") );
assertEquals("color for second is not correct." , true, secondColor.equals("red") );
}
public void testRelevanceHashMapString2Float() throws Exception
{
logger.info("executing test case testRelevanceHashMapString2Float");
String req = "{\"sort\":[\"_score\"],\"query\":{\"query_string\":{\"query\":\"\",\"relevance\":{\"model\":{\"function_params\":[\"_INNER_SCORE\",\"thisYear\",\"year\",\"goodYear\",\"mileageWeight\",\"mileage\",\"color\",\"yearcolor\",\"colorweight\",\"category\",\"categorycolor\"],\"facets\":{\"int\":[\"year\",\"mileage\"],\"string\":[\"color\",\"category\"],\"long\":[\"groupid\"]},\"function\":\" if(colorweight.containsKey(color) ) return 200f + colorweight.getFloat(color); if(year==thisYear) return 87f ; return _INNER_SCORE;\",\"variables\":{\"map_int_float\":[\"mileageWeight\"],\"map_int_string\":[\"yearcolor\"],\"set_int\":[\"goodYear\"],\"int\":[\"thisYear\"],\"map_string_float\":[\"colorweight\"],\"map_string_string\":[\"categorycolor\"]}},\"values\":{\"thisYear\":2001,\"yearcolor\":{\"1998\":\"red\"},\"mileageWeight\":{\"11400\":777.9, \"11000\":10.2},\"colorweight\":{\"red\":335.5},\"goodYear\":[1996,1997],\"categorycolor\":{\"compact\":\"red\"}}}}},\"fetchStored\":false,\"from\":0,\"explain\":false,\"size\":6}";
JSONObject res = search(new JSONObject(req));
assertEquals("numhits is wrong", 15000, res.getInt("numhits"));
//the first one should has socre 10777.900390625, and mileage: 11400;
JSONArray hits = res.getJSONArray("hits");
JSONObject firstHit = hits.getJSONObject(0);
JSONObject secondHit = hits.getJSONObject(1);
double firstScore = firstHit.getDouble("_score");
double secondScore = secondHit.getDouble("_score");
String firstColor = firstHit.getJSONArray("color").getString(0);
String secondColor = secondHit.getJSONArray("color").getString(0);
assertEquals("inner score for first is not correct." , true, Math.abs(firstScore - 535.5) < 1 );
assertEquals("inner score for second is not correct." , true, Math.abs(secondScore - 535.5) < 1 );
assertEquals("color for first is not correct." , true, firstColor.equals("red") );
assertEquals("color for second is not correct." , true, secondColor.equals("red") );
}
public void testRelevanceHashMapString2String() throws Exception
{
logger.info("executing test case testRelevanceHashMapString2String");
String req = "{\"sort\":[\"_score\"],\"query\":{\"query_string\":{\"query\":\"\",\"relevance\":{\"model\":{\"function_params\":[\"_INNER_SCORE\",\"thisYear\",\"year\",\"goodYear\",\"mileageWeight\",\"mileage\",\"color\",\"yearcolor\",\"colorweight\",\"category\",\"categorycolor\"],\"facets\":{\"int\":[\"year\",\"mileage\"],\"string\":[\"color\",\"category\"],\"long\":[\"groupid\"]},\"function\":\" if(categorycolor.containsKey(category) && categorycolor.get(category).equals(color)) return 10000f; if(year==thisYear) return 87f ; return _INNER_SCORE;\",\"variables\":{\"map_int_float\":[\"mileageWeight\"],\"map_int_string\":[\"yearcolor\"],\"set_int\":[\"goodYear\"],\"int\":[\"thisYear\"],\"map_string_float\":[\"colorweight\"],\"map_string_string\":[\"categorycolor\"]}},\"values\":{\"thisYear\":2001,\"yearcolor\":{\"1998\":\"red\"},\"mileageWeight\":{\"11400\":777.9, \"11000\":10.2},\"colorweight\":{\"red\":335.5},\"goodYear\":[1996,1997],\"categorycolor\":{\"compact\":\"red\"}}}}},\"fetchStored\":false,\"from\":0,\"explain\":false,\"size\":6}";
JSONObject res = search(new JSONObject(req));
assertEquals("numhits is wrong", 15000, res.getInt("numhits"));
//the first one should has socre 10777.900390625, and mileage: 11400;
JSONArray hits = res.getJSONArray("hits");
JSONObject firstHit = hits.getJSONObject(0);
JSONObject secondHit = hits.getJSONObject(1);
double firstScore = firstHit.getDouble("_score");
double secondScore = secondHit.getDouble("_score");
String firstCategory = firstHit.getJSONArray("category").getString(0);
String secondCategory = secondHit.getJSONArray("category").getString(0);
String firstColor = firstHit.getJSONArray("color").getString(0);
String secondColor = secondHit.getJSONArray("color").getString(0);
assertEquals("inner score for first is not correct." , true, Math.abs(firstScore - 10000) < 1 );
assertEquals("inner score for second is not correct." , true, Math.abs(secondScore - 10000) < 1 );
assertEquals("category for first is not correct." , true, firstCategory.equals("compact") );
assertEquals("category for second is not correct." , true, secondCategory.equals("compact") );
assertEquals("color for first is not correct." , true, firstColor.equals("red") );
assertEquals("color for second is not correct." , true, secondColor.equals("red") );
}
public void testRelevanceHashMapInt2FloatArrayWay() throws Exception
{
logger.info("executing test case testRelevanceHashMapInt2FloatArrayWay");
String req = "{\"sort\":[\"_score\"],\"query\":{\"query_string\":{\"query\":\"\",\"relevance\":{\"model\":{\"function_params\":[\"_INNER_SCORE\",\"thisYear\",\"year\",\"goodYear\",\"mileageWeight\",\"mileage\"],\"facets\":{\"int\":[\"year\",\"mileage\"],\"long\":[\"groupid\"]},\"function\":\" if(mileageWeight.containsKey(mileage)) return 10000+mileageWeight.get(mileage); if(goodYear.contains(year)) return (float)Math.exp(2d); if(year==thisYear) return 87f ; return _INNER_SCORE;\",\"variables\":{\"map_int_float\":[\"mileageWeight\"],\"set_int\":[\"goodYear\"],\"int\":[\"thisYear\"]}},\"values\":{\"thisYear\":2001,\"mileageWeight\":{\"value\":[777.9,10.2],\"key\":[11400,11000]},\"goodYear\":[1996,1997]}}}},\"fetchStored\":false,\"from\":0,\"explain\":false,\"size\":6}";
JSONObject res = search(new JSONObject(req));
assertEquals("numhits is wrong", 15000, res.getInt("numhits"));
//the first one should has socre 10777.900390625, and mileage: 11400;
JSONArray hits = res.getJSONArray("hits");
JSONObject firstHit = hits.getJSONObject(0);
JSONObject secondHit = hits.getJSONObject(1);
double firstScore = firstHit.getDouble("_score");
double secondScore = secondHit.getDouble("_score");
String firstMileage = firstHit.getJSONArray("mileage").getString(0);
String secondMileage = secondHit.getJSONArray("mileage").getString(0);
assertEquals("inner score for first is not correct." , true, Math.abs(firstScore - 10777.900390625) < 1 );
assertEquals("inner score for second is not correct." , true, Math.abs(secondScore - 10777.900390625) < 1 );
assertEquals("mileage for first is not correct." , true, Integer.parseInt(firstMileage)==11400 );
assertEquals("mileage for second is not correct." , true, Integer.parseInt(secondMileage)==11400 );
}
public void testRelevanceHashMapInt2StringArrayWay() throws Exception
{
logger.info("executing test case testRelevanceHashMapInt2StringArrayWay");
String req = "{\"sort\":[\"_score\"],\"query\":{\"query_string\":{\"query\":\"\",\"relevance\":{\"model\":{\"function_params\":[\"_INNER_SCORE\",\"thisYear\",\"year\",\"goodYear\",\"mileageWeight\",\"mileage\",\"color\",\"yearcolor\",\"colorweight\",\"category\",\"categorycolor\"],\"facets\":{\"int\":[\"year\",\"mileage\"],\"string\":[\"color\",\"category\"],\"long\":[\"groupid\"]},\"function\":\"if(yearcolor.containsKey(year) && yearcolor.get(year).equals(color)) return 100000f; if(goodYear.contains(year)) return (float)Math.exp(2d); if(year==thisYear) return 87f ; return _INNER_SCORE;\",\"variables\":{\"map_int_float\":[\"mileageWeight\"],\"map_int_string\":[\"yearcolor\"],\"set_int\":[\"goodYear\"],\"int\":[\"thisYear\"],\"map_string_float\":[\"colorweight\"],\"map_string_string\":[\"categorycolor\"]}},\"values\":{\"thisYear\":2001,\"yearcolor\":{\"value\":[\"red\"],\"key\":[1998]},\"mileageWeight\":{\"value\":[777.9,10.2],\"key\":[11400,11000]},\"colorweight\":{\"value\":[335.5],\"key\":[\"red\"]},\"goodYear\":[1996,1997],\"categorycolor\":{\"value\":[\"red\"],\"key\":[\"compact\"]}}}}},\"fetchStored\":false,\"from\":0,\"explain\":false,\"size\":6}";
JSONObject res = search(new JSONObject(req));
assertEquals("numhits is wrong", 15000, res.getInt("numhits"));
//the first one should has socre 10777.900390625, and mileage: 11400;
JSONArray hits = res.getJSONArray("hits");
JSONObject firstHit = hits.getJSONObject(0);
JSONObject secondHit = hits.getJSONObject(1);
double firstScore = firstHit.getDouble("_score");
double secondScore = secondHit.getDouble("_score");
String firstYear = firstHit.getJSONArray("year").getString(0);
String secondYear = secondHit.getJSONArray("year").getString(0);
String firstColor = firstHit.getJSONArray("color").getString(0);
String secondColor = secondHit.getJSONArray("color").getString(0);
assertEquals("inner score for first is not correct." , true, Math.abs(firstScore - 100000) < 1 );
assertEquals("inner score for second is not correct." , true, Math.abs(secondScore - 100000) < 1 );
assertEquals("year for first is not correct." , true, Integer.parseInt(firstYear)==1998 );
assertEquals("year for second is not correct." , true, Integer.parseInt(secondYear)==1998 );
assertEquals("color for first is not correct." , true, firstColor.equals("red") );
assertEquals("color for second is not correct." , true, secondColor.equals("red") );
}
public void testRelevanceHashMapString2FloatArrayWay() throws Exception
{
logger.info("executing test case testRelevanceHashMapString2FloatArrayWay");
String req = "{\"sort\":[\"_score\"],\"query\":{\"query_string\":{\"query\":\"\",\"relevance\":{\"model\":{\"function_params\":[\"_INNER_SCORE\",\"thisYear\",\"year\",\"goodYear\",\"mileageWeight\",\"mileage\",\"color\",\"yearcolor\",\"colorweight\",\"category\",\"categorycolor\"],\"facets\":{\"int\":[\"year\",\"mileage\"],\"string\":[\"color\",\"category\"],\"long\":[\"groupid\"]},\"function\":\" if(colorweight.containsKey(color) ) return 200f + colorweight.getFloat(color); if(year==thisYear) return 87f ; return _INNER_SCORE;\",\"variables\":{\"map_int_float\":[\"mileageWeight\"],\"map_int_string\":[\"yearcolor\"],\"set_int\":[\"goodYear\"],\"int\":[\"thisYear\"],\"map_string_float\":[\"colorweight\"],\"map_string_string\":[\"categorycolor\"]}},\"values\":{\"thisYear\":2001,\"yearcolor\":{\"value\":[\"red\"],\"key\":[1998]},\"mileageWeight\":{\"value\":[777.9,10.2],\"key\":[11400,11000]},\"colorweight\":{\"value\":[335.5],\"key\":[\"red\"]},\"goodYear\":[1996,1997],\"categorycolor\":{\"value\":[\"red\"],\"key\":[\"compact\"]}}}}},\"fetchStored\":false,\"from\":0,\"explain\":false,\"size\":6}";
JSONObject res = search(new JSONObject(req));
assertEquals("numhits is wrong", 15000, res.getInt("numhits"));
//the first one should has socre 10777.900390625, and mileage: 11400;
JSONArray hits = res.getJSONArray("hits");
JSONObject firstHit = hits.getJSONObject(0);
JSONObject secondHit = hits.getJSONObject(1);
double firstScore = firstHit.getDouble("_score");
double secondScore = secondHit.getDouble("_score");
String firstColor = firstHit.getJSONArray("color").getString(0);
String secondColor = secondHit.getJSONArray("color").getString(0);
assertEquals("inner score for first is not correct." , true, Math.abs(firstScore - 535.5) < 1 );
assertEquals("inner score for second is not correct." , true, Math.abs(secondScore - 535.5) < 1 );
assertEquals("color for first is not correct." , true, firstColor.equals("red") );
assertEquals("color for second is not correct." , true, secondColor.equals("red") );
}
public void testRelevanceHashMapString2StringArrayWay() throws Exception
{
logger.info("executing test case testRelevanceHashMapString2StringArrayWay");
String req = "{\"sort\":[\"_score\"],\"query\":{\"query_string\":{\"query\":\"\",\"relevance\":{\"model\":{\"function_params\":[\"_INNER_SCORE\",\"thisYear\",\"year\",\"goodYear\",\"mileageWeight\",\"mileage\",\"color\",\"yearcolor\",\"colorweight\",\"category\",\"categorycolor\"],\"facets\":{\"int\":[\"year\",\"mileage\"],\"string\":[\"color\",\"category\"],\"long\":[\"groupid\"]},\"function\":\" if(categorycolor.containsKey(category) && categorycolor.get(category).equals(color)) return 10000f; if(year==thisYear) return 87f ; return _INNER_SCORE;\",\"variables\":{\"map_int_float\":[\"mileageWeight\"],\"map_int_string\":[\"yearcolor\"],\"set_int\":[\"goodYear\"],\"int\":[\"thisYear\"],\"map_string_float\":[\"colorweight\"],\"map_string_string\":[\"categorycolor\"]}},\"values\":{\"thisYear\":2001,\"yearcolor\":{\"value\":[\"red\"],\"key\":[1998]},\"mileageWeight\":{\"value\":[777.9,10.2],\"key\":[11400,11000]},\"colorweight\":{\"value\":[335.5],\"key\":[\"red\"]},\"goodYear\":[1996,1997],\"categorycolor\":{\"value\":[\"red\"],\"key\":[\"compact\"]}}}}},\"fetchStored\":false,\"from\":0,\"explain\":false,\"size\":6}";
JSONObject res = search(new JSONObject(req));
assertEquals("numhits is wrong", 15000, res.getInt("numhits"));
//the first one should has socre 10777.900390625, and mileage: 11400;
JSONArray hits = res.getJSONArray("hits");
JSONObject firstHit = hits.getJSONObject(0);
JSONObject secondHit = hits.getJSONObject(1);
double firstScore = firstHit.getDouble("_score");
double secondScore = secondHit.getDouble("_score");
String firstCategory = firstHit.getJSONArray("category").getString(0);
String secondCategory = secondHit.getJSONArray("category").getString(0);
String firstColor = firstHit.getJSONArray("color").getString(0);
String secondColor = secondHit.getJSONArray("color").getString(0);
assertEquals("inner score for first is not correct." , true, Math.abs(firstScore - 10000) < 1 );
assertEquals("inner score for second is not correct." , true, Math.abs(secondScore - 10000) < 1 );
assertEquals("category for first is not correct." , true, firstCategory.equals("compact") );
assertEquals("category for second is not correct." , true, secondCategory.equals("compact") );
assertEquals("color for first is not correct." , true, firstColor.equals("red") );
assertEquals("color for second is not correct." , true, secondColor.equals("red") );
}
public void testRelevanceMultiContains() throws Exception