Tom
Tom

Reputation: 3336

How to index a field in elasticsearch but not store it in _source?

I have a collection of documents with a text field "features", and would like to make this field indexed (so documents can be searched through the field) but not stored (in order to save disk space).

How to index a field in elasticsearch like this "features" field but not store it in _source?

Upvotes: 0

Views: 1470

Answers (1)

Bhavya
Bhavya

Reputation: 16172

The following index mapping, will index a field value but not store it

Index Mapping:

{
  "mappings": {
    "properties": {
      "features": {
        "type": "text",
        "index": "true",
        "store": "false"
      }
    }
  }
}

Index Data:

{
  "features": "capacity"
}

Search Query:

{
  "stored_fields": [
    "features"
  ]
}

Search Result:

"hits": [
      {
        "_index": "67155998",
        "_type": "_doc",
        "_id": "1",
        "_score": 1.0
      }
    ]

UPDATE 1:

When a field is indexed, then you can perform queries on it. If a field is stored the contents of the field can be shown when the document matches.

But if you want that the content of the field should also not to be displayed in the _source, then you need to disable the _source field.

You need to modify your index mapping as

{
  "mappings": {
    "_source": {
      "enabled": false
    },
    "properties": {
      "features": {
        "type": "text",
        "index": "true",
        "store": "false"
      }
    }
  }
}

Search Query:

{
  "query":{
    "match":{
      "features":"capacity"
    }
  }
}

Search Result:

 "hits": [
      {
        "_index": "67155998",
        "_type": "_doc",
        "_id": "1",
        "_score": 0.2876821
      }
    ]

Upvotes: 2

Related Questions