| by Arround The Web | No comments

Elasticsearch Select Specific Fields

Elasticsearch will default return all the fields in a document upon performing a search query. This is defined by the _source parameter, which contains all the data stored in the record during the indexing.

curl -XGET "http://localhost:9200/netflix/_doc/HXYz_IIBLbuC0z3qKeN2?pretty" -H "kbn-xsrf: reporting"

Output:

However, you may not want to retrieve all the fields from a given document. In this tutorial, you will learn how select specific fields from a document.

Elasticsearch Fields Option

The fields parameter allows us to retrieve specific fields in a search request. In addition, the field parameter will enable us to fetch single or multiple fields. You can also format dates and spatial data types using the fields parameter.

For example, suppose we want to retrieve the index, id, title, release_year, listed in, duration, and rating fields from the Netflix index, we can run a query as shown below:

curl -XGET "http://localhost/netflix/_search" -H "kbn-xsrf: reporting" -H "Content-Type: application/json" -d'
{
  "query": {
    "match": {
      "_id": "HXYz_IIBLbuC0z3qKeN2"
    }
  },
  "fields": [
    "index",
    "id",
    "title",
    "release_year",
    "listed_in",
    "duration",
    "rating"
  ],
  "_source": false

}'

In the request above, we use the search API to search the document with the specified id in the match parameter.

We then use the fields parameter to fetch the specific fields from the target document.

Note that the Elasticsearch will default include the _source parameter, consisting of all the document fields. To ensure we only fetch the specified fields, we turn off the _source parameter:

The request above should return a response as shown:

{
 "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 1,
      "relation": "eq"
    },
    "max_score": 1,
    "hits": [
      {
        "_index": "netflix",
        "_id": "HXYz_IIBLbuC0z3qKeN2",
        "_score": 1,
        "fields": {
          "listed_in": [
            "Documentaries"
          ],
          "duration": [
            "90 min"
          ],
          "release_year": [
            2020
          ],
          "rating": [
            "PG-13"
          ],
          "title": [
            "Dick Johnson Is Dead"
          ]         }
      }
    ]   }
}

You can also use the _source parameter to define which fields to return from a search query. An example is shown below:

curl -XGET "http://localhost:9200/netflix/_search" -H "kbn-xsrf: reporting" -H "Content-Type: application/json" -d'
{
  "_source": ["title", "release_year", "rating", "duration"],
  "query": {
    "term": {
      "_id": {
        "value": "HXYz_IIBLbuC0z3qKeN2"
      }
    }
  }

}'

In this case, we specify the fields we wish to retrieve as an array in the source parameter. The request above should return the response as shown:

{
  "took": 0,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 1,
      "relation": "eq"
    },
    "max_score": 1,
    "hits": [
      {
        "_index": "netflix",
        "_id": "HXYz_IIBLbuC0z3qKeN2",
        "_score": 1,
        "_source": {
          "duration": "90 min",
          "release_year": 2020,
          "rating": "PG-13",
          "title": "Dick Johnson Is Dead"
        }
      }
    ]   }

}

Conclusion

In this article, you learned how to fetch specific fields from a search request using the fields and _source parameters.

To learn more about Elasticsearch and its capabilities, check out our tutorials on the topic. You are guaranteed to find something useful.

Thanks for reading & Catch you in the next one!!

Share Button

Source: linuxhint.com

Leave a Reply