Last active
April 7, 2021 10:56
-
-
Save mihow/5e96b1ef8d4d78f0577dfae83cd77588 to your computer and use it in GitHub Desktop.
Revisions
-
mihow revised this gist
Jan 8, 2019 . No changes.There are no files selected for viewing
-
mihow created this gist
Jan 8, 2019 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,32 @@ """Elasticsearch backend that enables fuzzy search to all plaintext queries.""" from wagtail.search.backends.elasticsearch6 import (Elasticsearch6SearchBackend, Elasticsearch6SearchQueryCompiler) class ElasticsearchQueryCompilerWithFuzziness(Elasticsearch6SearchQueryCompiler): """ Copy of Elasticsearch6SearchQueryCompiler class with a modified default query. Adds the "fuzziness" parameter to all queries so that we can return inexact matches for misspellings, etc. Elasticsearch docs on fuzziness: https://www.elastic.co/guide/en/elasticsearch/reference/current/common-options.html#fuzziness """ def _compile_plaintext_query(self, *args, **kwargs): # pylint: disable=W0221 """Add the `fuzziness` parameter to all `match` & `match_multi` queries.""" query = super()._compile_plaintext_query(*args, **kwargs) # The query is a dictionary with one key for the query type. # Either "match" or "match_multi" query_type = list(query.keys())[0] query[query_type]['fuzziness'] = 'AUTO' return query class CustomSearchBackend(Elasticsearch6SearchBackend): """Copy of the Elasticsearch6SearchBackend with a custom query class.""" query_compiler_class = ElasticsearchQueryCompilerWithFuzziness SearchBackend = CustomSearchBackend