Search in sources :

Example 1 with SpellChecker

use of org.apache.lucene.search.spell.SpellChecker in project elasticsearch-suggest-plugin by spinscale.

the class ShardSuggestService method refresh.

public ShardSuggestRefreshResponse refresh(ShardSuggestRefreshRequest shardSuggestRefreshRequest) {
    String field = shardSuggestRefreshRequest.field();
    if (!Strings.hasLength(field)) {
        update();
    } else {
        resetIndexReader();
        HighFrequencyDictionary dict = dictCache.getIfPresent(field);
        if (dict != null)
            dictCache.refresh(field);
        RAMDirectory ramDirectory = ramDirectoryCache.getIfPresent(field);
        if (ramDirectory != null) {
            ramDirectory.close();
            ramDirectoryCache.invalidate(field);
        }
        SpellChecker spellChecker = spellCheckerCache.getIfPresent(field);
        if (spellChecker != null) {
            spellCheckerCache.refresh(field);
            try {
                spellChecker.close();
            } catch (IOException e) {
                logger.error("Could not close spellchecker in indexshard [{}] for field [{}]", e, indexShard, field);
            }
        }
        FSTCompletionLookup lookup = lookupCache.getIfPresent(field);
        if (lookup != null)
            lookupCache.refresh(field);
        for (FieldType fieldType : analyzingSuggesterCache.asMap().keySet()) {
            if (fieldType.field().equals(shardSuggestRefreshRequest.field())) {
                analyzingSuggesterCache.refresh(fieldType);
            }
        }
        for (FieldType fieldType : fuzzySuggesterCache.asMap().keySet()) {
            if (fieldType.field().equals(shardSuggestRefreshRequest.field())) {
                fuzzySuggesterCache.refresh(fieldType);
            }
        }
    }
    return new ShardSuggestRefreshResponse(shardId.index().name(), shardId.id());
}
Also used : ShardSuggestRefreshResponse(de.spinscale.elasticsearch.action.suggest.refresh.ShardSuggestRefreshResponse) HighFrequencyDictionary(org.apache.lucene.search.spell.HighFrequencyDictionary) SpellChecker(org.apache.lucene.search.spell.SpellChecker) FSTCompletionLookup(org.apache.lucene.search.suggest.fst.FSTCompletionLookup) IOException(java.io.IOException) RAMDirectory(org.apache.lucene.store.RAMDirectory)

Example 2 with SpellChecker

use of org.apache.lucene.search.spell.SpellChecker in project camel by apache.

the class LuceneSuggestionStrategy method suggestEndpointOptions.

