1 package org.apache.lucene.search; 2 3 18 19 import org.apache.lucene.index.Term; 20 import org.apache.lucene.index.IndexWriter; 21 import org.apache.lucene.search.PhraseQuery; 22 import org.apache.lucene.search.Hits; 23 import org.apache.lucene.search.IndexSearcher; 24 import org.apache.lucene.store.RAMDirectory; 25 import org.apache.lucene.analysis.Analyzer; 26 import org.apache.lucene.analysis.Token; 27 import org.apache.lucene.analysis.TokenStream; 28 import org.apache.lucene.analysis.WhitespaceAnalyzer; 29 import org.apache.lucene.document.Document; 30 import org.apache.lucene.document.Field; 31 32 import java.io.Reader ; 33 import java.io.IOException ; 34 import java.io.StringReader ; 35 36 import junit.framework.TestCase; 37 38 44 public class TestPositionIncrement extends TestCase { 45 46 public void testSetPosition() throws Exception { 47 Analyzer analyzer = new Analyzer() { 48 public TokenStream tokenStream(String fieldName, Reader reader) { 49 return new TokenStream() { 50 private final String [] TOKENS = {"1", "2", "3", "4", "5"}; 51 private final int[] INCREMENTS = {1, 2, 1, 0, 1}; 52 private int i = 0; 53 54 public Token next() throws IOException { 55 if (i == TOKENS.length) 56 return null; 57 Token t = new Token(TOKENS[i], i, i); 58 t.setPositionIncrement(INCREMENTS[i]); 59 i++; 60 return t; 61 } 62 }; 63 } 64 }; 65 RAMDirectory store = new RAMDirectory(); 66 IndexWriter writer = new IndexWriter(store, analyzer, true); 67 Document d = new Document(); 68 d.add(Field.Text("field", "bogus")); 69 writer.addDocument(d); 70 writer.optimize(); 71 writer.close(); 72 73 IndexSearcher searcher = new IndexSearcher(store); 74 PhraseQuery q; 75 Hits hits; 76 77 q = new PhraseQuery(); 78 q.add(new Term("field", "1")); 79 q.add(new Term("field", "2")); 80 hits = searcher.search(q); 81 assertEquals(0, hits.length()); 82 83 q = new PhraseQuery(); 84 q.add(new Term("field", "2")); 85 q.add(new Term("field", "3")); 86 hits = searcher.search(q); 87 assertEquals(1, hits.length()); 88 89 q = new PhraseQuery(); 90 q.add(new Term("field", "3")); 91 q.add(new Term("field", "4")); 92 hits = searcher.search(q); 93 assertEquals(0, hits.length()); 94 95 q = new PhraseQuery(); 96 q.add(new Term("field", "2")); 97 q.add(new Term("field", "4")); 98 hits = searcher.search(q); 99 assertEquals(1, hits.length()); 100 101 q = new PhraseQuery(); 102 q.add(new Term("field", "3")); 103 q.add(new Term("field", "5")); 104 hits = searcher.search(q); 105 assertEquals(1, hits.length()); 106 107 q = new PhraseQuery(); 108 q.add(new Term("field", "4")); 109 q.add(new Term("field", "5")); 110 hits = searcher.search(q); 111 assertEquals(1, hits.length()); 112 113 q = new PhraseQuery(); 114 q.add(new Term("field", "2")); 115 q.add(new Term("field", "5")); 116 hits = searcher.search(q); 117 assertEquals(0, hits.length()); 118 } 119 120 124 public void testIncrementingPositions() throws Exception { 125 Analyzer analyzer = new WhitespaceAnalyzer(); 126 TokenStream ts = analyzer.tokenStream("field", 127 new StringReader ("one two three four five")); 128 129 while (true) { 130 Token token = ts.next(); 131 if (token == null) break; 132 assertEquals(token.termText(), 1, token.getPositionIncrement()); 133 } 134 } 135 } 136 | Popular Tags |