# Need to add doc to an index with default values

**URL:** <https://discuss.elastic.co/t/need-to-add-doc-to-an-index-with-default-values/300326>\
**Category:** Elasticsearch\
**Created:** [March 22, 2022, 11:12am UTC](https://discuss.elastic.co/t/need-to-add-doc-to-an-index-with-default-values/300326 "2022-03-22T11:12:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anika\_Mehrotra](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anika_mehrotra/32/100682_2.png) [@Anika\_Mehrotra](https://discuss.elastic.co/u/Anika_Mehrotra)\
**Post date:** [March 22, 2022, 11:12am UTC](https://discuss.elastic.co/t/need-to-add-doc-to-an-index-with-default-values/300326/1 "2022-03-22T11:12:33Z")

</div>

Suppose i have an index with 2 fields as  
firstname and lastname both having default values as "test"

Now if I insert a new record in that index  
POST my-index-000001/general  
{  
"firstname": "harry",  
"lastname": "potter"  
}  
and then  
POST my-index-000001/general  
{  
"firstname": "harry1"  
}

If i do GET my-index-000001/\_search  
I should get 2 records with 2nd record having lastname as "test.  
How to achieve this?  
Please help

---

<div class="post-metadata">

**Author:** ![jsanz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jsanz/32/53734_2.png) [@jsanz](https://discuss.elastic.co/u/jsanz)\
**Post date:** [March 23, 2022, 4:42pm UTC](https://discuss.elastic.co/t/need-to-add-doc-to-an-index-with-default-values/300326/2 "2022-03-23T16:42:06Z")

</div>

You are using the `null_value` parameter on your field mapping, right?

Check this code following your example:

```auto
# Create the index
PUT my-index-000001/
{
  "mappings": {
    "dynamic": false,
    "properties": {
      "firstname": {
        "type": "keyword",
        "null_value": "test"
      },
      "secondname": {
        "type": "keyword",
        "null_value": "test"
      }
    }
  }
}

# Add a document
POST my-index-000001/_doc/1
{
"firstname": "harry",
"secondname": "potter"
}

# Add another document without
POST my-index-000001/_doc/2
{
"firstname": "hermione",
"secondname": null
}

POST my-index-000001/_doc/3
{
"firstname": "ron"
}

GET my-index-000001/_search
{
  "fields": [
    "firstname",
    "secondname"
  ], 
  "query": {
    "term": {
      "secondname": "test" 
    }
  },
  "_source": false
}

```

The last search returns only the `hemione` document

```auto
...
    "hits" : [
      {
        "_index" : "my-index-000001",
        "_id" : "2",
        "_score" : 0.6931471,
        "fields" : {
          "secondname" : [
            "test"
          ],
          "firstname" : [
            "hermione"
          ]
        }
      }
    ]
...

```

The catch is the last note in the [`null_value`](https://www.elastic.co/guide/en/elasticsearch/reference/current/null-value.html) documentation

> The `null_value` only influences how data is indexed, it doesn’t modify the `_source` document.

So your `_source` is not mdified, and you need to add the `fields` parameter to your search to get your default indexed value, check the [docs](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-fields.html#search-fields-param).

Also you need the field to be added, that's why the result does not show `ron` document. If you want to avoid to add the null value you can use an [ingest pipeline](https://www.elastic.co/guide/en/elasticsearch/reference/current/ingest.html) with the [set processor](https://www.elastic.co/guide/en/elasticsearch/reference/current/set-processor.html) (and an `if` condition) or a [script processor](https://www.elastic.co/guide/en/elasticsearch/reference/current/script-processor.html).

---

<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:** [April 20, 2022, 4:42pm UTC](https://discuss.elastic.co/t/need-to-add-doc-to-an-index-with-default-values/300326/3 "2022-04-20T16:42:26Z")

</div>

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