Highlight Fields
Engine Type | Supported? |
---|---|
Crawler-based Engine | YES |
API-based Engine | YES |
Highlight query matches within result text for a given field.
Highlighting wraps relevant text in <em></em>
tags, which can be styled however you choose.
- engine_key
- required
- The Engine Key associated with your Engine. Found within your dashboard.
- q
- required
- The query used within your search.
- highlight_fields
- optional
- Returns highlighted query matches within the given fields. Expects a DocumentType, a field name, and parameters. The
size
parameter indicates the number of characters to return. Iffallback
is set to true, will return raw text instead of a styled result if no exact query matches are found. If false, will return nothing.
Example - Return documents from the
books
DocumentType that match the query "brothers", highlighting 60
characters of the title
field. It will fallback to raw, unstyled results if no matches are found. Crawler based Engines use page
as the default DocumentType.
curl -XGET 'https://search-api.swiftype.com/api/v1/public/engines/search.json' \
-H 'Content-Type: application/json' \
-d '{
"engine_key": "YOUR_ENGINE_KEY",
"q": "brothers",
"highlight_fields":{
"books":{"title":{"size":60,"fallback":true}}
}
}'
Example - Use escaped quotes to highlight only an exact match - case insensitive.
curl -XGET 'https://search-api.swiftype.com/api/v1/public/engines/search.json' \
-H 'Content-Type: application/json' \
-d '{
"engine_key": "YOUR_ENGINE_KEY",
"q": "\"brothers\"",
"highlight_fields": {
"books": { "title": { "size": 60, "fallback": true } }
}
}'
Stuck? Looking for help? Contact support or check out the Site Search community forum!