openlibrary icon indicating copy to clipboard operation
openlibrary copied to clipboard

Search Inside: honour typed quotes and show how a query was interpreted

Open lokesh opened this issue 1 month ago • 4 comments

Problem / Opportunity

#13429 made Search Inside quote the whole query as one phrase (phrase_query() in openlibrary/core/fulltext.py). That is the right default for bare input (happiness paradox: 332 hits as a phrase vs ~821K as loose words), but two things got lost:

  1. Typed quotes are discarded. "United Nations" "world hunger" is sent as "United Nations world hunger" (25 hits instead of ~34K, and the obvious book, World Hunger: A Reference Handbook, drops out). The backend handles multi-phrase and mixed queries fine; we're the ones merging them. There is currently no way to search for two terms that appear at different points in a book.
  2. The rewrite is silent. Patrons who typed bare words get phrase results with no sign that the query was changed, and no way to ask for all-words instead.

Patron feedback: "groups of words are now interpreted as single units even when placed between distinct sets of quotation marks" / "I find the 'search inside' useless now."

Success: the quoted queries above return multi-term results, and a bare multi-word query shows how it was interpreted with a one-click switch.

Proposal

  1. Honour typed quotes. If the query contains any " (after normalising curly quotes), pass it through as typed, closing or dropping a stray unbalanced quote. Only auto-phrase when the query has no quotes.
  2. Show the interpretation, with an escape hatch. When the query was auto-phrased, has 2+ words and no typed quotes, show a line above the results: Showing exact matches for "argentina brazil". Search for all of these words instead. The link reruns the query unphrased.
  3. Fall back on zero results. If the auto-phrased query returns no hits, rerun it as all-words and say so: No exact matches for "…". Showing books containing all of these words.

Not in scope: phrase-boosted-plus-terms scoring (what archive.org metadata search does). The backend's simple parser treats AND/OR as literal tokens and its Lucene mode ignores olonly, so that has to happen on the backend.

Breakdown

Implementation Details (for maintainers)

Related files

  • openlibrary/core/fulltext.py — phrase_query(), fulltext_search_async()
  • openlibrary/plugins/openlibrary/js/search-modal/fulltext.js — mirrors is_passage_query()
  • openlibrary/templates/search/inside.html and the modal's fulltext results partial

Requirements Checklist

  • [ ] phrase_query() keeps typed quotes; doctests for mixed, multi-phrase and unbalanced input
  • [ ] Results line with "all of these words" link, shown only for auto-phrased 2+ word queries
  • [ ] Zero-hit fallback to all-words, labelled
  • [ ] Same behaviour on /search/inside and in the search modal

lokesh avatar Oct 07 '26 00:10 lokesh

@lokesh I can take this up! I'm already familiar with the context from our earlier discussion in the community channel.

Prabh0608 avatar Oct 07 '26 00:10 Prabh0608

Tested the single-round-trip rewrite suggested by the archive.org search team ("fast horse"^10 OR (fast AND horse), sort by score) against be-api /fts/v1/search. It doesn't work from our side: OR doesn't union on this endpoint. "xyzzyqq plugh"^10 OR (fast AND horse) returns 0 hits in both parser modes (bare and text:(…)), so a non-matching clause zeroes the whole query. ^10 and ~N are accepted but can't help without a working OR. _score is returned per hit, so the scoring half would work if the union did.

Conclusion: phrase-boosted-plus-terms needs to happen on the backend. Items 2 and 3 above remain the client-side approximation.

lokesh avatar Oct 07 '26 00:10 lokesh

@Prabh0608 go for it! Let me know if you have any questions.

lokesh avatar Oct 07 '26 04:10 lokesh

@lokesh For the second proposal Should the link retain active language and readable filters, and should it reset pagination to page 1? also in the First proposal for the case " it was the best of times " will pass through without removing the extra space right?

Prabh0608 avatar Oct 10 '26 11:10 Prabh0608