forked from apache/lucenenet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestQPHelper.cs
More file actions
1477 lines (1265 loc) · 60.4 KB
/
Copy pathTestQPHelper.cs
File metadata and controls
1477 lines (1265 loc) · 60.4 KB
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
using Lucene.Net.Analysis;
using Lucene.Net.Analysis.TokenAttributes;
using Lucene.Net.Analysis.TokenAttributes.Extensions;
using Lucene.Net.Documents;
using Lucene.Net.Index;
using Lucene.Net.QueryParsers.Flexible.Core;
using Lucene.Net.QueryParsers.Flexible.Core.Messages;
using Lucene.Net.QueryParsers.Flexible.Core.Nodes;
using Lucene.Net.QueryParsers.Flexible.Core.Processors;
using Lucene.Net.QueryParsers.Flexible.Messages;
using Lucene.Net.QueryParsers.Flexible.Standard.Config;
using Lucene.Net.QueryParsers.Flexible.Standard.Nodes;
using Lucene.Net.Search;
using Lucene.Net.Support;
using Lucene.Net.Util;
using Lucene.Net.Util.Automaton;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using JCG = J2N.Collections.Generic;
namespace Lucene.Net.QueryParsers.Flexible.Standard
{
/*
* 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.
*/
/// <summary>
/// This test case is a copy of the core Lucene query parser test, it was adapted
/// to use new QueryParserHelper instead of the old query parser.
///
/// Tests QueryParser.
/// </summary>
// TODO: really this should extend QueryParserTestBase too!
public class TestQPHelper : LuceneTestCase
{
public static Analyzer qpAnalyzer;
[OneTimeSetUp]
public override void OneTimeSetUp()
{
base.OneTimeSetUp();
qpAnalyzer = new QPTestAnalyzer();
}
[OneTimeTearDown]
public override void OneTimeTearDown()
{
qpAnalyzer = null;
base.OneTimeTearDown();
}
public sealed class QPTestFilter : TokenFilter
{
private readonly ICharTermAttribute termAtt;
private readonly IOffsetAttribute offsetAtt;
/**
* Filter which discards the token 'stop' and which expands the token
* 'phrase' into 'phrase1 phrase2'
*/
public QPTestFilter(TokenStream @in)
: base(@in)
{
termAtt = AddAttribute<ICharTermAttribute>();
offsetAtt = AddAttribute<IOffsetAttribute>();
}
private bool inPhrase = false;
private int savedStart = 0;
private int savedEnd = 0;
public override bool IncrementToken()
{
if (inPhrase)
{
inPhrase = false;
ClearAttributes();
termAtt.SetEmpty().Append("phrase2");
offsetAtt.SetOffset(savedStart, savedEnd);
return true;
}
else
while (m_input.IncrementToken())
{
if (termAtt.toString().Equals("phrase", StringComparison.Ordinal))
{
inPhrase = true;
savedStart = offsetAtt.StartOffset;
savedEnd = offsetAtt.EndOffset;
termAtt.SetEmpty().Append("phrase1");
offsetAtt.SetOffset(savedStart, savedEnd);
return true;
}
else if (!termAtt.toString().Equals("stop", StringComparison.Ordinal))
return true;
}
return false;
}
public override void Reset()
{
base.Reset();
this.inPhrase = false;
this.savedStart = 0;
this.savedEnd = 0;
}
}
public sealed class QPTestAnalyzer : Analyzer
{
/** Filters MockTokenizer with StopFilter. */
protected internal override sealed TokenStreamComponents CreateComponents(String fieldName, TextReader reader)
{
Tokenizer tokenizer = new MockTokenizer(reader, MockTokenizer.SIMPLE, true);
return new TokenStreamComponents(tokenizer, new QPTestFilter(tokenizer));
}
}
public class QPTestParser : StandardQueryParser
{
public QPTestParser(Analyzer a)
{
((QueryNodeProcessorPipeline)QueryNodeProcessor)
.Add(new QPTestParserQueryNodeProcessor());
this.Analyzer = (a);
}
private class QPTestParserQueryNodeProcessor :
QueryNodeProcessor
{
protected override IQueryNode PostProcessNode(IQueryNode node)
{
return node;
}
protected override IQueryNode PreProcessNode(IQueryNode node)
{
if (node is WildcardQueryNode || node is FuzzyQueryNode)
{
// LUCENENET: Factored out NLS/Message/IMessage so end users can optionally utilize the built-in .NET localization.
throw new QueryNodeException(
QueryParserMessages.EMPTY_MESSAGE);
}
return node;
}
protected override IList<IQueryNode> SetChildrenOrder(IList<IQueryNode> children)
{
return children;
}
}
}
private int originalMaxClauses;
public override void SetUp()
{
base.SetUp();
originalMaxClauses = BooleanQuery.MaxClauseCount;
}
public StandardQueryParser GetParser(Analyzer a)
{
if (a is null)
a = new MockAnalyzer(Random, MockTokenizer.SIMPLE, true);
StandardQueryParser qp = new StandardQueryParser();
qp.Analyzer = (a);
qp.DefaultOperator = (StandardQueryConfigHandler.Operator.OR);
return qp;
}
public Query GetQuery(String query, Analyzer a)
{
return GetParser(a).Parse(query, "field");
}
public Query GetQueryAllowLeadingWildcard(String query, Analyzer a)
{
StandardQueryParser parser = GetParser(a);
parser.AllowLeadingWildcard = (true);
return parser.Parse(query, "field");
}
public void AssertQueryEquals(String query, Analyzer a, String result)
{
Query q = GetQuery(query, a);
String s = q.ToString("field");
if (!s.Equals(result, StringComparison.Ordinal))
{
fail("Query /" + query + "/ yielded /" + s + "/, expecting /" + result
+ "/");
}
}
public void AssertQueryEqualsAllowLeadingWildcard(String query, Analyzer a, String result)
{
Query q = GetQueryAllowLeadingWildcard(query, a);
String s = q.ToString("field");
if (!s.Equals(result, StringComparison.Ordinal))
{
fail("Query /" + query + "/ yielded /" + s + "/, expecting /" + result
+ "/");
}
}
public void AssertQueryEquals(StandardQueryParser qp, String field,
String query, String result)
{
Query q = qp.Parse(query, field);
String s = q.ToString(field);
if (!s.Equals(result, StringComparison.Ordinal))
{
fail("Query /" + query + "/ yielded /" + s + "/, expecting /" + result
+ "/");
}
}
public void AssertEscapedQueryEquals(String query, Analyzer a, String result)
{
String escapedQuery = QueryParserUtil.Escape(query);
if (!escapedQuery.Equals(result, StringComparison.Ordinal))
{
fail("Query /" + query + "/ yielded /" + escapedQuery + "/, expecting /"
+ result + "/");
}
}
public void AssertWildcardQueryEquals(String query, bool lowercase,
String result, bool allowLeadingWildcard)
{
StandardQueryParser qp = GetParser(null);
qp.LowercaseExpandedTerms = (lowercase);
qp.AllowLeadingWildcard = (allowLeadingWildcard);
Query q = qp.Parse(query, "field");
String s = q.ToString("field");
if (!s.Equals(result, StringComparison.Ordinal))
{
fail("WildcardQuery /" + query + "/ yielded /" + s + "/, expecting /"
+ result + "/");
}
}
public void AssertWildcardQueryEquals(String query, bool lowercase,
String result)
{
AssertWildcardQueryEquals(query, lowercase, result, false);
}
public void AssertWildcardQueryEquals(String query, String result)
{
StandardQueryParser qp = GetParser(null);
Query q = qp.Parse(query, "field");
String s = q.ToString("field");
if (!s.Equals(result, StringComparison.Ordinal))
{
fail("WildcardQuery /" + query + "/ yielded /" + s + "/, expecting /"
+ result + "/");
}
}
public Query GetQueryDOA(String query, Analyzer a)
{
if (a is null)
a = new MockAnalyzer(Random, MockTokenizer.SIMPLE, true);
StandardQueryParser qp = new StandardQueryParser();
qp.Analyzer = (a);
qp.DefaultOperator = (StandardQueryConfigHandler.Operator.AND);
return qp.Parse(query, "field");
}
public void AssertQueryEqualsDOA(String query, Analyzer a, String result)
{
Query q = GetQueryDOA(query, a);
String s = q.ToString("field");
if (!s.Equals(result, StringComparison.Ordinal))
{
fail("Query /" + query + "/ yielded /" + s + "/, expecting /" + result
+ "/");
}
}
[Test]
public void TestConstantScoreAutoRewrite()
{
StandardQueryParser qp = new StandardQueryParser(new MockAnalyzer(Random, MockTokenizer.WHITESPACE, false));
Query q = qp.Parse("foo*bar", "field");
assertTrue(q is WildcardQuery);
assertEquals(MultiTermQuery.CONSTANT_SCORE_AUTO_REWRITE_DEFAULT, ((MultiTermQuery)q).MultiTermRewriteMethod);
q = qp.Parse("foo*", "field");
assertTrue(q is PrefixQuery);
assertEquals(MultiTermQuery.CONSTANT_SCORE_AUTO_REWRITE_DEFAULT, ((MultiTermQuery)q).MultiTermRewriteMethod);
q = qp.Parse("[a TO z]", "field");
assertTrue(q is TermRangeQuery);
assertEquals(MultiTermQuery.CONSTANT_SCORE_AUTO_REWRITE_DEFAULT, ((MultiTermQuery)q).MultiTermRewriteMethod);
}
[Test]
public void TestCJK()
{
// Test Ideographic Space - As wide as a CJK character cell (fullwidth)
// used google to translate the word "term" to japanese -> ??
AssertQueryEquals("term\u3000term\u3000term", null,
"term\u0020term\u0020term");
AssertQueryEqualsAllowLeadingWildcard("??\u3000??\u3000??", null, "??\u0020??\u0020??");
}
//individual CJK chars as terms, like StandardAnalyzer
private sealed class SimpleCJKTokenizer : Tokenizer
{
private ICharTermAttribute termAtt;
public SimpleCJKTokenizer(TextReader input)
: base(input)
{
termAtt = AddAttribute<ICharTermAttribute>();
}
public override bool IncrementToken()
{
int ch = m_input.Read();
if (ch < 0)
return false;
ClearAttributes();
termAtt.SetEmpty().Append((char)ch);
return true;
}
}
private class SimpleCJKAnalyzer : Analyzer
{
protected internal override TokenStreamComponents CreateComponents(String fieldName, TextReader reader)
{
return new TokenStreamComponents(new SimpleCJKTokenizer(reader));
}
}
[Test]
public void TestCJKTerm()
{
// individual CJK chars as terms
SimpleCJKAnalyzer analyzer = new SimpleCJKAnalyzer();
BooleanQuery expected = new BooleanQuery();
expected.Add(new TermQuery(new Term("field", "中")), Occur.SHOULD);
expected.Add(new TermQuery(new Term("field", "国")), Occur.SHOULD);
assertEquals(expected, GetQuery("中国", analyzer));
expected = new BooleanQuery();
expected.Add(new TermQuery(new Term("field", "中")), Occur.MUST);
BooleanQuery inner = new BooleanQuery();
inner.Add(new TermQuery(new Term("field", "中")), Occur.SHOULD);
inner.Add(new TermQuery(new Term("field", "国")), Occur.SHOULD);
expected.Add(inner, Occur.MUST);
assertEquals(expected, GetQuery("中 AND 中国", new SimpleCJKAnalyzer()));
}
[Test]
public void TestCJKBoostedTerm()
{
// individual CJK chars as terms
SimpleCJKAnalyzer analyzer = new SimpleCJKAnalyzer();
BooleanQuery expected = new BooleanQuery();
expected.Boost = (0.5f);
expected.Add(new TermQuery(new Term("field", "中")), Occur.SHOULD);
expected.Add(new TermQuery(new Term("field", "国")), Occur.SHOULD);
assertEquals(expected, GetQuery("中国^0.5", analyzer));
}
[Test]
public void TestCJKPhrase()
{
// individual CJK chars as terms
SimpleCJKAnalyzer analyzer = new SimpleCJKAnalyzer();
PhraseQuery expected = new PhraseQuery();
expected.Add(new Term("field", "中"));
expected.Add(new Term("field", "国"));
assertEquals(expected, GetQuery("\"中国\"", analyzer));
}
[Test]
public void TestCJKBoostedPhrase()
{
// individual CJK chars as terms
SimpleCJKAnalyzer analyzer = new SimpleCJKAnalyzer();
PhraseQuery expected = new PhraseQuery();
expected.Boost = (0.5f);
expected.Add(new Term("field", "中"));
expected.Add(new Term("field", "国"));
assertEquals(expected, GetQuery("\"中国\"^0.5", analyzer));
}
[Test]
public void TestCJKSloppyPhrase()
{
// individual CJK chars as terms
SimpleCJKAnalyzer analyzer = new SimpleCJKAnalyzer();
PhraseQuery expected = new PhraseQuery();
expected.Slop = (3);
expected.Add(new Term("field", "中"));
expected.Add(new Term("field", "国"));
assertEquals(expected, GetQuery("\"中国\"~3", analyzer));
}
[Test]
public void TestSimple()
{
AssertQueryEquals("field=a", null, "a");
AssertQueryEquals("\"term germ\"~2", null, "\"term germ\"~2");
AssertQueryEquals("term term term", null, "term term term");
AssertQueryEquals("t�rm term term", new MockAnalyzer(Random, MockTokenizer.WHITESPACE, false),
"t�rm term term");
AssertQueryEquals("�mlaut", new MockAnalyzer(Random, MockTokenizer.WHITESPACE, false), "�mlaut");
// FIXME: change MockAnalyzer to not extend CharTokenizer for this test
//assertQueryEquals("\"\"", new KeywordAnalyzer(), "");
//assertQueryEquals("foo:\"\"", new KeywordAnalyzer(), "foo:");
AssertQueryEquals("a AND b", null, "+a +b");
AssertQueryEquals("(a AND b)", null, "+a +b");
AssertQueryEquals("c OR (a AND b)", null, "c (+a +b)");
AssertQueryEquals("a AND NOT b", null, "+a -b");
AssertQueryEquals("a AND -b", null, "+a -b");
AssertQueryEquals("a AND !b", null, "+a -b");
AssertQueryEquals("a && b", null, "+a +b");
AssertQueryEquals("a && ! b", null, "+a -b");
AssertQueryEquals("a OR b", null, "a b");
AssertQueryEquals("a || b", null, "a b");
AssertQueryEquals("a OR !b", null, "a -b");
AssertQueryEquals("a OR ! b", null, "a -b");
AssertQueryEquals("a OR -b", null, "a -b");
AssertQueryEquals("+term -term term", null, "+term -term term");
AssertQueryEquals("foo:term AND field:anotherTerm", null,
"+foo:term +anotherterm");
AssertQueryEquals("term AND \"phrase phrase\"", null,
"+term +\"phrase phrase\"");
AssertQueryEquals("\"hello there\"", null, "\"hello there\"");
assertTrue(GetQuery("a AND b", null) is BooleanQuery);
assertTrue(GetQuery("hello", null) is TermQuery);
assertTrue(GetQuery("\"hello there\"", null) is PhraseQuery);
AssertQueryEquals("germ term^2.0", null, "germ term^2.0");
AssertQueryEquals("(term)^2.0", null, "term^2.0");
AssertQueryEquals("(germ term)^2.0", null, "(germ term)^2.0");
AssertQueryEquals("term^2.0", null, "term^2.0");
AssertQueryEquals("term^2", null, "term^2.0");
AssertQueryEquals("\"germ term\"^2.0", null, "\"germ term\"^2.0");
AssertQueryEquals("\"term germ\"^2", null, "\"term germ\"^2.0");
AssertQueryEquals("(foo OR bar) AND (baz OR boo)", null,
"+(foo bar) +(baz boo)");
AssertQueryEquals("((a OR b) AND NOT c) OR d", null, "(+(a b) -c) d");
AssertQueryEquals("+(apple \"steve jobs\") -(foo bar baz)", null,
"+(apple \"steve jobs\") -(foo bar baz)");
AssertQueryEquals("+title:(dog OR cat) -author:\"bob dole\"", null,
"+(title:dog title:cat) -author:\"bob dole\"");
}
[Test]
public void TestPunct()
{
Analyzer a = new MockAnalyzer(Random, MockTokenizer.WHITESPACE, false);
AssertQueryEquals("a&b", a, "a&b");
AssertQueryEquals("a&&b", a, "a&&b");
AssertQueryEquals(".NET", a, ".NET");
}
[Test]
public void TestGroup()
{
AssertQueryEquals("!(a AND b) OR c", null, "-(+a +b) c");
AssertQueryEquals("!(a AND b) AND c", null, "-(+a +b) +c");
AssertQueryEquals("((a AND b) AND c)", null, "+(+a +b) +c");
AssertQueryEquals("(a AND b) AND c", null, "+(+a +b) +c");
AssertQueryEquals("b !(a AND b)", null, "b -(+a +b)");
AssertQueryEquals("(a AND b)^4 OR c", null, "((+a +b)^4.0) c");
}
[Test]
public void TestSlop()
{
AssertQueryEquals("\"term germ\"~2", null, "\"term germ\"~2");
AssertQueryEquals("\"term germ\"~2 flork", null, "\"term germ\"~2 flork");
AssertQueryEquals("\"term\"~2", null, "term");
AssertQueryEquals("\" \"~2 germ", null, "germ");
AssertQueryEquals("\"term germ\"~2^2", null, "\"term germ\"~2^2.0");
}
[Test]
public void TestNumber()
{
// The numbers go away because SimpleAnalzyer ignores them
AssertQueryEquals("3", null, "");
AssertQueryEquals("term 1.0 1 2", null, "term");
AssertQueryEquals("term term1 term2", null, "term term term");
Analyzer a = new MockAnalyzer(Random, MockTokenizer.WHITESPACE, false);
AssertQueryEquals("3", a, "3");
AssertQueryEquals("term 1.0 1 2", a, "term 1.0 1 2");
AssertQueryEquals("term term1 term2", a, "term term1 term2");
}
[Test]
public void TestWildcard()
{
AssertQueryEquals("term*", null, "term*");
AssertQueryEquals("term*^2", null, "term*^2.0");
AssertQueryEquals("term~", null, "term~2");
AssertQueryEquals("term~0.7", null, "term~1");
AssertQueryEquals("term~^3", null, "term~2^3.0");
AssertQueryEquals("term^3~", null, "term~2^3.0");
AssertQueryEquals("term*germ", null, "term*germ");
AssertQueryEquals("term*germ^3", null, "term*germ^3.0");
assertTrue(GetQuery("term*", null) is PrefixQuery);
assertTrue(GetQuery("term*^2", null) is PrefixQuery);
assertTrue(GetQuery("term~", null) is FuzzyQuery);
assertTrue(GetQuery("term~0.7", null) is FuzzyQuery);
FuzzyQuery fq = (FuzzyQuery)GetQuery("term~0.7", null);
assertEquals(1, fq.MaxEdits);
assertEquals(FuzzyQuery.DefaultPrefixLength, fq.PrefixLength);
fq = (FuzzyQuery)GetQuery("term~", null);
assertEquals(2, fq.MaxEdits);
assertEquals(FuzzyQuery.DefaultPrefixLength, fq.PrefixLength);
AssertQueryNodeException("term~1.1"); // value > 1, throws exception
assertTrue(GetQuery("term*germ", null) is WildcardQuery);
/*
* Tests to see that wild card terms are (or are not) properly lower-cased
* with property parser configuration
*/
// First prefix queries:
// by default, convert to lowercase:
AssertWildcardQueryEquals("Term*", true, "term*");
// explicitly set lowercase:
AssertWildcardQueryEquals("term*", true, "term*");
AssertWildcardQueryEquals("Term*", true, "term*");
AssertWildcardQueryEquals("TERM*", true, "term*");
// explicitly disable lowercase conversion:
AssertWildcardQueryEquals("term*", false, "term*");
AssertWildcardQueryEquals("Term*", false, "Term*");
AssertWildcardQueryEquals("TERM*", false, "TERM*");
// Then 'full' wildcard queries:
// by default, convert to lowercase:
AssertWildcardQueryEquals("Te?m", "te?m");
// explicitly set lowercase:
AssertWildcardQueryEquals("te?m", true, "te?m");
AssertWildcardQueryEquals("Te?m", true, "te?m");
AssertWildcardQueryEquals("TE?M", true, "te?m");
AssertWildcardQueryEquals("Te?m*gerM", true, "te?m*germ");
// explicitly disable lowercase conversion:
AssertWildcardQueryEquals("te?m", false, "te?m");
AssertWildcardQueryEquals("Te?m", false, "Te?m");
AssertWildcardQueryEquals("TE?M", false, "TE?M");
AssertWildcardQueryEquals("Te?m*gerM", false, "Te?m*gerM");
// Fuzzy queries:
AssertWildcardQueryEquals("Term~", "term~2");
AssertWildcardQueryEquals("Term~", true, "term~2");
AssertWildcardQueryEquals("Term~", false, "Term~2");
// Range queries:
// TODO: implement this on QueryParser
// Q0002E_INVALID_SYNTAX_CANNOT_PARSE: Syntax Error, cannot parse '[A TO
// C]': Lexical error at line 1, column 1. Encountered: "[" (91), after
// : ""
AssertWildcardQueryEquals("[A TO C]", "[a TO c]");
AssertWildcardQueryEquals("[A TO C]", true, "[a TO c]");
AssertWildcardQueryEquals("[A TO C]", false, "[A TO C]");
// Test suffix queries: first disallow
try
{
AssertWildcardQueryEquals("*Term", true, "*term");
fail();
}
#pragma warning disable 168
catch (QueryNodeException pe)
#pragma warning restore 168
{
// expected exception
}
try
{
AssertWildcardQueryEquals("?Term", true, "?term");
fail();
}
#pragma warning disable 168
catch (QueryNodeException pe)
#pragma warning restore 168
{
// expected exception
}
// Test suffix queries: then allow
AssertWildcardQueryEquals("*Term", true, "*term", true);
AssertWildcardQueryEquals("?Term", true, "?term", true);
}
[Test]
public void TestLeadingWildcardType()
{
StandardQueryParser qp = GetParser(null);
qp.AllowLeadingWildcard = (true);
assertEquals(typeof(WildcardQuery), qp.Parse("t*erm*", "field").GetType());
assertEquals(typeof(WildcardQuery), qp.Parse("?term*", "field").GetType());
assertEquals(typeof(WildcardQuery), qp.Parse("*term*", "field").GetType());
}
[Test]
public void TestQPA()
{
AssertQueryEquals("term term^3.0 term", qpAnalyzer, "term term^3.0 term");
AssertQueryEquals("term stop^3.0 term", qpAnalyzer, "term term");
AssertQueryEquals("term term term", qpAnalyzer, "term term term");
AssertQueryEquals("term +stop term", qpAnalyzer, "term term");
AssertQueryEquals("term -stop term", qpAnalyzer, "term term");
AssertQueryEquals("drop AND (stop) AND roll", qpAnalyzer, "+drop +roll");
AssertQueryEquals("term +(stop) term", qpAnalyzer, "term term");
AssertQueryEquals("term -(stop) term", qpAnalyzer, "term term");
AssertQueryEquals("drop AND stop AND roll", qpAnalyzer, "+drop +roll");
AssertQueryEquals("term phrase term", qpAnalyzer,
"term (phrase1 phrase2) term");
AssertQueryEquals("term AND NOT phrase term", qpAnalyzer,
"+term -(phrase1 phrase2) term");
AssertQueryEquals("stop^3", qpAnalyzer, "");
AssertQueryEquals("stop", qpAnalyzer, "");
AssertQueryEquals("(stop)^3", qpAnalyzer, "");
AssertQueryEquals("((stop))^3", qpAnalyzer, "");
AssertQueryEquals("(stop^3)", qpAnalyzer, "");
AssertQueryEquals("((stop)^3)", qpAnalyzer, "");
AssertQueryEquals("(stop)", qpAnalyzer, "");
AssertQueryEquals("((stop))", qpAnalyzer, "");
assertTrue(GetQuery("term term term", qpAnalyzer) is BooleanQuery);
assertTrue(GetQuery("term +stop", qpAnalyzer) is TermQuery);
}
[Test]
public void TestRange()
{
AssertQueryEquals("[ a TO z]", null, "[a TO z]");
assertEquals(MultiTermQuery.CONSTANT_SCORE_AUTO_REWRITE_DEFAULT, ((TermRangeQuery)GetQuery("[ a TO z]", null)).MultiTermRewriteMethod);
StandardQueryParser qp = new StandardQueryParser();
qp.MultiTermRewriteMethod = (MultiTermQuery.SCORING_BOOLEAN_QUERY_REWRITE);
assertEquals(MultiTermQuery.SCORING_BOOLEAN_QUERY_REWRITE, ((TermRangeQuery)qp.Parse("[ a TO z]", "field")).MultiTermRewriteMethod);
// test open ranges
AssertQueryEquals("[ a TO * ]", null, "[a TO *]");
AssertQueryEquals("[ * TO z ]", null, "[* TO z]");
AssertQueryEquals("[ * TO * ]", null, "[* TO *]");
AssertQueryEquals("field>=a", null, "[a TO *]");
AssertQueryEquals("field>a", null, "{a TO *]");
AssertQueryEquals("field<=a", null, "[* TO a]");
AssertQueryEquals("field<a", null, "[* TO a}");
// mixing exclude and include bounds
AssertQueryEquals("{ a TO z ]", null, "{a TO z]");
AssertQueryEquals("[ a TO z }", null, "[a TO z}");
AssertQueryEquals("{ a TO * ]", null, "{a TO *]");
AssertQueryEquals("[ * TO z }", null, "[* TO z}");
AssertQueryEquals("[ a TO z ]", null, "[a TO z]");
AssertQueryEquals("{ a TO z}", null, "{a TO z}");
AssertQueryEquals("{ a TO z }", null, "{a TO z}");
AssertQueryEquals("{ a TO z }^2.0", null, "{a TO z}^2.0");
AssertQueryEquals("[ a TO z] OR bar", null, "[a TO z] bar");
AssertQueryEquals("[ a TO z] AND bar", null, "+[a TO z] +bar");
AssertQueryEquals("( bar blar { a TO z}) ", null, "bar blar {a TO z}");
AssertQueryEquals("gack ( bar blar { a TO z}) ", null,
"gack (bar blar {a TO z})");
}
/** for testing DateTools support */
private String GetDate(String s, DateResolution resolution)
{
// we use the default Locale since LuceneTestCase randomizes it
//DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, Locale.getDefault());
//return getDate(df.parse(s), resolution);
return GetDate(DateTime.ParseExact(s, "d", null), resolution);
}
/** for testing DateTools support */
private String GetDate(DateTime d, DateResolution resolution)
{
return DateTools.DateToString(d, resolution);
}
private String EscapeDateString(String s)
{
if (s.Contains(" "))
{
return "\"" + s + "\"";
}
else
{
return s;
}
}
private String GetLocalizedDate(int year, int month, int day)
{
DateTime date = new GregorianCalendar().ToDateTime(year, month, day, 23, 59, 59, 999);
date = TimeZoneInfo.ConvertTime(date, TimeZoneInfo.Local);
return date.ToString("d"); //.ToShortDateString();
//// we use the default Locale/TZ since LuceneTestCase randomizes it
//DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, Locale.getDefault());
//Calendar calendar = new GregorianCalendar(TimeZone.getDefault(), Locale.getDefault());
//calendar.clear();
//calendar.set(year, month, day);
//calendar.set(Calendar.HOUR_OF_DAY, 23);
//calendar.set(Calendar.MINUTE, 59);
//calendar.set(Calendar.SECOND, 59);
//calendar.set(Calendar.MILLISECOND, 999);
//return df.format(calendar.getTime());
}
[Test]
public void TestDateRange()
{
String startDate = GetLocalizedDate(2002, 1, 1);
String endDate = GetLocalizedDate(2002, 1, 4);
//// we use the default Locale/TZ since LuceneTestCase randomizes it
//Calendar endDateExpected = new GregorianCalendar(TimeZone.getDefault(), Locale.getDefault());
//endDateExpected.clear();
//endDateExpected.set(2002, 1, 4, 23, 59, 59);
//endDateExpected.set(Calendar.MILLISECOND, 999);
// we use the default Locale/TZ since LuceneTestCase randomizes it
DateTime endDateExpected = new GregorianCalendar().ToDateTime(2002, 1, 4, 23, 59, 59, 999);
String defaultField = "default";
String monthField = "month";
String hourField = "hour";
StandardQueryParser qp = new StandardQueryParser();
IDictionary<string, DateResolution> dateRes = new JCG.Dictionary<string, DateResolution>();
// set a field specific date resolution
dateRes[monthField] = DateResolution.MONTH;
#pragma warning disable 612, 618
qp.SetDateResolution(dateRes);
#pragma warning restore 612, 618
// set default date resolution to MILLISECOND
qp.SetDateResolution(DateResolution.MILLISECOND);
// set second field specific date resolution
dateRes[hourField] = DateResolution.HOUR;
#pragma warning disable 612, 618
qp.SetDateResolution(dateRes);
#pragma warning restore 612, 618
// for this field no field specific date resolution has been set,
// so verify if the default resolution is used
AssertDateRangeQueryEquals(qp, defaultField, startDate, endDate,
endDateExpected/*.getTime()*/, DateResolution.MILLISECOND);
// verify if field specific date resolutions are used for these two
// fields
AssertDateRangeQueryEquals(qp, monthField, startDate, endDate,
endDateExpected/*.getTime()*/, DateResolution.MONTH);
AssertDateRangeQueryEquals(qp, hourField, startDate, endDate,
endDateExpected/*.getTime()*/, DateResolution.HOUR);
}
public void AssertDateRangeQueryEquals(StandardQueryParser qp,
String field, String startDate, String endDate, DateTime endDateInclusive,
DateResolution resolution)
{
AssertQueryEquals(qp, field, field + ":[" + EscapeDateString(startDate) + " TO " + EscapeDateString(endDate)
+ "]", "[" + GetDate(startDate, resolution) + " TO "
+ GetDate(endDateInclusive, resolution) + "]");
AssertQueryEquals(qp, field, field + ":{" + EscapeDateString(startDate) + " TO " + EscapeDateString(endDate)
+ "}", "{" + GetDate(startDate, resolution) + " TO "
+ GetDate(endDate, resolution) + "}");
}
[Test]
public void TestEscaped()
{
Analyzer a = new MockAnalyzer(Random, MockTokenizer.WHITESPACE, false);
/*
* assertQueryEquals("\\[brackets", a, "\\[brackets");
* assertQueryEquals("\\[brackets", null, "brackets");
* assertQueryEquals("\\\\", a, "\\\\"); assertQueryEquals("\\+blah", a,
* "\\+blah"); assertQueryEquals("\\(blah", a, "\\(blah");
*
* assertQueryEquals("\\-blah", a, "\\-blah"); assertQueryEquals("\\!blah",
* a, "\\!blah"); assertQueryEquals("\\{blah", a, "\\{blah");
* assertQueryEquals("\\}blah", a, "\\}blah"); assertQueryEquals("\\:blah",
* a, "\\:blah"); assertQueryEquals("\\^blah", a, "\\^blah");
* assertQueryEquals("\\[blah", a, "\\[blah"); assertQueryEquals("\\]blah",
* a, "\\]blah"); assertQueryEquals("\\\"blah", a, "\\\"blah");
* assertQueryEquals("\\(blah", a, "\\(blah"); assertQueryEquals("\\)blah",
* a, "\\)blah"); assertQueryEquals("\\~blah", a, "\\~blah");
* assertQueryEquals("\\*blah", a, "\\*blah"); assertQueryEquals("\\?blah",
* a, "\\?blah"); //assertQueryEquals("foo \\&\\& bar", a,
* "foo \\&\\& bar"); //assertQueryEquals("foo \\|| bar", a,
* "foo \\|| bar"); //assertQueryEquals("foo \\AND bar", a,
* "foo \\AND bar");
*/
AssertQueryEquals("\\*", a, "*");
AssertQueryEquals("\\a", a, "a");
AssertQueryEquals("a\\-b:c", a, "a-b:c");
AssertQueryEquals("a\\+b:c", a, "a+b:c");
AssertQueryEquals("a\\:b:c", a, "a:b:c");
AssertQueryEquals("a\\\\b:c", a, "a\\b:c");
AssertQueryEquals("a:b\\-c", a, "a:b-c");
AssertQueryEquals("a:b\\+c", a, "a:b+c");
AssertQueryEquals("a:b\\:c", a, "a:b:c");
AssertQueryEquals("a:b\\\\c", a, "a:b\\c");
AssertQueryEquals("a:b\\-c*", a, "a:b-c*");
AssertQueryEquals("a:b\\+c*", a, "a:b+c*");
AssertQueryEquals("a:b\\:c*", a, "a:b:c*");
AssertQueryEquals("a:b\\\\c*", a, "a:b\\c*");
AssertQueryEquals("a:b\\-?c", a, "a:b-?c");
AssertQueryEquals("a:b\\+?c", a, "a:b+?c");
AssertQueryEquals("a:b\\:?c", a, "a:b:?c");
AssertQueryEquals("a:b\\\\?c", a, "a:b\\?c");
AssertQueryEquals("a:b\\-c~", a, "a:b-c~2");
AssertQueryEquals("a:b\\+c~", a, "a:b+c~2");
AssertQueryEquals("a:b\\:c~", a, "a:b:c~2");
AssertQueryEquals("a:b\\\\c~", a, "a:b\\c~2");
// TODO: implement Range queries on QueryParser
AssertQueryEquals("[ a\\- TO a\\+ ]", null, "[a- TO a+]");
AssertQueryEquals("[ a\\: TO a\\~ ]", null, "[a: TO a~]");
AssertQueryEquals("[ a\\\\ TO a\\* ]", null, "[a\\ TO a*]");
AssertQueryEquals(
"[\"c\\:\\\\temp\\\\\\~foo0.txt\" TO \"c\\:\\\\temp\\\\\\~foo9.txt\"]",
a, "[c:\\temp\\~foo0.txt TO c:\\temp\\~foo9.txt]");
AssertQueryEquals("a\\\\\\+b", a, "a\\+b");
AssertQueryEquals("a \\\"b c\\\" d", a, "a \"b c\" d");
AssertQueryEquals("\"a \\\"b c\\\" d\"", a, "\"a \"b c\" d\"");
AssertQueryEquals("\"a \\+b c d\"", a, "\"a +b c d\"");
AssertQueryEquals("c\\:\\\\temp\\\\\\~foo.txt", a, "c:\\temp\\~foo.txt");
AssertQueryNodeException("XY\\"); // there must be a character after the
// escape char
// test unicode escaping
AssertQueryEquals("a\\u0062c", a, "abc");
AssertQueryEquals("XY\\u005a", a, "XYZ");
AssertQueryEquals("XY\\u005A", a, "XYZ");
AssertQueryEquals("\"a \\\\\\u0028\\u0062\\\" c\"", a, "\"a \\(b\" c\"");
AssertQueryNodeException("XY\\u005G"); // test non-hex character in escaped
// unicode sequence
AssertQueryNodeException("XY\\u005"); // test incomplete escaped unicode
// sequence
// Tests bug LUCENE-800
AssertQueryEquals("(item:\\\\ item:ABCD\\\\)", a, "item:\\ item:ABCD\\");
AssertQueryNodeException("(item:\\\\ item:ABCD\\\\))"); // unmatched closing
// paranthesis
AssertQueryEquals("\\*", a, "*");
AssertQueryEquals("\\\\", a, "\\"); // escaped backslash
AssertQueryNodeException("\\"); // a backslash must always be escaped
// LUCENE-1189
AssertQueryEquals("(\"a\\\\\") or (\"b\")", a, "a\\ or b");
}
[Test]
public void TestQueryStringEscaping()
{
Analyzer a = new MockAnalyzer(Random, MockTokenizer.WHITESPACE, false);
AssertEscapedQueryEquals("a-b:c", a, "a\\-b\\:c");
AssertEscapedQueryEquals("a+b:c", a, "a\\+b\\:c");
AssertEscapedQueryEquals("a:b:c", a, "a\\:b\\:c");
AssertEscapedQueryEquals("a\\b:c", a, "a\\\\b\\:c");
AssertEscapedQueryEquals("a:b-c", a, "a\\:b\\-c");
AssertEscapedQueryEquals("a:b+c", a, "a\\:b\\+c");
AssertEscapedQueryEquals("a:b:c", a, "a\\:b\\:c");
AssertEscapedQueryEquals("a:b\\c", a, "a\\:b\\\\c");
AssertEscapedQueryEquals("a:b-c*", a, "a\\:b\\-c\\*");
AssertEscapedQueryEquals("a:b+c*", a, "a\\:b\\+c\\*");
AssertEscapedQueryEquals("a:b:c*", a, "a\\:b\\:c\\*");
AssertEscapedQueryEquals("a:b\\\\c*", a, "a\\:b\\\\\\\\c\\*");
AssertEscapedQueryEquals("a:b-?c", a, "a\\:b\\-\\?c");
AssertEscapedQueryEquals("a:b+?c", a, "a\\:b\\+\\?c");
AssertEscapedQueryEquals("a:b:?c", a, "a\\:b\\:\\?c");
AssertEscapedQueryEquals("a:b?c", a, "a\\:b\\?c");
AssertEscapedQueryEquals("a:b-c~", a, "a\\:b\\-c\\~");
AssertEscapedQueryEquals("a:b+c~", a, "a\\:b\\+c\\~");
AssertEscapedQueryEquals("a:b:c~", a, "a\\:b\\:c\\~");
AssertEscapedQueryEquals("a:b\\c~", a, "a\\:b\\\\c\\~");
AssertEscapedQueryEquals("[ a - TO a+ ]", null, "\\[ a \\- TO a\\+ \\]");
AssertEscapedQueryEquals("[ a : TO a~ ]", null, "\\[ a \\: TO a\\~ \\]");
AssertEscapedQueryEquals("[ a\\ TO a* ]", null, "\\[ a\\\\ TO a\\* \\]");
// LUCENE-881
AssertEscapedQueryEquals("|| abc ||", a, "\\|\\| abc \\|\\|");
AssertEscapedQueryEquals("&& abc &&", a, "\\&\\& abc \\&\\&");
}
[Test]
[Ignore("flexible queryparser shouldn't escape wildcard terms")]
public void TestEscapedWildcard()
{
StandardQueryParser qp = new StandardQueryParser();
qp.Analyzer = (new MockAnalyzer(Random, MockTokenizer.WHITESPACE, false));
WildcardQuery q = new WildcardQuery(new Term("field", "foo\\?ba?r"));
assertEquals(q, qp.Parse("foo\\?ba?r", "field"));
}
[Test]
public void TestTabNewlineCarriageReturn()
{
AssertQueryEqualsDOA("+weltbank +worlbank", null, "+weltbank +worlbank");