Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 709
RamRajVasavi Avatar asked Dec 04 '25 23:12

RamRajVasavi


1 Answers

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
    }
}'
like image 167
Val Avatar answered Dec 06 '25 14:12

Val



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!