Tagging fields with Spring Data Solr - solrj

I'm using Spring Data Solr 4.0.5.RELEASE and can't find a way to properly tag fields in my filter query to achieve something like {!tag=price}price:10. There is a similar post here and an accepted answer https://stackoverflow.com/a/16903861/10225026, however this solution doesn't work when I use isNull() criteria like so Criteria.where("{!tag=price}price").isNull(). The query ends up being -{!tag=price}price:[* TO *] instead of {!tag=price}-price:[* TO *].
For the same reason it's not possible to use FieldWithFacetParameters with parameters like setMissing(true) on fields with tags/keys/exclusions as spring data produces queries like f.{!key=price ex=typeId,modelId,status}price.facet.missing=true when it should be f.price.facet.missing=true.
Is there a proper way to add tags / keys and exclusions to query fields?

I used the following to add query tag for filter query:
SimpleQuery query = new SimpleFacetQuery();
....
query.addFilterQuery(new SimpleQuery(new Criteria("{!tag=price}-price").isNull()));
And for facet options:
SimpleFacetQuery query = new SimpleFacetQuery();
...
FacetOptions facetOptions = new FacetOptions();
facetOptions.addFacetOnField("{!key=price ex=typeId,modelId,status}price");
query.setFacetOptions(facetOptions);
To make facet options work also need to add the following into request handler default parameters in solrconfig.xml:
<requestHandler name="/select" class="solr.SearchHandler">
....
<lst name="appends">
<str name="f.price.facet.missing">true</str>
</lst>
....

Related

Strawberry GraphQL Django limiting filtering options in GraphiQL interface

So I have two graphql filters for a foreign key that works in Strawberry GraphQL Django v0.31.
#strawberry.django.filters.filter(models.Client, lookups=True)
class ClientFilter:
id:auto
name:auto
#strawberry.django.filters.filter(models.Matter, lookups=True)
class MatterFilter:
id: auto
client:'ClientFilter'
category:auto
In the GraphiQL page they produce a filter that looks like this:
I don't need all the filter fields for the user because some of them don't make sense like using isNull or a regex field for an id. Is there a way to limit the filter options so it is less cluttered?
A bit late to the party, but yes you can do that but you would have to define your own input like so:
#strawberry.input
class LimitedFilterLookup(Generic[T]):
i_contains: Optional[T] = UNSET
And then instead of using auto for the fields of the filter you would declare them like this:
#strawberry.django.filters.filter(models.Client)
class ClientFilter:
name: LimitedFilterLookup[str]

Partial text search with postgreql & django

Tried to implement partial text search with postgresql and django,used the following query
Entry.objects.filter(headline__contains="search text")
This returns records having exact match,ie suppose checking for a match against the record "welcome to the new world" with query __contains="welcome world" , returns zero records
How can i implement this partial text search with postgresql-8.4 and django?
If you want this exact partial search you can use the startswitch field lookup method: Entry.objects.filter(headline__startswith="search text"). See more info at https://docs.djangoproject.com/en/dev/ref/models/querysets/#startswith.
This method creates a LIKE query ("SELECT ... WHERE headline LIKE 'search text%'") so if you're looking for a fulltext alternative you can check out PostgreSQL's built in Tsearch2 extension or other options such as Xapian, Solr, Sphinx, etc.
Each of the former engines mentioned have Django apps that makes them easier to integrate: Djapian for Xapian integration or Haystack for multiple integrations in one app.

SOLR Java APi - Adding multiple condition

I am using Solr API to index the records and for search functionality. I am using the following code to search through
ModifiableSolrParams params = new ModifiableSolrParams();
params.set("q", "country_id:("+id+")");
I would like to add one more parameter like state_id and I would like to do logical AND/OR operations and depending on the result the records should be retrieved. I searched through Google, but could not find a way to combine the conditions. Is it possible through the SOLR api? Or am I doing something wrong?
You can make Your Query as Following.....
String qr="cstm_text:"+"devotional";
SolrQuery qry = new SolrQuery(qr);
qry.setIncludeScore(true);
qry.setShowDebugInfo(true);
qry.setRows(1);
qry.setFacet(true);
ModifiableSolrParams params = new ModifiableSolrParams();
params.set("qt", "/spell");
params.set("spellcheck", "on");
params.set(MoreLikeThisParams.MLT,true);
qry.add(params);

Filter out multiple emails from a Queryset

I'm trying to filter out multiple emails from a queryset in order to stop our employee emails from affecting any exports. I have the following in settings.py:
EMAIL_FILTERS = ['email1', 'email2']
and I'm trying to use this to filter out any entries in the DB with our emails in the following query:
EMAIL_FILTERS = getattr(settings, 'EMAIL_FILTERS')
campaigns = CampaignsSignup.objects.filter(created_date__gte=self._get_start_date(dte),
created_date__lte=self._get_end_date(dte)).order_by('-created_date')
However after trying Q and exclude I can't seem to get it to work. I should also note as much as I would like to be able to use endswith some of the employees are using their personal email address so at the moment it wouldn't be viable.
Thanks.
I see you've found a solution, but the normal way to do this is with exclude:
campaigns = CampaignsSignup.objects.filter(
created_date__gte=self._get_start_date(dte),
created_date__lte=self._get_end_date(dte)
).order_by('-created_date').exclude(email__in=SAVCHAMP_EMAIL_FILTERS)
After throwing everything at it I stumbled upon a way of doing it. Using __in with Q correctly filters out the emails that have been included in the email_filter list in settings.
campaigns = CampaignsSignup.objects.filter(~Q(email__in=SAVCHAMP_EMAIL_FILTERS), created_date__gte=self._get_start_date(dte),
created_date__lte=self._get_end_date(dte)).order_by('-created_date')

Django query: field is substring

In Django I have my Site model that contains the field "base_url" that's the base url of the site. I've an object like this:
foo = Site(base_url="http://foo_base_url.com/")
foo.save()
I receive an url and I want to obtain the site object having this url. I would like to perform a query in django like this:
Site.objects.get(base_url__is_substring="http://foo_base_url.com/something_non_base_url")
How can I perform this query?
Thanx
edit:
It doesn't exist a pattern for base_url, my foo site can be:
foo = Site(base_url="http://foo.com/base/url/")
What you want is not provided by the Django ORM but you can use the where param described under the reference for QuerySet:
url = "http://foo_base_url.com/something_non_base_url"
Site.objects.extra(where=["%s LIKE CONCAT('%%',field,'%%')"], params=[url]).get()
Keep in mind that there is no standard method of concatenation across DMBS so if you migrate, you'll have to migrate this code.
The only portable method would be to filter it using Python:
sites = [site for site in Site.objects.all() if site.base_url in url]
although this is of course not ideal for huge data sets.