Skip to content
Snippets Groups Projects
Verified Commit f8e3e3c3 authored by TheJoeCoder's avatar TheJoeCoder
Browse files

Implement the same fuzzy search system in OPAC

parent 59fe81ca
Branches
No related tags found
No related merge requests found
Pipeline #319 passed
......@@ -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":
......
......@@ -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":
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment