use of org.apache.lucene.analysis.core.KeywordAnalyzer 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.analysis.core.KeywordAnalyzer in project languagetool by languagetool-org.
the class LuceneSimpleIndexCreator method main.
public static void main(String[] args) throws IOException {
IndexWriterConfig conf = new IndexWriterConfig(new KeywordAnalyzer());
try (IndexWriter iw1 = new IndexWriter(FSDirectory.open(new File("/tmp/1grams").toPath()), conf)) {
addDoc(iw1, "the", 55);
addDoc(iw1, "nice", 10);
addDoc(iw1, "building", 1);
Document document = new Document();
document.add(new TextField("totalTokenCount", String.valueOf(3), Field.Store.YES));
iw1.addDocument(document);
}
IndexWriterConfig conf2 = new IndexWriterConfig(new KeywordAnalyzer());
try (IndexWriter iw2 = new IndexWriter(FSDirectory.open(new File("/tmp/2grams").toPath()), conf2)) {
addDoc(iw2, "the nice", 3);
addDoc(iw2, "nice building", 2);
}
IndexWriterConfig conf3 = new IndexWriterConfig(new KeywordAnalyzer());
try (IndexWriter iw3 = new IndexWriter(FSDirectory.open(new File("/tmp/3grams").toPath()), conf3)) {
addDoc(iw3, "the nice building", 1);
}
}
use of org.apache.lucene.analysis.core.KeywordAnalyzer in project geode by apache.
the class LuceneIndexCommandsJUnitTest method testListIndexWithoutStats.
@Test
public void testListIndexWithoutStats() {
final InternalCache mockCache = mock(InternalCache.class, "InternalCache");
final String serverName = "mockServer";
final AbstractExecution mockFunctionExecutor = mock(AbstractExecution.class, "Function Executor");
final ResultCollector mockResultCollector = mock(ResultCollector.class, "ResultCollector");
String[] searchableFields = { "field1", "field2", "field3" };
Map<String, Analyzer> fieldAnalyzers = new HashMap<>();
fieldAnalyzers.put("field1", new StandardAnalyzer());
fieldAnalyzers.put("field2", new KeywordAnalyzer());
fieldAnalyzers.put("field3", null);
final LuceneIndexDetails indexDetails1 = createIndexDetails("memberFive", "/Employees", searchableFields, fieldAnalyzers, true, serverName);
final LuceneIndexDetails indexDetails2 = createIndexDetails("memberSix", "/Employees", searchableFields, fieldAnalyzers, false, serverName);
final LuceneIndexDetails indexDetails3 = createIndexDetails("memberTen", "/Employees", searchableFields, fieldAnalyzers, true, serverName);
final List<Set<LuceneIndexDetails>> results = new ArrayList<>();
results.add(CollectionUtils.asSet(indexDetails2, indexDetails1, indexDetails3));
when(mockFunctionExecutor.execute(isA(LuceneListIndexFunction.class))).thenReturn(mockResultCollector);
when(mockResultCollector.getResult()).thenReturn(results);
final LuceneIndexCommands commands = createIndexCommands(mockCache, mockFunctionExecutor);
CommandResult result = (CommandResult) commands.listIndex(false);
TabularResultData data = (TabularResultData) result.getResultData();
assertEquals(Arrays.asList("memberFive", "memberSix", "memberTen"), data.retrieveAllValues("Index Name"));
assertEquals(Arrays.asList("/Employees", "/Employees", "/Employees"), data.retrieveAllValues("Region Path"));
assertEquals(Arrays.asList("[field1, field2, field3]", "[field1, field2, field3]", "[field1, field2, field3]"), data.retrieveAllValues("Indexed Fields"));
assertEquals(Arrays.asList("{field1=StandardAnalyzer, field2=KeywordAnalyzer}", "{field1=StandardAnalyzer, field2=KeywordAnalyzer}", "{field1=StandardAnalyzer, field2=KeywordAnalyzer}"), data.retrieveAllValues("Field Analyzer"));
assertEquals(Arrays.asList("Initialized", "Defined", "Initialized"), data.retrieveAllValues("Status"));
}
use of org.apache.lucene.analysis.core.KeywordAnalyzer in project geode by apache.
the class LuceneIndexCommandsJUnitTest method testDescribeIndex.
@Test
public void testDescribeIndex() throws Exception {
final InternalCache mockCache = mock(InternalCache.class, "InternalCache");
final String serverName = "mockServer";
final ResultCollector mockResultCollector = mock(ResultCollector.class, "ResultCollector");
final LuceneIndexCommands commands = spy(createIndexCommands(mockCache, null));
String[] searchableFields = { "field1", "field2", "field3" };
Map<String, Analyzer> fieldAnalyzers = new HashMap<>();
fieldAnalyzers.put("field1", new StandardAnalyzer());
fieldAnalyzers.put("field2", new KeywordAnalyzer());
fieldAnalyzers.put("field3", null);
final LuceneIndexStats mockIndexStats = getMockIndexStats(1, 10, 5, 1);
final List<LuceneIndexDetails> indexDetails = new ArrayList<>();
indexDetails.add(createIndexDetails("memberFive", "/Employees", searchableFields, fieldAnalyzers, mockIndexStats, true, serverName));
doReturn(mockResultCollector).when(commands).executeFunctionOnRegion(isA(LuceneDescribeIndexFunction.class), any(LuceneIndexInfo.class), eq(true));
doReturn(indexDetails).when(mockResultCollector).getResult();
CommandResult result = (CommandResult) commands.describeIndex("memberFive", "/Employees");
TabularResultData data = (TabularResultData) result.getResultData();
assertEquals(Collections.singletonList("memberFive"), data.retrieveAllValues("Index Name"));
assertEquals(Collections.singletonList("/Employees"), data.retrieveAllValues("Region Path"));
assertEquals(Collections.singletonList("[field1, field2, field3]"), data.retrieveAllValues("Indexed Fields"));
assertEquals(Collections.singletonList("{field1=StandardAnalyzer, field2=KeywordAnalyzer}"), data.retrieveAllValues("Field Analyzer"));
assertEquals(Collections.singletonList("Initialized"), data.retrieveAllValues("Status"));
assertEquals(Collections.singletonList("1"), data.retrieveAllValues("Query Executions"));
assertEquals(Collections.singletonList("10"), data.retrieveAllValues("Commits"));
assertEquals(Collections.singletonList("5"), data.retrieveAllValues("Updates"));
assertEquals(Collections.singletonList("1"), data.retrieveAllValues("Documents"));
}
use of org.apache.lucene.analysis.core.KeywordAnalyzer in project geode by apache.
the class LuceneDescribeIndexFunctionJUnitTest method getMockLuceneIndex.
private LuceneIndexImpl getMockLuceneIndex(final String indexName) {
String[] searchableFields = { "field1", "field2" };
Map<String, Analyzer> fieldAnalyzers = new HashMap<>();
fieldAnalyzers.put("field1", new StandardAnalyzer());
fieldAnalyzers.put("field2", new KeywordAnalyzer());
LuceneIndexImpl index = mock(LuceneIndexImpl.class);
when(index.getName()).thenReturn(indexName);
when(index.getRegionPath()).thenReturn("/region");
when(index.getFieldNames()).thenReturn(searchableFields);
when(index.getFieldAnalyzers()).thenReturn(fieldAnalyzers);
return index;
}
Aggregations