Lupidon
Lupidon

Reputation: 619

Is there a way to enable _source on existing data?

I create an Index without _source field (considerations of memory). I want to enable this field on the existing data , there is a way to do that?

For example:
I will create dummy-index :

PUT /dummy-index?pretty
{
  "mappings": {
    "_doc": {
      "_source": {
        "enabled": false
      }
    }
  }
}

and I will add the next document :

PUT /dummy-index/_doc/1?pretty
{
  "name": "CoderIl"
}

I will get only the hit metadata when I search without the name field
{
   "_index" : "dummy-index",
   "_type" : "_doc",
   "_id" : "1",
   "_score" : 1.0
}

the question if I could change the _soruce to enable and when I search again I'll get the missing data (in this example "name" field) -

{
   "_index" : "dummy-index",
   "_type" : "_doc",
   "_id" : "1",
   "_score" : 1.0
   "_source" : {
        "name" :CoderIl"
   }
}

Upvotes: 0

Views: 2469

Answers (2)

Amit
Amit

Reputation: 32386

As clarified in the chat, the issue is

  1. _source field is disabled.
  2. In search result he wants what was stored in the fields which is returned as part if _source if enabled like below
 _source" : {
        "name" :CoderIl"
   }

Now in order to achieve it, store option must be enabled on the field, please note this can't be changed dynamically and you have to re-index data again with updated mapping.

Example

Index mapping

{
    "mappings": {
        "_source": {
            "enabled": false
        },
        "properties": {
            "name": {
                "type": "text"
            },
            "title" :{
                "type" : "text",
                "store" : true
            }
        }
    }
}

Index sample docs

{
   "name" : "coderIL"
}

{
   "name" : "coderIL",
   "title" : "seconds docs"
}

**Search doc with fields content using store fields

{
    "stored_fields": [
        "title"
    ],
    "query": {
        "match": {
            "name": "coderIL"
        }
    }
}

And search result

 "hits": [
            {
                "_index": "without_source",
                "_type": "_doc",
                "_id": "1",
                "_score": 0.18232156
            },
            {
                "_index": "without_source",
                "_type": "_doc",
                "_id": "2",
                "_score": 0.18232156,
                "fields": {
                    "title": [
                        "seconds docs"
                    ]
                }
            }
        ]

Upvotes: 1

Amit
Amit

Reputation: 32386

store option on field controls that, from the same official doc

By default, field values are indexed to make them searchable, but they are not stored. This means that the field can be queried, but the original field value cannot be retrieved.

Usually this doesn’t matter. The field value is already part of the _source field, which is stored by default. If you only want to retrieve the value of a single field or of a few fields, instead of the whole _source, then this can be achieved with source filtering.

As mentioned on the doc, by default its disabled, and if you want to save space, you can enable it on specific fields and need to re-index the data again

Edit: Index option controls(enabled by default) whether field is indexed or not(this is required for searching on the field) and store option controls whether it's stored or not, this is used if you want to get the non-analyzed value ie what you sent to ES in your index request, which based on field type goes through text analysis and part of index option, refer this SO question for more info.

Upvotes: 1

Related Questions