Reputation: 106
I am building an application that needs to have the function to allow users to search similar contents within a pdf(probably using ElasticSearch)... As I did write some code to find out how to find that exact pdf file using ElasticSearch but I have no idea how to get the exact location of the text... Or how can I highlight the search results? It would be really helpful if code examples are provided >_<
Upvotes: 0
Views: 836
Reputation: 16192
You can use highlighting where highlighters enable you to get highlighted snippets from one or more fields in your search results so you can show users where the query matches are.
Adding a working example with index data,search query, and search result
Index Data:
{
"content": "The nonprofit Wikimedia Foundation provides the essential infrastructure for free knowledge. We host Wikipedia, the free online encyclopedia, created, edited, and verified by volunteers around the world, as well as many other vital community projects. All of which is made possible thanks to donations from individuals like you. We welcome anyone who shares our vision to join us in collecting and sharing knowledge that fully represents human diversity."
}
Search Query:
{
"query": {
"match": {
"content": "online"
}
},
"highlight": {
"fields": {
"content": {}
}
}
}
Search Result:
"hits": [
{
"_index": "65463291",
"_type": "_doc",
"_id": "1",
"_score": 0.2876821,
"_source": {
"content": "The nonprofit Wikimedia Foundation provides the essential infrastructure for free knowledge. We host Wikipedia, the free online encyclopedia, created, edited, and verified by volunteers around the world, as well as many other vital community projects. All of which is made possible thanks to donations from individuals like you. We welcome anyone who shares our vision to join us in collecting and sharing knowledge that fully represents human diversity."
},
"highlight": { // note this
"content": [
"We host Wikipedia, the free <em>online</em> encyclopedia, created, edited, and verified by volunteers around the"
]
}
}
]
Upvotes: 1