Quoted (Phrase, Sentence) Search Support
SearchWP supports quoted searches, sometimes called phrase or sentence-based searches. This allows your visitors to add quotes to their searches so as to force exact matches for that quoted string.
For example searching for coffee mugs
vs. "coffee mugs"
To enable this functionality, tick the appropriate checkbox on the General Settings tab of the SearchWP Settings page:
When enabled, SearchWP will check search queries for terms wrapped in quotes, and attempt to find exact matches for the quoted string. Referencing again the earlier example of coffee mugs
vs. "coffee mugs"
:
coffee mugs
- In following how SearchWP works SearchWP will first perform a search using
AND
logic, ensuring that all search terms are present. If no results are found in that pass, SearchWP will automatically revert toOR
logic. This behavior can be customized using hooks. "coffee mugs"
- Because the search string is wrapped in quotes, SearchWP will attempt to find results that have an exact match of
coffee mugs
(note caveats) when performing a search. If no results are found that have exact matches, the quotes will be removed and search performed again following that pattern.
Notes
By nature, quoted searches restrict/refine search results. This can result in no results being returned. With SearchWP, if no results are found with a quoted search, the algorithm will fall back to performing the search without quotes. A notice is automatically output to indicate the change.
⚠️ Style (CSS) customization is likely required ⚠️
The above screenshot outlines the automatic output in Twenty Nineteen, a default WordPress theme. The following CSS can be added to improve its appearance:
Note: (You can add Custom CSS to your site by navigating to Appearance > Customize > Additional CSS in your WordPress Dashboard)
.searchwp-revised-search-notice { | |
margin: 0 calc(10% + 60px) 1em; | |
padding: 0.6em; | |
background: #f7f7f7; | |
border: 1px solid #e7e7e7; | |
border-radius: 2px; | |
} | |
.searchwp-suggested-revision-query { | |
font-style: italic; | |
font-weight: bold; | |
} |
Which results in a notification that better matches the site design:
Caveats
There are some limitations to consider when evaluating SearchWP’s exact match implementation. SearchWP is a token-based search system so by nature SearchWP’s index does not contain full details about exact matches throughout its search index. SearchWP’s indexer also allows for content customization during indexing, which can further obscure exact matches in some cases.
As a result, SearchWP is not able to find exact matches in the following cases:
- Shortcode-generated content
- Extra metadata (although there is a workaround)
- Content modified during indexing
Because of these limitations, SearchWP’s quoted search support is an opt-in feature, requiring you to enable the functionality either by ticking the checkbox on the Advanced tab of the SearchWP settings screen.
Workarounds
There are some limited ways to work around the limitations of SearchWP’s quoted search support, as outlined below.
Extra Metadata Workaround
When SearchWP indexes Extra metadata the data is stored only in SearchWP’s index, which means the only record of the data is one that has been tokenized by the indexer.
In order to avoid the issue of SearchWP not being able to de-tokenize the content to perform exact match queries, you can customize your hook to make a call to update_post_meta
which will persist the data and enable quoted phrase search support for that record.
NOTE: In order to enable quoted search support you will need to add the meta_key
you’re using to the Custom Fields of each Source you’re working with. SearchWP uses those keys explicitly when performing this action.
This is an opt-in behavior, as SearchWP stores Extra Metadata only in its index to keep database usage at a minimum.