Sahar Rabinoviz
Sahar Rabinoviz

Reputation: 1979

Elasticsearch return field of object

I have the following json object

{ "type_name" : 
    {
        "id" : "string"
        "timestamp" : "string"
        "item" : [{"name":"string"}, {...} ...]
    }
}

When I run a query like so,

    lastResponse = client.prepareSearch("index")
            .setSearchType(SearchType.QUERY_AND_FETCH)
            .setQuery(qb)
            .setFrom(0).setSize(60).setExplain(true)
            .execute()
            .actionGet();

The response gives me back a list of type_name that match the query.

Is there a way to have elasticsearch return a single "item" as oppose to the entire type_name?

Upvotes: 1

Views: 418

Answers (1)

Olly Cruickshank
Olly Cruickshank

Reputation: 6180

Use fields. Using the JSON API this would be as follows:

{
    "fields" : ["id", "timestamp"],
    "query" : {
        "term" : { "id" : "1234567" }
    }
}

If using the Java API, use setFields as follows:

GetResponse response = client.prepareGet(index, type, id)
       .setFields("title")
       .execute()
       .actionGet();

See this forum post for more details.

Upvotes: 2

Related Questions