0

my response from 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 fields _index, _type and _score to be removed from each hits.hits element. How do I do this?

musicsquad
  • 95
  • 1
  • 3
  • 9

1 Answers1

2

You can use response filtering by specifying the filter_path parameter in the query string like this:

curl -XPOST 'localhost:9200/_search?pretty&fields=name&filter_path=hits.hits.fields' -d '{
    "query": {
        "match": {
            "name": "john"
        }
    }
}'

or using source filtering instead

curl -XPOST 'localhost:9200/_search?pretty&_source=name&filter_path=hits.hits._source' -d '{
    "query": {
        "match": {
            "name": "john"
        }
    }
}'

Your response will look like this instead:

{  
    "hits":{  
        "hits":[  
            {  
                "fields":{  
                    "name":[  "John Smith"  ]
                }
            },
            {  
                "fields":{  
                    "name":[  "John Doe"  ]
                }
            }
        ]
    }
}
Val
  • 207,596
  • 13
  • 358
  • 360
  • nope, the request URL loks like so POST http:///myindex/mytype/_search?filterPath=hits.hits.fields&fields=name, but each hits element still has _index, _type and _score. btw, why does "name" automatically becomes array of string? – musicsquad Aug 08 '16 at 13:04
  • Are you sending the query via curl? Don't forget the single quotes around the whole URL – Val Aug 08 '16 at 13:05
  • Ok, then please update your question with your client code so it's easier to see where the error lies. – Val Aug 08 '16 at 13:07
  • hang on, this particular code is from someone else. I'm trying to recreate using another lib, so if it works with my code, then the problem is definitely with jest. I'm using AWS ES by the way, not sure if it has any effects, just an FYI – musicsquad Aug 09 '16 at 14:49
  • Just make sure to use `filter_path` and [not `filterPath`](https://github.com/aws-es-user/aws-es/blob/master/src/main/java/com/awses/main/ElasticSearchService.java#L22) and you'll be fine – Val Aug 10 '16 at 09:11
  • got and additional question, there is any way to remove the hits object, and just return the array? Thanks! – Gerardo Jaramillo Dec 22 '16 at 00:29
  • 1
    @GerardoJaramillo You should ask another question since this is a different issue. – Val Dec 22 '16 at 04:30
  • @musicsquad - Did you get the result you needed in the end and have just the array of indexes showing? Im facing the same issue and wanting to remove hits.hits from the response and also if possible _source without having to do a foreach loop on the client to remove this. Thanks – Birdy May 24 '17 at 16:22
  • Just for reference for other people checking this, that option is not available [if you are using version elasticsearch 1.5](https://stackoverflow.com/a/50506659/1280385). – Jorge May 24 '18 at 10:16