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 Mark N <ni...@gmail.com> on 2009/11/30 08:49:25 UTC

nested solr queries

Is it possible to write nested queries in Solr similar to sql like query
where  I can take results of the first query and use one or more of its
fields as an argument in the second query.


For example:

field1:XYZ AND (_query_: field3:{value of field4})

This should search for all types of XYZ and then iterate over the result set
and perform a query for where field3  is equal to the value of field1 from
each item of the first result set.

this is similar to SQL like query


select distinct ( fieldA ) from table where fieldA  IN

Re: nested solr queries

Posted by Chris Hostetter <ho...@fucit.org>.
: thanks for your help so do you think I should execute solr queries twice ?
: or is there any other workarounds

http://people.apache.org/~hossman/#xyproblem
XY Problem

Your question appears to be an "XY Problem" ... that is: you are dealing
with "X", you are assuming "Y" will help you, and you are asking about "Y"
without giving more details about the "X" so that we can understand the
full issue.  Perhaps the best solution doesn't involve "Y" at all?
See Also: http://www.perlmonks.org/index.pl?node_id=542341




-Hoss


RE: nested solr queries

Posted by "Smiley, David W." <ds...@mitre.org>.
Shameless plug here.... if you're having trouble grasping the schema and differences from relational databases then I think you'll find my book helpful (chapter 2).
https://www.packtpub.com/solr-1-4-enterprise-search-server/book
~ David Smiley

________________________________________
From: Mark N [nipen.mark@gmail.com]
Sent: Monday, November 30, 2009 4:36 AM
To: solr-user@lucene.apache.org
Subject: Re: nested solr queries

thanks for your help so do you think I should execute solr queries twice ?
or is there any other workarounds




On Mon, Nov 30, 2009 at 3:07 PM, Shalin Shekhar Mangar <
shalinmangar@gmail.com> wrote:

> On Mon, Nov 30, 2009 at 2:26 PM, Mark N <ni...@gmail.com> wrote:
>
> > field2="xyz" we dont know until we run query1
> >
> >
> Ah, ok. I thought xyz was a literal that you wanted to search.
>
>
> > To simply i was actually trying to do some kind of JOIN similar to
> > following
> > SQL query
> >
> >
> >  select  * from table1  where  *field2*  in
> >  ( select *field2  *from dbo.concept_db where field1='ABC' )
> >
> > if this is not possible then i will have to search inner query  (
> > select *field2
> > *from dbo.concept_db where field1='ABC' )  first and then only  run the
> > outer query
> >
> >
> No, there are no joins in Solr. Consider de-normalizing your schema, if you
> haven't.
>
> --
> Regards,
> Shalin Shekhar Mangar.
>



--
Nipen Mark

Re: nested solr queries

Posted by Shalin Shekhar Mangar <sh...@gmail.com>.
I don't know the use-case so I cannot suggest anything. Solr is different
from relational databases and techniques which are taken for granted in the
RDBMS world are usually not required or have bad performance
characteristics. You shouldn't try to solve problems the same way in solr
and databases.

You have told us that you need to do joins but you haven't told us *why* you
need to do joins. There maybe other ways of solving the same problem. If
not, two queries may be the only way to go.

On Mon, Nov 30, 2009 at 3:06 PM, Mark N <ni...@gmail.com> wrote:

> thanks for your help so do you think I should execute solr queries twice ?
> or is there any other workarounds
>
>
>
>
> On Mon, Nov 30, 2009 at 3:07 PM, Shalin Shekhar Mangar <
> shalinmangar@gmail.com> wrote:
>
> > On Mon, Nov 30, 2009 at 2:26 PM, Mark N <ni...@gmail.com> wrote:
> >
> > > field2="xyz" we dont know until we run query1
> > >
> > >
> > Ah, ok. I thought xyz was a literal that you wanted to search.
> >
> >
> > > To simply i was actually trying to do some kind of JOIN similar to
> > > following
> > > SQL query
> > >
> > >
> > >  select  * from table1  where  *field2*  in
> > >  ( select *field2  *from dbo.concept_db where field1='ABC' )
> > >
> > > if this is not possible then i will have to search inner query  (
> > > select *field2
> > > *from dbo.concept_db where field1='ABC' )  first and then only  run the
> > > outer query
> > >
> > >
> > No, there are no joins in Solr. Consider de-normalizing your schema, if
> you
> > haven't.
> >
> > --
> > Regards,
> > Shalin Shekhar Mangar.
> >
>
>
>
> --
> Nipen Mark
>



