1
package org.apache.solr.analysis;
3
* Licensed to the Apache Software Foundation (ASF) under one or more
4
* contributor license agreements. See the NOTICE file distributed with
5
* this work for additional information regarding copyright ownership.
6
* The ASF licenses this file to You under the Apache License, Version 2.0
7
* (the "License"); you may not use this file except in compliance with
8
* the License. You may obtain a copy of the License at
10
* http://www.apache.org/licenses/LICENSE-2.0
12
* Unless required by applicable law or agreed to in writing, software
13
* distributed under the License is distributed on an "AS IS" BASIS,
14
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
* See the License for the specific language governing permissions and
16
* limitations under the License.
20
import java.io.IOException;
21
import java.io.StringReader;
23
import java.util.HashMap;
26
import org.apache.lucene.analysis.Analyzer;
27
import org.apache.lucene.analysis.MockTokenizer;
28
import org.apache.lucene.analysis.TokenStream;
29
import org.apache.lucene.queryParser.ParseException;
30
import org.apache.lucene.search.Query;
31
import org.apache.solr.SolrTestCaseJ4;
32
import org.apache.solr.schema.IndexSchema;
33
import org.apache.solr.search.SolrQueryParser;
34
import org.junit.Before;
35
import org.junit.BeforeClass;
36
import org.junit.Test;
38
import static org.apache.solr.analysis.BaseTokenTestCase.*;
40
public class TestReversedWildcardFilterFactory extends SolrTestCaseJ4 {
41
Map<String,String> args = new HashMap<String, String>();
42
ReversedWildcardFilterFactory factory = new ReversedWildcardFilterFactory();
46
public static void beforeClass() throws Exception {
47
initCore("solrconfig.xml","schema-reversed.xml");
52
public void setUp() throws Exception {
54
schema = new IndexSchema(solrConfig, getSchemaFile(), null);
58
public void testReversedTokens() throws IOException {
59
String text = "simple text";
60
args.put("withOriginal", "true");
62
TokenStream input = factory.create(new MockTokenizer(new StringReader(text), MockTokenizer.WHITESPACE, false));
63
assertTokenStreamContents(input,
64
new String[] { "\u0001elpmis", "simple", "\u0001txet", "text" },
65
new int[] { 1, 0, 1, 0 });
67
// now without original tokens
68
args.put("withOriginal", "false");
70
input = factory.create(new MockTokenizer(new StringReader(text), MockTokenizer.WHITESPACE, false));
71
assertTokenStreamContents(input,
72
new String[] { "\u0001elpmis", "\u0001txet" },
77
public void testIndexingAnalysis() throws Exception {
78
Analyzer a = schema.getAnalyzer();
79
String text = "one two three si\uD834\uDD1Ex";
82
TokenStream input = a.tokenStream("one", new StringReader(text));
83
assertTokenStreamContents(input,
84
new String[] { "\u0001eno", "one", "\u0001owt", "two",
85
"\u0001eerht", "three", "\u0001x\uD834\uDD1Eis", "si\uD834\uDD1Ex" },
86
new int[] { 0, 0, 4, 4, 8, 8, 14, 14 },
87
new int[] { 3, 3, 7, 7, 13, 13, 19, 19 },
88
new int[] { 1, 0, 1, 0, 1, 0, 1, 0 }
91
input = a.tokenStream("two", new StringReader(text));
92
assertTokenStreamContents(input,
93
new String[] { "\u0001eno", "\u0001owt",
94
"\u0001eerht", "\u0001x\uD834\uDD1Eis" },
95
new int[] { 0, 4, 8, 14 },
96
new int[] { 3, 7, 13, 19 },
97
new int[] { 1, 1, 1, 1 }
100
input = a.tokenStream("three", new StringReader(text));
101
assertTokenStreamContents(input,
102
new String[] { "one", "two", "three", "si\uD834\uDD1Ex" },
103
new int[] { 0, 4, 8, 14 },
104
new int[] { 3, 7, 13, 19 }
109
public void testQueryParsing() throws IOException, ParseException {
111
SolrQueryParser parserOne = new SolrQueryParser(schema, "one");
112
assertTrue(parserOne.getAllowLeadingWildcard());
113
SolrQueryParser parserTwo = new SolrQueryParser(schema, "two");
114
assertTrue(parserTwo.getAllowLeadingWildcard());
115
SolrQueryParser parserThree = new SolrQueryParser(schema, "three");
116
// XXX note: this should be false, but for now we return true for any field,
117
// XXX if at least one field uses the reversing
118
assertTrue(parserThree.getAllowLeadingWildcard());
119
String text = "one +two *hree f*ur fiv* *si\uD834\uDD1Ex";
120
String expectedOne = "one:one +one:two one:\u0001eerh* one:\u0001ru*f one:fiv* one:\u0001x\uD834\uDD1Eis*";
121
String expectedTwo = "two:one +two:two two:\u0001eerh* two:\u0001ru*f two:fiv* two:\u0001x\uD834\uDD1Eis*";
122
String expectedThree = "three:one +three:two three:*hree three:f*ur three:fiv* three:*si\uD834\uDD1Ex";
123
Query q = parserOne.parse(text);
124
assertEquals(expectedOne, q.toString());
125
q = parserTwo.parse(text);
126
assertEquals(expectedTwo, q.toString());
127
q = parserThree.parse(text);
128
assertEquals(expectedThree, q.toString());
129
// test conditional reversal
130
String condText = "*hree t*ree th*ee thr*e ?hree t?ree th?ee th?*ee " +
131
"short*token ver*longtoken";
132
String expected = "two:\u0001eerh* two:\u0001eer*t two:\u0001ee*ht " +
134
"two:\u0001eerh? two:\u0001eer?t " +
138
"two:\u0001nekotgnol*rev";
139
q = parserTwo.parse(condText);
140
assertEquals(expected, q.toString());