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 Chris Harris <ry...@gmail.com> on 2009/04/01 01:26:34 UTC

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

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>