Home » lucene-3.0.1-src » org.apache » lucene » search » spans » [javadoc | source]

    1   package org.apache.lucene.search.spans;
    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.search.IndexSearcher;
   20   import org.apache.lucene.search.Query;
   21   import org.apache.lucene.search.Hits;
   22   import org.apache.lucene.search.CheckHits;
   23   import org.apache.lucene.store.RAMDirectory;
   24   import org.apache.lucene.index.IndexWriter;
   25   import org.apache.lucene.index.Term;
   26   import org.apache.lucene.analysis.WhitespaceAnalyzer;
   27   import org.apache.lucene.document.Document;
   28   import org.apache.lucene.document.Field;
   29   import junit.framework.TestCase;
   30   
   31   import java.io.IOException;
   32   import java.util.Set;
   33   import java.util.TreeSet;
   34   
   35   public class TestSpans extends TestCase {
   36     private IndexSearcher searcher;
   37   
   38     public static final String field = "field";
   39   
   40     public void setUp() throws Exception {
   41       RAMDirectory directory = new RAMDirectory();
   42       IndexWriter writer= new IndexWriter(directory, new WhitespaceAnalyzer(), true);
   43       StringBuffer buffer = new StringBuffer();
   44       for (int i = 0; i < docFields.length; i++) {
   45         Document doc = new Document();
   46         doc.add(Field.Text(field, docFields[i]));
   47         writer.addDocument(doc);
   48       }
   49       writer.close();
   50       searcher = new IndexSearcher(directory);
   51     }
   52   
   53     private String[] docFields = {
   54       "w1 w2 w3 w4 w5",
   55       "w1 w3 w2 w3",
   56       "w1 xx w2 yy w3",
   57       "w1 w3 xx w2 yy w3",
   58       ""
   59     };
   60   
   61     public SpanTermQuery makeSpanTermQuery(String text) {
   62       return new SpanTermQuery(new Term(field, text));
   63     }
   64     
   65     private void checkHits(Query query, int[] results) throws IOException {
   66       CheckHits.checkHits(query, field, searcher, results, this);
   67     }
   68     
   69     public void orderedSlopTest3(int slop, int[] expectedDocs) throws IOException {
   70       SpanTermQuery w1 = makeSpanTermQuery("w1");
   71       SpanTermQuery w2 = makeSpanTermQuery("w2");
   72       SpanTermQuery w3 = makeSpanTermQuery("w3");
   73       boolean ordered = true;
   74       SpanNearQuery snq = new SpanNearQuery( new SpanQuery[]{w1,w2,w3}, slop, ordered);
   75       checkHits(snq, expectedDocs);
   76     }
   77     
   78     public void testSpanNearOrdered01() throws Exception {
   79       orderedSlopTest3(0, new int[] {0});
   80     }
   81   
   82     public void testSpanNearOrdered02() throws Exception {
   83       orderedSlopTest3(1, new int[] {0,1});
   84     }
   85   
   86     public void testSpanNearOrdered03() throws Exception {
   87       orderedSlopTest3(2, new int[] {0,1,2});
   88     }
   89   
   90     public void testSpanNearOrdered04() throws Exception {
   91       orderedSlopTest3(3, new int[] {0,1,2,3});
   92     }
   93   
   94     public void testSpanNearOrdered05() throws Exception {
   95       orderedSlopTest3(4, new int[] {0,1,2,3});
   96     }
   97   }

Home » lucene-3.0.1-src » org.apache » lucene » search » spans » [javadoc | source]