forked from apache/solr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpellCheckCollatorTest.java
760 lines (718 loc) · 32.3 KB
/
SpellCheckCollatorTest.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
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.solr.spelling;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.apache.lucene.tests.util.LuceneTestCase.SuppressTempFileChecks;
import org.apache.lucene.tests.util.TestUtil;
import org.apache.solr.SolrTestCaseJ4;
import org.apache.solr.common.params.CommonParams;
import org.apache.solr.common.params.CursorMarkParams;
import org.apache.solr.common.params.GroupParams;
import org.apache.solr.common.params.ModifiableSolrParams;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.common.params.SpellingParams;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.common.util.SimpleOrderedMap;
import org.apache.solr.core.SolrCore;
import org.apache.solr.handler.component.SearchComponent;
import org.apache.solr.handler.component.SpellCheckComponent;
import org.apache.solr.request.LocalSolrQueryRequest;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.request.SolrRequestHandler;
import org.apache.solr.response.SolrQueryResponse;
import org.junit.BeforeClass;
import org.junit.Test;
@SuppressTempFileChecks(
bugUrl = "https://issues.apache.org/jira/browse/SOLR-1877 Spellcheck IndexReader leak bug?")
public class SpellCheckCollatorTest extends SolrTestCaseJ4 {
// if adding documents to this test, adjust me.
private static final int NUM_DOCS_WITH_TERM_EVERYOTHER = 8;
private static final int NUM_DOCS = 17;
@BeforeClass
public static void beforeClass() throws Exception {
initCore("solrconfig-spellcheckcomponent.xml", "schema.xml");
assertU(
adoc(
"id", "0",
"lowerfilt", "faith hope and love to",
"teststop", "metanoia"));
assertU(
adoc(
"id", "1",
"lowerfilt", "faith hope and loaves",
"teststop", "everyother"));
assertU(
adoc(
"id", "2",
"lowerfilt", "fat hops and loaves"));
assertU(
adoc(
"id", "3",
"lowerfilt", "faith of homer",
"teststop", "metanoia",
"teststop", "everyother"));
assertU(
adoc(
"id", "4",
"lowerfilt", "fat of homer"));
assertU(
adoc(
"id", "5",
"lowerfilt1", "peace",
"teststop", "everyother"));
assertU(
adoc(
"id", "6",
"lowerfilt", "hyphenated word"));
assertU(
adoc(
"id", "7",
"teststop", "Jane filled out a form at Charles De Gaulle",
"teststop", "everyother"));
assertU(
adoc(
"id", "8",
"teststop", "Dick flew from Heathrow"));
assertU(
adoc(
"id", "9",
"teststop", "Jane is stuck in customs because Spot chewed up the form",
"teststop", "everyother"));
assertU(
adoc(
"id", "10",
"teststop", "Once in Paris Dick built a fire on the hearth"));
assertU(
adoc(
"id", "11",
"teststop", "Dick waited for Jane as he watched the sparks flow upward",
"teststop", "everyother"));
assertU(
adoc(
"id", "12",
"teststop", "This June parisian rendez-vous is ruined because of a customs snafu"));
assertU(
adoc(
"id", "13",
"teststop", "partisan political machine",
"teststop", "metanoia",
"teststop", "everyother"));
assertU(
adoc(
"id", "14",
"teststop", "metanoia"));
assertU(
adoc(
"id", "15",
"teststop", "metanoia",
"teststop", "everyother"));
assertU(
adoc(
"id", "16",
"teststop", "metanoia"));
assertU(commit());
}
@Test
@SuppressWarnings({"unchecked", "rawtypes"})
public void testCollationWithRangeQuery() {
SolrCore core = h.getCore();
SearchComponent speller = core.getSearchComponent("spellcheck");
assertTrue("speller is null and it shouldn't be", speller != null);
ModifiableSolrParams params = new ModifiableSolrParams();
params.add(SpellCheckComponent.COMPONENT_NAME, "true");
params.add(SpellingParams.SPELLCHECK_BUILD, "true");
params.add(SpellingParams.SPELLCHECK_COUNT, "10");
params.add(SpellingParams.SPELLCHECK_COLLATE, "true");
params.add(SpellingParams.SPELLCHECK_ALTERNATIVE_TERM_COUNT, "10");
params.add(CommonParams.Q, "id:[1 TO 10] AND lowerfilt:lovw");
{
SolrRequestHandler handler = core.getRequestHandler("/spellCheckCompRH");
SolrQueryResponse rsp = new SolrQueryResponse();
rsp.addResponseHeader(new SimpleOrderedMap());
SolrQueryRequest req = new LocalSolrQueryRequest(core, params);
handler.handleRequest(req, rsp);
req.close();
NamedList values = rsp.getValues();
NamedList spellCheck = (NamedList) values.get("spellcheck");
NamedList collationHolder = (NamedList) spellCheck.get("collations");
List<String> collations = collationHolder.getAll("collation");
assertTrue(collations.size() == 1);
String collation = collations.iterator().next();
System.out.println(collation);
assertTrue(
"Incorrect collation: " + collation, "id:[1 TO 10] AND lowerfilt:love".equals(collation));
}
}
@Test
@SuppressWarnings({"unchecked", "rawtypes"})
public void testCollationWithHypens() {
SolrCore core = h.getCore();
SearchComponent speller = core.getSearchComponent("spellcheck");
assertTrue("speller is null and it shouldn't be", speller != null);
ModifiableSolrParams params = new ModifiableSolrParams();
params.add(SpellCheckComponent.COMPONENT_NAME, "true");
params.add(SpellingParams.SPELLCHECK_BUILD, "true");
params.add(SpellingParams.SPELLCHECK_COUNT, "10");
params.add(SpellingParams.SPELLCHECK_COLLATE, "true");
params.add(CommonParams.Q, "lowerfilt:(hypenated-wotd)");
{
SolrRequestHandler handler = core.getRequestHandler("/spellCheckCompRH");
SolrQueryResponse rsp = new SolrQueryResponse();
rsp.addResponseHeader(new SimpleOrderedMap());
SolrQueryRequest req = new LocalSolrQueryRequest(core, params);
handler.handleRequest(req, rsp);
req.close();
NamedList values = rsp.getValues();
NamedList spellCheck = (NamedList) values.get("spellcheck");
NamedList collationHolder = (NamedList) spellCheck.get("collations");
List<String> collations = collationHolder.getAll("collation");
assertTrue(collations.size() == 1);
String collation = collations.iterator().next();
assertTrue(
"Incorrect collation: " + collation, "lowerfilt:(hyphenated-word)".equals(collation));
}
params.remove(CommonParams.Q);
params.add("defType", "dismax");
params.add("qf", "/lowerfilt");
params.add(CommonParams.Q, "hypenated-wotd");
{
SolrRequestHandler handler = core.getRequestHandler("/spellCheckCompRH");
SolrQueryResponse rsp = new SolrQueryResponse();
rsp.add("responseHeader", new SimpleOrderedMap());
SolrQueryRequest req = new LocalSolrQueryRequest(core, params);
handler.handleRequest(req, rsp);
req.close();
NamedList values = rsp.getValues();
NamedList spellCheck = (NamedList) values.get("spellcheck");
NamedList collationHolder = (NamedList) spellCheck.get("collations");
List<String> collations = collationHolder.getAll("collation");
assertTrue(collations.size() == 1);
String collation = collations.iterator().next();
assertTrue("Incorrect collation: " + collation, "hyphenated-word".equals(collation));
}
}
public void testCollateWithOverride() {
assertQ(
req(
SpellCheckComponent.COMPONENT_NAME,
"true",
SpellCheckComponent.SPELLCHECK_DICT,
"direct",
SpellingParams.SPELLCHECK_COUNT,
"10",
SpellingParams.SPELLCHECK_COLLATE,
"true",
SpellingParams.SPELLCHECK_MAX_COLLATION_TRIES,
"10",
SpellingParams.SPELLCHECK_MAX_COLLATIONS,
"10",
"qt",
"/spellCheckCompRH",
"defType",
"edismax",
"qf",
"teststop",
"mm",
"1",
CommonParams.Q,
"partisian politcal mashine"),
"//lst[@name='spellcheck']/lst[@name='collations']/str[@name='collation']='parisian political machine'");
assertQ(
req(
SpellCheckComponent.COMPONENT_NAME,
"true",
SpellCheckComponent.SPELLCHECK_DICT,
"direct",
SpellingParams.SPELLCHECK_COUNT,
"10",
SpellingParams.SPELLCHECK_COLLATE,
"true",
SpellingParams.SPELLCHECK_MAX_COLLATION_TRIES,
"10",
SpellingParams.SPELLCHECK_MAX_COLLATIONS,
"10",
"qt",
"/spellCheckCompRH",
"defType",
"edismax",
"qf",
"teststop",
"mm",
"1",
SpellingParams.SPELLCHECK_COLLATE_PARAM_OVERRIDE + "mm",
"100%",
CommonParams.Q,
"partisian politcal mashine"),
"//lst[@name='spellcheck']/lst[@name='collations']/str[@name='collation']='partisan political machine'");
}
@Test
@SuppressWarnings({"unchecked", "rawtypes"})
public void testCollateWithFilter() {
SolrCore core = h.getCore();
SearchComponent speller = core.getSearchComponent("spellcheck");
assertTrue("speller is null and it shouldn't be", speller != null);
ModifiableSolrParams params = new ModifiableSolrParams();
params.add(SpellCheckComponent.COMPONENT_NAME, "true");
params.add(SpellingParams.SPELLCHECK_BUILD, "true");
params.add(SpellingParams.SPELLCHECK_COUNT, "10");
params.add(SpellingParams.SPELLCHECK_COLLATE, "true");
params.add(SpellingParams.SPELLCHECK_MAX_COLLATION_TRIES, "10");
params.add(SpellingParams.SPELLCHECK_MAX_COLLATIONS, "10");
params.add(CommonParams.Q, "lowerfilt:(+fauth +home +loane)");
params.add(CommonParams.FQ, "NOT(id:1)");
// Because a FilterQuery is applied which removes doc id#1 from possible hits, we would
// not want the collations to return us "lowerfilt:(+faith +hope +loaves)" as this only matches
// doc id#1.
SolrRequestHandler handler = core.getRequestHandler("/spellCheckCompRH");
SolrQueryResponse rsp = new SolrQueryResponse();
rsp.addResponseHeader(new SimpleOrderedMap());
SolrQueryRequest req = new LocalSolrQueryRequest(core, params);
handler.handleRequest(req, rsp);
req.close();
NamedList values = rsp.getValues();
NamedList spellCheck = (NamedList) values.get("spellcheck");
NamedList collationHolder = (NamedList) spellCheck.get("collations");
List<String> collations = collationHolder.getAll("collation");
assertTrue(collations.size() > 0);
for (String collation : collations) {
assertTrue(!collation.equals("lowerfilt:(+faith +hope +loaves)"));
}
}
@Test
@SuppressWarnings({"unchecked", "rawtypes"})
public void testCollateWithMultipleRequestHandlers() {
SolrCore core = h.getCore();
SearchComponent speller = core.getSearchComponent("spellcheck");
assertTrue("speller is null and it shouldn't be", speller != null);
ModifiableSolrParams params = new ModifiableSolrParams();
params.add(SpellCheckComponent.COMPONENT_NAME, "true");
params.add(SpellingParams.SPELLCHECK_DICT, "multipleFields");
params.add(SpellingParams.SPELLCHECK_BUILD, "true");
params.add(SpellingParams.SPELLCHECK_COUNT, "10");
params.add(SpellingParams.SPELLCHECK_COLLATE, "true");
params.add(SpellingParams.SPELLCHECK_MAX_COLLATION_TRIES, "1");
params.add(SpellingParams.SPELLCHECK_MAX_COLLATIONS, "1");
params.add(CommonParams.Q, "peac");
// SpellCheckCompRH has no "qf" defined. It will not find "peace" from "peac" despite it being
// in the dictionary because requrying against this Request Handler results in 0 hits.
SolrRequestHandler handler = core.getRequestHandler("/spellCheckCompRH");
SolrQueryResponse rsp = new SolrQueryResponse();
rsp.addResponseHeader(new SimpleOrderedMap());
SolrQueryRequest req = new LocalSolrQueryRequest(core, params);
handler.handleRequest(req, rsp);
req.close();
NamedList values = rsp.getValues();
NamedList spellCheck = (NamedList) values.get("spellcheck");
NamedList collationHolder = (NamedList) spellCheck.get("collations");
String singleCollation = (String) collationHolder.get("collation");
assertNull(singleCollation);
// SpellCheckCompRH1 has "lowerfilt1" defined in the "qf" param. It will find "peace" from
// "peac" because requrying field "lowerfilt1" returns the hit.
params.remove(SpellingParams.SPELLCHECK_BUILD);
handler = core.getRequestHandler("/spellCheckCompRH1");
rsp = new SolrQueryResponse();
rsp.addResponseHeader(new SimpleOrderedMap());
req = new LocalSolrQueryRequest(core, params);
handler.handleRequest(req, rsp);
req.close();
values = rsp.getValues();
spellCheck = (NamedList) values.get("spellcheck");
collationHolder = (NamedList) spellCheck.get("collations");
singleCollation = (String) collationHolder.get("collation");
assertEquals(singleCollation, "peace");
}
@Test
@SuppressWarnings({"unchecked", "rawtypes"})
public void testExtendedCollate() {
SolrCore core = h.getCore();
SearchComponent speller = core.getSearchComponent("spellcheck");
assertTrue("speller is null and it shouldn't be", speller != null);
ModifiableSolrParams params = new ModifiableSolrParams();
params.add(CommonParams.QT, "spellCheckCompRH");
params.add(CommonParams.Q, "lowerfilt:(+fauth +home +loane)");
params.add(SpellingParams.SPELLCHECK_EXTENDED_RESULTS, "true");
params.add(SpellCheckComponent.COMPONENT_NAME, "true");
params.add(SpellingParams.SPELLCHECK_BUILD, "true");
params.add(SpellingParams.SPELLCHECK_COUNT, "10");
params.add(SpellingParams.SPELLCHECK_COLLATE, "true");
// Testing backwards-compatible behavior.
// Returns 1 collation as a single string.
// All words are "correct" per the dictionary, but this collation would
// return no results if tried.
SolrRequestHandler handler = core.getRequestHandler("/spellCheckCompRH");
SolrQueryResponse rsp = new SolrQueryResponse();
rsp.addResponseHeader(new SimpleOrderedMap());
SolrQueryRequest req = new LocalSolrQueryRequest(core, params);
handler.handleRequest(req, rsp);
req.close();
NamedList values = rsp.getValues();
NamedList spellCheck = (NamedList) values.get("spellcheck");
NamedList collationHolder = (NamedList) spellCheck.get("collations");
String singleCollation = (String) collationHolder.get("collation");
assertEquals("lowerfilt:(+faith +homer +loaves)", singleCollation);
// Testing backwards-compatible response format but will only return a
// collation that would return results.
params.remove(SpellingParams.SPELLCHECK_BUILD);
params.add(SpellingParams.SPELLCHECK_MAX_COLLATION_TRIES, "5");
params.add(SpellingParams.SPELLCHECK_MAX_COLLATIONS, "1");
handler = core.getRequestHandler("/spellCheckCompRH");
rsp = new SolrQueryResponse();
rsp.addResponseHeader(new SimpleOrderedMap());
req = new LocalSolrQueryRequest(core, params);
handler.handleRequest(req, rsp);
req.close();
values = rsp.getValues();
spellCheck = (NamedList) values.get("spellcheck");
collationHolder = (NamedList) spellCheck.get("collations");
singleCollation = (String) collationHolder.get("collation");
assertEquals("lowerfilt:(+faith +hope +loaves)", singleCollation);
// Testing returning multiple collations if more than one valid
// combination exists.
params.remove(SpellingParams.SPELLCHECK_MAX_COLLATION_TRIES);
params.remove(SpellingParams.SPELLCHECK_MAX_COLLATIONS);
params.add(SpellingParams.SPELLCHECK_MAX_COLLATION_TRIES, "10");
params.add(SpellingParams.SPELLCHECK_MAX_COLLATIONS, "2");
handler = core.getRequestHandler("/spellCheckCompRH");
rsp = new SolrQueryResponse();
rsp.addResponseHeader(new SimpleOrderedMap());
req = new LocalSolrQueryRequest(core, params);
handler.handleRequest(req, rsp);
req.close();
values = rsp.getValues();
spellCheck = (NamedList) values.get("spellcheck");
collationHolder = (NamedList) spellCheck.get("collations");
List<String> collations = collationHolder.getAll("collation");
assertTrue(collations.size() == 2);
for (String multipleCollation : collations) {
assertTrue(
multipleCollation.equals("lowerfilt:(+faith +hope +love)")
|| multipleCollation.equals("lowerfilt:(+faith +hope +loaves)"));
}
// Testing return multiple collations with expanded collation response
// format.
params.add(SpellingParams.SPELLCHECK_COLLATE_EXTENDED_RESULTS, "true");
handler = core.getRequestHandler("/spellCheckCompRH");
rsp = new SolrQueryResponse();
rsp.addResponseHeader(new SimpleOrderedMap());
req = new LocalSolrQueryRequest(core, params);
handler.handleRequest(req, rsp);
req.close();
values = rsp.getValues();
spellCheck = (NamedList) values.get("spellcheck");
collationHolder = (NamedList) spellCheck.get("collations");
List<NamedList> expandedCollationList = collationHolder.getAll("collation");
Set<String> usedcollations = new HashSet<>();
assertTrue(expandedCollationList.size() == 2);
for (NamedList expandedCollation : expandedCollationList) {
String multipleCollation = (String) expandedCollation.get("collationQuery");
assertTrue(
multipleCollation.equals("lowerfilt:(+faith +hope +love)")
|| multipleCollation.equals("lowerfilt:(+faith +hope +loaves)"));
assertTrue(!usedcollations.contains(multipleCollation));
usedcollations.add(multipleCollation);
assertEquals(1L, expandedCollation.get("hits"));
NamedList misspellingsAndCorrections =
(NamedList) expandedCollation.get("misspellingsAndCorrections");
assertTrue(misspellingsAndCorrections.size() == 3);
String correctionForFauth = (String) misspellingsAndCorrections.get("fauth");
String correctionForHome = (String) misspellingsAndCorrections.get("home");
String correctionForLoane = (String) misspellingsAndCorrections.get("loane");
assertTrue(correctionForFauth.equals("faith"));
assertTrue(correctionForHome.equals("hope"));
assertTrue(correctionForLoane.equals("love") || correctionForLoane.equals("loaves"));
}
}
@Test
@SuppressWarnings({"unchecked", "rawtypes"})
public void testCollateWithGrouping() {
SolrCore core = h.getCore();
SearchComponent speller = core.getSearchComponent("spellcheck");
assertTrue("speller is null and it shouldn't be", speller != null);
ModifiableSolrParams params = new ModifiableSolrParams();
params.add(SpellCheckComponent.COMPONENT_NAME, "true");
params.add(SpellingParams.SPELLCHECK_BUILD, "true");
params.add(SpellingParams.SPELLCHECK_COUNT, "10");
params.add(SpellingParams.SPELLCHECK_COLLATE, "true");
params.add(SpellingParams.SPELLCHECK_MAX_COLLATION_TRIES, "5");
params.add(SpellingParams.SPELLCHECK_MAX_COLLATIONS, "1");
params.add(CommonParams.Q, "lowerfilt:(+fauth)");
params.add(GroupParams.GROUP, "true");
params.add(GroupParams.GROUP_FIELD, "id");
// Because a FilterQuery is applied which removes doc id#1 from possible hits, we would
// not want the collations to return us "lowerfilt:(+faith +hope +loaves)" as this only matches
// doc id#1.
SolrRequestHandler handler = core.getRequestHandler("/spellCheckCompRH");
SolrQueryResponse rsp = new SolrQueryResponse();
rsp.addResponseHeader(new SimpleOrderedMap());
SolrQueryRequest req = new LocalSolrQueryRequest(core, params);
handler.handleRequest(req, rsp);
req.close();
NamedList values = rsp.getValues();
NamedList spellCheck = (NamedList) values.get("spellcheck");
NamedList collationHolder = (NamedList) spellCheck.get("collations");
List<String> collations = collationHolder.getAll("collation");
assertTrue(collations.size() == 1);
}
@Test
public void testContextSensitiveCollate() {
// DirectSolrSpellChecker IndexBasedSpellChecker
String[] dictionary = {"direct", "default_teststop"};
for (int i = 0; i <= 1; i++) {
assertQ(
req(
"q",
"teststop:(flew AND form AND heathrow)",
"qt",
"/spellCheckCompRH",
"indent",
"true",
SpellCheckComponent.COMPONENT_NAME,
"true",
SpellCheckComponent.SPELLCHECK_BUILD,
"true",
SpellCheckComponent.SPELLCHECK_DICT,
dictionary[i],
SpellCheckComponent.SPELLCHECK_EXTENDED_RESULTS,
"true",
SpellCheckComponent.SPELLCHECK_COUNT,
"10",
SpellCheckComponent.SPELLCHECK_ALTERNATIVE_TERM_COUNT,
"5",
SpellCheckComponent.SPELLCHECK_MAX_RESULTS_FOR_SUGGEST,
"0",
SpellCheckComponent.SPELLCHECK_COLLATE,
"true",
SpellCheckComponent.SPELLCHECK_MAX_COLLATION_TRIES,
"10",
SpellCheckComponent.SPELLCHECK_MAX_COLLATIONS,
"1",
SpellCheckComponent.SPELLCHECK_COLLATE_EXTENDED_RESULTS,
"true"),
"//lst[@name='spellcheck']/lst[@name='suggestions']/lst[@name='flew']/arr[@name='suggestion']/lst/str[@name='word']='flow'",
"//lst[@name='spellcheck']/lst[@name='suggestions']/lst[@name='form']/arr[@name='suggestion']/lst/str[@name='word']='from'",
/* DirectSolrSpellChecker won't suggest if the edit distance > 2, so we can't test for this one...
"//lst[@name='spellcheck']/lst[@name='suggestions']/lst[@name='heathrow']/arr[@name='suggestion']/lst/str[@name='word']='hearth'",
*/
"//lst[@name='spellcheck']/bool[@name='correctlySpelled']='false'",
"//lst[@name='spellcheck']/lst[@name='collations']/lst[@name='collation']/str[@name='collationQuery']='teststop:(flew AND from AND heathrow)'",
"//lst[@name='spellcheck']/lst[@name='collations']/lst[@name='collation']/long[@name='hits']=1",
"//lst[@name='spellcheck']/lst[@name='collations']/lst[@name='collation']/lst[@name='misspellingsAndCorrections']/str[@name='form']='from'");
assertQ(
req(
"q",
"teststop:(june AND customs)",
"qt",
"/spellCheckCompRH",
"indent",
"true",
SpellCheckComponent.COMPONENT_NAME,
"true",
SpellCheckComponent.SPELLCHECK_DICT,
dictionary[i],
SpellCheckComponent.SPELLCHECK_EXTENDED_RESULTS,
"true",
SpellCheckComponent.SPELLCHECK_COUNT,
"10",
SpellCheckComponent.SPELLCHECK_ALTERNATIVE_TERM_COUNT,
"5",
SpellCheckComponent.SPELLCHECK_MAX_RESULTS_FOR_SUGGEST,
"1",
SpellCheckComponent.SPELLCHECK_COLLATE,
"true",
SpellCheckComponent.SPELLCHECK_MAX_COLLATION_TRIES,
"10",
SpellCheckComponent.SPELLCHECK_MAX_COLLATIONS,
"1",
SpellCheckComponent.SPELLCHECK_COLLATE_EXTENDED_RESULTS,
"true"),
"//result[@numFound=1]",
"//lst[@name='spellcheck']/lst[@name='suggestions']/lst[@name='june']/arr[@name='suggestion']/lst/str[@name='word']='jane'",
"//lst[@name='spellcheck']/bool[@name='correctlySpelled']='false'",
"//lst[@name='spellcheck']/lst[@name='collations']/lst[@name='collation']/str[@name='collationQuery']='teststop:(jane AND customs)'",
"//lst[@name='spellcheck']/lst[@name='collations']/lst[@name='collation']/long[@name='hits']=1",
"//lst[@name='spellcheck']/lst[@name='collations']/lst[@name='collation']/lst[@name='misspellingsAndCorrections']/str[@name='june']='jane'");
// SOLR-5090, alternativeTermCount==0 was being evaluated, would sometimes throw NPE
assertQ(
req(
"q",
"teststop:(june customs)",
"mm",
"2",
"qt",
"/spellCheckCompRH",
"indent",
"true",
SpellCheckComponent.COMPONENT_NAME,
"true",
SpellCheckComponent.SPELLCHECK_DICT,
dictionary[i],
SpellCheckComponent.SPELLCHECK_COUNT,
"10",
SpellCheckComponent.SPELLCHECK_ALTERNATIVE_TERM_COUNT,
"0",
SpellCheckComponent.SPELLCHECK_COLLATE,
"true"));
}
}
@Test
public void testEstimatedHitCounts() {
final String xpathPrefix =
"//lst[@name='spellcheck']/lst[@name='collations']/lst[@name='collation']/";
final SolrParams reusedParams =
params(
SpellCheckComponent.COMPONENT_NAME,
"true",
SpellCheckComponent.SPELLCHECK_DICT,
"direct",
SpellingParams.SPELLCHECK_COUNT,
"1",
SpellingParams.SPELLCHECK_COLLATE,
"true",
SpellingParams.SPELLCHECK_MAX_COLLATION_TRIES,
"1",
SpellingParams.SPELLCHECK_MAX_COLLATIONS,
"1",
SpellingParams.SPELLCHECK_COLLATE_EXTENDED_RESULTS,
"true",
"qt",
"/spellCheckCompRH");
// default case, no SPELLCHECK_COLLATE_MAX_COLLECT_DOCS should be exact num hits
assertQ(
req(reusedParams, CommonParams.Q, "teststop:metnoia"),
xpathPrefix + "str[@name='collationQuery']='teststop:metanoia'",
xpathPrefix + "long[@name='hits']=6");
// specifying 0 means "exact" same as default, but specifying a value greater
// than the total number of docs in the index should also result in it
// "estimating" and getting exact number as well.
for (String val : new String[] {"0", "30", "100", "10000"}) {
assertQ(
req(
reusedParams,
CommonParams.Q,
"teststop:metnoia",
SpellingParams.SPELLCHECK_COLLATE_MAX_COLLECT_DOCS,
val),
xpathPrefix + "str[@name='collationQuery']='teststop:metanoia'",
xpathPrefix + "long[@name='hits']=6");
}
// values between 0 and the num docs in the index should not error, and should
// produce an estimate no more than the total number of docs
final int iters = atLeast(10);
for (int iter = 0; iter < iters; iter++) {
final int val = TestUtil.nextInt(random(), 1, 17);
assertQ(
req(
reusedParams,
CommonParams.Q,
"teststop:metnoia",
SpellingParams.SPELLCHECK_COLLATE_MAX_COLLECT_DOCS,
"" + val),
xpathPrefix + "str[@name='collationQuery']='teststop:metanoia'",
xpathPrefix + "long[@name='hits' and . <= 17 and 0 < .]");
}
// "everYother" appears in every other doc in the index, so "everother"
// should produce a "decent" approximation of the number of hits (8)
// for any 5 <= SPELLCHECK_COLLATE_MAX_COLLECT_DOCS
//
// (we have to be kind of flexible with our definition of "decent"
// since we're dealing with a fairly small index here)
for (int val = 5; val <= 20; val++) {
String hitsXPath = xpathPrefix + "long[@name='hits']"; // we will append to this...
if (val <= NUM_DOCS_WITH_TERM_EVERYOTHER) {
// strongest assertions we can make given an arbitrary MergePolicy on such a small index
// is based on the idea that the docs may all come *first* or all come *last*
// and then do the math on what estimate should come from that if we collected *exactly*
// 'val'...
//
// if they are all "first" we will overestimate and assume everything is a match...
int max = NUM_DOCS;
// if they are all "last" we will under-estimate based on how non-matches we had to skip...
int min = (/* min collected */ val) / (/* max docs possibly scanned */ NUM_DOCS);
hitsXPath += "[" + min + " <= . and . <= " + max + "]";
} else {
// we've asked for a number greater than what can possibly be found in our tiny index, which
// should force it to scan all docs so our hits should be exact
hitsXPath += "[.=" + NUM_DOCS_WITH_TERM_EVERYOTHER + "]";
}
assertQ(
req(
reusedParams,
CommonParams.Q,
"teststop:everother",
SpellingParams.SPELLCHECK_COLLATE_MAX_COLLECT_DOCS,
"" + val),
xpathPrefix + "str[@name='collationQuery']='teststop:everyother'",
hitsXPath);
}
}
@Test
@SuppressWarnings({"unchecked", "rawtypes"})
public void testZeroTries() {
SolrCore core = h.getCore();
SearchComponent speller = core.getSearchComponent("spellcheck");
assertTrue("speller is null and it shouldn't be", speller != null);
ModifiableSolrParams params = new ModifiableSolrParams();
params.add(SpellCheckComponent.COMPONENT_NAME, "true");
params.add(SpellCheckComponent.SPELLCHECK_BUILD, "true");
params.add(SpellCheckComponent.SPELLCHECK_COUNT, "10");
params.add(SpellCheckComponent.SPELLCHECK_COLLATE, "true");
params.add(SpellCheckComponent.SPELLCHECK_MAX_COLLATION_TRIES, "0");
params.add(SpellCheckComponent.SPELLCHECK_MAX_COLLATIONS, "2");
params.add(CommonParams.Q, "lowerfilt:(+fauth)");
SolrRequestHandler handler = core.getRequestHandler("/spellCheckCompRH");
SolrQueryResponse rsp = new SolrQueryResponse();
rsp.addResponseHeader(new SimpleOrderedMap());
SolrQueryRequest req = new LocalSolrQueryRequest(core, params);
handler.handleRequest(req, rsp);
req.close();
NamedList values = rsp.getValues();
NamedList spellCheck = (NamedList) values.get("spellcheck");
NamedList collationList = (NamedList) spellCheck.get("collations");
List<?> collations = (List<?>) collationList.getAll("collation");
assertTrue(collations.size() == 2);
}
@Test
@SuppressWarnings({"unchecked", "rawtypes"})
public void testWithCursorMark() {
SolrCore core = h.getCore();
SearchComponent speller = core.getSearchComponent("spellcheck");
assertTrue("speller is null and it shouldn't be", speller != null);
ModifiableSolrParams params = new ModifiableSolrParams();
params.add(SpellCheckComponent.COMPONENT_NAME, "true");
params.add(SpellCheckComponent.SPELLCHECK_BUILD, "true");
params.add(SpellCheckComponent.SPELLCHECK_COUNT, "10");
params.add(SpellCheckComponent.SPELLCHECK_COLLATE, "true");
params.add(SpellCheckComponent.SPELLCHECK_MAX_COLLATION_TRIES, "2");
params.add(SpellCheckComponent.SPELLCHECK_MAX_COLLATIONS, "1");
params.add(CommonParams.Q, "lowerfilt:(+fauth)");
params.add(CommonParams.SORT, "id asc");
params.add(CursorMarkParams.CURSOR_MARK_PARAM, CursorMarkParams.CURSOR_MARK_START);
SolrRequestHandler handler = core.getRequestHandler("/spellCheckCompRH");
SolrQueryResponse rsp = new SolrQueryResponse();
rsp.addResponseHeader(new SimpleOrderedMap());
SolrQueryRequest req = new LocalSolrQueryRequest(core, params);
handler.handleRequest(req, rsp);
req.close();
NamedList values = rsp.getValues();
NamedList spellCheck = (NamedList) values.get("spellcheck");
NamedList collationList = (NamedList) spellCheck.get("collations");
List<?> collations = (List<?>) collationList.getAll("collation");
assertTrue(collations.size() == 1);
}
}