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 bbarani <bb...@gmail.com> on 2013/05/28 22:02:40 UTC

SOLR 4.3.0 - How to make fq optional?

I am using the SOLR geospatial capabilities for filtering the results based
on the particular radius (something like below).. I have added the below fq
query in solrconfig and passing the latitude and longitude information
dynamically..

select?q=firstName:john&fq={!bbox%20sfield=geo%20pt=40.279392,-81.85891723%20d=10}

    <str name="q">
        _query_:"{firstName=$firstName}"
        </str>

    <str name="fq">
        _query_:"{!bbox pt=$fps_latlong sfield=geo d=$fps_dist}"
        </str>

Now when I pass the latitude and longitude data, the query works fine but
whenever I dont pass the latitude / longitude data it throws exception.. Is
there a way to make fq optional? Is there a way to ignore spatial queries
when the co ordinates are not passed? Looking for something like dismax,
that doesnt throw any exceptions...





--
View this message in context: http://lucene.472066.n3.nabble.com/SOLR-4-3-0-How-to-make-fq-optional-tp4066592.html
Sent from the Solr - User mailing list archive at Nabble.com.

Re: SOLR 4.3.0 - How to make fq optional?

Posted by bbarani <bb...@gmail.com>.
Erik,

I am trying to enable / disable a part of fq based on a particular value
passed from the query.

For Ex: If I have the value for the keyword where in the query then I would
like to enable this fq, else just ignore it.. 

select?where="New york,NY"

Enable only when where has some value. (I get the value passed in query
inside fq - using $where)

I need something like..

*if($where!=null){*
<str name="fq">
{!bbox pt=$fps_latlong sfield=geo d=$fps_dist}
</str> 
*}*

 Is it possible to achieve this using the switch query parser?

Thanks,
BB



--
View this message in context: http://lucene.472066.n3.nabble.com/SOLR-4-3-0-How-to-make-fq-optional-tp4066592p4066624.html
Sent from the Solr - User mailing list archive at Nabble.com.

Re: SOLR 4.3.0 - How to make fq optional?

Posted by Erik Hatcher <er...@gmail.com>.
I imagine the new "switch" query parser could help here somehow.  

    Erik

On May 28, 2013, at 16:43, "David Smiley (@MITRE.org)" <DS...@mitre.org> wrote:

> Your client needs to know to submit the proper filter query conditionally. 
> It's not really a spatial issue, and I disagree with the idea to make bbox
> (and all other query parsers for that matter) do nothing if not given an
> expected input.
> 
> ~ David
> 
> 
> bbarani wrote
>> I am using the SOLR geospatial capabilities for filtering the results
>> based on the particular radius (something like below).. I have added the
>> below fq query in solrconfig and passing the latitude and longitude
>> information dynamically but I am hardcoding the dynamic query in
>> solrconfig.xml..
>> 
>> select?q=firstName:john&fq={!bbox%20sfield=geo%20pt=40.279392,-81.85891723%20d=10}
>> 
>> 
>> <str name="q">
>>        _query_:"{firstName=$firstName}"
>> 
>> </str>
>> 
>> <str name="fq">
>>        _query_:"{!bbox pt=$fps_latlong sfield=geo d=$fps_dist}"
>> 
>> </str>
>> Now when I pass the latitude and longitude data, the query works fine but
>> whenever I dont pass the latitude / longitude data it throws exception..
>> Is there a way to make fq optional? Is there a way to ignore spatial
>> queries when the co ordinates are not passed? Looking for something like
>> dismax, that doesnt throw any exceptions...
> 
> 
> 
> 
> 
> -----
> Author: http://www.packtpub.com/apache-solr-3-enterprise-search-server/book
> --
> View this message in context: http://lucene.472066.n3.nabble.com/SOLR-4-3-0-How-to-make-fq-optional-tp4066592p4066604.html
> Sent from the Solr - User mailing list archive at Nabble.com.

Re: SOLR 4.3.0 - How to make fq optional?

Posted by bbarani <bb...@gmail.com>.
 I totally missed that..Sorry about that :)...It seems to work fine now...



