1# Highlighting API 2 3The highlighting API allows you to have only the relevant portions of document matching a search query returned as a result. This allows users to quickly see how a document relates to their query, with the search terms highlighted, usually in bold letters. 4 5RediSearch implements high performance highlighting and summarization algorithms, with the following API: 6 7## Command syntax 8 9``` 10FT.SEARCH ... 11 SUMMARIZE [FIELDS {num} {field}] [FRAGS {numFrags}] [LEN {fragLen}] [SEPARATOR {sepstr}] 12 HIGHLIGHT [FIELDS {num} {field}] [TAGS {openTag} {closeTag}] 13 14``` 15 16There are two sub-commands commands used for highlighting. One is `HIGHLIGHT` which surrounds matching text with an open and/or close tag, and the other is `SUMMARIZE` which splits a field into contextual fragments surrounding the found terms. It is possible to summarize a field, highlight a field, or perform both actions in the same query. 17 18### Summarization 19 20``` 21FT.SEARCH ... 22 SUMMARIZE [FIELDS {num} {field}] [FRAGS {numFrags}] [LEN {fragLen}] [SEPARATOR {sepStr}] 23``` 24 25Summarization will fragment the text into smaller sized snippets; each snippet will contain the found term(s) and some additional surrounding context. 26 27RediSearch can perform summarization using the `SUMMARIZE` keyword. If no additional arguments are passed, all _returned fields_ are summarized using built-in defaults. 28 29The `SUMMARIZE` keyword accepts the following arguments: 30 31* **`FIELDS`**: If present, must be the first argument. This should be followed 32 by the number of fields to summarize, which itself is followed by a list of 33 fields. Each field present is summarized. If no `FIELDS` directive is passed, 34 then *all* fields returned are summarized. 35 36* **`FRAGS`**: How many fragments should be returned. If not specified, a default of 3 is used. 37 38* **`LEN`** The number of context words each fragment should contain. Context 39 words surround the found term. A higher value will return a larger block of 40 text. If not specified, the default value is 20. 41 42* **`SEPARATOR`** The string used to divide between individual summary snippets. 43 The default is `... ` which is common among search engines; but you may 44 override this with any other string if you desire to programmatically divide them 45 later on. You may use a newline sequence, as newlines are stripped from the 46 result body anyway (thus, it will not be conflated with an embedded newline 47 in the text) 48 49 50### Highlighting 51 52``` 53FT.SEARCH ... HIGHLIGHT [FIELDS {num} {field}] [TAGS {openTag} {closeTag}] 54``` 55 56Highlighting will highlight the found term (and its variants) with a user-defined tag. This may be used to display the matched text in a different typeface using a markup language, or to otherwise make the text appear differently. 57 58RediSearch can perform highlighting using the `HIGHLIGHT` keyword. If no additional arguments are passed, all _returned fields_ are highlighted using built-in defaults. 59 60The `HIGHLIGHT` keyword accepts the following arguments: 61 62* **`FIELDS`** If present, must be the first argument. This should be followed 63 by the number of fields to highlight, which itself is followed by a list of 64 fields. Each field present is highlighted. If no `FIELDS` directive is passed, 65 then *all* fields returned are highlighted. 66 67* **`TAGS`** If present, must be followed by two strings; the first is prepended 68 to each term match, and the second is appended to it. If no `TAGS` are 69 specified, a built-in tag value is appended and prepended. 70 71 72#### Field selection 73 74If no specific fields are passed to the `RETURN`, `SUMMARIZE`, or `HIGHLIGHT` keywords, then all of a document's fields are returned. However, if any of these keywords contain a `FIELD` directive, then the `SEARCH` command will only return the sum total of all fields enumerated in any of those directives. 75 76The `RETURN` keyword is treated specially, as it overrides any fields specified in `SUMMARIZE` or `HIGHLIGHT`. 77 78In the command `RETURN 1 foo SUMMARIZE FIELDS 1 bar HIGHLIGHT FIELDS 1 baz`, the fields `foo` is returned as-is, while `bar` and `baz` are not returned, because `RETURN` was specified, but did not include those fields. 79 80In the command `SUMMARIZE FIELDS 1 bar HIGHLIGHT FIELDS 1 baz`, `bar` is returned summarized and `baz` is returned highlighted. 81