How can I search the specific value in the _source from elasticSearch inquired result?

196 Views Asked by At

I'm collecting logs through Elastic Search. And I look up the results through a query.

When inquiring with the following query

GET test/_search
{
  "query": {
    "match_all":{
    }
  }
}

The result is inquired as follows.

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 100,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "test",
        "_id" : "1a2b3c4d5e6f",
        "_score" : 1.0,
        "_source" : {
          "team" : "Marketing"
          "number" : "3"
          "name" : "Mark"
        }
      },
      {
        "_index" : "test",
        "_id" : "1a2b3c4d5e66",
        "_score" : 1.0,
        "_source" : {
          "team" : "HR"
          "number" : "1"
          "name" : "John"
        }
      }, 
      ........

but, I want to be inquired as below.(Specific value of Inner_hits)

{
    "name": "Mark"
},
{
    "name": "John"
},

So, How can I query a specific value inner_hits?

Thanks.

1

There are 1 best solutions below

2
On BEST ANSWER

You could simply use the source_filtering feature of ES, so in your case, your query will like below:

{
    "_source": "name",
    "query": {
        "match_all": {}
    }
}

And it returns search results like

"hits": [
            {
                "_index": "64214413",
                "_type": "_doc",
                "_id": "1",
                "_score": 1.0,
                "_source": {
                    "name": "Mark"
                }
            },
            {
                "_index": "64214413",
                "_type": "_doc",
                "_id": "2",
                "_score": 1.0,
                "_source": {
                    "name": "John"
                }
            }
        ]