--
View this message in context: http://lucene.472066.n3.nabble.com/SOLR-4-3-0-How-to-make-fq-optional-tp4066592p4066891.html
Sent from the Solr - User mailing list archive at Nabble.com.

Re: SOLR 4.3.0 - How to make fq optional?

Posted by Chris Hostetter <ho...@fucit.org>.
:     <lst name="defaults">
	...
:           <lst name="appends">
:             <str name="fq">{!switch case='*:*' default=$fq_bbox
: v=$fps_latlong}</str>
:         </lst>
:         <lst name="invariants">
:             <str name="fq_bbox">_query_:"{!bbox pt=$fps_latlong sfield=geo
: d=$fps_dist}"</str>
:         </lst>
:     </lst>

...you have your "appends" and "invariants" nested inside your defaults -- 
they should be siblings...

     <lst name="defaults">
        ...
     </lst>
     <lst name="appends">
        ...
     </lst>
     <lst name="invariants">
        ...
     </lst>

-Hoss

Re: SOLR 4.3.0 - How to make fq optional?

Posted by bbarani <bb...@gmail.com>.
Ok..I removed all my custom components from findperson request handler..

  <requestHandler name="findperson" class="solr.SearchHandler"
default="false">
    <lst name="defaults">
      <str name="defType">lucene</str>
      <str name="echoParams">explicit</str>
      <int name="rows">10</int>
      <str name="q.op">AND</str>
      <str name="qf">person_name_all_i</str>
      <int name="score_truncation_cliff">50</int>
      <int name="fps_dist">32</int>
      <str name="q">
          *:*
      </str>
          <lst name="appends">
            <str name="fq">{!switch case='*:*' default=$fq_bbox
v=$fps_latlong}</str>
        </lst>
        <lst name="invariants">
            <str name="fq_bbox">_query_:"{!bbox pt=$fps_latlong sfield=geo
d=$fps_dist}"</str>
        </lst>
    </lst>
    <arr name="components">
        <str>query</str>
      <str>debug</str>
    </arr>
  </requestHandler>


My query:
select?fl=*,score&rows=10&qt=findperson&fps_latlong=42.3482,-75.1890

The above query just returns everything back from SOLR (should only return
results corresponding to lat and long values passed in the query)...

I even tried changing the below hack, but got the same results.

        <str name="fq_bbox">{!bbox pt=$fps_latlong sfield=geo
d=$fps_dist}</str>

Not sure if I am missing something...




--
View this message in context: http://lucene.472066.n3.nabble.com/SOLR-4-3-0-How-to-make-fq-optional-tp4066592p4066872.html
Sent from the Solr - User mailing list archive at Nabble.com.

Re: SOLR 4.3.0 - How to make fq optional?

Posted by Chris Hostetter <ho...@fucit.org>.
: Hoss, for some reason this doesn't work when I pass the latlong value via
: query..
	...
: fl=*,score&rows=10&qt=findperson&fps_latlong=26.22084,-80.29&fps_fname=peter
	
Hmmm, are these appends & invariants on your "findperson" requestHandler?

What does debugQuery=true show you the pplied filters are?

:         <lst name="invariants">
:             <str name="fq_bbox">_query_:"{!bbox pt=$fps_latlong sfield=geo
: d=$fps_dist}"</str>
:         </lst>

Why do you have the _query_ hack in there?  i haven't had a chance to test 
this, but perhaps that hack doesn't play nicely with localparam variable 
substitution? it should just be...

   <str name="fq_bbox">{!bbox pt=$fps_latlong sfield=geo d=$fps_dist}</str>

: This works fine when the latlong value is passed via custom component. We
: have a custom component which gets the location name via query, calculates
: the corresponding lat long co-ordinates stored in TSV file and passes the
: co-ordinates to the query.


Ok wait a minute -- all bets are off about this working if you have a 
custom component in the mix adding/removing params.  you need to provide 
us with more details about exactly how your component works, where it's 
configured in the component list, and how it is adding the "fps_latlong" 
param it generates to the query, becuase my guesses are one of two things 
are happening:

