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

« back to all changes in this revision

Viewing changes to solr/core/src/java/org/apache/solr/spelling/IndexBasedSpellChecker.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
 
package org.apache.solr.spelling;
2
 
/**
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
9
 
 *
10
 
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 
 *
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.
17
 
 */
18
 
 
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;
26
 
 
27
 
import java.io.File;
28
 
import java.io.IOException;
29
 
import org.slf4j.Logger;
30
 
import org.slf4j.LoggerFactory;
31
 
 
32
 
 
33
 
/**
34
 
 * <p>
35
 
 * A spell checker implementation that loads words from Solr as well as arbitary Lucene indices.
36
 
 * </p>
37
 
 * 
38
 
 * <p>
39
 
 * Refer to <a href="http://wiki.apache.org/solr/SpellCheckComponent">SpellCheckComponent</a>
40
 
 * for more details.
41
 
 * </p>
42
 
 * 
43
 
 * @since solr 1.3
44
 
 **/
45
 
public class IndexBasedSpellChecker extends AbstractLuceneSpellChecker {
46
 
  private static final Logger log = LoggerFactory.getLogger(IndexBasedSpellChecker.class);
47
 
 
48
 
  public static final String THRESHOLD_TOKEN_FREQUENCY = "thresholdTokenFrequency";
49
 
 
50
 
  protected float threshold;
51
 
  protected IndexReader reader;
52
 
 
53
 
  @Override
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);
58
 
    initSourceReader();
59
 
    return name;
60
 
  }
61
 
 
62
 
  private void initSourceReader() {
63
 
    if (sourceLocation != null) {
64
 
      try {
65
 
        FSDirectory luceneIndexDir = FSDirectory.open(new File(sourceLocation));
66
 
        this.reader = IndexReader.open(luceneIndexDir);
67
 
      } catch (IOException e) {
68
 
        throw new RuntimeException(e);
69
 
      }
70
 
    }
71
 
  }
72
 
 
73
 
  @Override
74
 
  public void build(SolrCore core, SolrIndexSearcher searcher) {
75
 
    IndexReader reader = null;
76
 
    try {
77
 
      if (sourceLocation == null) {
78
 
        // Load from Solr's index
79
 
        reader = searcher.getReader();
80
 
      } else {
81
 
        // Load from Lucene index at given sourceLocation
82
 
        reader = this.reader;
83
 
      }
84
 
 
85
 
      // Create the dictionary
86
 
      dictionary = new HighFrequencyDictionary(reader, field,
87
 
          threshold);
88
 
      spellChecker.clearIndex();
89
 
      spellChecker.indexDictionary(dictionary, new IndexWriterConfig(core.getSolrConfig().luceneMatchVersion, null), false);
90
 
 
91
 
    } catch (IOException e) {
92
 
      throw new RuntimeException(e);
93
 
    }
94
 
  }
95
 
 
96
 
  @Override
97
 
  protected IndexReader determineReader(IndexReader reader) {
98
 
    IndexReader result = null;
99
 
    if (sourceLocation != null) {
100
 
      result = this.reader;
101
 
    } else {
102
 
      result = reader;
103
 
    }
104
 
    return result;
105
 
  }
106
 
 
107
 
  @Override
108
 
  public void reload(SolrCore core, SolrIndexSearcher searcher) throws IOException {
109
 
    super.reload(core, searcher);
110
 
    //reload the source
111
 
    initSourceReader();
112
 
  }
113
 
 
114
 
  public float getThreshold() {
115
 
    return threshold;
116
 
  }
117
 
}