1
package org.apache.solr.spelling;
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.
19
import org.apache.lucene.index.IndexReader;
20
import org.apache.lucene.index.IndexWriterConfig;
21
import org.apache.lucene.store.FSDirectory;
22
import org.apache.solr.common.util.NamedList;
23
import org.apache.solr.core.SolrCore;
24
import org.apache.solr.search.SolrIndexSearcher;
25
import org.apache.lucene.search.spell.HighFrequencyDictionary;
28
import java.io.IOException;
29
import org.slf4j.Logger;
30
import org.slf4j.LoggerFactory;
35
* A spell checker implementation that loads words from Solr as well as arbitary Lucene indices.
39
* Refer to <a href="http://wiki.apache.org/solr/SpellCheckComponent">SpellCheckComponent</a>
45
public class IndexBasedSpellChecker extends AbstractLuceneSpellChecker {
46
private static final Logger log = LoggerFactory.getLogger(IndexBasedSpellChecker.class);
48
public static final String THRESHOLD_TOKEN_FREQUENCY = "thresholdTokenFrequency";
50
protected float threshold;
51
protected IndexReader reader;
54
public String init(NamedList config, SolrCore core) {
55
super.init(config, core);
56
threshold = config.get(THRESHOLD_TOKEN_FREQUENCY) == null ? 0.0f
57
: (Float) config.get(THRESHOLD_TOKEN_FREQUENCY);
62
private void initSourceReader() {
63
if (sourceLocation != null) {
65
FSDirectory luceneIndexDir = FSDirectory.open(new File(sourceLocation));
66
this.reader = IndexReader.open(luceneIndexDir);
67
} catch (IOException e) {
68
throw new RuntimeException(e);
74
public void build(SolrCore core, SolrIndexSearcher searcher) {
75
IndexReader reader = null;
77
if (sourceLocation == null) {
78
// Load from Solr's index
79
reader = searcher.getReader();
81
// Load from Lucene index at given sourceLocation
85
// Create the dictionary
86
dictionary = new HighFrequencyDictionary(reader, field,
88
spellChecker.clearIndex();
89
spellChecker.indexDictionary(dictionary, new IndexWriterConfig(core.getSolrConfig().luceneMatchVersion, null), false);
91
} catch (IOException e) {
92
throw new RuntimeException(e);
97
protected IndexReader determineReader(IndexReader reader) {
98
IndexReader result = null;
99
if (sourceLocation != null) {
100
result = this.reader;
108
public void reload(SolrCore core, SolrIndexSearcher searcher) throws IOException {
109
super.reload(core, searcher);
114
public float getThreshold() {