Skip to content

Commit

Permalink
chore: add support for autocomplete search
Browse files Browse the repository at this point in the history
  • Loading branch information
gersmann committed Jan 26, 2024
1 parent be3fd5b commit 4983c2b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
28 changes: 27 additions & 1 deletion django_mongodb/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,33 @@ def _get_mongo_search(self, compiler, connection) -> dict:
query = [expression.value for expression in lhs_expressions]
# weight = lhs.weight
# config = lhs.config
search_query = {"wildcard": {"path": columns, "query": query}}
auto_complete_columns = [
key
for key, value in self.mongo_meta["search_fields"].items()
if "autocomplete" in value
]
query_columns = [
key
for key, value in self.mongo_meta["search_fields"].items()
if "string" in value and key not in auto_complete_columns
]
search_query = dict()
if auto_complete_columns and query_columns:
search_query = {
"compound": {
"should": [
{"wildcard": {"path": columns, "query": query}},
*[
{"autocomplete": {"path": column, "query": query}}
for column in auto_complete_columns
],
]
}
}
elif auto_complete_columns:
search_query = {"autocomplete": {"path": auto_complete_columns, "query": query}}
elif query_columns:
search_query = {"wildcard": {"path": columns, "query": query}}
return search_query


Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ classifiers = [
"Intended Audience :: Developers",
"Operating System :: OS Independent",
]
version = "0.24"
version = "0.24.1"
description = ""
authors = ["gersmann"]
readme = "README.md"
Expand Down

0 comments on commit 4983c2b

Please sign in to comment.