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 Aakanksha Gupta <aa...@gmail.com> on 2018/02/19 13:44:33 UTC

Help required with SolrJ

Hi all,
I'm looking for some help with SolrJ for querying spatial data. I have the
following URL query working fine, where it returns the results which are
within 100km radius from the 'pt' provided in the URL and where the
timestamp field is between the two timestamps provided in the URL. It also
returns me the 'distance' for each result.

http://localhost:8983/solr/geoloc/select/?q=*:*&fq={!geofilt}&sfield=latlong&pt=-6.08165,145.8612430&d=100&wt=json&fq=timestamp:[1518908400000%20TO%201518912000000]&fl=*,_dist_:geodist()

But I'm not sure how to build the SolrJ equivalent of this query using
SolrQuery. I tried something like

SolrQuery query = new SolrQuery();
query.setQuery("
&fq={!geofilt}&sfield=latlong&pt=-6.08165,145.8612430&d=100&wt=json&fq=timestamp:[1518908400000%20TO%201518912000000]&fl=*,_dist_:geodist()")

and it throws errors. I've been trying to find some samples for 'SolrQuery'
class but without luck. Can someone give some sample usage of SolrQuery for
spatial data?

Re: Help required with SolrJ

Posted by Aakanksha Gupta <aa...@gmail.com>.
Thanks Shawn! That was just a small fix from my side. Thanks for your help!

On Tue, Feb 20, 2018 at 1:43 AM, Shawn Heisey <ap...@elyograg.org> wrote:

> On 2/19/2018 8:49 AM, Aakanksha Gupta wrote:
> > Thanks for the quick solution. It works. I just had to replace %20 to
> space
> > in query.addFilterQuery("timestamp:[1518908400000 TO 1518912000000]");
> >
> > Thanks a ton! :)
>
> Right, I didn't even really look closely at what was in the fq
> parameter, I just copied it. :)  Sorry about that -- if I'd looked
> better, I would have seen that what I was sending wouldn't work.
>
> SolrJ will handle the URL encoding for you, so it would have URL encoded
> the URL encoding, and Solr would receive the fq parameter with the %20
> intact.
>
> Glad you figured it out even with my mistake!
>
> Thanks,
> Shawn
>

Re: Help required with SolrJ

Posted by Shawn Heisey <ap...@elyograg.org>.
On 2/19/2018 8:49 AM, Aakanksha Gupta wrote:
> Thanks for the quick solution. It works. I just had to replace %20 to space
> in query.addFilterQuery("timestamp:[1518908400000 TO 1518912000000]");
> 
> Thanks a ton! :)

Right, I didn't even really look closely at what was in the fq
parameter, I just copied it. :)  Sorry about that -- if I'd looked
better, I would have seen that what I was sending wouldn't work.

SolrJ will handle the URL encoding for you, so it would have URL encoded
the URL encoding, and Solr would receive the fq parameter with the %20
intact.

Glad you figured it out even with my mistake!

Thanks,
Shawn

Re: Help required with SolrJ

Posted by Aakanksha Gupta <aa...@gmail.com>.
Thanks Erick.

On Tue, Feb 20, 2018 at 1:11 AM, Erick Erickson <er...@gmail.com>
wrote:

