You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-user@lucene.apache.org by Michel Conrad <mi...@trendiction.com> on 2013/01/17 17:01:42 UTC

Combine two BooleanQueries by a SpanNearQuery.

Hi,

I am looking to get a combination of multiple subqueries.

What I want to do is to have two queries which have to be near one to another.

As an example:
Query1: (A AND (B OR C))
Query2: D

Then I want to use something like a SpanNearQuery to combine both (slop 5):
Both would then have to match and D should be within slop 5 to A, B or C.

So my question is if there is a query that combines two BooleanQuery
trees into a SpanNearQuery.
It would have to take the terms that match Query 1 and the terms that
match Query 2, and look if there is a combination within the required
slop.

Can I rewrite the BooleanQuery after parsing the query as a
MultiTermQuery, than wrap these in SpanMultiTermQueryWrapper, which
can be combined by the SpanNearQuery?

Best regards,
Michel

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


Re: Combine two BooleanQueries by a SpanNearQuery.

Posted by Michel Conrad <mi...@trendiction.com>.
I created a feature request:
https://issues.apache.org/jira/browse/LUCENE-4696

Thanks for your help,
Michel

On Thu, Jan 17, 2013 at 6:33 PM, Jack Krupansky <ja...@basetechnology.com> wrote:
> Currently there isn't. SpanNearQuery can take only other SpanQuery objects,
> which includes other spans, span terms, and span wrapped multi-term queries
> (e.g., wildcard, fuzzy query), but not Boolean queries.
>
> But it does sound like a good feature request.
>
> There is SpanNotQuery, so you can exclude terms from a span.
>
>
> -- Jack Krupansky
>
> -----Original Message----- From: Michel Conrad
> Sent: Thursday, January 17, 2013 12:14 PM
> To: java-user@lucene.apache.org
> Subject: Re: Combine two BooleanQueries by a SpanNearQuery.
>
>
> The problem I would like to solve is to have two queries that I will
> get from the query parser (this could include wildcardqueries and
> phrasequeries).
> Both of these queries would have to match the document and as an
> additional restriction I would like to add that a matching term from
> the first query
> is near a matching term from the second query.
>
> So that you can search for instance for matching documents with
> in your first query 'apple AND NOT computer'
> and in your second 'monkey'
> with a slop of 10 between the two queries, then it would be equivalent
> to '"apple monkey"~10 AND NOT computer'.
>
> I was wondering if there is a method to combine more complicated
> queries in a similar way. (Some kind of generic solution)
>
> Thanks for your help,
>
> Michel
>
> On Thu, Jan 17, 2013 at 5:14 PM, Jack Krupansky <ja...@basetechnology.com>
> wrote:
>>
>> You need to express the "boolean" query solely in terms of SpanOrQuery and
>> SpanNearQuery. If you can't, ... then it probably can't be done, but you
>> should be able to.
>>
>> How about starting with a plan English description of the problem you are
>> trying to solve?
>>
>> -- Jack Krupansky
>>
>> -----Original Message----- From: Michel Conrad
>> Sent: Thursday, January 17, 2013 11:01 AM
>> To: java-user@lucene.apache.org
>> Subject: Combine two BooleanQueries by a SpanNearQuery.
>>
>>
>> Hi,
>>
>> I am looking to get a combination of multiple subqueries.
>>
>> What I want to do is to have two queries which have to be near one to
>> another.
>>
>> As an example:
>> Query1: (A AND (B OR C))
>> Query2: D
>>
>> Then I want to use something like a SpanNearQuery to combine both (slop
>> 5):
>> Both would then have to match and D should be within slop 5 to A, B or C.
>>
>> So my question is if there is a query that combines two BooleanQuery
>> trees into a SpanNearQuery.
>> It would have to take the terms that match Query 1 and the terms that
>> match Query 2, and look if there is a combination within the required
>> slop.
>>
>> Can I rewrite the BooleanQuery after parsing the query as a
>> MultiTermQuery, than wrap these in SpanMultiTermQueryWrapper, which
>> can be combined by the SpanNearQuery?
>>
>> Best regards,
>> Michel
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
>> For additional commands, e-mail: java-user-help@lucene.apache.org
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
>> For additional commands, e-mail: java-user-help@lucene.apache.org
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
> For additional commands, e-mail: java-user-help@lucene.apache.org

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


Re: Combine two BooleanQueries by a SpanNearQuery.

Posted by Jack Krupansky <ja...@basetechnology.com>.
Currently there isn't. SpanNearQuery can take only other SpanQuery objects, 
which includes other spans, span terms, and span wrapped multi-term queries 
(e.g., wildcard, fuzzy query), but not Boolean queries.

