Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add docstrings and base description
Artur Smętb8f76228d728
diff --git a/docs/elasticsearch.rst b/docs/elasticsearch.rstindex 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_indexBy 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 resultsdiff --git a/saleor/search/backends/base.py b/saleor/search/backends/base.pyindex 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 = querysetself.query_string = query_stringself.fields = fields@@ -26,7 +30,6 @@ class BaseSearchQuery(object):self.order_by_relevance = order_by_relevancedef _get_filterable_field(self, field_attname):- # Get fieldfield = 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 = backendself.query = query@@ -105,6 +112,7 @@ class BaseSearchResults(object):self._score_field = Nonedef _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.startnew.stop = self.stopreturn newdef _do_search(self):+ """+ Execute search+ """raise NotImplementedErrordef _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/querysetif isinstance(model_or_queryset, QuerySet):model = model_or_queryset.model@@ -225,10 +241,6 @@ class BaseSearchBackend(object):model = model_or_querysetqueryset = 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 upif query_string == "":return []@@ -261,6 +273,6 @@ class BaseSearchBackend(object):# Searchsearch_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.pyindex 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_classclass 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.pyindex 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 resultspks = [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 dictionaryresults = dict((str(pk), None) for pk in pks)