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.solr.SolrTestCaseJ4;
21
import org.apache.solr.common.util.NamedList;
22
import org.apache.solr.core.SolrCore;
23
import org.apache.solr.search.SolrIndexSearcher;
24
import org.apache.solr.util.RefCounted;
25
import org.apache.lucene.analysis.Token;
26
import org.junit.AfterClass;
27
import org.junit.BeforeClass;
28
import org.junit.Test;
31
import java.util.Date;
33
import java.util.Collection;
39
public class FileBasedSpellCheckerTest extends SolrTestCaseJ4 {
41
private static SpellingQueryConverter queryConverter;
44
public static void beforeClass() throws Exception {
45
initCore("solrconfig.xml","schema.xml");
46
//Index something with a title
47
assertNull(h.validateUpdate(adoc("id", "0", "teststop", "This is a title")));
48
assertNull(h.validateUpdate(adoc("id", "1", "teststop", "The quick reb fox jumped over the lazy brown dogs.")));
49
assertNull(h.validateUpdate(adoc("id", "2", "teststop", "This is a Solr")));
50
assertNull(h.validateUpdate(adoc("id", "3", "teststop", "solr foo")));
51
assertNull(h.validateUpdate(commit()));
52
queryConverter = new SimpleQueryConverter();
53
queryConverter.init(new NamedList());
57
public static void afterClass() throws Exception {
58
queryConverter = null;
62
public void test() throws Exception {
63
FileBasedSpellChecker checker = new FileBasedSpellChecker();
64
NamedList spellchecker = new NamedList();
65
spellchecker.add("classname", FileBasedSpellChecker.class.getName());
67
spellchecker.add(SolrSpellChecker.DICTIONARY_NAME, "external");
68
spellchecker.add(AbstractLuceneSpellChecker.LOCATION, "spellings.txt");
69
spellchecker.add(IndexBasedSpellChecker.FIELD, "teststop");
70
spellchecker.add(FileBasedSpellChecker.SOURCE_FILE_CHAR_ENCODING, "UTF-8");
71
File indexDir = new File(TEMP_DIR, "spellingIdx" + new Date().getTime());
73
spellchecker.add(FileBasedSpellChecker.INDEX_DIR, indexDir.getAbsolutePath());
74
SolrCore core = h.getCore();
75
String dictName = checker.init(spellchecker, core);
76
assertTrue(dictName + " is not equal to " + "external", dictName.equals("external") == true);
77
checker.build(core, null);
79
RefCounted<SolrIndexSearcher> searcher = core.getSearcher();
80
Collection<Token> tokens = queryConverter.convert("fob");
81
SpellingOptions spellOpts = new SpellingOptions(tokens, searcher.get().getReader());
82
SpellingResult result = checker.getSuggestions(spellOpts);
83
assertTrue("result is null and it shouldn't be", result != null);
84
Map<String, Integer> suggestions = result.get(tokens.iterator().next());
85
Map.Entry<String, Integer> entry = suggestions.entrySet().iterator().next();
86
assertTrue(entry.getKey() + " is not equal to " + "foo", entry.getKey().equals("foo") == true);
87
assertTrue(entry.getValue() + " does not equal: " + SpellingResult.NO_FREQUENCY_INFO, entry.getValue() == SpellingResult.NO_FREQUENCY_INFO);
89
spellOpts.tokens = queryConverter.convert("super");
90
result = checker.getSuggestions(spellOpts);
91
assertTrue("result is null and it shouldn't be", result != null);
92
suggestions = result.get(tokens.iterator().next());
93
assertTrue("suggestions is not null and it should be", suggestions == null);
99
public void testFieldType() throws Exception {
100
FileBasedSpellChecker checker = new FileBasedSpellChecker();
101
NamedList spellchecker = new NamedList();
102
spellchecker.add("classname", FileBasedSpellChecker.class.getName());
103
spellchecker.add(SolrSpellChecker.DICTIONARY_NAME, "external");
104
spellchecker.add(AbstractLuceneSpellChecker.LOCATION, "spellings.txt");
105
spellchecker.add(IndexBasedSpellChecker.FIELD, "teststop");
106
spellchecker.add(FileBasedSpellChecker.SOURCE_FILE_CHAR_ENCODING, "UTF-8");
107
File indexDir = new File(TEMP_DIR, "spellingIdx" + new Date().getTime());
109
spellchecker.add(FileBasedSpellChecker.INDEX_DIR, indexDir.getAbsolutePath());
110
spellchecker.add(FileBasedSpellChecker.FIELD_TYPE, "teststop");
111
spellchecker.add(AbstractLuceneSpellChecker.SPELLCHECKER_ARG_NAME, spellchecker);
112
SolrCore core = h.getCore();
113
String dictName = checker.init(spellchecker, core);
114
assertTrue(dictName + " is not equal to " + "external", dictName.equals("external") == true);
115
checker.build(core, null);
117
RefCounted<SolrIndexSearcher> searcher = core.getSearcher();
118
Collection<Token> tokens = queryConverter.convert("Solar");
120
SpellingOptions spellOpts = new SpellingOptions(tokens, searcher.get().getReader());
121
SpellingResult result = checker.getSuggestions(spellOpts);
122
assertTrue("result is null and it shouldn't be", result != null);
123
//should be lowercased, b/c we are using a lowercasing analyzer
124
Map<String, Integer> suggestions = result.get(tokens.iterator().next());
125
assertTrue("suggestions Size: " + suggestions.size() + " is not: " + 1, suggestions.size() == 1);
126
Map.Entry<String, Integer> entry = suggestions.entrySet().iterator().next();
127
assertTrue(entry.getKey() + " is not equal to " + "solr", entry.getKey().equals("solr") == true);
128
assertTrue(entry.getValue() + " does not equal: " + SpellingResult.NO_FREQUENCY_INFO, entry.getValue() == SpellingResult.NO_FREQUENCY_INFO);
130
//test something not in the spell checker
131
spellOpts.tokens = queryConverter.convert("super");
132
result = checker.getSuggestions(spellOpts);
133
assertTrue("result is null and it shouldn't be", result != null);
134
suggestions = result.get(tokens.iterator().next());
135
assertTrue("suggestions is not null and it should be", suggestions == null);
140
* No indexDir location set
144
public void testRAMDirectory() throws Exception {
145
FileBasedSpellChecker checker = new FileBasedSpellChecker();
146
NamedList spellchecker = new NamedList();
147
spellchecker.add("classname", FileBasedSpellChecker.class.getName());
149
spellchecker.add(SolrSpellChecker.DICTIONARY_NAME, "external");
150
spellchecker.add(AbstractLuceneSpellChecker.LOCATION, "spellings.txt");
151
spellchecker.add(FileBasedSpellChecker.SOURCE_FILE_CHAR_ENCODING, "UTF-8");
152
spellchecker.add(IndexBasedSpellChecker.FIELD, "teststop");
153
spellchecker.add(FileBasedSpellChecker.FIELD_TYPE, "teststop");
154
spellchecker.add(AbstractLuceneSpellChecker.SPELLCHECKER_ARG_NAME, spellchecker);
156
SolrCore core = h.getCore();
157
String dictName = checker.init(spellchecker, core);
158
assertTrue(dictName + " is not equal to " + "external", dictName.equals("external") == true);
159
checker.build(core, null);
161
RefCounted<SolrIndexSearcher> searcher = core.getSearcher();
162
Collection<Token> tokens = queryConverter.convert("solar");
163
SpellingOptions spellOpts = new SpellingOptions(tokens, searcher.get().getReader());
164
SpellingResult result = checker.getSuggestions(spellOpts);
165
assertTrue("result is null and it shouldn't be", result != null);
166
//should be lowercased, b/c we are using a lowercasing analyzer
167
Map<String, Integer> suggestions = result.get(tokens.iterator().next());
168
assertTrue("suggestions Size: " + suggestions.size() + " is not: " + 1, suggestions.size() == 1);
169
Map.Entry<String, Integer> entry = suggestions.entrySet().iterator().next();
170
assertTrue(entry.getKey() + " is not equal to " + "solr", entry.getKey().equals("solr") == true);
171
assertTrue(entry.getValue() + " does not equal: " + SpellingResult.NO_FREQUENCY_INFO, entry.getValue() == SpellingResult.NO_FREQUENCY_INFO);
174
spellOpts.tokens = queryConverter.convert("super");
175
result = checker.getSuggestions(spellOpts);
176
assertTrue("result is null and it shouldn't be", result != null);
177
suggestions = result.get(spellOpts.tokens.iterator().next());
178
assertTrue("suggestions is not null and it should be", suggestions == null);