pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / test / org / apache / lucene / search / spans / TestSpanFirstQuery.java
1 package org.apache.lucene.search.spans;
2
3 /**
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements.  See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 import org.apache.lucene.analysis.Analyzer;
21 import org.apache.lucene.analysis.StopAnalyzer;
22 import org.apache.lucene.document.Document;
23 import org.apache.lucene.document.Field;
24 import org.apache.lucene.index.IndexReader;
25 import org.apache.lucene.index.RandomIndexWriter;
26 import org.apache.lucene.index.Term;
27 import org.apache.lucene.search.IndexSearcher;
28 import org.apache.lucene.store.Directory;
29 import org.apache.lucene.util.LuceneTestCase;
30
31 public class TestSpanFirstQuery extends LuceneTestCase {
32   public void testStartPositions() throws Exception {
33     Directory dir = newDirectory();
34     
35     // mimic StopAnalyzer
36     Analyzer analyzer = new StopAnalyzer(TEST_VERSION_CURRENT);
37     
38     RandomIndexWriter writer = new RandomIndexWriter(random, dir, analyzer);
39     Document doc = new Document();
40     doc.add(newField("field", "the quick brown fox", Field.Index.ANALYZED));
41     writer.addDocument(doc);
42     Document doc2 = new Document();
43     doc2.add(newField("field", "quick brown fox", Field.Index.ANALYZED));
44     writer.addDocument(doc2);
45     
46     IndexReader reader = writer.getReader();
47     IndexSearcher searcher = newSearcher(reader);
48     
49     // user queries on "starts-with quick"
50     SpanQuery sfq = new SpanFirstQuery(new SpanTermQuery(new Term("field", "quick")), 1);
51     assertEquals(1, searcher.search(sfq, 10).totalHits);
52     
53     // user queries on "starts-with the quick"
54     SpanQuery include = new SpanFirstQuery(new SpanTermQuery(new Term("field", "quick")), 2);
55     sfq = new SpanNotQuery(include, sfq);
56     assertEquals(1, searcher.search(sfq, 10).totalHits);
57     
58     writer.close();
59     searcher.close();
60     reader.close();
61     dir.close();
62   }
63 }