~slub.team/goobi-indexserver/3.x

« back to all changes in this revision

Viewing changes to solr/core/src/test/org/apache/solr/spelling/FileBasedSpellCheckerTest.java

  • Committer: Sebastian Meyer
  • Date: 2012-08-03 09:12:40 UTC
  • Revision ID: sebastian.meyer@slub-dresden.de-20120803091240-x6861b0vabq1xror
Remove Lucene and Solr source code and add patches instead
Fix Bug #985487: Auto-suggestion for the search interface

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/**
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
8
 
 *
9
 
 *     http://www.apache.org/licenses/LICENSE-2.0
10
 
 *
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.
16
 
 */
17
 
 
18
 
package org.apache.solr.spelling;
19
 
 
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;
29
 
 
30
 
import java.io.File;
31
 
import java.util.Date;
32
 
import java.util.Map;
33
 
import java.util.Collection;
34
 
 
35
 
/**
36
 
 *
37
 
 * @since solr 1.3
38
 
 **/
39
 
public class FileBasedSpellCheckerTest extends SolrTestCaseJ4 {
40
 
 
41
 
  private static SpellingQueryConverter queryConverter;
42
 
 
43
 
  @BeforeClass
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());
54
 
  }
55
 
  
56
 
  @AfterClass
57
 
  public static void afterClass() throws Exception {
58
 
    queryConverter = null;
59
 
  }
60
 
 
61
 
  @Test
62
 
  public void test() throws Exception {
63
 
    FileBasedSpellChecker checker = new FileBasedSpellChecker();
64
 
    NamedList spellchecker = new NamedList();
65
 
    spellchecker.add("classname", FileBasedSpellChecker.class.getName());
66
 
 
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());
72
 
    indexDir.mkdirs();
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);
78
 
 
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);
88
 
 
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);
94
 
    searcher.decref();
95
 
 
96
 
  }
97
 
 
98
 
  @Test
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());
108
 
    indexDir.mkdirs();
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);
116
 
 
117
 
    RefCounted<SolrIndexSearcher> searcher = core.getSearcher();
118
 
    Collection<Token> tokens = queryConverter.convert("Solar");
119
 
 
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);
129
 
 
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);
136
 
    searcher.decref();
137
 
  }
138
 
 
139
 
  /**
140
 
   * No indexDir location set
141
 
   * @throws Exception
142
 
   */
143
 
  @Test
144
 
  public void testRAMDirectory() throws Exception {
145
 
    FileBasedSpellChecker checker = new FileBasedSpellChecker();
146
 
    NamedList spellchecker = new NamedList();
147
 
    spellchecker.add("classname", FileBasedSpellChecker.class.getName());
148
 
 
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);
155
 
 
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);
160
 
 
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);
172
 
 
173
 
 
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);
179
 
    searcher.decref();
180
 
  }
181
 
}