diff --git a/librarian/views.py b/librarian/views.py index 4e35d4b8f04e37a10bec1ec87ab9762f3e3d33de..00760b8e57f7eba2c0f631a22f8cd8e4cded2606 100644 --- a/librarian/views.py +++ b/librarian/views.py @@ -634,12 +634,10 @@ def search_all(request): *[Q("term", **{f: query}) for f in term_fields], ]) - print(query_conditions) - qur = srch.query(query_conditions).execute() - print(qur.to_dict()) # Get results + # TODO this does 1 query per result, which is inefficient. results = [] for hit in qur: if hit.meta.index == "books": diff --git a/opac/views.py b/opac/views.py index af6760f3803536063d0ca7fb6c41b51119cc71dd..07cb9a21e4173929cdf97d8dae320d5bf10f56b0 100644 --- a/opac/views.py +++ b/opac/views.py @@ -40,10 +40,36 @@ def search_all(request): """ query = request.GET.get("q") - fields = ["first_name", "last_name", "title", "description", "isbn10", "isbn13"] + fuzzy_search_fields = [ + "first_name", + "last_name", + "title", + "description" + ] + term_fields = [] + if query.isdigit(): + # If the query is a number, search for ISBNs and years + term_fields += [ + "published_year", + "isbn10", + "isbn13", + ] + + # Create the search object search = Search(index=["books", "authors"]) - qry = search.query("multi_match", query=query, fields=fields).execute() + + # Build the query from the fields + query_conditions = Q("bool", should=[ + *[Q("fuzzy", **{f: query}) for f in fuzzy_search_fields], + *[Q("term", **{f: query}) for f in term_fields], + ]) + + # Query using the search object + qry = search.query(query_conditions).execute() + + # Gather results + # TODO this does 1 query per result, which is inefficient. results = [] for result in qry: if result.meta.index == "books":