I'm using Elastic Search to Index and Search my Documents, I want to retrieve field-name from Index for given Search Term. Here is the Explanation of my Question with Example.
--EXAMPLE--
Template:
PUT /books
{
"Chapters":
{
"dynamic_templates":
[
{
"nested_feature":
{
"match":"*",
"mapping":
{
"type":"nested",
"include_in_parent":true
}
}
},
{
"nested_template":
{
"path_match":"*.*",
"match_mapping_type":"string",
"mapping":
{
"type":"multi_field",
"fields":
{
"{name}":
{
"type":"{dynamic_type}",
"index":"analyzed"
}
}
}
}
}
]
}
}
Index Mapping:
PUT /books/_mapping/new
{
"properties":
{
"BookName":
{
"type":"string"
},
"ISBN":
{
"type":"string"
},
"Chapters":
{
"type":"nested"
}
}
}
Indexed Documents:
POST /books/new
{
"BookName":"Book1",
"ISBN":"978-3-16-148410-0",
"Chapters":{
"Chapter1":"Before getting into computer programming, let us first understand computer programs and what they...",
"Chapter2":"Today computer programs are being used in almost every field, household, agriculture, medical, entertainment, defense..",
"Chapter3":"MS Word, MS Excel, Adobe Photoshop, Internet Explorer, Chrome, etc., are...",
"ChapterN":"Other Content..."
}
}
POST /books/new
{
"BookName":"Book2",
"ISBN":"548-1-13-344410-1",
"Chapters":{
"The Adoption":"When Paul Jobs was mustered out of the Coast Guard after World War II, he...",
"Silicon Valley":"The childhood that Paul and Clara Jobs created for their new son was, in many ways...",
"School ":"Even before Jobs started elementary school, his mother had taught him how to read. This...",
"ChapterN":"Other Content..."
}
}
Search Query to get result:
GET books/new/_search
{
"query" : {
"match" : {
"_all" : "mustered"
}
},
"highlight": {
"fields" : {
"*" : {}
}
}
}
Result I got:
{.....
"_source": {
"BookName": "Book2",
"ISBN": "548-1-13-344410-1",
"Chapters": {
"The Adoption": "When Paul Jobs was mustered out of the Coast Guard after World War II, he...",
"Silicon Valley": "The childhood that Paul and Clara Jobs created for their new son was, in many ways...",
"School ": "Even before Jobs started elementary school, his mother had taught him how to read. This...",
"ChapterN": "Other Content..."
}
}
}
]
}
}
Expected Result:
{....
"_source": {
"BookName": "Book2",
"ISBN": "548-1-13-344410-1",
"Chapters": {
"The Adoption": "When Paul Jobs was mustered out of the Coast Guard after World War II, he...",
"Silicon Valley": "The childhood that Paul and Clara Jobs created for their new son was, in many ways...",
"School ": "Even before Jobs started elementary school, his mother had taught him how to read. This...",
"ChapterN": "Other Content..."
}
},
"highlight": {
"The Adoption": [
"When Paul Jobs was <em>mustered</em> out of the Coast Guard after World War II, he..."
]
}
}
]
}
}
--END OF EXAMPLE--
Basically what I want to do so far is to retrieve particular field name when any one searches for it's data, as in example I searched for 'mustered' and It should return 'The Adoption'.
If there is any other method available to do this is accepted, It is not compulsory to use Highlight. Any methods accepted that can fulfill my requirement and give Expected Result.