Mappings and field types

You can define how documents and their fields are stored and indexed by creating a mapping. The mapping specifies the list of fields for a document. Every field in the document has a field type, which corresponds to the type of data the field contains. For example, you may want to specify that the year field should be of type date. To learn more, see Supported field types.

If you’re just starting to build out your cluster and data, you may not know exactly how your data should be stored. In those cases, you can use dynamic mappings, which tell OpenSearch to dynamically add data and its fields. However, if you know exactly what types your data falls under and want to enforce that standard, then you can use explicit mappings.

For example, if you want to indicate that year should be of type text instead of an integer, and age should be an integer, you can do so with explicit mappings. By using dynamic mapping, OpenSearch might interpret both year and age as integers.

This section provides an example for how to create an index mapping and how to add a document to it that will get ip_range validated.


Dynamic mapping

When you index a document, OpenSearch adds fields automatically with dynamic mapping. You can also explicitly add fields to an index mapping.

Dynamic mapping types

TypeDescription
nullA null field can’t be indexed or searched. When a field is set to null, OpenSearch behaves as if that field has no values.
booleanOpenSearch accepts true and false as boolean values. An empty string is equal to false.
floatA single-precision 32-bit floating point number.
doubleA double-precision 64-bit floating point number.
integerA signed 32-bit number.
objectObjects are standard JSON objects, which can have fields and mappings of their own. For example, a movies object can have additional properties such as title, year, and director.
arrayArrays in OpenSearch can only store values of one type, such as an array of just integers or strings. Empty arrays are treated as though they are fields with no values.
textA string sequence of characters that represent full-text values.
keywordA string sequence of structured characters, such as an email address or ZIP code.
date detection stringEnabled by default, if new string fields match a date’s format, then the string is processed as a date field. For example, date: “2012/03/11” is processed as a date.
numeric detection stringIf disabled, OpenSearch may automatically process numeric values as strings when they should be processed as numbers. When enabled, OpenSearch can process strings into long, integer, short, byte, double, float, half_float, scaled_float, and unsigned_long. Default is disabled.

Explicit mapping

If you know exactly what your field data types need to be, you can specify them in your request body when creating your index.

  1. PUT sample-index1
  2. {
  3. "mappings": {
  4. "properties": {
  5. "year": { "type" : "text" },
  6. "age": { "type" : "integer" },
  7. "director":{ "type" : "text" }
  8. }
  9. }
  10. }

Response

  1. {
  2. "acknowledged": true,
  3. "shards_acknowledged": true,
  4. "index": "sample-index1"
  5. }

To add mappings to an existing index or data stream, you can send a request to the _mapping endpoint using the PUT or POST HTTP method:

  1. POST sample-index1/_mapping
  2. {
  3. "properties": {
  4. "year": { "type" : "text" },
  5. "age": { "type" : "integer" },
  6. "director":{ "type" : "text" }
  7. }
  8. }

You cannot change the mapping of an existing field, you can only modify the field’s mapping parameters.


Mapping example usage

The following example shows how to create a mapping to specify that OpenSearch should ignore any documents with malformed IP addresses that do not conform to the ip data type. You accomplish this by setting the ignore_malformed parameter to true.

Create an index with an ip mapping

To create an index, use a PUT request:

  1. PUT /test-index
  2. {
  3. "mappings" : {
  4. "properties" : {
  5. "ip_address" : {
  6. "type" : "ip",
  7. "ignore_malformed": true
  8. }
  9. }
  10. }
  11. }

You can add a document that has a malformed IP address to your index:

  1. PUT /test-index/_doc/1
  2. {
  3. "ip_address" : "malformed ip address"
  4. }

This indexed IP address does not throw an error because ignore_malformed is set to true.

You can query the index using the following request:

  1. GET /test-index/_search

The response shows that the ip_address field is ignored in the indexed document:

  1. {
  2. "took": 14,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 1,
  6. "successful": 1,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": {
  12. "value": 1,
  13. "relation": "eq"
  14. },
  15. "max_score": 1,
  16. "hits": [
  17. {
  18. "_index": "test-index",
  19. "_id": "1",
  20. "_score": 1,
  21. "_ignored": [
  22. "ip_address"
  23. ],
  24. "_source": {
  25. "ip_address": "malformed ip address"
  26. }
  27. }
  28. ]
  29. }
  30. }

Get a mapping

To get all mappings for one or more indexes, use the following request:

  1. GET <index>/_mapping

In the above request, <index> may be an index name or a comma-separated list of index names.

To get all mappings for all indexes, use the following request:

  1. GET _mapping

To get a mapping for a specific field, provide the index name and the field name:

  1. GET _mapping/field/<fields>
  2. GET /<index>/_mapping/field/<fields>

Both <index> and <fields> can be specified as one value or a comma-separated list.

For example, the following request retrieves the mapping for the year and age fields in sample-index1:

  1. GET sample-index1/_mapping/field/year,age

The response contains the specified fields:

  1. {
  2. "sample-index1" : {
  3. "mappings" : {
  4. "year" : {
  5. "full_name" : "year",
  6. "mapping" : {
  7. "year" : {
  8. "type" : "text"
  9. }
  10. }
  11. },
  12. "age" : {
  13. "full_name" : "age",
  14. "mapping" : {
  15. "age" : {
  16. "type" : "integer"
  17. }
  18. }
  19. }
  20. }
  21. }
  22. }