diff --git a/solr/cores/conf/schema.xml b/solr/cores/conf/schema.xml index c42b48d8..93abed83 100644 --- a/solr/cores/conf/schema.xml +++ b/solr/cores/conf/schema.xml @@ -79,16 +79,29 @@ - + + + + + + + + + + + + - + + + \ No newline at end of file diff --git a/solr/cores/conf/solrconfig.xml b/solr/cores/conf/solrconfig.xml index 86d0a5e2..76256f92 100644 --- a/solr/cores/conf/solrconfig.xml +++ b/solr/cores/conf/solrconfig.xml @@ -72,7 +72,16 @@ explicit 10 text + + true + default + true + true + + + spellcheck + @@ -84,6 +93,20 @@ text + + + + + default + spellcheck + solr.DirectSolrSpellChecker + 3 + 0.3 + 2 + internal + + + text diff --git a/web-app/django/VIM/apps/instruments/views/instrument_list.py b/web-app/django/VIM/apps/instruments/views/instrument_list.py index 3dce35b8..f847d23b 100644 --- a/web-app/django/VIM/apps/instruments/views/instrument_list.py +++ b/web-app/django/VIM/apps/instruments/views/instrument_list.py @@ -141,6 +141,7 @@ def get_context_data(self, **kwargs): instruments, has_other_pages, facet_data, + effective_query, ) = self._paginate_solr_results(page_size) # Add pagination data to context @@ -163,6 +164,9 @@ def get_context_data(self, **kwargs): context["hbs_facet"] = hbs_facet context["search_query"] = search_query if search_query else None + # Add spellcheck suggestion to context + context["effective_query"] = effective_query + # Get contextual HBS facets (respects current search) hbs_facet_list = self._get_contextual_hbs_facets(facet_data) context["hbs_facets"] = hbs_facet_list @@ -263,7 +267,7 @@ def _build_solr_query(self, language: Language, include_facets: bool = False): def _get_solr_page_results( self, solr, query_params: dict, page_size: int, start: int ): - """Get a specific page of Solr search results with filter queries and total count.""" + """Get a specific page of Solr search results with consistent facets and spellcheck support.""" solr_params = { **query_params, "rows": page_size, @@ -272,20 +276,44 @@ def _get_solr_page_results( # Remove our custom params lang_code = solr_params.pop("lang_code") - solr_response = solr.search(**solr_params) + effective_query = solr_params.get("q") + solr_params["spellcheck.q"] = effective_query + + # Execute initial query + response = solr.search(**solr_params) + + # Attempt spellcheck if no results + if response.hits == 0: + raw = response.raw_response + spellcheck = raw.get("spellcheck", {}) + collations = spellcheck.get("collations", []) + if len(collations) >= 2: + spellcheck_suggestion = collations[1] + + corrected_params = solr_params.copy() + corrected_params["q"] = spellcheck_suggestion + + corrected_response = solr.search(**corrected_params) + + if corrected_response.hits > 0: + response = corrected_response + effective_query = spellcheck_suggestion + instruments = [ - SolrInstrument(doc, lang_code=lang_code) for doc in solr_response.docs + SolrInstrument(doc, lang_code=lang_code) for doc in response.docs ] - total_count = solr_response.hits # pysolr's hits corresponds to Solr's numFound + total_count = response.hits - # Return facet data if available - facet_data = None - if hasattr(solr_response, "facets") and solr_response.facets: - facet_data = solr_response.facets.get("facet_pivot", {}).get( - "hbs_prim_cat_s,hbs_prim_cat_label_s", [] - ) + facet_data = response.facets.get("facet_pivot", {}).get( + "hbs_prim_cat_s,hbs_prim_cat_label_s", [] + ) - return instruments, total_count, facet_data + return ( + instruments, + total_count, + facet_data, + effective_query, + ) def _get_contextual_hbs_facets(self, contextual_facet_data): """Get HBS facets showing all categories with contextual counts (including zero).""" @@ -358,15 +386,25 @@ def _paginate_solr_results(self, page_size): query_params = self._build_solr_query(language, include_facets=True) # Get page results, total count, and facet data in one query - page_results, total_count, facet_data = self._get_solr_page_results( - solr, query_params, page_size, start - ) + ( + page_results, + total_count, + facet_data, + effective_query, + ) = self._get_solr_page_results(solr, query_params, page_size, start) # Create paginator and page objects paginator = SolrPaginator(page_results, page_size, total_count) page = Page(page_results, page_number, paginator) - return (paginator, page, page_results, page.has_other_pages(), facet_data) + return ( + paginator, + page, + page_results, + page.has_other_pages(), + facet_data, + effective_query, + ) def get(self, request, *args, **kwargs): language_en = request.GET.get("language", None) diff --git a/web-app/django/VIM/templates/instruments/index.html b/web-app/django/VIM/templates/instruments/index.html index aa2c41ee..8a8dd30f 100644 --- a/web-app/django/VIM/templates/instruments/index.html +++ b/web-app/django/VIM/templates/instruments/index.html @@ -169,11 +169,27 @@

- Showing {{ page_obj.start_index }} to {{ page_obj.end_index }} of {{ page_obj.paginator.count }} entries - {% if hbs_facet_name %}for{% endif %} - {{ hbs_facet_name }} - {% if search_query %} - for search query "{{ search_query }}" + + {% if page_obj.paginator.count == 0 %} + No results found + {% if hbs_facet_name %}for {{ hbs_facet_name }}{% endif %} + {% if search_query %} + for search query "{{ search_query }}". + {% endif %} + + {% elif effective_query != search_query %} + Showing {{ page_obj.start_index }} to {{ page_obj.end_index }} of {{ page_obj.paginator.count }} + {% if hbs_facet_name %}for {{ hbs_facet_name }}{% endif %} + {% if search_query %} + for search query "{{ effective_query }}", as no results were found for "{{ search_query }}". + {% endif %} + + {% else %} + Showing {{ page_obj.start_index }} to {{ page_obj.end_index }} of {{ page_obj.paginator.count }} + {% if hbs_facet_name %}for {{ hbs_facet_name }}{% endif %} + {% if search_query %} + for search query "{{ search_query }}". + {% endif %} {% endif %}