We are designing a search algorithm where the requirements dictate, "users may include quotes around their search term in order to return an exact match." This is simple to execute given input like:

"more perfect union"

Expected results should include the entire phrase, "more perfect union" - not a slew of results that include the word "more".

But what are the expected results for search terms that stray from the happy path?

  1. Quotes that contain part of the searched phrase: more "perfect union"
  2. Quotes that don't contain anything: more ""perfect union
  3. Quotes that start in the middle of the word: more per"fect union"
  4. Uneven / Open quotes: more "perfect union
  5. Uneven quotes interrupting a word: more per"fect union
  6. Multiple sets of quotes: more "perfect union" establish justice, ensure "domestic tranquility"
  7. Nested quotes: more "perfect union, "establish justice", ensure domestic tranquility"
  8. Trailing spaces: more "perfect union "
  • One trick is to replace spaces and noise words with plus signs when visualizing how a certain scenario looks logically. Commented Dec 31, 2016 at 7:02

2 Answers 2


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...

  1. Some of your users will use quotes to match exact phrases (as you intend)
  2. Some of your users will just add quotes as if they were any other character
  3. 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).

1. more "perfect union"

Looks like your user might be intending to use phrase matching (but maybe not!). Two strategies are reasonable here:

  1. Prioritize results that match the exact phrase, but include other results lower down.
  2. 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.

2. more ""perfect union

It's highly unlikely your user is attempting to search for an empty string. Disregard the quotes.

3. 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.

4. more "perfect union

That's not a valid boolean search. Disregard the quote.

5. 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.


In my person opinion, and limited testing with Google, I'd say:

  1. Search for phrases including "more" and the exact match of "perfect union", so "perfect union more" works but not "perfect more union".

  2. Ignore the quotes, if your search included the empty string it should be the same as ignoring it anyway.

  3. Search for phrases including "more", "per", and the exact match of "fect union".

  4. Strip the quote mark the same way Google strips most special characters.

  5. Same as 4

  6. Search for quotes with "perfect union" and "domestic tranquility" exact matches (i.e. in that order) and all of the other words in any order.

  7. This one is tricky. You would need to search for the phrase "perfect union, establish justice, ensure domestic tranquility" exact matches for sure, but do you treat the nested quotes as string (i.e. the result must also have quotation marks there) or as a search special character (i.e. the result doesn't need the quotes in it, it just needs the words establish justice in it). I would personally go with the first option.

  8. Again very tricky, I would strip trailing spaces as more often then not they are going to be erroneous and not caught by the user. But it's hard to go around the wording EXACT MATCH if you're not going to be exact. I would user test this one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.