Flat object field type

In OpenSearch, you don’t have to specify a mapping before indexing documents. If you don’t specify a mapping, OpenSearch uses dynamic mapping to map every field and its subfields in the document automatically. When you ingest documents such as logs, you may not know every field’s subfield name and type in advance. In this case, dynamically mapping all new subfields can quickly lead to a “mapping explosion,” where the growing number of fields may degrade the performance of your cluster.

The flat object field type solves this problem by treating the entire JSON object as a string. Subfields within the JSON object are accessible using standard dot path notation, but they are not indexed for fast lookup.

The maximum field value length in the dot notation is 224 − 1.

The flat object field type provides the following benefits:

  • Efficient reads: Fetching performance is similar to that of a keyword field.
  • Memory efficiency: Storing the entire complex JSON object in one field without indexing all of its subfields reduces the number of fields in an index.
  • Space efficiency: OpenSearch does not create an inverted index for subfields in flat objects, thereby saving space.
  • Compatibility for migration: You can migrate your data from systems that support similar flat types to OpenSearch.

Mapping a field as a flat object applies when a field and its subfields are mostly read and not used as search criteria because the subfields are not indexed. Flat objects are useful for objects with a large number of fields or when you don’t know the keys in advance.

Flat objects support exact match queries with and without dot path notation. For a complete list of supported query types, see Supported queries.

Searching for a specific value of a nested field in a document may be inefficient because it may require a full scan of the index, which can be an expensive operation.

Flat objects do not support:

  • Type-specific parsing.
  • Numerical operations, such as numerical comparison or numerical sorting.
  • Text analysis.
  • Highlighting.
  • Aggregations of subfields using dot notation.
  • Filtering by subfields.

Supported queries

The flat object field type supports the following queries:

Limitations

The following limitations apply to flat objects in OpenSearch 2.7:

  • Flat objects do not support open parameters.
  • Painless scripting and wildcard queries are not supported for retrieving values of subfields.

This functionality is planned for a future release.

Using flat object

The following example illustrates mapping a field as a flat object, indexing documents with flat object fields, and searching for leaf values of the flat object in those documents.

First, create a mapping for your index, where issue is of type flat_object:

  1. PUT /test-index/
  2. {
  3. "mappings": {
  4. "properties": {
  5. "issue": {
  6. "type": "flat_object"
  7. }
  8. }
  9. }
  10. }

copy

Next, index two documents with flat object fields:

  1. PUT /test-index/_doc/1
  2. {
  3. "issue": {
  4. "number": "123456",
  5. "labels": {
  6. "version": "2.1",
  7. "backport": [
  8. "2.0",
  9. "1.3"
  10. ],
  11. "category": {
  12. "type": "API",
  13. "level": "enhancement"
  14. }
  15. }
  16. }
  17. }

copy

  1. PUT /test-index/_doc/2
  2. {
  3. "issue": {
  4. "number": "123457",
  5. "labels": {
  6. "version": "2.2",
  7. "category": {
  8. "type": "API",
  9. "level": "bug"
  10. }
  11. }
  12. }
  13. }

copy

To search for a leaf value of the flat object, use either a GET or a POST request. Even if you don’t know the field names, you can search for a leaf value in the entire flat object. For example, the following request searches for all issues labeled as bugs:

  1. GET /test-index/_search
  2. {
  3. "query": {
  4. "match": {"issue": "bug"}
  5. }
  6. }

Alternatively, if you know the subfield name in which to search, provide the field’s path in dot notation:

  1. GET /test-index/_search
  2. {
  3. "query": {
  4. "match": {"issue.labels.category.level": "bug"}
  5. }
  6. }

copy

In both cases, the response is the same and contains document 2:

  1. {
  2. "took": 1,
  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.0303539,
  16. "hits": [
  17. {
  18. "_index": "test-index",
  19. "_id": "2",
  20. "_score": 1.0303539,
  21. "_source": {
  22. "issue": {
  23. "number": "123457",
  24. "labels": {
  25. "version": "2.2",
  26. "category": {
  27. "type": "API",
  28. "level": "bug"
  29. }
  30. }
  31. }
  32. }
  33. }
  34. ]
  35. }
  36. }

Using a prefix query, you can search for all issues for the versions that start with 2.:

  1. GET /test-index/_search
  2. {
  3. "query": {
  4. "prefix": {"issue.labels.version": "2."}
  5. }
  6. }

With a range query, you can search for all issues for versions 2.0–2.1:

  1. GET /test-index/_search
  2. {
  3. "query": {
  4. "range": {
  5. "issue": {
  6. "gte": "2.0",
  7. "lte": "2.1"
  8. }
  9. }
  10. }
  11. }

Defining a subfield as a flat object

You can define a subfield of a JSON object as a flat object. For example, use the following query to define the issue.labels as flat_object:

  1. PUT /test-index/
  2. {
  3. "mappings": {
  4. "properties": {
  5. "issue": {
  6. "properties": {
  7. "number": {
  8. "type": "double"
  9. },
  10. "labels": {
  11. "type": "flat_object"
  12. }
  13. }
  14. }
  15. }
  16. }
  17. }

copy

Because issue.number is not part of the flat object, you can use it to aggregate and sort documents.