But it does sound like a good feature request.

There is SpanNotQuery, so you can exclude terms from a span.

-- Jack Krupansky

-----Original Message----- 
From: Michel Conrad
Sent: Thursday, January 17, 2013 12:14 PM
To: java-user@lucene.apache.org
Subject: Re: Combine two BooleanQueries by a SpanNearQuery.

The problem I would like to solve is to have two queries that I will
get from the query parser (this could include wildcardqueries and
phrasequeries).
Both of these queries would have to match the document and as an
additional restriction I would like to add that a matching term from
the first query
is near a matching term from the second query.

So that you can search for instance for matching documents with
in your first query 'apple AND NOT computer'
and in your second 'monkey'
with a slop of 10 between the two queries, then it would be equivalent
to '"apple monkey"~10 AND NOT computer'.

I was wondering if there is a method to combine more complicated
queries in a similar way. (Some kind of generic solution)

Thanks for your help,

Michel

On Thu, Jan 17, 2013 at 5:14 PM, Jack Krupansky <ja...@basetechnology.com> 
wrote:
> You need to express the "boolean" query solely in terms of SpanOrQuery and
> SpanNearQuery. If you can't, ... then it probably can't be done, but you
> should be able to.
>
> How about starting with a plan English description of the problem you are
> trying to solve?
>
> -- Jack Krupansky
>
> -----Original Message----- From: Michel Conrad
> Sent: Thursday, January 17, 2013 11:01 AM
> To: java-user@lucene.apache.org
> Subject: Combine two BooleanQueries by a SpanNearQuery.
>
>
> Hi,
>
> I am looking to get a combination of multiple subqueries.
>
> What I want to do is to have two queries which have to be near one to
> another.
>
> As an example:
> Query1: (A AND (B OR C))
> Query2: D
>
> Then I want to use something like a SpanNearQuery to combine both (slop 
> 5):
> Both would then have to match and D should be within slop 5 to A, B or C.
>
> So my question is if there is a query that combines two BooleanQuery
> trees into a SpanNearQuery.
> It would have to take the terms that match Query 1 and the terms that
> match Query 2, and look if there is a combination within the required
> slop.
>
> Can I rewrite the BooleanQuery after parsing the query as a
> MultiTermQuery, than wrap these in SpanMultiTermQueryWrapper, which
> can be combined by the SpanNearQuery?
>
> Best regards,
> Michel
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
> For additional commands, e-mail: java-user-help@lucene.apache.org
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
> For additional commands, e-mail: java-user-help@lucene.apache.org
>

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


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


RE: Combine two BooleanQueries by a SpanNearQuery.

Posted by Michael Ryan <mr...@moreover.com>.
I've had to do something exactly like this. My approach was to turn AND queries into a SpanNearQuery with a slop of Integer.MAX_VALUE and inOrder false, and to turn OR queries into a SpanOrQuery. It's a bit hacky, but is much simpler than creating your own Query class to implement this.

-Michael 

-----Original Message-----
From: Michel Conrad [mailto:michel.conrad@trendiction.com] 
Sent: Thursday, January 17, 2013 12:15 PM
To: java-user@lucene.apache.org
Subject: Re: Combine two BooleanQueries by a SpanNearQuery.

The problem I would like to solve is to have two queries that I will get from the query parser (this could include wildcardqueries and phrasequeries).
Both of these queries would have to match the document and as an additional restriction I would like to add that a matching term from the first query is near a matching term from the second query.

So that you can search for instance for matching documents with in your first query 'apple AND NOT computer'
and in your second 'monkey'
with a slop of 10 between the two queries, then it would be equivalent to '"apple monkey"~10 AND NOT computer'.

I was wondering if there is a method to combine more complicated queries in a similar way. (Some kind of generic solution)

Thanks for your help,

Michel

