You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@lucene.apache.org by Bernhard Messer <be...@intrafind.de> on 2003/10/29 17:27:02 UTC

MultiFieldQueryParser, can't change default search operator

hi all,

just played around with the MultiFieldQueryParser and didn't find a 
working way to change the "operator" value.

The problem is that MultiFieldQueryParser is implementing two public 
static methods "parse" only. Calling one of those, in the extended 
superclass, the static method implementation for "parse" is called. Due 
to the fact that the QueryParser class creates a new Instance for each 
call thru the static method, the "operator" flag is simply ignored.

There would be a simple fix within MultiFieldQueryParser class without 
touching the rest of the system. One have to add a new non static method 
"parse" which could look like this:

/***********************************************************/
public Query parse(String query, String[] fields) throws ParseException {
        BooleanQuery bQuery = new BooleanQuery();

        for (int i = 0; i < fields.length; i++)
        {
            QueryParser parser = new QueryParser(fields[i], analyzer);
            parser.setOperator(getOperator());
            Query q = parser.parse(query);
            bQuery.add(q, false, false);
        }
        return bQuery;
      }
/***********************************************************/


To test the new implementation, following code fragment can be used:

/***********************************************************/
            Directory directory = 
FSDirectory.getDirectory("/tmp/idx-test", false);
            Analyzer analyzer = new SimpleAnalyzer();
            Searcher searcher = new IndexSearcher(directory);
            Hits hits = null;
           
            String[] fields = { "contents", "title" };
            MultiFieldQueryParser parser = new 
MultiFieldQueryParser("britney spears", analyzer);
           
            parser.setOperator(QueryParser.DEFAULT_OPERATOR_AND);
                   
            Query query = parser.parse("britney spears", fields);
           
            System.out.println("Query: " + query.toString());
           
            hits = searcher.search(query);

            System.out.println ("Results: " + hits.length());
           
            searcher.close();
/***********************************************************/


best regards

Bernhard



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


Re: MultiFieldQueryParser, can't change default search operator

Posted by Erik Hatcher <er...@ehatchersolutions.com>.
There are settings other than operator that should be taken into 
account also (lowercase wildcard flag, default slop, and now locale).   
If you would be so kind as to add this as a Bugzilla issue, I'll add it 
to my to-do list.

	Erik


On Wednesday, October 29, 2003, at 11:27  AM, Bernhard Messer wrote:

> hi all,
>
> just played around with the MultiFieldQueryParser and didn't find a 
> working way to change the "operator" value.
>
> The problem is that MultiFieldQueryParser is implementing two public 
> static methods "parse" only. Calling one of those, in the extended 
> superclass, the static method implementation for "parse" is called. 
> Due to the fact that the QueryParser class creates a new Instance for 
> each call thru the static method, the "operator" flag is simply 
> ignored.
>
> There would be a simple fix within MultiFieldQueryParser class without 
> touching the rest of the system. One have to add a new non static 
> method "parse" which could look like this:
>
> /***********************************************************/
> public Query parse(String query, String[] fields) throws 
> ParseException {
>        BooleanQuery bQuery = new BooleanQuery();
>
>        for (int i = 0; i < fields.length; i++)
>        {
>            QueryParser parser = new QueryParser(fields[i], analyzer);
>            parser.setOperator(getOperator());
>            Query q = parser.parse(query);
>            bQuery.add(q, false, false);
>        }
>        return bQuery;
>      }
> /***********************************************************/
>
>
> To test the new implementation, following code fragment can be used:
>
> /***********************************************************/
>            Directory directory = 
> FSDirectory.getDirectory("/tmp/idx-test", false);
>            Analyzer analyzer = new SimpleAnalyzer();
>            Searcher searcher = new IndexSearcher(directory);
>            Hits hits = null;
>                      String[] fields = { "contents", "title" };
>            MultiFieldQueryParser parser = new 
> MultiFieldQueryParser("britney spears", analyzer);
>                      
> parser.setOperator(QueryParser.DEFAULT_OPERATOR_AND);
>                              Query query = parser.parse("britney 
> spears", fields);
>                      System.out.println("Query: " + query.toString());
>                      hits = searcher.search(query);
>
>            System.out.println ("Results: " + hits.length());
>                      searcher.close();
> /***********************************************************/
>
>
> best regards
>
> Bernhard
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: lucene-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: lucene-dev-help@jakarta.apache.org


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