RamRajVasavi
RamRajVasavi

Reputation: 906

Get specific fields from index in elasticsearch

I have an index in elastic-search.

Sample structure :

{
    "Article": "Article7645674712",
    "Genre": "Genre92231455",
    "relationDesc": [
        "Article",
        "Genre"
    ],
    "org": "user",
    "dateCreated": {
        "date": "08/05/2015",
        "time": "16:22 IST"
    },
    "dateModified": "08/05/2015"
}

From this index i want to retrieve selected fields: org and dateModified.

I want result like this

{
    "took": 265,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 28,
        "max_score": 1,
        "hits": [
            {
                "_index": "couchrecords",
                "_type": "couchbaseDocument",
                "_id": "3",
                "_score": 1,
                "_source": {
                    "doc": {
                        "org": "user",
                        "dateModified": "08/05/2015"
                    }
                }
            },
            {
                "_index": "couchrecords",
                "_type": "couchbaseDocument",
                "_id": "4",
                "_score": 1,
                "_source": {
                    "doc": {
                        "org": "user",
                        "dateModified": "10/05/2015"
                    }
                }
            }
        ]
    }
}

How to query elastic-search to get only selected specific fields ?

Upvotes: 3

Views: 13372

Answers (3)

Radu Linu
Radu Linu

Reputation: 1271

{
  "_source" : ["org","dateModified"],
  "query": {
  ...
  }
}

Check ElasticSearch source filtering.

Upvotes: 0

Val
Val

Reputation: 217554

You can retrieve only a specific set of fields in the result hits using the _source parameter like this:

curl -XGET localhost:9200/couchrecords/couchbaseDocument/_search?_source=org,dateModified

Or in this format:

curl -XPOST localhost:9200/couchrecords/couchbaseDocument/_search -d '{
    "_source": ["doc.org", "doc.dateModified"],  <---- you just need to add this
    "query": {
        "match_all":{}   <----- or whatever query you have
    }
}'

Upvotes: 9

eliasah
eliasah

Reputation: 40380

That's easy. Considering any query of this format :

{
  "query": {
  ...
  },
}

You'll just need to add the fields field into your query which in your case will result in the following :

{
  "query": {
  ...
  },
  "fields" : ["org","dateModified"]
}

Upvotes: 1

Related Questions