On Thu, Jan 17, 2013 at 5:14 PM, Jack Krupansky <ja...@basetechnology.com> wrote:
> You need to express the "boolean" query solely in terms of SpanOrQuery 
> and SpanNearQuery. If you can't, ... then it probably can't be done, 
> but you should be able to.
>
> How about starting with a plan English description of the problem you 
> are trying to solve?
>
> -- Jack Krupansky
>
> -----Original Message----- From: Michel Conrad
> Sent: Thursday, January 17, 2013 11:01 AM
> To: java-user@lucene.apache.org
> Subject: Combine two BooleanQueries by a SpanNearQuery.
>
>
> Hi,
>
> I am looking to get a combination of multiple subqueries.
>
> What I want to do is to have two queries which have to be near one to 
> another.
>
> As an example:
> Query1: (A AND (B OR C))
> Query2: D
>
> Then I want to use something like a SpanNearQuery to combine both (slop 5):
> Both would then have to match and D should be within slop 5 to A, B or C.
>
> So my question is if there is a query that combines two BooleanQuery 
> trees into a SpanNearQuery.
> It would have to take the terms that match Query 1 and the terms that 
> match Query 2, and look if there is a combination within the required 
> slop.
>
> Can I rewrite the BooleanQuery after parsing the query as a 
> MultiTermQuery, than wrap these in SpanMultiTermQueryWrapper, which 
> can be combined by the SpanNearQuery?
>
> Best regards,
> Michel
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
> For additional commands, e-mail: java-user-help@lucene.apache.org
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
> For additional commands, e-mail: java-user-help@lucene.apache.org
>

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


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


Re: Combine two BooleanQueries by a SpanNearQuery.

Posted by Michel Conrad <mi...@trendiction.com>.
The problem I would like to solve is to have two queries that I will
get from the query parser (this could include wildcardqueries and
phrasequeries).
Both of these queries would have to match the document and as an
additional restriction I would like to add that a matching term from
the first query
is near a matching term from the second query.

So that you can search for instance for matching documents with
in your first query 'apple AND NOT computer'
and in your second 'monkey'
with a slop of 10 between the two queries, then it would be equivalent
to '"apple monkey"~10 AND NOT computer'.

I was wondering if there is a method to combine more complicated
queries in a similar way. (Some kind of generic solution)

Thanks for your help,

Michel

On Thu, Jan 17, 2013 at 5:14 PM, Jack Krupansky <ja...@basetechnology.com> wrote:
> You need to express the "boolean" query solely in terms of SpanOrQuery and
> SpanNearQuery. If you can't, ... then it probably can't be done, but you
> should be able to.
>
> How about starting with a plan English description of the problem you are
> trying to solve?
>
> -- Jack Krupansky
>
> -----Original Message----- From: Michel Conrad
> Sent: Thursday, January 17, 2013 11:01 AM
> To: java-user@lucene.apache.org
> Subject: Combine two BooleanQueries by a SpanNearQuery.
>
>
> Hi,
>
> I am looking to get a combination of multiple subqueries.
>
> What I want to do is to have two queries which have to be near one to
> another.
>
> As an example:
> Query1: (A AND (B OR C))
> Query2: D
>
> Then I want to use something like a SpanNearQuery to combine both (slop 5):
> Both would then have to match and D should be within slop 5 to A, B or C.
>
> So my question is if there is a query that combines two BooleanQuery
> trees into a SpanNearQuery.
> It would have to take the terms that match Query 1 and the terms that
> match Query 2, and look if there is a combination within the required
> slop.
>
> Can I rewrite the BooleanQuery after parsing the query as a
> MultiTermQuery, than wrap these in SpanMultiTermQueryWrapper, which
> can be combined by the SpanNearQuery?
>
> Best regards,
> Michel
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
> For additional commands, e-mail: java-user-help@lucene.apache.org
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
> For additional commands, e-mail: java-user-help@lucene.apache.org
>

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


Re: Combine two BooleanQueries by a SpanNearQuery.

Posted by Jack Krupansky <ja...@basetechnology.com>.
You need to express the "boolean" query solely in terms of SpanOrQuery and 
SpanNearQuery. If you can't, ... then it probably can't be done, but you 
should be able to.

How about starting with a plan English description of the problem you are 
trying to solve?

-- Jack Krupansky

-----Original Message----- 
From: Michel Conrad
Sent: Thursday, January 17, 2013 11:01 AM
To: java-user@lucene.apache.org
Subject: Combine two BooleanQueries by a SpanNearQuery.

Hi,

I am looking to get a combination of multiple subqueries.

What I want to do is to have two queries which have to be near one to 
another.

As an example:
Query1: (A AND (B OR C))
Query2: D

Then I want to use something like a SpanNearQuery to combine both (slop 5):
Both would then have to match and D should be within slop 5 to A, B or C.

So my question is if there is a query that combines two BooleanQuery
trees into a SpanNearQuery.
It would have to take the terms that match Query 1 and the terms that
match Query 2, and look if there is a combination within the required
slop.

Can I rewrite the BooleanQuery after parsing the query as a
MultiTermQuery, than wrap these in SpanMultiTermQueryWrapper, which
can be combined by the SpanNearQuery?

Best regards,
Michel

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


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