Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Add docstrings and base description

Artur Smętb8f76228d728
diff --git a/docs/elasticsearch.rst b/docs/elasticsearch.rst
index bd5b259a8a..8b5ddf0c7a 100644
--- a/docs/elasticsearch.rst
+++ b/docs/elasticsearch.rst
@@ -45,3 +45,13 @@ Initial search index can be created with following command:
$ python manage.py update_index
By default products are reindexed after saving forms in the dashboard. Orders are updated after they're placed.
+
+
+Search integration architecture
+-------------------------------
+
+There are three base classes that are handling whole communication between Saleor and Elasticsearch service.
+
+* `ElasticsearchSearchBackend` - Core implementation of elasticsearch communication. Here is the starting point of any search queries.
+* `ElasticsearchSearchQuery` - Abstration for queries passed to elasticsearch. Any filtering or aggregations should be implemented there.
+* `ElasticsearchSearchResults` - Abstration for search results
diff --git a/saleor/search/backends/base.py b/saleor/search/backends/base.py
index 5997ab49a0..a4a747a5d7 100644
--- a/saleor/search/backends/base.py
+++ b/saleor/search/backends/base.py
@@ -16,9 +16,13 @@ class FieldError(Exception):
class BaseSearchQuery(object):
+ """
+ Wraps search query, contains all filters and query parameters.
+ """
DEFAULT_OPERATOR = 'or'
- def __init__(self, queryset, query_string, fields=None, operator=None, order_by_relevance=True):
+ def __init__(self, queryset, query_string, fields=None, operator=None,
+ order_by_relevance=True):
self.queryset = queryset
self.query_string = query_string
self.fields = fields
@@ -26,7 +30,6 @@ class BaseSearchQuery(object):
self.order_by_relevance = order_by_relevance
def _get_filterable_field(self, field_attname):
- # Get field
field = dict(
(field.get_attname(self.queryset.model), field)
for field in self.queryset.model.get_filterable_search_fields()
@@ -94,6 +97,10 @@ class BaseSearchQuery(object):
class BaseSearchResults(object):
+ """
+ This class wraps search results. Core search communication is performed
+ here.
+ """
def __init__(self, backend, query, prefetch_related=None):
self.backend = backend
self.query = query
@@ -105,6 +112,7 @@ class BaseSearchResults(object):
self._score_field = None
def _set_limits(self, start=None, stop=None):
+ # type (int, int)
if stop is not None:
if self.stop is not None:
self.stop = min(self.stop, self.start + stop)
@@ -119,12 +127,16 @@ class BaseSearchResults(object):
def _clone(self):
klass = self.__class__
- new = klass(self.backend, self.query, prefetch_related=self.prefetch_related)
+ new = klass(self.backend, self.query,
+ prefetch_related=self.prefetch_related)
new.start = self.start
new.stop = self.stop
return new
def _do_search(self):
+ """
+ Execute search
+ """
raise NotImplementedError
def _do_count(self):
@@ -217,6 +229,10 @@ class BaseSearchBackend(object):
def search(self, query_string, model_or_queryset, fields=None, filters=None,
prefetch_related=None, operator=None, order_by_relevance=True):
+ """
+ Applies all fillters and prefetches and constructs SearchQuery and
+ SearchResult objects.
+ """
# Find model/queryset
if isinstance(model_or_queryset, QuerySet):
model = model_or_queryset.model
@@ -225,10 +241,6 @@ class BaseSearchBackend(object):
model = model_or_queryset
queryset = model_or_queryset.objects.all()
- # # Model must be a class that is in the index
- # if not class_is_indexed(model):
- # return []
-
# Check that theres still a query string after the clean up
if query_string == "":
return []
@@ -261,6 +273,6 @@ class BaseSearchBackend(object):
# Search
search_query = self.query_class(
- queryset, query_string, fields=fields, operator=operator, order_by_relevance=order_by_relevance
- )
+ queryset, query_string, fields=fields, operator=operator,
+ order_by_relevance=order_by_relevance)
return self.results_class(self, search_query)
diff --git a/saleor/search/backends/dashboard.py b/saleor/search/backends/dashboard.py
index b64e962952..408da75ddd 100644
--- a/saleor/search/backends/dashboard.py
+++ b/saleor/search/backends/dashboard.py
@@ -14,6 +14,9 @@ DEFAULT_BACKEND_RESULTS_CLASS = DEFAULT_BACKEND.results_class
class DashboardSearchQuery(BaseSearchQuery):
+ """
+ Query that will search in multiple indexes
+ """
def __init__(self, query_string,
fields=None, operator=None, order_by_relevance=True,
diff --git a/saleor/search/backends/elasticsearch.py b/saleor/search/backends/elasticsearch.py
index 7fa6a658a7..6113b7c0ef 100644
--- a/saleor/search/backends/elasticsearch.py
+++ b/saleor/search/backends/elasticsearch.py
@@ -448,7 +448,8 @@ class ElasticsearchSearchResults(BaseSearchResults):
# Get pks from results
pks = [hit['fields']['pk'][0] for hit in hits['hits']['hits']]
- scores = {str(hit['fields']['pk'][0]): hit['_score'] for hit in hits['hits']['hits']}
+ scores = {str(hit['fields']['pk'][0]): hit['_score']
+ for hit in hits['hits']['hits']}
# Initialise results dictionary
results = dict((str(pk), None) for pk in pks)