Taxon queries in Spree API - spree

I'm trying to run searches using ransack queries, but I'm getting invalid search term results on the following /api/v1/products?q[classifications_taxon_id_eq]=3. Can someone please tell me what I'm doing wrong?
I deliberately set ransack to not ignore invalid terms, just to try to figure out what's going on.
The error I get is something like:
Invalid search term classifications_taxon_id_eq

You need to whitelist classifications in the product model:
After that in the classification model, you need to whitelist the taxon attribute.
https://github.com/spree/spree/blob/master/core/app/models/spree/product.rb#L117

Related

Django-dsl-drf Exclude phrase query

I am working on integrating Elastic Search in my existing Django REST application. I am using the django-dsl-drf module provided in the link below:
https://django-elasticsearch-dsl-drf.readthedocs.io/
In their documentation 'exclude' query param is provided. But the query only when we provide the full field value.
search-url?exclude=<field-value
For eg: If I have a value 'Stackoverflow' in field 'name'. I'll have to provide query param a
?name__exclude=Stackoverflow to exclude records having 'Stackoverflow' as name in the result. I would like to implement a search in such a way that when I provide 'over', I need to exclude these records, similar to ?name__exclude=over
I checked the above tutorial, but I couldn't find it. Is there any work around so that I can exclude records, fields containing terms instead of providing full field value, which is also case-insensitive.
Thanks a lot.
Using the contains functional filter, you can target documents that have their name field value containing the characters over anywhere in their terms:
?name__contains=over
However, as far as I know, there is no way to negate that filter in django-dsl-drf. You can create an issue requesting that feature, though, because odds are high that you're not the only who needs that, since it's a pretty common way of searching.

Query not responding to order_by

I'm trying to order_by a query but i cannot seem to accomplish it, any help would be appreciated
users = User.objects.filter(Q(groups__name=group)).distinct()
This is the starting query i have tried many ways to make this work with the order_by method. But cant seem to get it working i am trying to order the query by the first_name in descending order.
.order_by('-first_name'.desc())
Something like this?
I get an error 'str' object has no attribute 'desc'
I have tried to look this up but cant see it being produced in the context that i am using it so i cant relate to the answers
-- Edit --
i have had some progress, Names are changing places but i cant seem to match a pattern, So i am thinking that (Q(groups__name=group)) could be the cause of it but i have no idea what this actually does, Can anyone explain this to me? thanks
use
.order_by('-first_name')
instead of
.order_by('-first_name'.desc())
- stands for descending already. otherwise you are trying to call a method on a string instead of QuerySet object

OSM Nominatim search strange behaviour

I have ran into some troubles using Open Street Map Nominatim search API. I am trying to search and geocode addresses, but for some queries, the results are quite strange.
For example, when I use query:
http://nominatim.openstreetmap.org/search?format=json&countrycodes=cz&limit=10&accept-language=cz&addressdetails=1&q=Jihlava
I get expected results - city Jihlava.
But when I use queries like (only part of name):
http://nominatim.openstreetmap.org/search?format=json&countrycodes=cz&limit=10&accept-language=cz&addressdetails=1&q=Jihl
or
http://nominatim.openstreetmap.org/search?format=json&countrycodes=cz&limit=10&accept-language=cz&addressdetails=1&q=Jihla
or
http://nominatim.openstreetmap.org/search?format=json&countrycodes=cz&limit=10&accept-language=cz&addressdetails=1&q=Jihlav
I get empty result list.
Is there anything wrong with my query?
Thanks.
That's expected behavior, for now. Nominatim has no auto-correction feature yet. Thus only partly matching queries aren't always handled correctly.
If you need auto-correction then please see if one of the other search engines for OSM fits your needs.

Django Haystack similarity search

I'm a Django newbie doing a primitive website. I installed haystack and Whoosh as its search engine cause it was the simplest thing to do. It works fine, but there is a problem and I don't know how to Google it. I have some categories on my site and I have indexed their names to search. So, when a user enters "Computing" it finds the computing category and links to it. But there is a problem. If a user enters "Comp" into search field, it doesn't find "Computing" at all. Is this something that can be configured and how?
EDIT:
What else have I tried? Installing haystack 2.0, following this tutorial, installing solr instead of whoosh, trying Ngram fields, rebuilding indexes 10 times, rewriting search_indexes.py. Everything. Doesn't work. If I type in Comp, it doesn't find Computing. Is there anything else I could do? I have noticed that in the tutorial above, everything works like a charm instantly.
When you do the usual:
SearchQuerySet().filter(title='Computing')
in Haystack 1.x, it filters on everything exactly matching 'Computing'.
You can change that behaviour by using Haystack's Field Lookups, for example, using 'contains' will filter on anything containing the given string (Computing, Utingcomp, Comp):
SearchQuerySet().filter(title__contains='Comp')
In Haystack 2.x, the default filter is 'contains', so it should behave as you would expect it to "out-of-the-box"
Check out the documentation on autocomplete. You need to setup your indices to support Ngram's, but this should be exactly what you need.
from haystack.query import SearchQuerySet
SearchQuerySet().autocomplete(content_auto='old')
# Result match things like 'goldfish', 'cuckold' & 'older'.
So, if I'm understanding, what you're looking for is the equivalent of 'LIKE' in SQL.
The problem is search engines that back Haystack aren't like an RDBMS.
The low level implementation of this filter will involve using wildcard characters but most of the Haystack backends don't support a leading wildcard, something required for an icontains/endswith filter. However, since most backends support trailing wildcards, Haystack 2.x includes a startswith filter. The only case this doesn't handle is searching for the end of a word, which doesn't look to be possible.
So, if you have indexed:
"Look at our great discounts in Computer section"
Then the following Haystack query DO match:
SearchQuerySet().filter(title__startswith='comp')
# match!
Notice the difference between Django vs. Haystack startswith filters. Django startswith will match at the beginning of the complete sentence (i.e. a CharField), but the Haystack one will match at the beginning of a token (i.e. each word in a complete sentence).
Hope it helps!

Using django-haystack, how do I perform a search with only partial terms?

I've got a Haystack/xapian search index for django.contrib.auth.models.User. The template is simply
{{object.get_full_name}}
as I intend for a user to type in a name and be able to search for it.
My issue is this: if I search, say, Sri (my full first name) I come up with a result for the user object pertaining to my name. However, if I search Sri Ragh - that is, my full name, and part of my last name, I get no results.
How can I set Haystack up so that I can get the appropriate results for partial queries?
(I essentially want it to search *Sri Ragh*, but I don't know if wildcards would actually do the trick, or how to implement them).
This is my search query:
results = SearchQuerySet().filter(content='Sri Ragh')
I use to have a similar problem, as workaround or maybe a Fix you can change the query lookup
results = SearchQuerySet().filter(content__startswith='Sri Ragh')
The issue is that django-haystack doesn't implement all lingos from search engines. Of course you can do this.
results = SearchQuerySet().raw_search('READ THE SEARCH ENGINE QUERY SYNTAX FOR GET WILDCARD LOOKUPS')
As Django-haystack says, this is not portable.
You can use icontains or startswith.
Be careful with this one, if a query is for example 'r', this will bring you all 'Model' entities that have a 'r' in its content.
Model.objects.filter(content__icontains=query)
Model.objects.filter(content__startswith=query)
Look at the documentation