org.apache.lucene.search.spans
Class FieldMaskingSpanQuery

java.lang.Object
  extended by org.apache.lucene.search.Query
      extended by org.apache.lucene.search.spans.SpanQuery
          extended by org.apache.lucene.search.spans.FieldMaskingSpanQuery
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable

public class FieldMaskingSpanQuery
extends org.apache.lucene.search.spans.SpanQuery

Wrapper to allow SpanQuery objects participate in composite single-field SpanQueries by 'lying' about their search field. That is, the masked SpanQuery will function as normal, but SpanQuery.getField() simply hands back the value supplied in this class's constructor.

This can be used to support Queries like SpanNearQuery or SpanOrQuery across different fields, which is not ordinarily permitted.

This can be useful for denormalized relational data: for example, when indexing a document with conceptually many 'children':

  teacherid: 1
  studentfirstname: james
  studentsurname: jones
  
  teacherid: 2
  studenfirstname: james
  studentsurname: smith
  studentfirstname: sally
  studentsurname: jones
 

a SpanNearQuery with a slop of 0 can be applied across two SpanTermQuery objects as follows:

    SpanQuery q1  = new SpanTermQuery(new Term("studentfirstname", "james"));
    SpanQuery q2  = new SpanTermQuery(new Term("studentsurname", "jones"));
    SpanQuery q2m new FieldMaskingSpanQuery(q2, "studentfirstname");
    Query q = new SpanNearQuery(new SpanQuery[]{q1, q2m}, -1, false);
 
to search for 'studentfirstname:james studentsurname:jones' and find teacherid 1 without matching teacherid 2 (which has a 'james' in position 0 and 'jones' in position 1).

Note: as getField() returns the masked field, scoring will be done using the norms of the field name supplied. This may lead to unexpected scoring behaviour.

See Also:
Serialized Form

Constructor Summary
FieldMaskingSpanQuery(org.apache.lucene.search.spans.SpanQuery maskedQuery, java.lang.String maskedField)
           
 
Method Summary
protected  org.apache.lucene.search.Weight createWeight(org.apache.lucene.search.Searcher searcher)
           
 boolean equals(java.lang.Object o)
           
 void extractTerms(java.util.Set terms)
           
 java.lang.String getField()
           
 org.apache.lucene.search.spans.SpanQuery getMaskedQuery()
           
 org.apache.lucene.search.spans.PayloadSpans getPayloadSpans(org.apache.lucene.index.IndexReader reader)
           
 org.apache.lucene.search.Similarity getSimilarity(org.apache.lucene.search.Searcher searcher)
           
 org.apache.lucene.search.spans.Spans getSpans(org.apache.lucene.index.IndexReader reader)
           
 java.util.Collection getTerms()
           
 int hashCode()
           
 org.apache.lucene.search.Query rewrite(org.apache.lucene.index.IndexReader reader)
           
 java.lang.String toString(java.lang.String field)
           
 
Methods inherited from class org.apache.lucene.search.Query
clone, combine, getBoost, mergeBooleanQueries, setBoost, toString, weight
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

FieldMaskingSpanQuery

public FieldMaskingSpanQuery(org.apache.lucene.search.spans.SpanQuery maskedQuery,
                             java.lang.String maskedField)
Method Detail

getField

public java.lang.String getField()
Specified by:
getField in class org.apache.lucene.search.spans.SpanQuery

getMaskedQuery

public org.apache.lucene.search.spans.SpanQuery getMaskedQuery()

getSpans

public org.apache.lucene.search.spans.Spans getSpans(org.apache.lucene.index.IndexReader reader)
                                              throws java.io.IOException
Specified by:
getSpans in class org.apache.lucene.search.spans.SpanQuery
Throws:
java.io.IOException

getPayloadSpans

public org.apache.lucene.search.spans.PayloadSpans getPayloadSpans(org.apache.lucene.index.IndexReader reader)
                                                            throws java.io.IOException
Overrides:
getPayloadSpans in class org.apache.lucene.search.spans.SpanQuery
Throws:
java.io.IOException

getTerms

public java.util.Collection getTerms()
Specified by:
getTerms in class org.apache.lucene.search.spans.SpanQuery

extractTerms

public void extractTerms(java.util.Set terms)
Overrides:
extractTerms in class org.apache.lucene.search.Query

createWeight

protected org.apache.lucene.search.Weight createWeight(org.apache.lucene.search.Searcher searcher)
                                                throws java.io.IOException
Overrides:
createWeight in class org.apache.lucene.search.spans.SpanQuery
Throws:
java.io.IOException

getSimilarity

public org.apache.lucene.search.Similarity getSimilarity(org.apache.lucene.search.Searcher searcher)
Overrides:
getSimilarity in class org.apache.lucene.search.Query

rewrite

public org.apache.lucene.search.Query rewrite(org.apache.lucene.index.IndexReader reader)
                                       throws java.io.IOException
Overrides:
rewrite in class org.apache.lucene.search.Query
Throws:
java.io.IOException

toString

public java.lang.String toString(java.lang.String field)
Specified by:
toString in class org.apache.lucene.search.Query

equals

public boolean equals(java.lang.Object o)
Overrides:
equals in class java.lang.Object

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

Copyright: (c) 2004-2006 Mayo Foundation for Medical Education and Research (MFMER). All rights reserved. MAYO, MAYO CLINIC, and the triple-shield Mayo logo are trademarks and service marks of MFMER.