@Override
public String[] suggestEndpointOptions(Set<String> names, String unknownOption) {
    // each option must be on a separate line in a String
    StringBuilder sb = new StringBuilder();
    for (String name : names) {
        sb.append(name);
        sb.append("\n");
    }
    StringReader reader = new StringReader(sb.toString());
    try {
        PlainTextDictionary words = new PlainTextDictionary(reader);
        // use in-memory lucene spell checker to make the suggestions
        RAMDirectory dir = new RAMDirectory();
        SpellChecker checker = new SpellChecker(dir);
        checker.indexDictionary(words, new IndexWriterConfig(new KeywordAnalyzer()), false);
        return checker.suggestSimilar(unknownOption, maxSuggestions);
    } catch (Exception e) {
    // ignore
    }
    return null;
}
Also used : KeywordAnalyzer(org.apache.lucene.analysis.core.KeywordAnalyzer) PlainTextDictionary(org.apache.lucene.search.spell.PlainTextDictionary) StringReader(java.io.StringReader) SpellChecker(org.apache.lucene.search.spell.SpellChecker) RAMDirectory(org.apache.lucene.store.RAMDirectory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Example 3 with SpellChecker

use of org.apache.lucene.search.spell.SpellChecker in project lucene-solr by apache.

the class IndexBasedSpellCheckerTest method testAlternateDistance.

@Test
public void testAlternateDistance() throws Exception {
    TestSpellChecker checker = new TestSpellChecker();
    NamedList spellchecker = new NamedList();
    spellchecker.add("classname", IndexBasedSpellChecker.class.getName());
    File indexDir = createTempDir().toFile();
    spellchecker.add(AbstractLuceneSpellChecker.INDEX_DIR, indexDir.getAbsolutePath());
    spellchecker.add(AbstractLuceneSpellChecker.FIELD, "title");
    spellchecker.add(AbstractLuceneSpellChecker.SPELLCHECKER_ARG_NAME, spellchecker);
    spellchecker.add(AbstractLuceneSpellChecker.STRING_DISTANCE, JaroWinklerDistance.class.getName());
    SolrCore core = h.getCore();
    String dictName = checker.init(spellchecker, core);
    assertTrue(dictName + " is not equal to " + SolrSpellChecker.DEFAULT_DICTIONARY_NAME, dictName.equals(SolrSpellChecker.DEFAULT_DICTIONARY_NAME) == true);
    RefCounted<SolrIndexSearcher> holder = core.getSearcher();
    SolrIndexSearcher searcher = holder.get();
    try {
        checker.build(core, searcher);
        SpellChecker sc = checker.getSpellChecker();
        assertTrue("sc is null and it shouldn't be", sc != null);
        StringDistance sd = sc.getStringDistance();
        assertTrue("sd is null and it shouldn't be", sd != null);
        assertTrue("sd is not an instance of " + JaroWinklerDistance.class.getName(), sd instanceof JaroWinklerDistance);
    } finally {
        holder.decref();
    }
}
Also used : JaroWinklerDistance(org.apache.lucene.search.spell.JaroWinklerDistance) StringDistance(org.apache.lucene.search.spell.StringDistance) NamedList(org.apache.solr.common.util.NamedList) SolrCore(org.apache.solr.core.SolrCore) SpellChecker(org.apache.lucene.search.spell.SpellChecker) SolrIndexSearcher(org.apache.solr.search.SolrIndexSearcher) File(java.io.File) Test(org.junit.Test)

Example 4 with SpellChecker

use of org.apache.lucene.search.spell.SpellChecker in project lucene-solr by apache.

the class AbstractLuceneSpellChecker method init.

@Override
public String init(NamedList config, SolrCore core) {
    super.init(config, core);
    indexDir = (String) config.get(INDEX_DIR);
    String accuracy = (String) config.get(ACCURACY);
    //If indexDir is relative then create index inside core.getDataDir()
    if (indexDir != null) {
        if (!new File(indexDir).isAbsolute()) {
            indexDir = core.getDataDir() + File.separator + indexDir;
        }
    }
    sourceLocation = (String) config.get(LOCATION);
    String compClass = (String) config.get(COMPARATOR_CLASS);
    Comparator<SuggestWord> comp = null;
    if (compClass != null) {
        if (compClass.equalsIgnoreCase(SCORE_COMP)) {
            comp = SuggestWordQueue.DEFAULT_COMPARATOR;
        } else if (compClass.equalsIgnoreCase(FREQ_COMP)) {
            comp = new SuggestWordFrequencyComparator();
        } else {
            //must be a FQCN
            comp = (Comparator<SuggestWord>) core.getResourceLoader().newInstance(compClass, Comparator.class);
        }
    } else {
        comp = SuggestWordQueue.DEFAULT_COMPARATOR;
    }
    String strDistanceName = (String) config.get(STRING_DISTANCE);
    if (strDistanceName != null) {
        sd = core.getResourceLoader().newInstance(strDistanceName, StringDistance.class);
    //TODO: Figure out how to configure options.  Where's Spring when you need it?  Or at least BeanUtils...
    } else {
        sd = new LevensteinDistance();
    }
    try {
        initIndex();
        spellChecker = new SpellChecker(index, sd, comp);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    if (accuracy != null) {
        try {
            this.accuracy = Float.parseFloat(accuracy);
            spellChecker.setAccuracy(this.accuracy);
        } catch (NumberFormatException e) {
            throw new RuntimeException("Unparseable accuracy given for dictionary: " + name, e);
        }
    }
    return name;
}
Also used : SuggestWordFrequencyComparator(org.apache.lucene.search.spell.SuggestWordFrequencyComparator) StringDistance(org.apache.lucene.search.spell.StringDistance) SuggestWord(org.apache.lucene.search.spell.SuggestWord) SpellChecker(org.apache.lucene.search.spell.SpellChecker) IOException(java.io.IOException) File(java.io.File) LevensteinDistance(org.apache.lucene.search.spell.LevensteinDistance) SuggestWordFrequencyComparator(org.apache.lucene.search.spell.SuggestWordFrequencyComparator) Comparator(java.util.Comparator)

Example 5 with SpellChecker

use of org.apache.lucene.search.spell.SpellChecker in project elasticsearch-suggest-plugin by spinscale.

the class ShardSuggestService method update.

public void update() {
    resetIndexReader();
    for (String field : dictCache.asMap().keySet()) {
        dictCache.refresh(field);
    }
    try {
        for (String field : spellCheckerCache.asMap().keySet()) {
            SpellChecker oldSpellchecker = spellCheckerCache.getUnchecked(field);
            RAMDirectory oldRamDirectory = ramDirectoryCache.getUnchecked(field);
            ramDirectoryCache.refresh(field);
            spellCheckerCache.refresh(field);
            oldRamDirectory.close();
            oldSpellchecker.close();
        }
    } catch (IOException e) {
        logger.error("Error refreshing spell checker cache [{}]", e, shardId);
    }
    for (String field : lookupCache.asMap().keySet()) {
        lookupCache.refresh(field);
    }
    for (FieldType fieldType : analyzingSuggesterCache.asMap().keySet()) {
        analyzingSuggesterCache.refresh(fieldType);
    }
    for (FieldType fieldType : fuzzySuggesterCache.asMap().keySet()) {
        fuzzySuggesterCache.refresh(fieldType);
    }
}
Also used : SpellChecker(org.apache.lucene.search.spell.SpellChecker) IOException(java.io.IOException) RAMDirectory(org.apache.lucene.store.RAMDirectory)

Aggregations

SpellChecker (org.apache.lucene.search.spell.SpellChecker)5 IOException (java.io.IOException)3 RAMDirectory (org.apache.lucene.store.RAMDirectory)3 File (java.io.File)2 StringDistance (org.apache.lucene.search.spell.StringDistance)2 ShardSuggestRefreshResponse (de.spinscale.elasticsearch.action.suggest.refresh.ShardSuggestRefreshResponse)1 StringReader (java.io.StringReader)1 Comparator (java.util.Comparator)1 KeywordAnalyzer (org.apache.lucene.analysis.core.KeywordAnalyzer)1 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)1 HighFrequencyDictionary (org.apache.lucene.search.spell.HighFrequencyDictionary)1 JaroWinklerDistance (org.apache.lucene.search.spell.JaroWinklerDistance)1 LevensteinDistance (org.apache.lucene.search.spell.LevensteinDistance)1 PlainTextDictionary (org.apache.lucene.search.spell.PlainTextDictionary)1 SuggestWord (org.apache.lucene.search.spell.SuggestWord)1 SuggestWordFrequencyComparator (org.apache.lucene.search.spell.SuggestWordFrequencyComparator)1 FSTCompletionLookup (org.apache.lucene.search.suggest.fst.FSTCompletionLookup)1 NamedList (org.apache.solr.common.util.NamedList)1 SolrCore (org.apache.solr.core.SolrCore)1 SolrIndexSearcher (org.apache.solr.search.SolrIndexSearcher)1