1) your component is doing it's logic after the query parsing has already 
happened and the variables have been evaluated -- at which point 
fps_latlong isn't set yet, so you get the case='*:*' behavior

2) your component is doing it's logic before the query parsing happens, 
but it is setting the value of fps_latlong in a way that the query parsing 
code doens't see it hen resolving the local variables.


-Hoss

Re: SOLR 4.3.0 - How to make fq optional?

Posted by bbarani <bb...@gmail.com>.
Hoss, for some reason this doesn't work when I pass the latlong value via
query..

This is the query.. It just returns all the values for fname='peter'
(doesn't filter for Tarmac, Florida).

fl=*,score&rows=10&qt=findperson&fps_latlong=26.22084,-80.29&fps_fname=peter

*solrconfig.xml*

        <lst name="appends">
            <str name="fq">{!switch case='*:*' default=$fq_bbox
v=$fps_latlong}</str>
        </lst>
        <lst name="invariants">
            <str name="fq_bbox">_query_:"{!bbox pt=$fps_latlong sfield=geo
d=$fps_dist}"</str>
        </lst>

*Works when used via custom component:*

This works fine when the latlong value is passed via custom component. We
have a custom component which gets the location name via query, calculates
the corresponding lat long co-ordinates stored in TSV file and passes the
co-ordinates to the query.


*Custom component config:*

 <searchComponent name="geo" class="com.customcomponent>
    <str name="placenameFile">centroids.tsv</str>
    <str name="placenameQueryParam">fps_where</str>
    <str name="latQueryParam">fps_latitude</str>
    <str name="lonQueryParam">fps_longitude</str>
    <str name="latlonQueryParam">fps_latlong</str>
    <str name="distQueryParam">fps_dist</str>
    <float name="defaultDist">48.2803</float>
    <float name="boost">1.0</float>
  </searchComponent>

*Custom component query:*
fl=*,score&rows=10&*fps_where="new york,
ny"*&qt=findperson&fps_latlong=26.22084,-80.29&fps_dist=.10&fps_fname=peter

Is it a bug?



--
View this message in context: http://lucene.472066.n3.nabble.com/SOLR-4-3-0-How-to-make-fq-optional-tp4066592p4066862.html
Sent from the Solr - User mailing list archive at Nabble.com.

Re: SOLR 4.3.0 - How to make fq optional?

Posted by Chris Hostetter <ho...@fucit.org>.
: As erik alluded to in his response, you should be able to configure an 
: "appended" fq using the "switch" QParserPlugin to get something like what you are 
: describing, by taking advantage of the "default" behavior.

I've updated the javadocs with 2 additiona examples inspired by this 
thread..

http://svn.apache.org/viewvc?view=revision&revision=1487166


-Hoss

Re: SOLR 4.3.0 - How to make fq optional?

Posted by bbarani <bb...@gmail.com>.
Hoss, you read my mind!!!! Thanks a lotttttt for your awesome
explanation!!!!! You rock!!!



--
View this message in context: http://lucene.472066.n3.nabble.com/SOLR-4-3-0-How-to-make-fq-optional-tp4066592p4066630.html
Sent from the Solr - User mailing list archive at Nabble.com.

Re: SOLR 4.3.0 - How to make fq optional?

Posted by Chris Hostetter <ho...@fucit.org>.
: David, I felt like there should be a flag with which we can either throw the
: error message or do nothing in case of bad inputs.. 

As erik alluded to in his response, you should be able to configure an 
"appended" fq using the "switch" QParserPlugin to get something like what you are 
describing, by taking advantage of the "default" behavior.

I think you'd want something along the lines of...

<lst name="defaults">
  <int name="fps_dist">50</int> <!-- or whatever default you want -->
</lst>
<lst name="appends">
  <str name="fq">{!switch case='*:*' default=$fq_bbox v=$fps_latlong}</str>
</lst>
<lst name="invariants">
  <str name="fq_bbox">{!bbox pt=$fps_latlong sfield=geo d=$fps_dist}</str>
