KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > search > TestPositionIncrement


1 package org.apache.lucene.search;
2
3 /**
4  * Copyright 2004 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

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 JavaDoc;
33 import java.io.IOException JavaDoc;
34 import java.io.StringReader JavaDoc;
35
36 import junit.framework.TestCase;
37
38 /**
39  * Term position unit test.
40  *
41  * @author Doug Cutting
42  * @version $Revision: 1.4 $
43  */

44 public class TestPositionIncrement extends TestCase {
45
46   public void testSetPosition() throws Exception JavaDoc {
47     Analyzer analyzer = new Analyzer() {
48       public TokenStream tokenStream(String JavaDoc fieldName, Reader JavaDoc reader) {
49         return new TokenStream() {
50           private final String JavaDoc[] 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 JavaDoc {
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   /**
121    * Basic analyzer behavior should be to keep sequential terms in one
122    * increment from one another.
123    */

124   public void testIncrementingPositions() throws Exception JavaDoc {
125     Analyzer analyzer = new WhitespaceAnalyzer();
126     TokenStream ts = analyzer.tokenStream("field",
127                                 new StringReader JavaDoc("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