2
* Licensed to the Apache Software Foundation (ASF) under one or more
3
* contributor license agreements. See the NOTICE file distributed with
4
* this work for additional information regarding copyright ownership.
5
* The ASF licenses this file to You under the Apache License, Version 2.0
6
* (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
18
package org.apache.solr.spelling;
20
import org.apache.lucene.analysis.Token;
21
import org.apache.lucene.analysis.WhitespaceAnalyzer;
22
import org.apache.lucene.util.LuceneTestCase;
23
import org.apache.solr.common.util.NamedList;
24
import org.junit.Test;
26
import java.util.Collection;
30
* Test for SpellingQueryConverter
32
* @version $Id: SpellingQueryConverterTest.java 1040505 2010-11-30 13:06:02Z rmuir $
35
public class SpellingQueryConverterTest extends LuceneTestCase {
38
public void test() throws Exception {
39
SpellingQueryConverter converter = new SpellingQueryConverter();
40
converter.init(new NamedList());
41
converter.setAnalyzer(new WhitespaceAnalyzer(TEST_VERSION_CURRENT));
42
Collection<Token> tokens = converter.convert("field:foo");
43
assertTrue("tokens is null and it shouldn't be", tokens != null);
44
assertTrue("tokens Size: " + tokens.size() + " is not: " + 1, tokens.size() == 1);
48
public void testSpecialChars() {
49
SpellingQueryConverter converter = new SpellingQueryConverter();
50
converter.init(new NamedList());
51
converter.setAnalyzer(new WhitespaceAnalyzer(TEST_VERSION_CURRENT));
52
String original = "field_with_underscore:value_with_underscore";
53
Collection<Token> tokens = converter.convert(original);
54
assertTrue("tokens is null and it shouldn't be", tokens != null);
55
assertEquals("tokens Size: " + tokens.size() + " is not 1", 1, tokens.size());
56
assertTrue("Token offsets do not match", isOffsetCorrect(original, tokens));
58
original = "field_with_digits123:value_with_digits123";
59
tokens = converter.convert(original);
60
assertTrue("tokens is null and it shouldn't be", tokens != null);
61
assertEquals("tokens Size: " + tokens.size() + " is not 1", 1, tokens.size());
62
assertTrue("Token offsets do not match", isOffsetCorrect(original, tokens));
64
original = "field-with-hyphens:value-with-hyphens";
65
tokens = converter.convert(original);
66
assertTrue("tokens is null and it shouldn't be", tokens != null);
67
assertEquals("tokens Size: " + tokens.size() + " is not 1", 1, tokens.size());
68
assertTrue("Token offsets do not match", isOffsetCorrect(original, tokens));
70
// mix 'em up and add some to the value
71
// original = "field_with-123s:value_,.|with-hyphens";
72
// tokens = converter.convert(original);
73
// assertTrue("tokens is null and it shouldn't be", tokens != null);
74
// assertEquals("tokens Size: " + tokens.size() + " is not 1", 1, tokens.size());
75
// assertTrue("Token offsets do not match", isOffsetCorrect(original, tokens));
77
original = "foo:bar^5.0";
78
tokens = converter.convert(original);
79
assertTrue("tokens is null and it shouldn't be", tokens != null);
80
assertEquals("tokens Size: " + tokens.size() + " is not 1", 1, tokens.size());
81
assertTrue("Token offsets do not match", isOffsetCorrect(original, tokens));
84
private boolean isOffsetCorrect(String s, Collection<Token> tokens) {
85
for (Token token : tokens) {
86
int start = token.startOffset();
87
int end = token.endOffset();
88
if (!s.substring(start, end).equals(token.toString())) return false;
94
public void testUnicode() {
95
SpellingQueryConverter converter = new SpellingQueryConverter();
96
converter.init(new NamedList());
97
converter.setAnalyzer(new WhitespaceAnalyzer(TEST_VERSION_CURRENT));
100
Collection<Token> tokens = converter.convert("text_field:我购买了道具和服装。");
101
assertTrue("tokens is null and it shouldn't be", tokens != null);
102
assertEquals("tokens Size: " + tokens.size() + " is not 1", 1, tokens.size());
104
tokens = converter.convert("text_购field:我购买了道具和服装。");
105
assertTrue("tokens is null and it shouldn't be", tokens != null);
106
assertEquals("tokens Size: " + tokens.size() + " is not 1", 1, tokens.size());
108
tokens = converter.convert("text_field:我购xyz买了道具和服装。");
109
assertTrue("tokens is null and it shouldn't be", tokens != null);
110
assertEquals("tokens Size: " + tokens.size() + " is not 1", 1, tokens.size());
114
public void testMultipleClauses() {
115
SpellingQueryConverter converter = new SpellingQueryConverter();
116
converter.init(new NamedList());
117
converter.setAnalyzer(new WhitespaceAnalyzer(TEST_VERSION_CURRENT));
119
// two field:value pairs should give two tokens
120
Collection<Token> tokens = converter.convert("买text_field:我购买了道具和服装。 field2:bar");
121
assertTrue("tokens is null and it shouldn't be", tokens != null);
122
assertEquals("tokens Size: " + tokens.size() + " is not 2", 2, tokens.size());
124
// a field:value pair and a search term should give two tokens
125
tokens = converter.convert("text_field:我购买了道具和服装。 bar");
126
assertTrue("tokens is null and it shouldn't be", tokens != null);
127
assertEquals("tokens Size: " + tokens.size() + " is not 2", 2, tokens.size());