FileDocCategorySizeDatePackage
TestNearSpansOrdered.javaAPI DocApache Lucene 2.1.06127Wed Feb 14 10:46:34 GMT 2007org.apache.lucene.search.spans

TestNearSpansOrdered

public class TestNearSpansOrdered extends TestCase
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Fields Summary
protected IndexSearcher
searcher
public static final String
FIELD
public static final QueryParser
qp
protected String[]
docFields
Constructors Summary
Methods Summary
protected org.apache.lucene.search.spans.SpanNearQuerymakeQuery(java.lang.String s1, java.lang.String s2, java.lang.String s3, int slop, boolean inOrder)


         
                                        
    return new SpanNearQuery
      (new SpanQuery[] {
        new SpanTermQuery(new Term(FIELD, s1)),
        new SpanTermQuery(new Term(FIELD, s2)),
        new SpanTermQuery(new Term(FIELD, s3)) },
       slop,
       inOrder);
  
protected org.apache.lucene.search.spans.SpanNearQuerymakeQuery()

    return makeQuery("w1","w2","w3",1,true);
  
public java.lang.Strings(org.apache.lucene.search.spans.Spans span)

    return s(span.doc(), span.start(), span.end());
  
public java.lang.Strings(int doc, int start, int end)

    return "s(" + doc + "," + start + "," + end +")";
  
public voidsetUp()

    RAMDirectory directory = new RAMDirectory();
    IndexWriter writer= new IndexWriter(directory, new WhitespaceAnalyzer(), true);
    for (int i = 0; i < docFields.length; i++) {
      Document doc = new Document();
      doc.add(new Field(FIELD, docFields[i], Field.Store.NO, Field.Index.TOKENIZED));
      writer.addDocument(doc);
    }
    writer.close();
    searcher = new IndexSearcher(directory);
  
public voidtearDown()


       
    searcher.close();
  
public voidtestNearSpansNext()

    SpanNearQuery q = makeQuery();
    Spans span = q.getSpans(searcher.getIndexReader());
    assertEquals(true, span.next());
    assertEquals(s(0,0,3), s(span));
    assertEquals(true, span.next());
    assertEquals(s(1,0,4), s(span));
    assertEquals(false, span.next());
  
public voidtestNearSpansNextThenSkipPast()

    SpanNearQuery q = makeQuery();
    Spans span = q.getSpans(searcher.getIndexReader());
    assertEquals(true, span.next());
    assertEquals(s(0,0,3), s(span));
    assertEquals(false, span.skipTo(2));
  
public voidtestNearSpansNextThenSkipTo()

    SpanNearQuery q = makeQuery();
    Spans span = q.getSpans(searcher.getIndexReader());
    assertEquals(true, span.next());
    assertEquals(s(0,0,3), s(span));
    assertEquals(true, span.skipTo(1));
    assertEquals(s(1,0,4), s(span));
    assertEquals(false, span.next());
  
public voidtestNearSpansSkipPast()

    SpanNearQuery q = makeQuery();
    Spans span = q.getSpans(searcher.getIndexReader());
    assertEquals(false, span.skipTo(2));
  
public voidtestNearSpansSkipTo0()

    SpanNearQuery q = makeQuery();
    Spans span = q.getSpans(searcher.getIndexReader());
    assertEquals(true, span.skipTo(0));
    assertEquals(s(0,0,3), s(span));
  
public voidtestNearSpansSkipTo1()

    SpanNearQuery q = makeQuery();
    Spans span = q.getSpans(searcher.getIndexReader());
    assertEquals(true, span.skipTo(1));
    assertEquals(s(1,0,4), s(span));
  
public voidtestNearSpansSkipToLikeNext()
test does not imply that skipTo(doc+1) should work exactly the same as next -- it's only applicable in this case since we know doc does not contain more than one span

    SpanNearQuery q = makeQuery();
    Spans span = q.getSpans(searcher.getIndexReader());
    assertEquals(true, span.skipTo(0));
    assertEquals(s(0,0,3), s(span));
    assertEquals(true, span.skipTo(1));
    assertEquals(s(1,0,4), s(span));
    assertEquals(false, span.skipTo(2));
  
public voidtestSpanNearQuery()

    SpanNearQuery q = makeQuery();
    CheckHits.checkHits(q, FIELD, searcher, new int[] {0,1});
  
public voidtestSpanNearScorerExplain()
not a direct test of NearSpans, but a demonstration of how/when this causes problems

    SpanNearQuery q = makeQuery();
    Weight w = q.createWeight(searcher);
    Scorer s = w.scorer(searcher.getIndexReader());
    Explanation e = s.explain(1);
    assertTrue("Scorer explanation value for doc#1 isn't positive: "
               + e.toString(),
               0.0f < e.getValue());
  
public voidtestSpanNearScorerSkipTo1()
not a direct test of NearSpans, but a demonstration of how/when this causes problems

    SpanNearQuery q = makeQuery();
    Weight w = q.createWeight(searcher);
    Scorer s = w.scorer(searcher.getIndexReader());
    assertEquals(true, s.skipTo(1));
    assertEquals(1, s.doc());