How to control what fields returned in response

Hi,

my response from a search API looks like this

{  
    "took":88,
    "timed_out":false,
    "_shards":{  
        "total":3,
        "successful":3,
        "failed":0
    },
    "hits":{  
        "total":2,
        "max_score":1.0,
        "hits":[  
            {  
                "_index":"myindex",
                "_type":"mytype",
                "_id":"first",
                "_score":1.0,
                "fields":{  
                    "name":[  "John Smith"  ]
                }
            },
            {  
                "_index":"myindex",
                "_type":"mytype",
                "_id":"second",
                "_score":1.0,
                "fields":{  
                    "name":[  "John Doe"  ]
                }
            }
        ]
    }
}

I want the response not to include _index, _type, _score in each hits element.
how do I do this?
Thanks

There are a few basic fields which are always returned with a search hit. That includes __index, _type, id. If you wish to turn off score, you can specify query argument: track_scores=false

If you're looking for a very lean interface to use externally, you'd want to write a simple proxy client. In fact it would be advisable to write a proxy client for any external communication period in order to control serialization, business logic, and security.

so, there's just no way to get rid of _index and _type?

You can use response filtering and specify the filter_path parameter in your query:

Using

GET _search?filter_path=hits.hits.fields

You'll only get this in your response

{  
    "hits":{  
        "hits":[  
            {  
                "fields":{  
                    "name":[  "John Smith"  ]
                }
            },
            {  
                "fields":{  
                    "name":[  "John Doe"  ]
                }
            }
        ]
    }
}
1 Like

Hi Val,

the code doesnt work. it'll still return _index and _type and _score in every hits
I;ve uploaded in https://github.com/aws-es-user/aws-es
Main method is in ElasticSearchService. You'll need an AWS account to test

Thanks

You're using filterPath instead of filter_path.
See here: https://github.com/aws-es-user/aws-es/blob/master/src/main/java/com/awses/main/ElasticSearchService.java#L22

Please fix and try again.

dear God, that's so embarassing. Thank you so much for the help.

No problem, my pleasure!