You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@lucene.apache.org by Gregory Tarr <Gr...@detica.com> on 2011/05/17 17:02:53 UTC

SpanNearQuery - inOrder parameter

I attach a junit test which shows strange behaviour of the inOrder
parameter on the SpanNearQuery constructor, using Lucene 2.9.4.

My understanding of this parameter is that true forces the order and
false doesn't care about the order.

Using true always works. However using false works fine when the terms
in the query are distinct, but if they are equivalent, e.g. searching
for "john john", I do not get the expected results. The workaround seems
to be to always use true for queries with repeated terms.

Any help?

Thanks

Greg

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.TopDocsCollector;
import org.apache.lucene.search.TopScoreDocCollector;
import org.apache.lucene.search.spans.SpanNearQuery;
import org.apache.lucene.search.spans.SpanQuery;
import org.apache.lucene.search.spans.SpanTermQuery;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;
import org.junit.Assert;
import org.junit.Test; 
public class TestSpanNearQueryInOrder { 
@Test
public void testSpanNearQueryInOrder() {
RAMDirectory directory = new RAMDirectory();
IndexWriter writer = new IndexWriter(directory, new
StandardAnalyzer(Version.LUCENE_29), true,
IndexWriter.MaxFieldLength.UNLIMITED);
TopDocsCollector collector = TopScoreDocCollector.create(3, false);

Document doc = new Document();

// DOC1
doc.add(new Field("text","dddd aaaa bbbb cccc", Field.Store.YES,
Field.Index.ANALYZED));

writer.addDocument(doc);
doc = new Document(); 

// DOC2
doc.add(new Field("text","dddd aaaa aaaa cccc"));

writer.addDocument(doc); 
doc = new Document();

// DOC3
doc.add(new Field("text","dddd aaaa yyyy aaaa xxxx cccc"));

writer.addDocument(doc);
writer.optimize();
writer.close(); 
searcher = new IndexSearcher(directory, false); 
SpanQuery[] clauses = new SpanQuery[2];
clauses[0] = new SpanTermQuery(new Term("text", "aaaa"));
clauses[1] = new SpanTermQuery(new Term("text", "aaaa"));

// Don't care about order, so setting inOrder = false
SpanNearQuery q = new SpanNearQuery(clauses, 1, false);
searcher.search(q, collector); 
// This assert fails - 3 docs are returned. Expecting only DOC2 and DOC3
Assert.assertEquals("Check 2 results", 2, collector.getTotalHits()); 
collector = new TopScoreDocCollector.create(3, false);
clauses = new SpanQuery[2];
clauses[0] = new SpanTermQuery(new Term("text", "aaaa"));
clauses[1] = new SpanTermQuery(new Term("text", "aaaa"));

// Don't care about order, so setting inOrder = false
q = new SpanNearQuery(clauses, 0, false);
searcher.search(q, collector); 
// This assert fails - 3 docs are returned. Expecting only DOC2
Assert.assertEquals("Check 1 result", 1, collector.getTotalHits()); 
} 
}
 <<TestSpanNearQueryInOrder.java>> 

Please consider the environment before printing this email.

This message should be regarded as confidential. If you have received this email in error please notify the sender and destroy it immediately.
Statements of intent shall only become binding when confirmed in hard copy by an authorised signatory.  The contents of this email may relate to dealings with other companies within the Detica Limited group of companies.

Detica Limited is registered in England under No: 1337451.

Registered offices: Surrey Research Park, Guildford, Surrey, GU2 7YP, England.


Re: SpanNearQuery - inOrder parameter

Posted by Chris Hostetter <ho...@fucit.org>.
: 
: I attach a junit test which shows strange behaviour of the inOrder
: parameter on the SpanNearQuery constructor, using Lucene 2.9.4.

De-Ja-Vu? ... didn't we just talk about this on java-user recently?

Here was my comment...

http://markmail.org/search/?q=SpanNearQuery+inOrder#query:SpanNearQuery%20inOrder+page:1+mid:zhhlhojw55bf43r6+state:results

...i guess this is actually the same thread now on the dev list?

As i said before: i don't really see how this is bug.  when you specify 
inOrder=true, you are requiring that the positions of the subspans must 
be "in order" so position_span_1 < position_span_2.  when inOrder=false 
there is nothing stoping position_span_1 == position_span_2 ... the fact 
that span_1 == span_2 doesn't matter.


-Hoss

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@lucene.apache.org
For additional commands, e-mail: dev-help@lucene.apache.org