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());
}
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;
}
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();
}
}
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;
}
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);
}
}
Aggregations