> I was looking through some lucene
> source codes and found the following class
> org.apache.lucene.search.payloads.PayloadSpanUtil
> 
> There is a function named queryToSpanQuery in this class.
> Is this the
> preferred way to convert a PhraseQuery to
> PayloadNearQuery?

queryToSpanQuery method does not return PayloadNearQuery type.

You need to override getFieldQuery(String field, String queryText, int slop) of 
SolrQueryParser or QueryParser.

This code is modified from Lucene In Action Book (2nd edition) Chapter 6.3.4 
Allowing ordered phrase queries

protected Query getFieldQuery(String field, String queryText, int slop) throws 
ParseException {

        Query orig = super.getFieldQuery(field, queryText, slop);

        if (!(orig instanceof PhraseQuery)) return orig;

        PhraseQuery pq = (PhraseQuery) orig;
        Term[] terms = pq.getTerms(); 
        SpanQuery[] clauses = new SpanQuery[terms.length];

        for (int i = 0; i < terms.length; i++)
            clauses[i] = new PayloadTermQuery(terms[i], new 
AveragePayloadFunction());
        return new PayloadNearQuery(clauses, slop, true);

    }


> Also, are there any performance considerations while using
> a PayloadNearQuery instead of a PhraseQuery?

I don't think there will be significant performance difference. 


      

Reply via email to