mirror of
https://github.com/Hopiu/django-watson.git
synced 2026-05-05 12:24:43 +00:00
Merge pull request #86 from alexey-grom/master
Extension point for query formatting at MySQLSearchBackend
This commit is contained in:
commit
42e769747d
2 changed files with 14 additions and 11 deletions
|
|
@ -333,7 +333,7 @@ def escape_mysql_boolean_query(search_text):
|
|||
)
|
||||
for word in escape_mysql_boolean_query_chars(search_text).split()
|
||||
)
|
||||
|
||||
|
||||
|
||||
class MySQLSearchBackend(SearchBackend):
|
||||
|
||||
|
|
@ -374,17 +374,20 @@ class MySQLSearchBackend(SearchBackend):
|
|||
requires_installation = True
|
||||
|
||||
supports_ranking = True
|
||||
|
||||
def _format_query(self, search_text):
|
||||
return escape_mysql_boolean_query(search_text)
|
||||
|
||||
def do_search(self, engine_slug, queryset, search_text):
|
||||
"""Performs the full text search."""
|
||||
return queryset.extra(
|
||||
where = ("MATCH (title, description, content) AGAINST (%s IN BOOLEAN MODE)",),
|
||||
params = (escape_mysql_boolean_query(search_text),),
|
||||
params = (self._format_query(search_text),),
|
||||
)
|
||||
|
||||
def do_search_ranking(self, engine_slug, queryset, search_text):
|
||||
"""Performs full text ranking."""
|
||||
search_text = escape_mysql_boolean_query(search_text)
|
||||
search_text = self._format_query(search_text)
|
||||
return queryset.extra(
|
||||
select = {
|
||||
"watson_rank": """
|
||||
|
|
@ -418,12 +421,12 @@ class MySQLSearchBackend(SearchBackend):
|
|||
),
|
||||
"watson_searchentry.content_type_id = %s",
|
||||
),
|
||||
params = (engine_slug, escape_mysql_boolean_query(search_text), content_type.id),
|
||||
params = (engine_slug, self._format_query(search_text), content_type.id),
|
||||
)
|
||||
|
||||
def do_filter_ranking(self, engine_slug, queryset, search_text):
|
||||
"""Performs the full text ranking."""
|
||||
search_text = escape_mysql_boolean_query(search_text)
|
||||
search_text = self._format_query(search_text)
|
||||
return queryset.extra(
|
||||
select = {
|
||||
"watson_rank": """
|
||||
|
|
|
|||
|
|
@ -264,7 +264,7 @@ class SearchContext(object):
|
|||
def __init__(self, context_manager):
|
||||
"""Initializes the search index context."""
|
||||
self._context_manager = context_manager
|
||||
|
||||
|
||||
def __enter__(self):
|
||||
"""Enters a block of search index management."""
|
||||
self._context_manager.start()
|
||||
|
|
@ -578,15 +578,15 @@ default_search_engine = SearchEngine("default")
|
|||
|
||||
|
||||
# The cache for the initialized backend.
|
||||
_backend_cache = None
|
||||
_backends_cache = {}
|
||||
|
||||
|
||||
def get_backend(backend_name=None):
|
||||
"""Initializes and returns the search backend."""
|
||||
global _backend_cache
|
||||
global _backends_cache
|
||||
# Try to use the cached backend.
|
||||
if _backend_cache is not None:
|
||||
return _backend_cache
|
||||
if backend_name in _backends_cache:
|
||||
return _backends_cache[backend_name]
|
||||
# Load the backend class.
|
||||
if not backend_name:
|
||||
backend_name = getattr(settings, "WATSON_BACKEND", "watson.backends.AdaptiveSearchBackend")
|
||||
|
|
@ -601,5 +601,5 @@ def get_backend(backend_name=None):
|
|||
))
|
||||
# Initialize the backend.
|
||||
backend = backend_cls()
|
||||
_backend_cache = backend
|
||||
_backends_cache[backend_name] = backend
|
||||
return backend
|
||||
|
|
|
|||
Loading…
Reference in a new issue