What did Elasticsearch index?

Elasticsearch is an amazing search server built for developers. In particular, we love the simplicity and depth of control of Elasticsearch’s API.

Lately, we stumbled upon a question for which the answer wasn’t that straight forward though: Which terms did Elasticsearch index for a specific document?

For example: Searching for books, we expected our document hitchhiker-guide-to-the-galaxy to match when querying for science fiction in field category. As it didn’t show up, we suspected that the field was wrongly indexed.

Luckily, script fields came to our help:

{
"query":{
"term":{
"id":"hitchhiker-guide-to-the-galaxy"
}
},
"script_fields":{
"terms":{
"script":"doc[field].values",
"params":{
"field":"category"
}
}

}
}

Finally, the indexed terms highlighted the issue in our analyzer configuration:

{

"hits":{
"total":1,
"hits":[
{

"fields":{
"terms":[
[ "sciencefiction" ]
]

}
}
]
}
}

Happy coding!


Photo: Ronny Roeller