lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Eric Jain <>
Subject Re: Weighted queries
Date Fri, 06 Aug 2004 16:06:48 GMT
>>   (title:foo^4 OR abstract:foo^2 OR content:foo) AND
>>   (title:bar^4 OR abstract:bar^2 OR content:bar)

> That's not the way MultiFieldQueryParser will rewrite your query.

You are right - what happens is this:

   (title:foo OR title:bar) OR
   (abstract:foo OR abstract:bar) OR
   (content:foo OR content:bar) OR

Looks like a dead end... On the other hand I just realize I could 
subclass the QueryParser, e.g.:

import java.util.Vector;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;

public class CustomQueryParser
   extends QueryParser
   private String[] fields;

   public CustomQueryParser(String[] fields, Analyzer analyzer)
     super(null, analyzer);
     this.fields = fields;

   protected Query getFieldQuery(String field, Analyzer analyzer, String 
     throws ParseException
     if (field == null)
       Vector clauses = new Vector();
       for (int i = 0; i < fields.length; i++)
         clauses.add(new BooleanClause(super.getFieldQuery(fields[i], 
analyzer, queryText), false, false));
       return getBooleanQuery(clauses);

     return super.getFieldQuery(field, analyzer, queryText);


String[] fields = new String[] { "title", "abstract", "content" };
QueryParser parser = new CustomQueryParser(fields, new SimpleAnalyzer());
Query query = parser.parse("foo -bar (baz OR title:bla)");
System.out.println("? " + query);


? +(title:foo abstract:foo content:foo) -(title:bar abstract:bar 
content:bar) +((title:baz abstract:baz content:baz) title:bla)


To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message