Skip to content

Commit

Permalink
Add single-quoted literals
Browse files Browse the repository at this point in the history
  • Loading branch information
smotornyuk committed Aug 20, 2021
1 parent 1bd980e commit b36e409
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
18 changes: 17 additions & 1 deletion ckanext/composite_search/plugin/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,19 @@
from ..interfaces import ICompositeSearch
from ..utils import SearchParam

CONFIG_LITERAL_QUOTES = "ckanext.composite_search.literal.quotes"
DEFAULT_LITERAL_QUOTES = "double"

def single_quote_solr_literal(t: str) -> str:
escaped = t.replace("'", r"\'")
return f"'{escaped}'"


_literals = {
"single": single_quote_solr_literal,
"double": solr_literal,
}


class DefaultSearchPlugin(plugins.SingletonPlugin):
plugins.implements(ICompositeSearch)
Expand All @@ -19,8 +32,11 @@ def before_composite_search(
self, search_params: dict[str, Any], params: list[SearchParam]
) -> tuple[dict[str, Any], list[SearchParam]]:
query = ''

literal = _literals.get(tk.config.get(CONFIG_LITERAL_QUOTES, DEFAULT_LITERAL_QUOTES), _literals[DEFAULT_LITERAL_QUOTES])

for param in reversed(params):
value = ' '.join([solr_literal(word) for word in param.value.split()])
value = ' '.join([literal(word) for word in param.value.split()])
if not value:
continue
sign = '-' if tk.asbool(param.negation) else '+'
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# Versions should comply with PEP440. For a discussion on single-sourcing
# the version across setup.py and the project code, see
# http://packaging.python.org/en/latest/tutorial.html#version
version='0.2.5',
version='0.2.6',

description='''''',
long_description=long_description,
Expand Down

0 comments on commit b36e409

Please sign in to comment.