|
| 1 | +package com.baeldung.lucene; |
| 2 | + |
| 3 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 4 | +import static org.hamcrest.collection.IsIterableContainingInOrder.contains; |
| 5 | +import static org.junit.Assert.assertEquals; |
| 6 | + |
| 7 | +import java.io.IOException; |
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.HashMap; |
| 10 | +import java.util.List; |
| 11 | +import java.util.Map; |
| 12 | + |
| 13 | +import org.apache.lucene.analysis.Analyzer; |
| 14 | +import org.apache.lucene.analysis.TokenStream; |
| 15 | +import org.apache.lucene.analysis.core.KeywordAnalyzer; |
| 16 | +import org.apache.lucene.analysis.core.SimpleAnalyzer; |
| 17 | +import org.apache.lucene.analysis.core.StopAnalyzer; |
| 18 | +import org.apache.lucene.analysis.core.WhitespaceAnalyzer; |
| 19 | +import org.apache.lucene.analysis.custom.CustomAnalyzer; |
| 20 | +import org.apache.lucene.analysis.en.EnglishAnalyzer; |
| 21 | +import org.apache.lucene.analysis.miscellaneous.PerFieldAnalyzerWrapper; |
| 22 | +import org.apache.lucene.analysis.standard.StandardAnalyzer; |
| 23 | +import org.apache.lucene.analysis.tokenattributes.CharTermAttribute; |
| 24 | +import org.apache.lucene.document.Document; |
| 25 | +import org.apache.lucene.index.Term; |
| 26 | +import org.apache.lucene.search.Query; |
| 27 | +import org.apache.lucene.search.TermQuery; |
| 28 | +import org.apache.lucene.store.RAMDirectory; |
| 29 | +import org.junit.Test; |
| 30 | + |
| 31 | +public class LuceneAnalyzerIntegrationTest { |
| 32 | + |
| 33 | + private static final String SAMPLE_TEXT = "This is baeldung.com Lucene Analyzers test"; |
| 34 | + private static final String FIELD_NAME = "sampleName"; |
| 35 | + |
| 36 | + @Test |
| 37 | + public void whenUseStandardAnalyzer_thenAnalyzed() throws IOException { |
| 38 | + List<String> result = analyze(SAMPLE_TEXT, new StandardAnalyzer()); |
| 39 | + |
| 40 | + assertThat(result, contains("baeldung.com", "lucene", "analyzers", "test")); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + public void whenUseStopAnalyzer_thenAnalyzed() throws IOException { |
| 45 | + List<String> result = analyze(SAMPLE_TEXT, new StopAnalyzer()); |
| 46 | + |
| 47 | + assertThat(result, contains("baeldung", "com", "lucene", "analyzers", "test")); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void whenUseSimpleAnalyzer_thenAnalyzed() throws IOException { |
| 52 | + List<String> result = analyze(SAMPLE_TEXT, new SimpleAnalyzer()); |
| 53 | + |
| 54 | + assertThat(result, contains("this", "is", "baeldung", "com", "lucene", "analyzers", "test")); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void whenUseWhiteSpaceAnalyzer_thenAnalyzed() throws IOException { |
| 59 | + List<String> result = analyze(SAMPLE_TEXT, new WhitespaceAnalyzer()); |
| 60 | + |
| 61 | + assertThat(result, contains("This", "is", "baeldung.com", "Lucene", "Analyzers", "test")); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void whenUseKeywordAnalyzer_thenAnalyzed() throws IOException { |
| 66 | + List<String> result = analyze(SAMPLE_TEXT, new KeywordAnalyzer()); |
| 67 | + |
| 68 | + assertThat(result, contains("This is baeldung.com Lucene Analyzers test")); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void whenUseEnglishAnalyzer_thenAnalyzed() throws IOException { |
| 73 | + List<String> result = analyze(SAMPLE_TEXT, new EnglishAnalyzer()); |
| 74 | + |
| 75 | + assertThat(result, contains("baeldung.com", "lucen", "analyz", "test")); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void whenUseCustomAnalyzerBuilder_thenAnalyzed() throws IOException { |
| 80 | + Analyzer analyzer = CustomAnalyzer.builder() |
| 81 | + .withTokenizer("standard") |
| 82 | + .addTokenFilter("lowercase") |
| 83 | + .addTokenFilter("stop") |
| 84 | + .addTokenFilter("porterstem") |
| 85 | + .addTokenFilter("capitalization") |
| 86 | + .build(); |
| 87 | + List<String> result = analyze(SAMPLE_TEXT, analyzer); |
| 88 | + |
| 89 | + assertThat(result, contains("Baeldung.com", "Lucen", "Analyz", "Test")); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void whenUseCustomAnalyzer_thenAnalyzed() throws IOException { |
| 94 | + List<String> result = analyze(SAMPLE_TEXT, new MyCustomAnalyzer()); |
| 95 | + |
| 96 | + assertThat(result, contains("Baeldung.com", "Lucen", "Analyz", "Test")); |
| 97 | + } |
| 98 | + |
| 99 | + // ================= usage example |
| 100 | + |
| 101 | + @Test |
| 102 | + public void givenTermQuery_whenUseCustomAnalyzer_thenCorrect() { |
| 103 | + InMemoryLuceneIndex luceneIndex = new InMemoryLuceneIndex(new RAMDirectory(), new MyCustomAnalyzer()); |
| 104 | + luceneIndex.indexDocument("introduction", "introduction to lucene"); |
| 105 | + luceneIndex.indexDocument("analyzers", "guide to lucene analyzers"); |
| 106 | + Query query = new TermQuery(new Term("body", "Introduct")); |
| 107 | + |
| 108 | + List<Document> documents = luceneIndex.searchIndex(query); |
| 109 | + assertEquals(1, documents.size()); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + public void givenTermQuery_whenUsePerFieldAnalyzerWrapper_thenCorrect() { |
| 114 | + Map<String,Analyzer> analyzerMap = new HashMap<>(); |
| 115 | + analyzerMap.put("title", new MyCustomAnalyzer()); |
| 116 | + analyzerMap.put("body", new EnglishAnalyzer()); |
| 117 | + |
| 118 | + PerFieldAnalyzerWrapper wrapper = |
| 119 | + new PerFieldAnalyzerWrapper(new StandardAnalyzer(), analyzerMap); |
| 120 | + InMemoryLuceneIndex luceneIndex = new InMemoryLuceneIndex(new RAMDirectory(), wrapper); |
| 121 | + luceneIndex.indexDocument("introduction", "introduction to lucene"); |
| 122 | + luceneIndex.indexDocument("analyzers", "guide to lucene analyzers"); |
| 123 | + |
| 124 | + Query query = new TermQuery(new Term("body", "introduct")); |
| 125 | + List<Document> documents = luceneIndex.searchIndex(query); |
| 126 | + assertEquals(1, documents.size()); |
| 127 | + |
| 128 | + query = new TermQuery(new Term("title", "Introduct")); |
| 129 | + |
| 130 | + documents = luceneIndex.searchIndex(query); |
| 131 | + assertEquals(1, documents.size()); |
| 132 | + } |
| 133 | + |
| 134 | + // =================================================================== |
| 135 | + |
| 136 | + public List<String> analyze(String text, Analyzer analyzer) throws IOException { |
| 137 | + List<String> result = new ArrayList<String>(); |
| 138 | + TokenStream tokenStream = analyzer.tokenStream(FIELD_NAME, text); |
| 139 | + CharTermAttribute attr = tokenStream.addAttribute(CharTermAttribute.class); |
| 140 | + tokenStream.reset(); |
| 141 | + while (tokenStream.incrementToken()) { |
| 142 | + result.add(attr.toString()); |
| 143 | + } |
| 144 | + return result; |
| 145 | + } |
| 146 | + |
| 147 | +} |
0 commit comments