0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-11-29 17:36:49 +01:00

BaseSearchQuery → SearchQueryCompiler.

This commit is contained in:
Bertrand Bordage 2017-11-23 16:30:13 +01:00
parent 39da11cce6
commit 69789d7c35
5 changed files with 31 additions and 30 deletions

View File

@ -10,7 +10,7 @@ from django.db.models.functions import Cast
from django.utils.encoding import force_text
from wagtail.wagtailsearch.backends.base import (
BaseSearchBackend, BaseSearchQuery, BaseSearchResults)
BaseSearchBackend, SearchQueryCompiler, BaseSearchResults)
from wagtail.wagtailsearch.index import RelatedFields, SearchField
from wagtail.wagtailsearch.query import MatchAll, PlainText
@ -167,11 +167,11 @@ class Index(object):
return self.name
class PostgresSearchQuery(BaseSearchQuery):
class PostgresSearchQueryCompiler(SearchQueryCompiler):
DEFAULT_OPERATOR = 'and'
def __init__(self, *args, **kwargs):
super(PostgresSearchQuery, self).__init__(*args, **kwargs)
super(PostgresSearchQueryCompiler, self).__init__(*args, **kwargs)
self.search_fields = self.queryset.model.get_search_fields()
def get_search_query(self, config):
@ -235,11 +235,11 @@ class PostgresSearchQuery(BaseSearchQuery):
class PostgresSearchResults(BaseSearchResults):
def _do_search(self):
return list(self.query.search(self.backend.get_config(),
self.start, self.stop))
return list(self.query_compiler.search(self.backend.get_config(),
self.start, self.stop))
def _do_count(self):
return self.query.search(self.backend.get_config(), None, None).count()
return self.query_compiler.search(self.backend.get_config(), None, None).count()
class PostgresSearchRebuilder:
@ -277,7 +277,7 @@ class PostgresSearchAtomicRebuilder(PostgresSearchRebuilder):
class PostgresSearchBackend(BaseSearchBackend):
query_class = PostgresSearchQuery
query_compiler_class = PostgresSearchQueryCompiler
results_class = PostgresSearchResults
rebuilder_class = PostgresSearchRebuilder
atomic_rebuilder_class = PostgresSearchAtomicRebuilder

View File

@ -19,7 +19,7 @@ class FieldError(Exception):
pass
class BaseSearchQuery(object):
class SearchQueryCompiler(object):
DEFAULT_OPERATOR = 'or'
def __init__(self, queryset, query, fields=None, operator=None, order_by_relevance=True):
@ -104,9 +104,9 @@ class BaseSearchQuery(object):
class BaseSearchResults(object):
def __init__(self, backend, query, prefetch_related=None):
def __init__(self, backend, query_compiler, prefetch_related=None):
self.backend = backend
self.query = query
self.query_compiler = query_compiler
self.prefetch_related = prefetch_related
self.start = 0
self.stop = None
@ -129,7 +129,8 @@ 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_compiler,
prefetch_related=self.prefetch_related)
new.start = self.start
new.stop = self.stop
new._score_field = self._score_field
@ -209,7 +210,7 @@ class EmptySearchResults(BaseSearchResults):
class BaseSearchBackend(object):
query_class = None
query_compiler_class = None
results_class = None
rebuilder_class = None
@ -285,7 +286,7 @@ class BaseSearchBackend(object):
raise ValueError("operator must be either 'or' or 'and'")
# Search
search_query = self.query_class(
search_query = self.query_compiler_class(
queryset, query_string, fields=fields, operator=operator, order_by_relevance=order_by_relevance
)
return self.results_class(self, search_query)

View File

@ -4,11 +4,11 @@ from django.db import models
from django.db.models.expressions import Value
from wagtail.wagtailsearch.backends.base import (
BaseSearchBackend, BaseSearchQuery, BaseSearchResults)
BaseSearchBackend, SearchQueryCompiler, BaseSearchResults)
from wagtail.wagtailsearch.query import MatchAll, PlainText
class DatabaseSearchQuery(BaseSearchQuery):
class DatabaseSearchQueryCompiler(SearchQueryCompiler):
DEFAULT_OPERATOR = 'and'
def _process_lookup(self, field, lookup, value):
@ -78,8 +78,8 @@ class DatabaseSearchQuery(BaseSearchQuery):
class DatabaseSearchResults(BaseSearchResults):
def get_queryset(self):
queryset = self.query.queryset
q = self.query.get_extra_q()
queryset = self.query_compiler.queryset
q = self.query_compiler.get_extra_q()
return queryset.filter(q).distinct()[self.start:self.stop]
@ -96,7 +96,7 @@ class DatabaseSearchResults(BaseSearchResults):
class DatabaseSearchBackend(BaseSearchBackend):
query_class = DatabaseSearchQuery
query_compiler_class = DatabaseSearchQueryCompiler
results_class = DatabaseSearchResults
def reset_index(self):

