A Different Kind of Query (JTeam's SpatialSolr)

32 views
Skip to first unread message

Joe K

unread,
Dec 10, 2009, 9:48:53 PM12/10/09
to django-haystack
I'm using JTeam's SpatialSolr plugin, and am just now getting around
to upgrading to Haystack. One problem, though, is the required format
of the query.

To query using the SpatialSolr plugin, my query contains this:

q={!spatial lat=37.0842271 long=-94.513281 radius=25 unit=miles
calc=arc threadCount=2}dog

When I pass this query into Haystack, like this:

query = "{!spatial lat=37.0842271 long=-94.513281 radius=25 unit=miles
calc=arc threadCount=2}dog"
results = SearchQuerySet().filter(content=query)

Haystack (or the solr backend) interprets this as

q="{!spatial lat=37.0842271 long=-94.513281 radius=25 unit=miles
calc=arc threadCount=2}dog"

(Note the quotes)

This, naturally, does not yield the expected results, and I get back
nothing.

Any ideas for how to go about passing an ugly query like this an not
have it get quoted? Is there a super top-secret setting, or param?
Or is there a method that is doing this that I can override in my
backend?

Thanks for your help.

JoeK

Daniel Lindsley

unread,
Dec 10, 2009, 11:19:36 PM12/10/09
to django-...@googlegroups.com
JoeK,


Interesting. I still have getting around to testing SpatialSolr on
my TODO list. Since I don't have a working environment, here's what I
think might work:

sqs = SearchQuerySet().filter(content='dog')
sqs = sqs.raw_query('{!spatial lat=37.0842271 long=-94.513281
radius=25 unit=miles
> calc=arc threadCount=2}')

I believe that ought to pass all of the SpatialSolr bits to the
backend untouched and IIRC do the right thing with the 'dog' part of
the query as well. If not, I think there are other options but I'm
hoping that will work for you.


Daniel
> --
>
> You received this message because you are subscribed to the Google Groups "django-haystack" group.
> To post to this group, send email to django-...@googlegroups.com.
> To unsubscribe from this group, send email to django-haysta...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/django-haystack?hl=en.
>
>
>

Joe Kueser

unread,
Dec 11, 2009, 7:50:13 AM12/11/09
to django-...@googlegroups.com
Thanks, Daniel.

Unfortunately, your suggestion didn't work, but it seemed like a good idea.

I did find out that this type of query is referred to LocalParams, as described here: http://wiki.apache.org/solr/LocalParams

Based on what I found here http://wiki.apache.org/solr/SolrQuerySyntax I was hoping that I could just reformat the query like this:

q=dog&lat=37.0842271&long=-94.513281&radius=25&unit=miles&calc=arc&threadCount=2

But that doesn't work. My guess is JTeam's plugin just doesn't support it. SpatialSolr's source code is available, so if that's the case, maybe I can fix it and submit the fix back to JTeam.

In the mean time, if anyone has any more bright ideas, please let me know :-)

Thanks.

JoeK

Joe Kueser

unread,
Dec 11, 2009, 9:42:25 AM12/11/09
to django-...@googlegroups.com
Well, a couple of more things I've noticed.

The problem seems to be centered around that, somewhere along the line, Haystack (or the Solr backend) is seeing a query with spaces in it and is putting the query in quotes. This is the correct thing to happen 99% of the time, I'm sure, just not in my case :-)

So I tried being clever and replace the spaces with %20's. Can you guess what happened? The %20 was replaced with %2520. So then I tried + signs. No go there, either, though in both cases, the query was not quoted.

So I need to track down where the quotes are being injected. Shouldn't be too hard. Probably code somewhere that looks like:

'"%s"' % query

I'll post a solution if I find one.

In the mean time, if anyone has any ideas, they are still very welcome :-)

Thanks.

Joe K


On Dec 10, 2009, at 10:19 PM, Daniel Lindsley wrote:

Joe K

unread,
Dec 11, 2009, 9:56:09 AM12/11/09
to django-haystack
Ok, I think I figured it out.

In the solr backend's SearchQuery.build_query_fragment there's a
couple of lines of code that look like this:

# Check to see if it's a phrase for an exact match.
if ' ' in value:
value = '"%s"' % value

By changing the if statement just a hair (slight hack, the "right" way
would be to check for beginning and end braces) everything works
peachy.

# Check to see if it's a phrase for an exact match.
if ' ' in value and not '{' in value:
value = '"%s"' % value

So I just created a subclass of the solr backend, which I cleverly
called geosolr, which has a new build_query_fragment method. I'll
hand my final solution over to the authors so they can integrate it
into the solr backend if they so choose.

JoeK


On Dec 11, 8:42 am, Joe Kueser <joe.kue...@gmail.com> wrote:
> Well, a couple of more things I've noticed.
>
> The problem seems to be centered around that, somewhere along the line, Haystack (or the Solr backend) is seeing a query with spaces in it and is putting the query in quotes.  This is the correct thing to happen 99% of the time, I'm sure, just not in my case :-)
>
> So I tried being clever and replace the spaces with %20's.  Can you guess what happened?  The %20 was replaced with %2520.  So then I tried + signs.  No go there, either, though in both cases, the query was not quoted.
>
> So I need to track down where the quotes are being injected.  Shouldn't be too hard.  Probably code somewhere that looks like:
>
>  '"%s"' % query
>
> I'll post a solution if I find one.
>
> In the mean time, if anyone has any ideas, they are still very welcome :-)
>
> Thanks.
>
> Joe K
>
> On Dec 10, 2009, at 10:19 PM, Daniel Lindsley wrote:
>
>
>
> > JoeK,
>
> >   Interesting. I still have getting around to testing SpatialSolr on
> > my TODO list. Since I don't have a working environment, here's what I
> > think might work:
>
> > sqs = SearchQuerySet().filter(content='dog')
> > sqs = sqs.raw_query('{!spatial lat=37.0842271 long=-94.513281
> > radius=25 unit=miles
> >> calc=arc threadCount=2}')
>
> >   I believe that ought to pass all of the SpatialSolr bits to the
> > backend untouched and IIRC do the right thing with the 'dog' part of
> > the query as well. If not, I think there are other options but I'm
> > hoping that will work for you.
>
> > Daniel
>
> >> For more options, visit this group athttp://groups.google.com/group/django-haystack?hl=en.
Reply all
Reply to author
Forward
0 new messages