Allow query_string to be set to None

This allows match_all queries to be created
This commit is contained in:
Karl Hobley 2014-06-22 16:01:04 +01:00 committed by Karl Hobley
parent 41199dd576
commit d872768baf
2 changed files with 39 additions and 32 deletions

View file

@ -28,42 +28,43 @@ class DBSearch(BaseSearch):
pass # Not needed
def search(self, query_string, model, fields=None, filters={}, prefetch_related=[]):
# Normalise query string
query_string = normalise_query_string(query_string)
# Get terms
terms = query_string.split()
if not terms:
return model.objects.none()
# Get fields
if fields is None:
fields = [field.field_name for field in model.get_searchable_search_fields()]
# Start will all objects
# Start with all objects
query = model.objects.all()
# Apply filters
if filters:
query = query.filter(**filters)
# Filter by terms
for term in terms:
term_query = models.Q()
for field_name in fields:
# Check if the field exists (this will filter out indexed callables)
try:
model._meta.get_field_by_name(field_name)
except:
continue
if query_string is not None:
# Normalise query string
query_string = normalise_query_string(query_string)
# Filter on this field
term_query |= models.Q(**{'%s__icontains' % field_name: term})
# Get terms
terms = query_string.split()
if not terms:
return model.objects.none()
query = query.filter(term_query)
# Filter by terms
for term in terms:
term_query = models.Q()
for field_name in fields:
# Check if the field exists (this will filter out indexed callables)
try:
model._meta.get_field_by_name(field_name)
except:
continue
# Distinct
query = query.distinct()
# Filter on this field
term_query |= models.Q(**{'%s__icontains' % field_name: term})
query = query.filter(term_query)
# Distinct
query = query.distinct()
# Prefetch related
if prefetch_related:

View file

@ -248,15 +248,20 @@ class ElasticSearchQuery(object):
def to_es(self):
# Query
query = {
'query_string': {
'query': self.query_string,
if self.query_string is not None:
query = {
'query_string': {
'query': self.query_string,
}
}
}
# Fields
if self.fields:
query['query_string']['fields'] = self.fields
# Fields
if self.fields:
query['query_string']['fields'] = self.fields
else:
query = {
'match_all': {}
}
# Filters
filters = self._get_filters()
@ -585,10 +590,11 @@ class ElasticSearch(BaseSearch):
return []
# Normalise query string
query_string = normalise_query_string(query_string)
if query_string is not None:
query_string = normalise_query_string(query_string)
# Check that theres still a query string after the clean up
if not query_string:
if query_string == "":
return []
# Apply filters to queryset