You are viewing a plain text version of this content. The canonical link for it is here.
Posted to solr-user@lucene.apache.org by gdeconto <ge...@topproducer.com> on 2009/10/05 17:29:03 UTC

about modifying querystring parameters submitted to solr

sorry if this is a very simple question, but I am stuck (and online searches
for this info havent been fruitful).

Lets say that, in certain circumstances, I want to change the field names
and/or field query values being passed to SOLR.

For example, lets say my unmodified query is
"http://localhost:8994/solr/select?q=xxx:[* TO 3] AND yyy:[3 TO
*]&defType=myQParser" and (JUST for the sake of argument) lets say I want to
rewrite it as "http://localhost:8994/solr/select?q=aaa:[1 TO 2] AND bbb:[3
TO 10]&defType=myQParser".

I think I can do it by extending QParserPlugin, and overriding the
createParser method (see my code snippet below). The qstr parameter
apparently contains the parts I want to examine and/or modify.

now to my questions:
1. is that the correct location to do this sort of manipulation?
2. is there an existing method for parsing out the fields and their
parameters? i.e. to break a qstr of "xxx:[* TO 3] AND yyy:[3 TO *]" into an
array something like  x[0][0] = "xxx", x[0][1]="* TO 3", x[1][0] = "yyy",
x[1][1]="3 TO *".  Or possibly even finer granularity than that.  I could
write it myself but its much nicer not to have to (especially since the
queries could be very complex).

thanks in advance for any help.
--------------------------------------------------------
package com.topproducer.rentals.solr.search;

import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.search.Query;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.search.QParser;
import org.apache.solr.search.QParserPlugin;

public class myQParserPlugin extends QParserPlugin {

        @Override
        public QParser createParser(String qstr, SolrParams localParams,
SolrParams params, SolrQueryRequest req)
        {
                return new QParser(qstr, localParams, params, req) {
                  QParser baseParser;

                  public Query parse() throws ParseException {
                        StringBuilder queryBuilder = new StringBuilder();

                        // extract and/or view and/or change qstr content
here
                        // ..
                        // is there an existing function/method to parse
qstr into its component parts?
                        // i.e. to break "?q=xxx:[1 TO 3] AND yyy:[3 TO *]"
into something like:
                        // x[0][0] = "xxx", x[0][1]="1 TO 3"
                        // x[1][0] = "xxx", x[1][1]="3 TO *"

                        // after modifying qstr, store it into queryBuilder
here
                        queryBuild.append(new_qstr);


                        // prepare queryBuilder for any additional solr
handling
                        baseParser = subQuery(queryBuilder.toString(),
null);
                        Query q = baseParser.parse();
                        return q;
                  }


                  public String[] getDefaultHighlightFields() {
                        return baseParser.getDefaultHighlightFields();
                  }
                                                                                           
                  public Query getHighlightQuery() throws ParseException {
                        return baseParser.getHighlightQuery();
                  }

                  public void addDebugInfo(NamedList debugInfo) {
                        baseParser.addDebugInfo(debugInfo);
                  }
                };
          }

        @Override
        public void init(NamedList arg0) {
               
                // TODO Auto-generated method stub
               
        }
} 
-- 
View this message in context: http://www.nabble.com/about-modifying-querystring-parameters-submitted-to-solr-tp25752898p25752898.html
Sent from the Solr - User mailing list archive at Nabble.com.


Re: about modifying querystring parameters submitted to solr

Posted by gdeconto <ge...@topproducer.com>.
Thanks for the reply hossman.

I have successfully created a custom QParserPlugin that implements some
special syntax to help me handle my many dynamic fields.

The syntax looks something like this:

  http://127.0.0.1:8994/solr/select?q=@fn:(1,2,3,4)

Had to create java code to extract the virtual function and its parameters. 
Will look at your comment further to see if it can help me any with parsing.



hossman wrote:
> 
> 
> : 2. is there an existing method for parsing out the fields and their
> : parameters? i.e. to break a qstr of "xxx:[* TO 3] AND yyy:[3 TO *]" into
> an
> : array something like  x[0][0] = "xxx", x[0][1]="* TO 3", x[1][0] =
> "yyy",
> : x[1][1]="3 TO *".  Or possibly even finer granularity than that.  I
> could
> : write it myself but its much nicer not to have to (especially since the
> : queries could be very complex).
> 
> if you wnat to reuse the existing Lucene syntax (and based on your example 
> here it seems like you do) then your best bet is probably to have your 
> QParser create a subclass of the Lucen QueryParser where you override each 
> of the get methods to inspect the "field" argument, change it as you see 
> fit, and then delegate to the superclass to build the actual query object.  
> that's the closest thing to what you describe.
> 
> 
> -Hoss
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/about-modifying-querystring-parameters-submitted-to-solr-tp25752898p25827695.html
Sent from the Solr - User mailing list archive at Nabble.com.


Re: about modifying querystring parameters submitted to solr

Posted by Chris Hostetter <ho...@fucit.org>.
: For example, lets say my unmodified query is
: "http://localhost:8994/solr/select?q=xxx:[* TO 3] AND yyy:[3 TO
: *]&defType=myQParser" and (JUST for the sake of argument) lets say I want to
: rewrite it as "http://localhost:8994/solr/select?q=aaa:[1 TO 2] AND bbb:[3
: TO 10]&defType=myQParser".
: 
: I think I can do it by extending QParserPlugin, and overriding the
: createParser method (see my code snippet below). The qstr parameter
: apparently contains the parts I want to examine and/or modify.
: 
: now to my questions:
: 1. is that the correct location to do this sort of manipulation?

yes, you'd definitely need a custom QParserPlugin, but you have a lot of 
options in what that plugin should do...

: 2. is there an existing method for parsing out the fields and their
: parameters? i.e. to break a qstr of "xxx:[* TO 3] AND yyy:[3 TO *]" into an
: array something like  x[0][0] = "xxx", x[0][1]="* TO 3", x[1][0] = "yyy",
: x[1][1]="3 TO *".  Or possibly even finer granularity than that.  I could
: write it myself but its much nicer not to have to (especially since the
: queries could be very complex).

if you wnat to reuse the existing Lucene syntax (and based on your example 
here it seems like you do) then your best bet is probably to have your 
QParser create a subclass of the Lucen QueryParser where you override each 
of the get methods to inspect the "field" argument, change it as you see 
fit, and then delegate to the superclass to build the actual query object.  
that's the closest thing to what you describe.


-Hoss


Re: about modifying querystring parameters submitted to solr

Posted by gdeconto <ge...@topproducer.com>.
Note that there is a similar question in 
http://www.nabble.com/TermsComponent-to25302503.html#a25312549
http://www.nabble.com/TermsComponent-to25302503.html#a25312549 


-- 
View this message in context: http://www.nabble.com/about-modifying-querystring-parameters-submitted-to-solr-tp25752898p25754727.html
Sent from the Solr - User mailing list archive at Nabble.com.