| 1 |
|
package org.apache.lucene.analysis; |
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
import junit.framework.TestCase; |
| 20 |
|
import org.apache.lucene.index.IndexWriter; |
| 21 |
|
import org.apache.lucene.store.RAMDirectory; |
| 22 |
|
import org.apache.lucene.document.Document; |
| 23 |
|
import org.apache.lucene.document.Field; |
| 24 |
|
import org.apache.lucene.search.IndexSearcher; |
| 25 |
|
import org.apache.lucene.search.Query; |
| 26 |
|
import org.apache.lucene.search.Hits; |
| 27 |
|
import org.apache.lucene.queryParser.QueryParser; |
| 28 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (17) |
Complexity: 2 |
Complexity Density: 0.13 |
|
| 29 |
|
public class TestKeywordAnalyzer extends TestCase { |
| 30 |
|
|
| 31 |
|
private RAMDirectory directory; |
| 32 |
|
private IndexSearcher searcher; |
| 33 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
|
| 34 |
1
|
public void setUp() throws Exception {... |
| 35 |
1
|
directory = new RAMDirectory(); |
| 36 |
1
|
IndexWriter writer = new IndexWriter(directory, |
| 37 |
|
new SimpleAnalyzer(), |
| 38 |
|
true); |
| 39 |
|
|
| 40 |
1
|
Document doc = new Document(); |
| 41 |
1
|
doc.add(new Field("partnum", "Q36", Field.Store.YES, Field.Index.UN_TOKENIZED)); |
| 42 |
1
|
doc.add(new Field("description", "Illidium Space Modulator", Field.Store.YES, Field.Index.TOKENIZED)); |
| 43 |
1
|
writer.addDocument(doc); |
| 44 |
|
|
| 45 |
1
|
writer.close(); |
| 46 |
|
|
| 47 |
1
|
searcher = new IndexSearcher(directory); |
| 48 |
|
} |
| 49 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1
PASS
|
|
| 50 |
1
|
public void testPerFieldAnalyzer() throws Exception {... |
| 51 |
1
|
PerFieldAnalyzerWrapper analyzer = new PerFieldAnalyzerWrapper(new SimpleAnalyzer()); |
| 52 |
1
|
analyzer.addAnalyzer("partnum", new KeywordAnalyzer()); |
| 53 |
|
|
| 54 |
1
|
QueryParser queryParser = new QueryParser("description", analyzer); |
| 55 |
1
|
Query query = queryParser.parse("partnum:Q36 AND SPACE"); |
| 56 |
|
|
| 57 |
1
|
Hits hits = searcher.search(query); |
| 58 |
1
|
assertEquals("Q36 kept as-is", |
| 59 |
|
"+partnum:Q36 +space", query.toString("description")); |
| 60 |
1
|
assertEquals("doc found!", 1, hits.length()); |
| 61 |
|
} |
| 62 |
|
} |