js-solr-highlighter
A JavaScript library for highlighting HTML text based on the query in the lucene/solr query syntax
Run in the browser or Node.js environment
Built based on lucene and text-annotator
The general highlighting process is:
- Derive which text to highlight from a query in the lucene syntax
- Highlight the derived text in the HTML
js-solr-highlighter has been used to highlight the article titles in the search results of Europe PMC. An example is https://europepmc.org/search?query=blood%20AND%20TITLE%3Acancer
Basic usage
No options
var query = 'cancer AND blood'
var content = 'Platelet Volume Is Reduced In Metastasing Breast Cancer: Blood Profiles Reveal Significant Shifts.'
var highlightedContent = highlightByQuery(query, content)
With the validFields options that specify the fields valid in the query syntax. If not specified, all like x:x will be valid fields
var query = 'TITLE:blood AND CONTENT:cell'
var content = 'A molecular map of lymph node blood vascular endothelium at single cell resolution'
var options = { validFields: ['TITLE'] }
var highlightedContent = highlightByQuery(query, content, options)
With the highlightedFields options that specify the valid fields whose values will be highlighted. If not specified, the values of all valid fields will be highlighted
var query = 'TITLE:blood OR CONTENT:cell'
var content = 'A molecular map of lymph node blood vascular endothelium at single cell resolution'
var options = { validFields: ['TITLE', 'CONTENT'], highlightedFields: ['CONTENT'] }
var highlightedContent = highlightByQuery(query, content, options)
Options
Field | Type | Description |
---|
validFields | array | validFields are those parsed as fields. If undefined, all will be parsed as fields if they are like x:x |
highlightedFields | array | highlightedFields are those among validFields whose values will be highlighted. If undefined, the values of all valid fields will be highlighted. |
highlightAll | boolean | highlightAll indicates whether to highlight all occurances of the text or the first found occurance only. If undefined, it is true. |
highlightIdPattern | string | highlightIdPattern is the same pattern of the IDs associated with the highlights in the HTML. A highlight ID consists of highlightIdPattern followed by the index of the highlight, such as "highlight-0", "highlight-1"... If undefined, it is "highlight-". |
highlightClass | string | highlightClass is the classname of every highlight in the HTML. If undefined, it is "highlight". |
Highlighting rules
Contact
Zhan Huang