Quoted (Phrase, Sentence) Search Support
Note: This documentation is for SearchWP 4
SearchWP 3.x: 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.
To enable this functionality, tick the appropriate checkbox on the Advanced tab of the SearchWP settings screen:
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.
- In following how SearchWP works SearchWP will first perform a search using
ANDlogic, ensuring that all search terms are present. If no results are found in that pass, SearchWP will automatically revert to
ORlogic. This behavior can be customized using hooks.
- 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.
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)
|margin: 0 calc(10% + 60px) 1em;|
|border: 1px solid #e7e7e7;|
Which results in a notification that better matches the site design:
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.
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.