Return field where text was found in ElasticSearch
I need help. I have these docs for elasticsearch 1.6
{
"name":"Sam",
"age":25,
"description":"Something"
},
{
"name":"Michael",
"age":23,
"description":"Something else"
}
with this query:
GET /MyIndex/MyType/_search?q=Michael
Elasticity returns this object:
{
"name":"Michael",
"age":23,
"description":"Something else"
}
... That's correct, but I want to get exactly the key where the text "Michael" was found. Is it possible? Many thanks.
+3
source to share
1 answer
I am assuming that by key you mean the document ID.
When indexing the following documents:
PUT my_index/my_type/1
{
"name":"Sam",
"age":25,
"description":"Something"
}
PUT my_index/my_type/2
{
"name":"Michael",
"age":23,
"description":"Something else"
}
Look for:
GET /my_index/my_type/_search?q=Michael
You will receive the following response:
{
"took": 8,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.15342641,
"hits": [
{
"_index": "my_index",
"_type": "my_type",
"_id": "2",
"_score": 0.15342641,
"_source": {
"name": "Michael",
"age": 23,
"description": "Something else"
}
}
]
}
}
As you can see, the hits array contains an object for each search. The key for Michael in this case is "_id": "2", which has his document id.
Hope it helps.
+1
source to share