View File

@ -13,7 +13,7 @@ from elasticsearch.helpers import bulk
from wagtail.utils.utils import deep_update
from wagtail.wagtailsearch.backends.base import (
BaseSearchBackend, BaseSearchQuery, BaseSearchResults)
BaseSearchBackend, SearchQueryCompiler, BaseSearchResults)
from wagtail.wagtailsearch.index import (
FilterField, Indexed, RelatedFields, SearchField, class_is_indexed)
from wagtail.wagtailsearch.query import MatchAll, PlainText
@ -261,12 +261,12 @@ class Elasticsearch2Mapping(object):
return '<ElasticsearchMapping: %s>' % (self.model.__name__, )
class Elasticsearch2SearchQuery(BaseSearchQuery):
class Elasticsearch2SearchQueryCompiler(SearchQueryCompiler):
mapping_class = Elasticsearch2Mapping
DEFAULT_OPERATOR = 'or'
def __init__(self, *args, **kwargs):
super(Elasticsearch2SearchQuery, self).__init__(*args, **kwargs)
super(Elasticsearch2SearchQueryCompiler, self).__init__(*args, **kwargs)
self.mapping = self.mapping_class(self.queryset.model)
# Convert field names into index column names
@ -496,11 +496,11 @@ class Elasticsearch2SearchResults(BaseSearchResults):
def _get_es_body(self, for_count=False):
body = {
'query': self.query.get_query()
'query': self.query_compiler.get_query()
}
if not for_count:
sort = self.query.get_sort()
sort = self.query_compiler.get_sort()
if sort is not None:
body['sort'] = sort
@ -519,7 +519,7 @@ class Elasticsearch2SearchResults(BaseSearchResults):
results = {str(pk): None for pk in pks}
# Find objects in database and add them to dict
for obj in self.query.queryset.filter(pk__in=pks):
for obj in self.query_compiler.queryset.filter(pk__in=pks):
results[str(obj.pk)] = obj
if self._score_field:
@ -542,7 +542,7 @@ class Elasticsearch2SearchResults(BaseSearchResults):
use_scroll = limit is None or limit > PAGE_SIZE
params = {
'index': self.backend.get_index_for_model(self.query.queryset.model).name,
'index': self.backend.get_index_for_model(self.query_compiler.queryset.model).name,
'body': self._get_es_body(),
'_source': False,
self.fields_param_name: 'pk',
@ -611,7 +611,7 @@ class Elasticsearch2SearchResults(BaseSearchResults):
def _do_count(self):
# Get count
hit_count = self.backend.es.count(
index=self.backend.get_index_for_model(self.query.queryset.model).name,
index=self.backend.get_index_for_model(self.query_compiler.queryset.model).name,
body=self._get_es_body(for_count=True),
)['count']
@ -819,7 +819,7 @@ class ElasticsearchAtomicIndexRebuilder(ElasticsearchIndexRebuilder):
class Elasticsearch2SearchBackend(BaseSearchBackend):
index_class = Elasticsearch2Index
query_class = Elasticsearch2SearchQuery
query_compiler_class = Elasticsearch2SearchQueryCompiler
results_class = Elasticsearch2SearchResults
mapping_class = Elasticsearch2Mapping
basic_rebuilder_class = ElasticsearchIndexRebuilder

View File

@ -2,7 +2,7 @@ from __future__ import absolute_import, unicode_literals
from .elasticsearch2 import (
Elasticsearch2Index, Elasticsearch2Mapping, Elasticsearch2SearchBackend,
Elasticsearch2SearchQuery, Elasticsearch2SearchResults)
Elasticsearch2SearchQueryCompiler, Elasticsearch2SearchResults)
class Elasticsearch5Mapping(Elasticsearch2Mapping):
@ -15,7 +15,7 @@ class Elasticsearch5Index(Elasticsearch2Index):
pass
class Elasticsearch5SearchQuery(Elasticsearch2SearchQuery):
class Elasticsearch5SearchQuery(Elasticsearch2SearchQueryCompiler):
mapping_class = Elasticsearch5Mapping
def _connect_filters(self, filters, connector, negated):
@ -77,7 +77,7 @@ class Elasticsearch5SearchResults(Elasticsearch2SearchResults):
class Elasticsearch5SearchBackend(Elasticsearch2SearchBackend):
mapping_class = Elasticsearch5Mapping
index_class = Elasticsearch5Index
query_class = Elasticsearch5SearchQuery
query_compiler_class = Elasticsearch5SearchQuery
results_class = Elasticsearch5SearchResults