</lst>

The result of something like that should be:

 * if a user specifies a non blank fps_latlong param, the the query will 
be filtered using a bbox based on that center and the specified  fps_dist 
is (or the default fps_dist if the user didn't specify one)
 * if the user does not specify an fps_latlong param, or the fpls_latlong 
param is blank, then no effective filtering is done (the filter matches 
all docs)

Here's an equivilent example using the Solr 4.2.1 examle data and 
configs...

fps_latlong specified, matches a single document in the radius..

http://localhost:8983/solr/select?q=*:*&fps_dist=100&fps_bbox={!bbox%20pt=$fps_latlong%20sfield=store%20d=$fps_dist}&fq={!switch%20case=%27*:*%27%20default=$fps_bbox%20v=$fps_latlong}&fps_latlong=35.0752,-97.032


fps_latlong not specified, or blank, matches all docs...

http://localhost:8983/solr/select?q=*:*&fps_dist=100&fps_bbox={!bbox%20pt=$fps_latlong%20sfield=store%20d=$fps_dist}&fq={!switch%20case=%27*:*%27%20default=$fps_bbox%20v=$fps_latlong}&fps_latlong=
http://localhost:8983/solr/select?q=*:*&fps_dist=100&fps_bbox={!bbox%20pt=$fps_latlong%20sfield=store%20d=$fps_dist}&fq={!switch%20case=%27*:*%27%20default=$fps_bbox%20v=$fps_latlong}&fps_latlong=+++
http://localhost:8983/solr/select?q=*:*&fps_dist=100&fps_bbox={!bbox%20pt=$fps_latlong%20sfield=store%20d=$fps_dist}&fq={!switch%20case=%27*:*%27%20default=$fps_bbox%20v=$fps_latlong}


More info...

https://lucene.apache.org/solr/4_3_0/solr-core/org/apache/solr/search/SwitchQParserPlugin.html
http://searchhub.org/2013/02/20/custom-solr-request-params/


-Hoss

Re: SOLR 4.3.0 - How to make fq optional?

Posted by bbarani <bb...@gmail.com>.
David, I felt like there should be a flag with which we can either throw the
error message or do nothing in case of bad inputs.. 



--
View this message in context: http://lucene.472066.n3.nabble.com/SOLR-4-3-0-How-to-make-fq-optional-tp4066592p4066610.html
Sent from the Solr - User mailing list archive at Nabble.com.

Re: SOLR 4.3.0 - How to make fq optional?

Posted by "David Smiley (@MITRE.org)" <DS...@mitre.org>.
Your client needs to know to submit the proper filter query conditionally. 
It's not really a spatial issue, and I disagree with the idea to make bbox
(and all other query parsers for that matter) do nothing if not given an
expected input.

~ David


bbarani wrote
> I am using the SOLR geospatial capabilities for filtering the results
> based on the particular radius (something like below).. I have added the
> below fq query in solrconfig and passing the latitude and longitude
> information dynamically but I am hardcoding the dynamic query in
> solrconfig.xml..
> 
> select?q=firstName:john&fq={!bbox%20sfield=geo%20pt=40.279392,-81.85891723%20d=10}
> 
>     
> <str name="q">
>         _query_:"{firstName=$firstName}"
>         
> </str>
>     
> <str name="fq">
>         _query_:"{!bbox pt=$fps_latlong sfield=geo d=$fps_dist}"
>         
> </str>
> Now when I pass the latitude and longitude data, the query works fine but
> whenever I dont pass the latitude / longitude data it throws exception..
> Is there a way to make fq optional? Is there a way to ignore spatial
> queries when the co ordinates are not passed? Looking for something like
> dismax, that doesnt throw any exceptions...





-----
 Author: http://www.packtpub.com/apache-solr-3-enterprise-search-server/book
--
View this message in context: http://lucene.472066.n3.nabble.com/SOLR-4-3-0-How-to-make-fq-optional-tp4066592p4066604.html
Sent from the Solr - User mailing list archive at Nabble.com.