> Aakanksha:
>
> Be a little careful here, filter queries with timestamps can be
> tricky. The example you have is fine, but for end-points with finer
> granularity may be best if you don't cache them, see:
> https://lucidworks.com/2012/02/23/date-math-now-and-filter-queries/
>
> Best,
> Erick
>
> On Mon, Feb 19, 2018 at 7:49 AM, Aakanksha Gupta
> <aa...@gmail.com> wrote:
> > Hi Shawn,
> > Thanks for the quick solution. It works. I just had to replace %20 to
> space
> > in query.addFilterQuery("timestamp:[1518908400000 TO 1518912000000]");
> >
> > Thanks a ton! :)
> >
> > On Mon, Feb 19, 2018 at 11:43 PM, Shawn Heisey <el...@elyograg.org>
> > wrote:
> >
> >> On 2/19/2018 6:44 AM, Aakanksha Gupta wrote:
> >>
> >>> http://localhost:8983/solr/geoloc/select/?q=*:*&fq={!geofilt
> >>> }&sfield=latlong&pt=-6.08165,145.8612430&d=100&wt=json&fq=
> >>> timestamp:[1518908400000%20TO%201518912000000]&fl=*,_dist_:geodist()
> >>> <http://localhost:8983/solr/geoloc/select/?q=*:*&fq=%7B!
> geofilt%7D&sfield=latlong&pt=-6.08165,145.8612430&d=100&wt=
> json&fq=timestamp:[1518908400000%20TO%201518912000000]&fl=*,_dist_:
> geodist()>
> >>>
> >>> But I'm not sure how to build the SolrJ equivalent of this query using
> >>> SolrQuery.
> >>>
> >>
> >> I haven't done anything with spatial yet.  But I do know how to
> translate
> >> Solr URLs into SolrJ code.  The code below constructs a query object
> >> equivalent to that URL.  If that URL works as-is, this code should do
> the
> >> same.
> >>
> >> I did not include the "wt" parameter, which controls the format of the
> >> response.  With SolrJ, the transfer format defaults to binary and should
> >> not be changed.  It CAN be changed, but any other choice would be less
> >> efficient, and the programmer doesn't need to worry about it.
> >>
> >>   query.setQuery("*:*");
> >>   query.addFilterQuery("{!geofilt}");
> >> query.addFilterQuery("timestamp:[1518908400000%20TO%201518912000000]");
> >>   query.set("sfield", "latlong");
> >>   query.set("pt", "-6.08165,145.8612430");
> >>   query.set("d", "100");
> >>   query.setFields("*", "_dist_:geodist()");
> >>
> >> I couldn't actually test this code, as I don't have any indexes with
> >> spatial data.
> >>
> >> Thanks,
> >> Shawn
> >>
> >>
>

Re: Help required with SolrJ

Posted by Erick Erickson <er...@gmail.com>.
Aakanksha:

Be a little careful here, filter queries with timestamps can be
tricky. The example you have is fine, but for end-points with finer
granularity may be best if you don't cache them, see:
https://lucidworks.com/2012/02/23/date-math-now-and-filter-queries/

Best,
Erick

On Mon, Feb 19, 2018 at 7:49 AM, Aakanksha Gupta
<aa...@gmail.com> wrote:
> Hi Shawn,
> Thanks for the quick solution. It works. I just had to replace %20 to space
> in query.addFilterQuery("timestamp:[1518908400000 TO 1518912000000]");
>
> Thanks a ton! :)
>
> On Mon, Feb 19, 2018 at 11:43 PM, Shawn Heisey <el...@elyograg.org>
> wrote:
>
>> On 2/19/2018 6:44 AM, Aakanksha Gupta wrote:
>>
>>> http://localhost:8983/solr/geoloc/select/?q=*:*&fq={!geofilt
>>> }&sfield=latlong&pt=-6.08165,145.8612430&d=100&wt=json&fq=
>>> timestamp:[1518908400000%20TO%201518912000000]&fl=*,_dist_:geodist()
>>> <http://localhost:8983/solr/geoloc/select/?q=*:*&fq=%7B!geofilt%7D&sfield=latlong&pt=-6.08165,145.8612430&d=100&wt=json&fq=timestamp:[1518908400000%20TO%201518912000000]&fl=*,_dist_:geodist()>
>>>
>>> But I'm not sure how to build the SolrJ equivalent of this query using
>>> SolrQuery.
>>>
>>
>> I haven't done anything with spatial yet.  But I do know how to translate
>> Solr URLs into SolrJ code.  The code below constructs a query object
>> equivalent to that URL.  If that URL works as-is, this code should do the
>> same.
>>
>> I did not include the "wt" parameter, which controls the format of the
>> response.  With SolrJ, the transfer format defaults to binary and should
>> not be changed.  It CAN be changed, but any other choice would be less
>> efficient, and the programmer doesn't need to worry about it.
>>
>>   query.setQuery("*:*");
>>   query.addFilterQuery("{!geofilt}");
>> query.addFilterQuery("timestamp:[1518908400000%20TO%201518912000000]");
>>   query.set("sfield", "latlong");
>>   query.set("pt", "-6.08165,145.8612430");
>>   query.set("d", "100");
>>   query.setFields("*", "_dist_:geodist()");
>>
>> I couldn't actually test this code, as I don't have any indexes with
>> spatial data.
>>
>> Thanks,
>> Shawn
>>
>>

