Search code examples
elasticsearchspring-data-elasticsearch

How to query and fetch just partial attributes using elasticsearch


  1. How to fetch Foo.id alone for multiple documents in an index
  2. How to fetch Foo.bi for multiple documents in an index

Sample response from Spring Data Elasticsearch for Page is given below

"content": [ { "id": "MMb-sHUBU93O1WjGd0M1", "pid": "12500000", "bi": { "bar": 27, "height": { "feet": 5, "inches": 6, }, }, ]


Solution

  • As to retrieving partial attributes, use _source:

    GET /_search
    {
      "_source": {
        "includes": [
          "Foo.id",
          "Foo.bi"
        ],
        "excludes": []
      }
    }
    

    Regarding

    How to query just partial attributes

    you're sending a query againt the whole index in ES and the query parameter enables you to filter concrete fields so it does not really make sense to talk about querying partial attributes -- that's what the query already does.