-- 
Regards,
Shalin Shekhar Mangar.

Re: nested solr queries

Posted by Mark N <ni...@gmail.com>.
thanks for your help so do you think I should execute solr queries twice ?
or is there any other workarounds




On Mon, Nov 30, 2009 at 3:07 PM, Shalin Shekhar Mangar <
shalinmangar@gmail.com> wrote:

> On Mon, Nov 30, 2009 at 2:26 PM, Mark N <ni...@gmail.com> wrote:
>
> > field2="xyz" we dont know until we run query1
> >
> >
> Ah, ok. I thought xyz was a literal that you wanted to search.
>
>
> > To simply i was actually trying to do some kind of JOIN similar to
> > following
> > SQL query
> >
> >
> >  select  * from table1  where  *field2*  in
> >  ( select *field2  *from dbo.concept_db where field1='ABC' )
> >
> > if this is not possible then i will have to search inner query  (
> > select *field2
> > *from dbo.concept_db where field1='ABC' )  first and then only  run the
> > outer query
> >
> >
> No, there are no joins in Solr. Consider de-normalizing your schema, if you
> haven't.
>
> --
> Regards,
> Shalin Shekhar Mangar.
>



-- 
Nipen Mark

Re: nested solr queries

Posted by Shalin Shekhar Mangar <sh...@gmail.com>.
On Mon, Nov 30, 2009 at 2:26 PM, Mark N <ni...@gmail.com> wrote:

> field2="xyz" we dont know until we run query1
>
>
Ah, ok. I thought xyz was a literal that you wanted to search.


> To simply i was actually trying to do some kind of JOIN similar to
> following
> SQL query
>
>
>  select  * from table1  where  *field2*  in
>  ( select *field2  *from dbo.concept_db where field1='ABC' )
>
> if this is not possible then i will have to search inner query  (
> select *field2
> *from dbo.concept_db where field1='ABC' )  first and then only  run the
> outer query
>
>
No, there are no joins in Solr. Consider de-normalizing your schema, if you
haven't.

-- 
Regards,
Shalin Shekhar Mangar.

Re: nested solr queries

Posted by Mark N <ni...@gmail.com>.
field2="xyz" we dont know until we run query1

To simply i was actually trying to do some kind of JOIN similar to following
SQL query


 select  * from table1  where  *field2*  in
 ( select *field2  *from dbo.concept_db where field1='ABC' )

if this is not possible then i will have to search inner query  (
select *field2
*from dbo.concept_db where field1='ABC' )  first and then only  run the
outer query

thanks
chandan




On Mon, Nov 30, 2009 at 2:25 PM, Shalin Shekhar Mangar <
shalinmangar@gmail.com> wrote:

> On Mon, Nov 30, 2009 at 2:02 PM, Mark N <ni...@gmail.com> wrote:
>
> > hi shalin
> >
> > I am trying to achieve something like JOIN. Previously am doing this with
> > two queries on solr
> >
> > solr index  = ( field1 ,field 2, field3)
> >
> > query1 = (  for  example field1="ABC" )
> >
> > suppose query1 returns results set1= { 1, 2 ,3 ,4 } which matches query1
> >
> > query2 = (   get all records having field2="xyz" for each records  i.e
>  for
> > set1= {1,2,3,4} returned by query1 )
> >
> >
> That sequence of queries will return documents which have field1="ABC" and
> field2="xyz". The same result can be obtained in one query with
> q=+field1:"ABC" +field2:"xyz"
>
> Have I misunderstood the problem?
>
>
> > Am not sure if I could do something like this using the nested solr query
> > from link
> >
> > http://www.lucidimagination.com/blog/2009/03/31/nested-queries-in-solr/
> >
> >
> No, nested queries can only influence scores. They do not filter the
> results.
>
> --
> Regards,
> Shalin Shekhar Mangar.
>

Re: nested solr queries