Re: Help required with SolrJ

Posted by Aakanksha Gupta <aa...@gmail.com>.
Hi Shawn,
Thanks for the quick solution. It works. I just had to replace %20 to space
in query.addFilterQuery("timestamp:[1518908400000 TO 1518912000000]");

Thanks a ton! :)

On Mon, Feb 19, 2018 at 11:43 PM, Shawn Heisey <el...@elyograg.org>
wrote:

> On 2/19/2018 6:44 AM, Aakanksha Gupta wrote:
>
>> http://localhost:8983/solr/geoloc/select/?q=*:*&fq={!geofilt
>> }&sfield=latlong&pt=-6.08165,145.8612430&d=100&wt=json&fq=
>> timestamp:[1518908400000%20TO%201518912000000]&fl=*,_dist_:geodist()
>> <http://localhost:8983/solr/geoloc/select/?q=*:*&fq=%7B!geofilt%7D&sfield=latlong&pt=-6.08165,145.8612430&d=100&wt=json&fq=timestamp:[1518908400000%20TO%201518912000000]&fl=*,_dist_:geodist()>
>>
>> But I'm not sure how to build the SolrJ equivalent of this query using
>> SolrQuery.
>>
>
> I haven't done anything with spatial yet.  But I do know how to translate
> Solr URLs into SolrJ code.  The code below constructs a query object
> equivalent to that URL.  If that URL works as-is, this code should do the
> same.
>
> I did not include the "wt" parameter, which controls the format of the
> response.  With SolrJ, the transfer format defaults to binary and should
> not be changed.  It CAN be changed, but any other choice would be less
> efficient, and the programmer doesn't need to worry about it.
>
>   query.setQuery("*:*");
>   query.addFilterQuery("{!geofilt}");
> query.addFilterQuery("timestamp:[1518908400000%20TO%201518912000000]");
>   query.set("sfield", "latlong");
>   query.set("pt", "-6.08165,145.8612430");
>   query.set("d", "100");
>   query.setFields("*", "_dist_:geodist()");
>
> I couldn't actually test this code, as I don't have any indexes with
> spatial data.
>
> Thanks,
> Shawn
>
>

Re: Help required with SolrJ

Posted by Shawn Heisey <el...@elyograg.org>.
On 2/19/2018 6:44 AM, Aakanksha Gupta wrote:
> http://localhost:8983/solr/geoloc/select/?q=*:*&fq={!geofilt}&sfield=latlong&pt=-6.08165,145.8612430&d=100&wt=json&fq=timestamp:[1518908400000%20TO%201518912000000]&fl=*,_dist_:geodist()
>
> But I'm not sure how to build the SolrJ equivalent of this query using
> SolrQuery.

I haven't done anything with spatial yet.  But I do know how to 
translate Solr URLs into SolrJ code.  The code below constructs a query 
object equivalent to that URL.  If that URL works as-is, this code 
should do the same.

I did not include the "wt" parameter, which controls the format of the 
response.  With SolrJ, the transfer format defaults to binary and should 
not be changed.  It CAN be changed, but any other choice would be less 
efficient, and the programmer doesn't need to worry about it.

   query.setQuery("*:*");
   query.addFilterQuery("{!geofilt}");
query.addFilterQuery("timestamp:[1518908400000%20TO%201518912000000]");
   query.set("sfield", "latlong");
   query.set("pt", "-6.08165,145.8612430");
   query.set("d", "100");
   query.setFields("*", "_dist_:geodist()");

I couldn't actually test this code, as I don't have any indexes with 
spatial data.

Thanks,
Shawn