How to store data in elasticsearch _source but not index it? How to store data in elasticsearch _source but not index it? elasticsearch elasticsearch

How to store data in elasticsearch _source but not index it?


By default the _source of the document is stored regardless of the fields that you choose to index. The _source is used to return the document in the search results, whereas the fields that are indexed are used for searching.

You can't set index: no on an object to prevent all fields in an object being indexed, but you can do what you want with Dynamic Templates using path_match property to apply the index: no setting to every field within an object. Here is a simple example.

Create an index with your mapping that includes the dynamic templates for the author object and the nested categories object:

POST /shop{    "mappings": {        "book": {            "dynamic_templates": [                {                    "author_object_template": {                        "path_match": "author.*",                        "mapping": {                            "index": "no"                        }                    }                },                {                    "categories_object_template": {                        "path_match": "categories.*",                        "mapping": {                            "index": "no"                        }                    }                }            ],            "properties": {                "categories": {                    "type": "nested"                }            }        }    }}

Index a document:

POST /shop/book/1{    "title": "book one",    "author": {        "first_name": "jon",        "last_name": "doe"    },    "categories": [        {            "cat_id": 1,            "cat_name": "category one"        },        {            "cat_id": 2,            "cat_name": "category two"        }    ]}

If you searched on the title field with the search term book the document would be returned. If you search on the author.first_name or author.last_name, there won't be a match because this fields were not indexed:

POST /shop/book/_search{    "query": {        "match": {            "author.first_name": "jon"        }    }}

The same would be the case for a nested query on the category fields:

POST /shop/book/_search{    "query": {        "nested": {            "path": "categories",            "query": {                "match": {                    "categories.cat_name": "category"                }            }        }    }}

Also you can use the Luke tool to expect the Lucene index and see what fields have been indexed.