Search in sources :

Example 6 with KeywordAnalyzer

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;
}
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 7 with KeywordAnalyzer

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);
    }
}
Also used : KeywordAnalyzer(org.apache.lucene.analysis.core.KeywordAnalyzer) IndexWriter(org.apache.lucene.index.IndexWriter) TextField(org.apache.lucene.document.TextField) Document(org.apache.lucene.document.Document) File(java.io.File) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Example 8 with KeywordAnalyzer

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"));
}
Also used : KeywordAnalyzer(org.apache.lucene.analysis.core.KeywordAnalyzer) AbstractExecution(org.apache.geode.internal.cache.execute.AbstractExecution) Set(java.util.Set) HashSet(java.util.HashSet) TabularResultData(org.apache.geode.management.internal.cli.result.TabularResultData) HashMap(java.util.HashMap) LuceneListIndexFunction(org.apache.geode.cache.lucene.internal.cli.functions.LuceneListIndexFunction) ArrayList(java.util.ArrayList) InternalCache(org.apache.geode.internal.cache.InternalCache) Mockito.anyString(org.mockito.Mockito.anyString) KeywordAnalyzer(org.apache.lucene.analysis.core.KeywordAnalyzer) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) Analyzer(org.apache.lucene.analysis.Analyzer) CommandResult(org.apache.geode.management.internal.cli.result.CommandResult) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) ResultCollector(org.apache.geode.cache.execute.ResultCollector) UnitTest(org.apache.geode.test.junit.categories.UnitTest) Test(org.junit.Test)

Example 9 with KeywordAnalyzer

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"));
}
Also used : KeywordAnalyzer(org.apache.lucene.analysis.core.KeywordAnalyzer) LuceneIndexStats(org.apache.geode.cache.lucene.internal.LuceneIndexStats) TabularResultData(org.apache.geode.management.internal.cli.result.TabularResultData) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) InternalCache(org.apache.geode.internal.cache.InternalCache) Mockito.anyString(org.mockito.Mockito.anyString) KeywordAnalyzer(org.apache.lucene.analysis.core.KeywordAnalyzer) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) Analyzer(org.apache.lucene.analysis.Analyzer) CommandResult(org.apache.geode.management.internal.cli.result.CommandResult) LuceneDescribeIndexFunction(org.apache.geode.cache.lucene.internal.cli.functions.LuceneDescribeIndexFunction) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) ResultCollector(org.apache.geode.cache.execute.ResultCollector) UnitTest(org.apache.geode.test.junit.categories.UnitTest) Test(org.junit.Test)

Example 10 with KeywordAnalyzer

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;
}
Also used : KeywordAnalyzer(org.apache.lucene.analysis.core.KeywordAnalyzer) HashMap(java.util.HashMap) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) Analyzer(org.apache.lucene.analysis.Analyzer) KeywordAnalyzer(org.apache.lucene.analysis.core.KeywordAnalyzer) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) LuceneIndexImpl(org.apache.geode.cache.lucene.internal.LuceneIndexImpl)

Aggregations

KeywordAnalyzer (org.apache.lucene.analysis.core.KeywordAnalyzer)24 Analyzer (org.apache.lucene.analysis.Analyzer)12 HashMap (java.util.HashMap)11 StandardAnalyzer (org.apache.lucene.analysis.standard.StandardAnalyzer)11 Document (org.apache.lucene.document.Document)5 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)5 Test (org.junit.Test)5 ArrayList (java.util.ArrayList)4 HashSet (java.util.HashSet)4 UnitTest (org.apache.geode.test.junit.categories.UnitTest)4 StringField (org.apache.lucene.document.StringField)4 IndexWriter (org.apache.lucene.index.IndexWriter)4 ResultCollector (org.apache.geode.cache.execute.ResultCollector)3 InternalCache (org.apache.geode.internal.cache.InternalCache)3 CommandResult (org.apache.geode.management.internal.cli.result.CommandResult)3 TabularResultData (org.apache.geode.management.internal.cli.result.TabularResultData)3 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)3 QueryParser (org.apache.lucene.queryparser.classic.QueryParser)3 Set (java.util.Set)2 LuceneService (org.apache.geode.cache.lucene.LuceneService)2