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 da...@ontrenet.com on 2011/05/31 20:24:07 UTC

Obtaining query AST?

Hi,
 I want to write my own query expander. It needs to obtain the AST
(abstract syntax tree) of an already parsed query string, navigate to
certain parts of it (words) and make logical phrases of those words by
adding to the AST - where necessary.

This cannot be done to the string because the query logic cannot be
semantically altered. (e.g. AND, OR, paren's etc) so it must be parsed
first.

How can this be done with SolrJ?

thanks for any tips.
Darren



Re: Obtaining query AST?

Posted by Darren Govoni <da...@ontrenet.com>.
Thats pretty awesome. Thanks Renaud!

On Tue, 2011-05-31 at 22:56 +0100, Renaud Delbru wrote:

> Hi,
> 
> have a look at the flexible query parser of lucene (contrib package) 
> [1]. It provides a framework to easily create different parsing logic. 
> You should be able to access the AST and to modify as you want how it 
> can be translated into a Lucene query (look at processors and pipeline 
> processors).
> One time you have your own query parser, then it is straightforward to 
> plug it into Solr.
> 
> [1] http://lucene.apache.org/java/3_1_0/api/contrib-queryparser/index.html



Re: Obtaining query AST?

Posted by Renaud Delbru <re...@deri.org>.
Hi,

have a look at the flexible query parser of lucene (contrib package) 
[1]. It provides a framework to easily create different parsing logic. 
You should be able to access the AST and to modify as you want how it 
can be translated into a Lucene query (look at processors and pipeline 
processors).
One time you have your own query parser, then it is straightforward to 
plug it into Solr.

[1] http://lucene.apache.org/java/3_1_0/api/contrib-queryparser/index.html
-- 
Renaud Delbru

On 31/05/11 19:24, darren@ontrenet.com wrote:
> Hi,
>   I want to write my own query expander. It needs to obtain the AST
> (abstract syntax tree) of an already parsed query string, navigate to
> certain parts of it (words) and make logical phrases of those words by
> adding to the AST - where necessary.
>
> This cannot be done to the string because the query logic cannot be
> semantically altered. (e.g. AND, OR, paren's etc) so it must be parsed
> first.
>
> How can this be done with SolrJ?
>
> thanks for any tips.
> Darren
>
>


Re: Obtaining query AST?

Posted by Darren Govoni <da...@ontrenet.com>.
Ludovic,
   Thank you for this tip, it sounds useful.

Darren

On Tue, 2011-05-31 at 14:38 -0700, lboutros wrote:

> Darren,
> 
> you can even take a look to the DebugComponent which returns the parsed
> query in a string form.
> It uses the QueryParsing class to parse the query, you could perhaps do the
> same.
> 
> Ludovic.
> 
> -----
> Jouve
> France.
> --
> View this message in context: http://lucene.472066.n3.nabble.com/Obtaining-query-AST-tp3007289p3008349.html
> Sent from the Solr - User mailing list archive at Nabble.com.



Re: Obtaining query AST?

Posted by lboutros <bo...@gmail.com>.
Darren,

you can even take a look to the DebugComponent which returns the parsed
query in a string form.
It uses the QueryParsing class to parse the query, you could perhaps do the
same.

Ludovic.

-----
Jouve
France.
--
View this message in context: http://lucene.472066.n3.nabble.com/Obtaining-query-AST-tp3007289p3008349.html
Sent from the Solr - User mailing list archive at Nabble.com.

Re: Obtaining query AST?

Posted by lboutros <bo...@gmail.com>.
Hi Darren,

I think that if I had to get the parsing result, I would create my own
QueryComponent which would create the parser in the 'prepare' function (you
can take a look to the actual QueryComponent class) and instead of resolving
the query in the 'process' function, I would just parse the query and then
it should be possible to serialize the returned Query object to the
response.

Then you could declare this new query component in the solr config file.
And finally, with solrj, you should be able to get the parsed query in the
response, unserialize it and do your stuff ;)

The Query object could be considered as an AST, I think :).

This is how I would start, if I had to do that.

Ludovic. 

 

-----
Jouve
France.
--
View this message in context: http://lucene.472066.n3.nabble.com/Obtaining-query-AST-tp3007289p3008330.html
Sent from the Solr - User mailing list archive at Nabble.com.

Re: Obtaining query AST?

Posted by Jonathan Rochkind <ro...@jhu.edu>.
You're going to have to parse it yourself.  Or, since Solr is open 
source, you can take pieces of the existing query parsers (dismax or 
lucene), and repurpose them. But I don't _think_ (I could be wrong) 
there is any public API in Solr/SolrJ that will give you an AST.

On 5/31/2011 3:18 PM, darren@ontrenet.com wrote:
> Hi, thanks for the tip. I noticed the XML stuff, but the trouble is I am
> taking a query string entered by a user such as "this OR that AND (this
> AND that)" so I'm not sure how to go from that to a representational AST
> parse tree...
>
>> I believe there is a query parser that accepts queries formatted in XML,
>> allowing you to provide a parse tree to Solr; perhaps that would get you
>> the control you're after.
>>
>> -Mike
>>
>> On 05/31/2011 02:24 PM, darren@ontrenet.com wrote:
>>> Hi,
>>>    I want to write my own query expander. It needs to obtain the AST
>>> (abstract syntax tree) of an already parsed query string, navigate to
>>> certain parts of it (words) and make logical phrases of those words by
>>> adding to the AST - where necessary.
>>>
>>> This cannot be done to the string because the query logic cannot be
>>> semantically altered. (e.g. AND, OR, paren's etc) so it must be parsed
>>> first.
>>>
>>> How can this be done with SolrJ?
>>>
>>> thanks for any tips.
>>> Darren
>>>
>>>
>>>
>

Re: Obtaining query AST?

Posted by da...@ontrenet.com.
Hi, thanks for the tip. I noticed the XML stuff, but the trouble is I am
taking a query string entered by a user such as "this OR that AND (this
AND that)" so I'm not sure how to go from that to a representational AST
parse tree...

> I believe there is a query parser that accepts queries formatted in XML,
> allowing you to provide a parse tree to Solr; perhaps that would get you
> the control you're after.
>
> -Mike
>
> On 05/31/2011 02:24 PM, darren@ontrenet.com wrote:
>> Hi,
>>   I want to write my own query expander. It needs to obtain the AST
>> (abstract syntax tree) of an already parsed query string, navigate to
>> certain parts of it (words) and make logical phrases of those words by
>> adding to the AST - where necessary.
>>
>> This cannot be done to the string because the query logic cannot be
>> semantically altered. (e.g. AND, OR, paren's etc) so it must be parsed
>> first.
>>
>> How can this be done with SolrJ?
>>
>> thanks for any tips.
>> Darren
>>
>>
>>
>


Re: Obtaining query AST?

Posted by Mike Sokolov <so...@ifactory.com>.
I believe there is a query parser that accepts queries formatted in XML, 
allowing you to provide a parse tree to Solr; perhaps that would get you 
the control you're after.

-Mike

On 05/31/2011 02:24 PM, darren@ontrenet.com wrote:
> Hi,
>   I want to write my own query expander. It needs to obtain the AST
> (abstract syntax tree) of an already parsed query string, navigate to
> certain parts of it (words) and make logical phrases of those words by
> adding to the AST - where necessary.
>
> This cannot be done to the string because the query logic cannot be
> semantically altered. (e.g. AND, OR, paren's etc) so it must be parsed
> first.
>
> How can this be done with SolrJ?
>
> thanks for any tips.
> Darren
>
>
>