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 Development Team <de...@gmail.com> on 2009/03/31 20:13:12 UTC

How to create a query directly (bypassing the query-parser)?

Hi everybody, after reading the documentation on the Solr site, I have the
following newbie-ish question:

On the Lucene query parser syntax page (
http://lucene.apache.org/java/2_4_0/queryparsersyntax.html) linked to from
the Solr query syntax page, they mention:
"If you are programmatically generating a query string and then parsing it
with the query parser then you should seriously consider building your
queries directly with the query API. In other words, the query parser is
designed for human-entered text, not for program-generated text."

What do they mean by "using the API"? If I use SolrJ to construct a
SolrQuery, doesn't that get processed by the query parser? How do I bypass
the query parser to set up a query directly?

Especially for token-values (values that fit a defined set, such as Enum
values), it seems silly for me to continually be appending, "+tokenField:(1,
2, 3)" to my query. Why should I write code to construct the query string,
then send this to the parser to parse the string into an object? Can't I set
these query parameters directly? If so, how?

- Daryl.

Re: How to create a query directly (bypassing the query-parser)?

Posted by Chris Harris <ry...@gmail.com>.
2009/3/31 Erik Hatcher <er...@ehatchersolutions.com>:
>
> On Mar 31, 2009, at 2:13 PM, Development Team wrote:
>>
>> On the Lucene query parser syntax page (
>> http://lucene.apache.org/java/2_4_0/queryparsersyntax.html) linked to from
>> the Solr query syntax page, they mention:
>> "If you are programmatically generating a query string and then parsing it
>> with the query parser then you should seriously consider building your
>> queries directly with the query API. In other words, the query parser is
>> designed for human-entered text, not for program-generated text."
>>
>> What do they mean by "using the API"? If I use SolrJ to construct a
>> SolrQuery, doesn't that get processed by the query parser? How do I bypass
>> the query parser to set up a query directly?
>
> Without a custom QParser implementation, there is no way to avoid the string
> concatenation -> parser step, even using SolrJ.  The bit you refer to in the
> Lucene query parser documentation is at the Lucene API level, which is only
> accessible via QParser plugins, not by a Solr client API.
>
>        Erik
>

One way to get a step removed from raw string manipulations would be
to install the Xml Query Parser patch for Solr

  https://issues.apache.org/jira/browse/SOLR-839

and then to construct XML representations of the desired queries using
your client language's standard XML libraries. Here's an example of
the sort of XML that you might then pass as your query string:

<BooleanQuery fieldName="contents">
	             <Clause occurs="should">
		              <TermQuery>merger</TermQuery>
	             </Clause>
	             <Clause occurs="mustnot">
		              <TermQuery>sumitomo</TermQuery>
	             </Clause>
	             <Clause occurs="must">
		              <TermQuery>bank</TermQuery>
	             </Clause>
            </BooleanQuery>

Re: How to create a query directly (bypassing the query-parser)?

Posted by Erik Hatcher <er...@ehatchersolutions.com>.
On Mar 31, 2009, at 2:13 PM, Development Team wrote:
> On the Lucene query parser syntax page (
> http://lucene.apache.org/java/2_4_0/queryparsersyntax.html) linked  
> to from
> the Solr query syntax page, they mention:
> "If you are programmatically generating a query string and then  
> parsing it
> with the query parser then you should seriously consider building your
> queries directly with the query API. In other words, the query  
> parser is
> designed for human-entered text, not for program-generated text."
>
> What do they mean by "using the API"? If I use SolrJ to construct a
> SolrQuery, doesn't that get processed by the query parser? How do I  
> bypass
> the query parser to set up a query directly?
>
> Especially for token-values (values that fit a defined set, such as  
> Enum
> values), it seems silly for me to continually be appending,  
> "+tokenField:(1,
> 2, 3)" to my query. Why should I write code to construct the query  
> string,
> then send this to the parser to parse the string into an object?  
> Can't I set
> these query parameters directly? If so, how?

Without a custom QParser implementation, there is no way to avoid the  
string concatenation -> parser step, even using SolrJ.  The bit you  
refer to in the Lucene query parser documentation is at the Lucene API  
level, which is only accessible via QParser plugins, not by a Solr  
client API.

	Erik