Be careful about implementing any search that excludes results your users might want
Nielson Norman Group provides this alarming statistic about users of web search engines:
the rough estimate from our available data is obvious: users change search strategy only 1% of the time; 99% of the time they plod along a single unwavering path. Whether the true number is 2% or 0.5%, the big-picture conclusion is the same: users have extraordinarily inadequate research skills when it comes to solving problems on the Web.
That is to say, a user is going to do what they do with a search engine box. They are not that likely to analyze if they should change how they use it. So...
- Some of your users will use quotes to match exact phrases (as you intend)
- Some of your users will just add quotes as if they were any other character
- Neither of these groups is likely to consider how your search engine interprets quotes
With this in mind, exact phrase matching can be problematic. If a user enters enters the search phrase:
american “one life to loose for my country" [sic] they might find no results (because of the word "lose" being mispelled). A user would be highly unlikely to step back and try try another method like
American Revolutionary war quotes.
At best, they will pogo stick and tweak their search a little. At worst, they immediately assume you have no relevant results, or your search is “broken.”
So let’s consider your examples, bearing in mind some of your users are not intending to use exact phrase matching (or even know what it is).
more "perfect union"
Looks like your user might be intending to use phrase matching (but maybe not!). Two strategies are reasonable here:
- Prioritize results that match the exact phrase, but include other results lower down.
- If there are exact phrases available, display them. Otherwise, disregard the quotes.
Also, make sure your search return results that return anything that matches either the phrase or the single word (below results that match both). This is how common search tools like Solr and Lucene work.
more ""perfect union
It's highly unlikely your user is attempting to search for an empty string. Disregard the quotes.
more per"fect union"
It's possible that a user was attempting a boolean search and missed a space. Assume a space "outside" the double quote, and treat as case 1 above.
more "perfect union
That's not a valid boolean search. Disregard the quote.
more per"fect union
I can’t find examples of this type of search in my search logs (admittedly, they aren't the largest around). I would guess it's more likely a missing space than a misplaced quote, so I would treat “per” and “fect” as separate words.
It's also an invalid syntax case, so just replace the quote with a space.
6. Multiple sets of quotes:
more "perfect union" establish justice, ensure "domestic tranquility"
As this is a valid syntax, treat similarly to how your treat case #1. Results that match all exact phrases should appear first. Other results appear below.
(I would not bother with trying to make results that match just one/some of the exact phrases appear above those that match none. The first set of results are the ones that would match the strict logic, then there are all the other results.)
7. Nested quotes
It’s unlikely a user providing nested quotes is attempting an exact phrase match. Disregard the quotes.
8. Trailing spaces:
more "perfect union "
Search engines generally do not search for spaces. Disregard the trailing space.