How can we eliminate the extra fields from elasticsearch query result?

{
"_id": "0048160a463a73faaa6c90f5af027772",

"_rev": "1-ff6255309f1b873a4e482310843a8a15",

"timestamp": 1496275536932.6602,

"results": {

"lines": {

  "S1": [

    {

      "needed_key": "foo",

      "not_needed_key": 1

    },
{

      "needed_key": "bar",

      "not_needed_key": 1

    },

    {

      "needed_key": "foo_bar",

      "not_needed_key": 1

    }

  ],

  ...

}

},

"station": "5002270",

"another_not_needed_key": "something"

}

How can I write an elasticsearch query to get the output by eliminating the unnecessary fields from above document?

Query Which I have used to return result :-
GET Index/_search
{
"query": {
"bool": {
"filter": {
"term": {
"results.lines.S1.needed_key":"foo"
}
}
}
}
}

Below is my expected output.

{
"_id": "fd298368a7a344b217698677f3f5a07d",
"timestamp": 1496275536932.6602,
"station": "5002270",
"S1": {
"needed_key": "foo",
}
}

Below is my Actual output :
{
"_id": "0048160a463a73faaa6c90f5af027772",

"_rev": "1-ff6255309f1b873a4e482310843a8a15",

"timestamp": 1496275536932.6602,

"results": {

"lines": {

  "S1": [

    {

      "needed_key": "foo",

    },
     {

      "needed_key": "bar",

    },
    {

      "needed_key": "foo_bar",

    }

  ],
  ...

}

},

"station": "5002270",

"another_not_needed_key": "something"

}

This seems to be a duplicate of this thread. Please do not open multiple threads for the same question.

1 Like