Posted by Shalin Shekhar Mangar <sh...@gmail.com>.
On Mon, Nov 30, 2009 at 2:02 PM, Mark N <ni...@gmail.com> wrote:

> hi shalin
>
> I am trying to achieve something like JOIN. Previously am doing this with
> two queries on solr
>
> solr index  = ( field1 ,field 2, field3)
>
> query1 = (  for  example field1="ABC" )
>
> suppose query1 returns results set1= { 1, 2 ,3 ,4 } which matches query1
>
> query2 = (   get all records having field2="xyz" for each records  i.e  for
> set1= {1,2,3,4} returned by query1 )
>
>
That sequence of queries will return documents which have field1="ABC" and
field2="xyz". The same result can be obtained in one query with
q=+field1:"ABC" +field2:"xyz"

Have I misunderstood the problem?


> Am not sure if I could do something like this using the nested solr query
> from link
>
> http://www.lucidimagination.com/blog/2009/03/31/nested-queries-in-solr/
>
>
No, nested queries can only influence scores. They do not filter the
results.

-- 
Regards,
Shalin Shekhar Mangar.

Re: nested solr queries

Posted by Mark N <ni...@gmail.com>.
hi shalin

I am trying to achieve something like JOIN. Previously am doing this with
two queries on solr

solr index  = ( field1 ,field 2, field3)

query1 = (  for  example field1="ABC" )

suppose query1 returns results set1= { 1, 2 ,3 ,4 } which matches query1

query2 = (   get all records having field2="xyz" for each records  i.e  for
set1= {1,2,3,4} returned by query1 )

Am not sure if I could do something like this using the nested solr query
from link

http://www.lucidimagination.com/blog/2009/03/31/nested-queries-in-solr/



thanks


On Mon, Nov 30, 2009 at 1:50 PM, Shalin Shekhar Mangar <
shalinmangar@gmail.com> wrote:

> On Mon, Nov 30, 2009 at 1:19 PM, Mark N <ni...@gmail.com> wrote:
>
> > Is it possible to write nested queries in Solr similar to sql like query
> > where  I can take results of the first query and use one or more of its
> > fields as an argument in the second query.
> >
> >
> That sounds like a join. If so, the answer would be no.
>
>
> >
> > For example:
> >
> > field1:XYZ AND (_query_: field3:{value of field4})
> >
> > This should search for all types of XYZ and then iterate over the result
> > set
> > and perform a query for where field3  is equal to the value of field1
> from
> > each item of the first result set.
> >
> >
> Your description is not consistent with the query you have given. If
> field:XYZ is specified, then what are "types" of XYZ? Also, if you want to
> perform a query where field3 is equal to the value of field1 then, what is
> field4 in the query you have given?
>
>
> > this is similar to SQL like query
> >
> >
> > select distinct ( fieldA ) from table where fieldA  IN
> >
>
> That sounds similar to faceting. See
> http://wiki.apache.org/solr/SimpleFacetParameters
>
> Perhaps you can give more details on what you want to achieve.
>
> --
> Regards,
> Shalin Shekhar Mangar.
>

Re: nested solr queries

Posted by Shalin Shekhar Mangar <sh...@gmail.com>.
On Mon, Nov 30, 2009 at 1:19 PM, Mark N <ni...@gmail.com> wrote:

> Is it possible to write nested queries in Solr similar to sql like query
> where  I can take results of the first query and use one or more of its
> fields as an argument in the second query.
>
>
That sounds like a join. If so, the answer would be no.


>
> For example:
>
> field1:XYZ AND (_query_: field3:{value of field4})
>
> This should search for all types of XYZ and then iterate over the result
> set
> and perform a query for where field3  is equal to the value of field1 from
> each item of the first result set.
>
>
Your description is not consistent with the query you have given. If
field:XYZ is specified, then what are "types" of XYZ? Also, if you want to
perform a query where field3 is equal to the value of field1 then, what is
field4 in the query you have given?


> this is similar to SQL like query
>
>
> select distinct ( fieldA ) from table where fieldA  IN
>

That sounds similar to faceting. See
http://wiki.apache.org/solr/SimpleFacetParameters

Perhaps you can give more details on what you want to achieve.

-- 
Regards,
Shalin Shekhar Mangar.