# How to use a field's meta data in search results

**URL:** <https://discuss.elastic.co/t/how-to-use-a-fields-meta-data-in-search-results/228996>\
**Category:** Elasticsearch\
**Created:** [April 21, 2020, 8:36am UTC](https://discuss.elastic.co/t/how-to-use-a-fields-meta-data-in-search-results/228996 "2020-04-21T08:36:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![arun\_001](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@arun\_001](https://discuss.elastic.co/u/arun_001)\
**Post date:** [April 21, 2020, 8:36am UTC](https://discuss.elastic.co/t/how-to-use-a-fields-meta-data-in-search-results/228996/1 "2020-04-21T08:36:14Z")

</div>

My requirement is to store some meta data against some fields and retrieve them as part of search results.  
Looks like the following is what I'm looking for but I'm not sure how use that in a single query.  
[https://www.elastic.co/guide/en/elasticsearch/reference/7.6/mapping-field-meta.html](https://www.elastic.co/guide/en/elasticsearch/reference/7.6/mapping-field-meta.html)

Ex:

```auto
    PUT my_index
    {
      "mappings": {
        "properties": {
          "latency": {
            "type": "long",
            "meta": {
              "unit": "ms"
            }
          }
        }
      }
    }

```

I want that unit 'ms' to be added to that field in all the documents retrieved for a query.

---

<div class="post-metadata">

**Author:** ![cbuescher](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cbuescher/32/60402_2.png) [@cbuescher](https://discuss.elastic.co/u/cbuescher)\
**Post date:** [April 21, 2020, 2:11pm UTC](https://discuss.elastic.co/t/how-to-use-a-fields-meta-data-in-search-results/228996/2 "2020-04-21T14:11:58Z")

</div>

Hi,

there are different ways to achieve this, all have pros and cons depending on what you want to do with the meta data. I assume you don't want to search on it since you want it added to all documents. One was would be to add a keyword field for this and set it to [`"index":false`](https://www.elastic.co/guide/en/elasticsearch/reference/7.x/mapping-index.html) to save some space, but you would have to add the value for each document for it to show up in the "\_source", e.g.

```auto
PUT my_index
{
  "mappings": {
    "properties": {
      "latency": {
        "type": "long"
      },
      "latency_unit": {
        "type": "keyword",
        "index": false
      }
      }
    }
  }
}

PUT /my_index/_doc/1
{
  "latency" : 1234567,
  "latency_unit" : "ms"
}

```

Of course you could also group the two fields under one parent field of type `object`.

If you don't want to add the same value all over in your input documents, we have a new field type [called `constant_keyword`](https://www.elastic.co/guide/en/elasticsearch/reference/7.7/constant-keyword.html) coming up in the next minor version (7.7) that allows you to define a field with a fixed value per index. If you want to show its value in search hits, you would need to add the field as a [`docvalue_fields` entry in the query](https://www.elastic.co/guide/en/elasticsearch/reference/7.7/search-request-body.html#request-body-search-docvalue-fields), e.g.

```auto
PUT my_index
{
  "mappings": {
    "properties": {
      "latency": {
        "type": "long"
      },
      "latency_unit_constant": {
        "type": "constant_keyword",
        "value": "ms"
      }
    }
  }
}

PUT /my_index/_doc/1
{
  "latency" : 1234567
}

PUT /my_index/_doc/2
{
  "latency" : 124
}

GET /my_index/_search
{
  "query": {
    "range": {
      "latency": {
        "gte": 1000
      }
    }
  },
  "docvalue_fields": [
    "latency_unit_constant"
  ]
}

```

---

<div class="post-metadata">

**Author:** ![arun\_001](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@arun\_001](https://discuss.elastic.co/u/arun_001)\
**Post date:** [April 22, 2020, 8:25am UTC](https://discuss.elastic.co/t/how-to-use-a-fields-meta-data-in-search-results/228996/3 "2020-04-22T08:25:54Z")

</div>

Thank you for the reply/answer @cbuescher

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [May 20, 2020, 8:26am UTC](https://discuss.elastic.co/t/how-to-use-a-fields-meta-data-in-search-results/228996/4 "2020-05-20T08:26:08Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
