Skip to content

Instantly share code, notes, and snippets.

@tamros
Last active October 13, 2021 17:29
Show Gist options
  • Save tamros/1927cf97ece62b6da2eda7ec2d525f10 to your computer and use it in GitHub Desktop.
Save tamros/1927cf97ece62b6da2eda7ec2d525f10 to your computer and use it in GitHub Desktop.
highlight subset of characters in word(s)
# (Re)create the index
DELETE /highlight
PUT /highlight
{
"settings": {
"analysis": {
"analyzer": {
"standard_ngram": {
"tokenizer": "standard",
"filter": [
"ngram"
]
}
}
}
},
"mappings": {
"properties": {
"body": {
"type": "text",
"analyzer": "standard_ngram",
"term_vector": "with_positions_offsets"
}
}
}
}
# Insert the data
POST /highlight/_doc
{ "body" : "The quick brown fox jumps over the lazy dog" }
POST /highlight/_doc
{ "body" : "Lazy dog jumps over the quick brown fox" }
POST /highlight/_doc
{ "body" : "A Fox one day fell into a deep well and could find no means of escape. A Goat, overcome with thirst, came to the same well, and seeing the Fox, inquired if the water was good. Concealing his sad plight under a merry guise, the Fox indulged in a lavish praise of the water, saying it was excellent beyond measure, and encouraging him to descend. The Goat, mindful only of his thirst, thoughtlessly jumped down, but just as he drank, the Fox informed him of the difficulty they were both in and suggested a scheme for their common escape. \"If,\" said he, \"you will place your forefeet upon the wall and bend your head, I will run up your back and escape, and will help you out afterwards.\" The Goat readily assented and the Fox leaped upon his back. Steadying himself with the Goat horns, he safely reached the mouth of the well and made off as fast as he could. When the Goat upbraided him for breaking his promise, he turned around and cried out, \"You foolish old fellow! If you had as many brains in your head as you have hairs in your beard, you would never have gone down before you had inspected the way up, nor have exposed yourself to dangers from which you had no means of escape.\" Look before you leap." }
POST /highlight/_refresh
POST /highlight/_search?pretty=true
{
"query" : {
"query_string": {
"fields": ["body"],
"query" : "in",
"analyzer": "simple"
}
},
"highlight" : {
"fields" : {
"body" : {"number_of_fragments" : 0}
},
"pre_tags" : ["******"],
"post_tags" : ["******"]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment