Derived field type

Introduced 2.15

Derived fields allow you to create new fields dynamically by executing scripts on existing fields. The existing fields can be either retrieved from the _source field, which contains the original document, or from a field’s doc values. Once you define a derived field either in an index mapping or within a search request, you can use the field in a query in the same way you would use a regular field.

When to use derived fields

Derived fields offer flexibility in field manipulation and prioritize storage efficiency. However, because they are computed at query time, they can reduce query performance. Derived fields are particularly useful in scenarios requiring real-time data transformation, such as:

  • Log analysis: Extracting timestamps and log levels from log messages.
  • Performance metrics: Calculating response times from start and end timestamps.
  • Security analytics: Real-time IP geolocation and user-agent parsing for threat detection.
  • Experimental use cases: Testing new data transformations, creating temporary fields for A/B testing, or generating one-time reports without altering mappings or reindexing data.

Despite the potential performance impact of query-time computations, the flexibility and storage efficiency of derived fields make them a valuable tool for these applications.

Current limitations

Currently, derived fields have the following limitations:

  • Scoring and sorting: Not yet supported.
  • Aggregations: Starting with OpenSearch 2.17, derived fields support most aggregation types. The following aggregations are not supported: geographic (geodistance, geohash grid, geohex grid, geotile grid, geobounds, geocentroid), significant terms, significant text, and scripted metric.
  • Dashboard support: These fields are not displayed in the list of available fields in OpenSearch Dashboards. However, you can still use them for filtering if you know the derived field name.
  • Chained derived fields: One derived field cannot be used to define another derived field.
  • Join field type: Derived fields are not supported for the join field type.

We are planning to address these limitations in future versions.

Prerequisites

Before using a derived field, be sure to satisfy the following prerequisites:

  • Enable _source or doc_values: Ensure that either the _source field or doc values is enabled for the fields used in your script.
  • Enable expensive queries: Ensure that search.allow_expensive_queries is set to true.
  • Feature control: Derived fields are enabled by default. You can enable or disable derived fields by using the following settings:
    • Index level: Update the index.query.derived_field.enabled setting.
    • Cluster level: Update the search.derived_field.enabled setting. Both settings are dynamic, so they can be changed without reindexing or node restarts.
  • Performance considerations: Before using derived fields, evaluate the performance implications to ensure that derived fields meet your scale requirements.

Defining derived fields

You can define derived fields in index mappings or directly within a search request.

Example setup

To try the examples on this page, first create the following logs index:

  1. PUT logs
  2. {
  3. "mappings": {
  4. "properties": {
  5. "request": {
  6. "type": "text",
  7. "fields": {
  8. "keyword": {
  9. "type": "keyword"
  10. }
  11. }
  12. },
  13. "clientip": {
  14. "type": "keyword"
  15. }
  16. }
  17. }
  18. }

copy

Add sample documents to the index:

  1. POST _bulk
  2. { "index" : { "_index" : "logs", "_id" : "1" } }
  3. { "request": "894030400 GET /english/images/france98_venues.gif HTTP/1.0 200 778", "clientip": "61.177.2.0" }
  4. { "index" : { "_index" : "logs", "_id" : "2" } }
  5. { "request": "894140400 GET /french/playing/mascot/mascot.html HTTP/1.1 200 5474", "clientip": "185.92.2.0" }
  6. { "index" : { "_index" : "logs", "_id" : "3" } }
  7. { "request": "894250400 POST /english/venues/images/venue_header.gif HTTP/1.0 200 711", "clientip": "61.177.2.0" }
  8. { "index" : { "_index" : "logs", "_id" : "4" } }
  9. { "request": "894360400 POST /images/home_fr_button.gif HTTP/1.1 200 2140", "clientip": "129.178.2.0" }
  10. { "index" : { "_index" : "logs", "_id" : "5" } }
  11. { "request": "894470400 DELETE /images/102384s.gif HTTP/1.0 200 785", "clientip": "227.177.2.0" }

copy

Defining derived fields in index mappings

To derive the timestamp, method, and size fields from the request field indexed in the logs index, configure the following mappings:

  1. PUT /logs/_mapping
  2. {
  3. "derived": {
  4. "timestamp": {
  5. "type": "date",
  6. "format": "MM/dd/yyyy",
  7. "script": {
  8. "source": """
  9. emit(Long.parseLong(doc["request.keyword"].value.splitOnToken(" ")[0]))
  10. """
  11. }
  12. },
  13. "method": {
  14. "type": "keyword",
  15. "script": {
  16. "source": """
  17. emit(doc["request.keyword"].value.splitOnToken(" ")[1])
  18. """
  19. }
  20. },
  21. "size": {
  22. "type": "long",
  23. "script": {
  24. "source": """
  25. emit(Long.parseLong(doc["request.keyword"].value.splitOnToken(" ")[5]))
  26. """
  27. }
  28. }
  29. }
  30. }

copy

Note that the timestamp field has an additional format parameter that specifies the format in which to display date fields. If you don’t include a format parameter, then the format defaults to strict_date_time_no_millis. For more information about supported date formats, see Parameters.

Parameters

The following table lists the parameters accepted by derived field types. All parameters are dynamic and can be modified without reindexing documents.

ParameterRequired/OptionalDescription
typeRequiredThe type of the derived field. Supported types are boolean, date, geo_point, ip, keyword, text, long, double, float, and object.
scriptRequiredThe script associated with the derived field. Any value emitted from the script must be emitted using emit(). The type of the emitted value must match the type of the derived field. Scripts have access to both the doc_values and _source fields if those are enabled. The doc value of a field can be accessed using doc[‘field_name’].value, and the source can be accessed using params._source[“field_name”].
formatOptionalThe format used for parsing dates. Only applicable to date fields. Valid values are strict_date_time_no_millis, strict_date_optional_time, and epoch_millis. For more information, see Formats.
ignore_malformedOptionalA Boolean value that specifies whether to ignore malformed values when running a query on a derived field. Default value is false (throw an exception when encountering malformed values).
prefilter_fieldOptionalAn indexed text field provided to boost the performance of derived fields. Specifies an existing indexed field on which to filter prior to filtering on the derived field. For more information, see Prefilter field.

Emitting values in scripts

The emit() function is available only within the derived field script context. It is used to emit one or multiple (for a multi-valued field) script values for a document on which the script runs.

The following table lists the emit() function formats for the supported field types.

TypeEmit formatMulti-valued fields supported
booleanemit(boolean)No
doubleemit(double)Yes
dateemit(long timeInMilis)Yes
floatemit(float)Yes
geo_pointemit(double lat, double lon)Yes
ipemit(String ip)Yes
keywordemit(String)Yes
longemit(long)Yes
objectemit(String json) (valid JSON)Yes
textemit(String)Yes

By default, a type mismatch between a derived field and its emitted value will result in the search request failing with an error. If ignore_malformed is set to true, then the failing document is skipped and the search request succeeds.

The size limit of the emitted values is 1 MB per document.

Searching derived fields defined in index mappings

To search derived fields, use the same syntax as when searching regular fields. For example, the following request searches for documents with derived timestamp field in the specified range:

  1. POST /logs/_search
  2. {
  3. "query": {
  4. "range": {
  5. "timestamp": {
  6. "gte": "1970-01-11T08:20:30.400Z",
  7. "lte": "1970-01-11T08:26:00.400Z"
  8. }
  9. }
  10. },
  11. "fields": ["timestamp"]
  12. }

copy

The response contains the matching documents:

Response

  1. {
  2. "took": 315,
  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": 4,
  13. "relation": "eq"
  14. },
  15. "max_score": 1,
  16. "hits": [
  17. {
  18. "_index": "logs",
  19. "_id": "1",
  20. "_score": 1,
  21. "_source": {
  22. "request": "894030400 GET /english/images/france98_venues.gif HTTP/1.0 200 778",
  23. "clientip": "61.177.2.0"
  24. },
  25. "fields": {
  26. "timestamp": [
  27. "1970-01-11T08:20:30.400Z"
  28. ]
  29. }
  30. },
  31. {
  32. "_index": "logs",
  33. "_id": "2",
  34. "_score": 1,
  35. "_source": {
  36. "request": "894140400 GET /french/playing/mascot/mascot.html HTTP/1.1 200 5474",
  37. "clientip": "185.92.2.0"
  38. },
  39. "fields": {
  40. "timestamp": [
  41. "1970-01-11T08:22:20.400Z"
  42. ]
  43. }
  44. },
  45. {
  46. "_index": "logs",
  47. "_id": "3",
  48. "_score": 1,
  49. "_source": {
  50. "request": "894250400 POST /english/venues/images/venue_header.gif HTTP/1.0 200 711",
  51. "clientip": "61.177.2.0"
  52. },
  53. "fields": {
  54. "timestamp": [
  55. "1970-01-11T08:24:10.400Z"
  56. ]
  57. }
  58. },
  59. {
  60. "_index": "logs",
  61. "_id": "4",
  62. "_score": 1,
  63. "_source": {
  64. "request": "894360400 POST /images/home_fr_button.gif HTTP/1.1 200 2140",
  65. "clientip": "129.178.2.0"
  66. },
  67. "fields": {
  68. "timestamp": [
  69. "1970-01-11T08:26:00.400Z"
  70. ]
  71. }
  72. }
  73. ]
  74. }
  75. }

Defining and searching derived fields in a search request

You can also define derived fields directly in a search request and query them along with regular indexed fields. For example, the following request creates the url and status derived fields and searches those fields along with the regular request and clientip fields:

  1. POST /logs/_search
  2. {
  3. "derived": {
  4. "url": {
  5. "type": "text",
  6. "script": {
  7. "source": """
  8. emit(doc["request"].value.splitOnToken(" ")[2])
  9. """
  10. }
  11. },
  12. "status": {
  13. "type": "keyword",
  14. "script": {
  15. "source": """
  16. emit(doc["request"].value.splitOnToken(" ")[4])
  17. """
  18. }
  19. }
  20. },
  21. "query": {
  22. "bool": {
  23. "must": [
  24. {
  25. "term": {
  26. "clientip": "61.177.2.0"
  27. }
  28. },
  29. {
  30. "match": {
  31. "url": "images"
  32. }
  33. },
  34. {
  35. "term": {
  36. "status": "200"
  37. }
  38. }
  39. ]
  40. }
  41. },
  42. "fields": ["request", "clientip", "url", "status"]
  43. }

copy

The response contains the matching documents:

Response

  1. {
  2. "took": 6,
  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": 2,
  13. "relation": "eq"
  14. },
  15. "max_score": 2.8754687,
  16. "hits": [
  17. {
  18. "_index": "logs",
  19. "_id": "1",
  20. "_score": 2.8754687,
  21. "_source": {
  22. "request": "894030400 GET /english/images/france98_venues.gif HTTP/1.0 200 778",
  23. "clientip": "61.177.2.0"
  24. },
  25. "fields": {
  26. "request": [
  27. "894030400 GET /english/images/france98_venues.gif HTTP/1.0 200 778"
  28. ],
  29. "clientip": [
  30. "61.177.2.0"
  31. ],
  32. "url": [
  33. "/english/images/france98_venues.gif"
  34. ],
  35. "status": [
  36. "200"
  37. ]
  38. }
  39. },
  40. {
  41. "_index": "logs",
  42. "_id": "3",
  43. "_score": 2.8754687,
  44. "_source": {
  45. "request": "894250400 POST /english/venues/images/venue_header.gif HTTP/1.0 200 711",
  46. "clientip": "61.177.2.0"
  47. },
  48. "fields": {
  49. "request": [
  50. "894250400 POST /english/venues/images/venue_header.gif HTTP/1.0 200 711"
  51. ],
  52. "clientip": [
  53. "61.177.2.0"
  54. ],
  55. "url": [
  56. "/english/venues/images/venue_header.gif"
  57. ],
  58. "status": [
  59. "200"
  60. ]
  61. }
  62. }
  63. ]
  64. }
  65. }

Derived fields use the default analyzer specified in the index analysis settings during search. You can override the default analyzer or specify a search analyzer within a search request in the same way as with regular fields. For more information, see Analyzers.

When both an index mapping and a search definition are present for a field, the search definition takes precedence.

Retrieving fields

You can retrieve derived fields using the fields parameter in the search request in the same way as with regular fields, as shown in the preceding examples. You can also use wildcards to retrieve all derived fields that match a given pattern.

Highlighting

Derived fields of type text support highlighting using the unified highlighter. For example, the following request specifies to highlight the derived url field:

  1. POST /logs/_search
  2. {
  3. "derived": {
  4. "url": {
  5. "type": "text",
  6. "script": {
  7. "source": """
  8. emit(doc["request"].value.splitOnToken(" " )[2])
  9. """
  10. }
  11. }
  12. },
  13. "query": {
  14. "bool": {
  15. "must": [
  16. {
  17. "term": {
  18. "clientip": "61.177.2.0"
  19. }
  20. },
  21. {
  22. "match": {
  23. "url": "images"
  24. }
  25. }
  26. ]
  27. }
  28. },
  29. "fields": ["request", "clientip", "url"],
  30. "highlight": {
  31. "fields": {
  32. "url": {}
  33. }
  34. }
  35. }

copy

The response specifies highlighting in the url field:

Response

  1. {
  2. "took": 45,
  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": 2,
  13. "relation": "eq"
  14. },
  15. "max_score": 1.8754687,
  16. "hits": [
  17. {
  18. "_index": "logs",
  19. "_id": "1",
  20. "_score": 1.8754687,
  21. "_source": {
  22. "request": "894030400 GET /english/images/france98_venues.gif HTTP/1.0 200 778",
  23. "clientip": "61.177.2.0"
  24. },
  25. "fields": {
  26. "request": [
  27. "894030400 GET /english/images/france98_venues.gif HTTP/1.0 200 778"
  28. ],
  29. "clientip": [
  30. "61.177.2.0"
  31. ],
  32. "url": [
  33. "/english/images/france98_venues.gif"
  34. ]
  35. },
  36. "highlight": {
  37. "url": [
  38. "/english/<em>images</em>/france98_venues.gif"
  39. ]
  40. }
  41. },
  42. {
  43. "_index": "logs",
  44. "_id": "3",
  45. "_score": 1.8754687,
  46. "_source": {
  47. "request": "894250400 POST /english/venues/images/venue_header.gif HTTP/1.0 200 711",
  48. "clientip": "61.177.2.0"
  49. },
  50. "fields": {
  51. "request": [
  52. "894250400 POST /english/venues/images/venue_header.gif HTTP/1.0 200 711"
  53. ],
  54. "clientip": [
  55. "61.177.2.0"
  56. ],
  57. "url": [
  58. "/english/venues/images/venue_header.gif"
  59. ]
  60. },
  61. "highlight": {
  62. "url": [
  63. "/english/venues/<em>images</em>/venue_header.gif"
  64. ]
  65. }
  66. }
  67. ]
  68. }
  69. }

Aggregations

Starting with OpenSearch 2.17, derived fields support most aggregation types.

Geographic, significant terms, significant text, and scripted metric aggregations are not supported.

For example, the following request creates a simple terms aggregation on the method derived field:

  1. POST /logs/_search
  2. {
  3. "size": 0,
  4. "aggs": {
  5. "methods": {
  6. "terms": {
  7. "field": "method"
  8. }
  9. }
  10. }
  11. }

copy

The response contains the following buckets:

Response

  1. {
  2. "took" : 12,
  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" : 5,
  13. "relation" : "eq"
  14. },
  15. "max_score" : null,
  16. "hits" : [ ]
  17. },
  18. "aggregations" : {
  19. "methods" : {
  20. "doc_count_error_upper_bound" : 0,
  21. "sum_other_doc_count" : 0,
  22. "buckets" : [
  23. {
  24. "key" : "GET",
  25. "doc_count" : 2
  26. },
  27. {
  28. "key" : "POST",
  29. "doc_count" : 2
  30. },
  31. {
  32. "key" : "DELETE",
  33. "doc_count" : 1
  34. }
  35. ]
  36. }
  37. }
  38. }

Performance

Derived fields are not indexed but are computed dynamically by retrieving values from the _source field or doc values. Thus, they run more slowly. To improve performance, try the following:

  • Prune the search space by adding query filters on indexed fields in conjunction with derived fields.
  • Use doc values instead of _source in the script for faster access, whenever applicable.
  • Consider using a prefilter_field to automatically prune the search space without explicit filters in the search request.

Prefilter field

Specifying a prefilter field helps to prune the search space without adding explicit filters in the search request. The prefilter field specifies an existing indexed field (prefilter_field) on which to filter automatically when constructing the query. The prefilter_field must be a text field (either text or match_only_text).

For example, you can add a prefilter_field to the method derived field. Update the index mapping, specifying to prefilter on the request field:

  1. PUT /logs/_mapping
  2. {
  3. "derived": {
  4. "method": {
  5. "type": "keyword",
  6. "script": {
  7. "source": """
  8. emit(doc["request.keyword"].value.splitOnToken(" ")[1])
  9. """
  10. },
  11. "prefilter_field": "request"
  12. }
  13. }
  14. }

copy

Now search using a query on the method derived field:

  1. POST /logs/_search
  2. {
  3. "profile": true,
  4. "query": {
  5. "term": {
  6. "method": {
  7. "value": "GET"
  8. }
  9. }
  10. },
  11. "fields": ["method"]
  12. }

copy

OpenSearch automatically adds a filter on the request field to your query:

  1. "#request:GET #DerivedFieldQuery (Query: [ method:GET])"

You can use the profile option to analyze derived field performance, as shown in the preceding example.

Derived object fields

A script can emit a valid JSON object so that you can query subfields without indexing them, in the same way as with regular fields. This is useful for large JSON objects that require occasional searches on some subfields. In this case, indexing the subfields is expensive, while defining derived fields for each subfield also adds a lot of resource overhead. If you don’t explicitly provide the subfield type, then the subfield type is inferred.

For example, the following request defines a derived_request_object derived field as an object type:

  1. PUT logs_object
  2. {
  3. "mappings": {
  4. "properties": {
  5. "request_object": { "type": "text" }
  6. },
  7. "derived": {
  8. "derived_request_object": {
  9. "type": "object",
  10. "script": {
  11. "source": "emit(params._source[\"request_object\"])"
  12. }
  13. }
  14. }
  15. }
  16. }

copy

Consider the following documents, in which the request_object is a string representation of a JSON object:

  1. POST _bulk
  2. { "index" : { "_index" : "logs_object", "_id" : "1" } }
  3. { "request_object": "{\"@timestamp\": 894030400, \"clientip\":\"61.177.2.0\", \"request\": \"GET /english/venues/images/venue_header.gif HTTP/1.0\", \"status\": 200, \"size\": 711}" }
  4. { "index" : { "_index" : "logs_object", "_id" : "2" } }
  5. { "request_object": "{\"@timestamp\": 894140400, \"clientip\":\"129.178.2.0\", \"request\": \"GET /images/home_fr_button.gif HTTP/1.1\", \"status\": 200, \"size\": 2140}" }
  6. { "index" : { "_index" : "logs_object", "_id" : "3" } }
  7. { "request_object": "{\"@timestamp\": 894240400, \"clientip\":\"227.177.2.0\", \"request\": \"GET /images/102384s.gif HTTP/1.0\", \"status\": 400, \"size\": 785}" }
  8. { "index" : { "_index" : "logs_object", "_id" : "4" } }
  9. { "request_object": "{\"@timestamp\": 894340400, \"clientip\":\"61.177.2.0\", \"request\": \"GET /english/images/venue_bu_city_on.gif HTTP/1.0\", \"status\": 400, \"size\": 1397}\n" }
  10. { "index" : { "_index" : "logs_object", "_id" : "5" } }
  11. { "request_object": "{\"@timestamp\": 894440400, \"clientip\":\"132.176.2.0\", \"request\": \"GET /french/news/11354.htm HTTP/1.0\", \"status\": 200, \"size\": 3460, \"is_active\": true}" }

copy

The following query searches the @timestamp subfield of the derived_request_object:

  1. POST /logs_object/_search
  2. {
  3. "query": {
  4. "range": {
  5. "derived_request_object.@timestamp": {
  6. "gte": "894030400",
  7. "lte": "894140400"
  8. }
  9. }
  10. },
  11. "fields": ["derived_request_object.@timestamp"]
  12. }

copy

The response contains the matching documents:

Response

  1. {
  2. "took": 26,
  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": 2,
  13. "relation": "eq"
  14. },
  15. "max_score": 1,
  16. "hits": [
  17. {
  18. "_index": "logs_object",
  19. "_id": "1",
  20. "_score": 1,
  21. "_source": {
  22. "request_object": """{"@timestamp": 894030400, "clientip":"61.177.2.0", "request": "GET /english/venues/images/venue_header.gif HTTP/1.0", "status": 200, "size": 711}"""
  23. },
  24. "fields": {
  25. "derived_request_object.@timestamp": [
  26. 894030400
  27. ]
  28. }
  29. },
  30. {
  31. "_index": "logs_object",
  32. "_id": "2",
  33. "_score": 1,
  34. "_source": {
  35. "request_object": """{"@timestamp": 894140400, "clientip":"129.178.2.0", "request": "GET /images/home_fr_button.gif HTTP/1.1", "status": 200, "size": 2140}"""
  36. },
  37. "fields": {
  38. "derived_request_object.@timestamp": [
  39. 894140400
  40. ]
  41. }
  42. }
  43. ]
  44. }
  45. }

You can also specify to highlight a derived object field:

  1. POST /logs_object/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "must": [
  6. {
  7. "term": {
  8. "derived_request_object.clientip": "61.177.2.0"
  9. }
  10. },
  11. {
  12. "match": {
  13. "derived_request_object.request": "images"
  14. }
  15. }
  16. ]
  17. }
  18. },
  19. "fields": ["derived_request_object.*"],
  20. "highlight": {
  21. "fields": {
  22. "derived_request_object.request": {}
  23. }
  24. }
  25. }

copy

The response adds highlighting to the derived_request_object.request field:

Response

  1. {
  2. "took": 5,
  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": 2,
  13. "relation": "eq"
  14. },
  15. "max_score": 2,
  16. "hits": [
  17. {
  18. "_index": "logs_object",
  19. "_id": "1",
  20. "_score": 2,
  21. "_source": {
  22. "request_object": """{"@timestamp": 894030400, "clientip":"61.177.2.0", "request": "GET /english/venues/images/venue_header.gif HTTP/1.0", "status": 200, "size": 711}"""
  23. },
  24. "fields": {
  25. "derived_request_object.request": [
  26. "GET /english/venues/images/venue_header.gif HTTP/1.0"
  27. ],
  28. "derived_request_object.clientip": [
  29. "61.177.2.0"
  30. ]
  31. },
  32. "highlight": {
  33. "derived_request_object.request": [
  34. "GET /english/venues/<em>images</em>/venue_header.gif HTTP/1.0"
  35. ]
  36. }
  37. },
  38. {
  39. "_index": "logs_object",
  40. "_id": "4",
  41. "_score": 2,
  42. "_source": {
  43. "request_object": """{"@timestamp": 894340400, "clientip":"61.177.2.0", "request": "GET /english/images/venue_bu_city_on.gif HTTP/1.0", "status": 400, "size": 1397}
  44. """
  45. },
  46. "fields": {
  47. "derived_request_object.request": [
  48. "GET /english/images/venue_bu_city_on.gif HTTP/1.0"
  49. ],
  50. "derived_request_object.clientip": [
  51. "61.177.2.0"
  52. ]
  53. },
  54. "highlight": {
  55. "derived_request_object.request": [
  56. "GET /english/<em>images</em>/venue_bu_city_on.gif HTTP/1.0"
  57. ]
  58. }
  59. }
  60. ]
  61. }
  62. }

Inferred subfield type

Type inference is based on the same logic as Dynamic mapping. Instead of inferring the subfield type from the first document, a random sample of documents is used to infer the type. If the subfield isn’t found in any documents from the random sample, type inference fails and logs a warning. For subfields that seldom occur in documents, consider defining the explicit field type. Using dynamic type inference for such subfields may result in a query returning no results, like for a missing field.

Explicit subfield type

To define the explicit subfield type, provide the type parameter in the properties object. In the following example, the derived_logs_object.is_active field is defined as boolean. Because this field is only present in one of the documents, its type inference might fail, so it’s important to define the explicit type:

  1. POST /logs_object/_search
  2. {
  3. "derived": {
  4. "derived_request_object": {
  5. "type": "object",
  6. "script": {
  7. "source": "emit(params._source[\"request_object\"])"
  8. },
  9. "properties": {
  10. "is_active": "boolean"
  11. }
  12. }
  13. },
  14. "query": {
  15. "term": {
  16. "derived_request_object.is_active": true
  17. }
  18. },
  19. "fields": ["derived_request_object.is_active"]
  20. }

copy

The response contains the matching documents:

Response

  1. {
  2. "took": 13,
  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": "logs_object",
  19. "_id": "5",
  20. "_score": 1,
  21. "_source": {
  22. "request_object": """{"@timestamp": 894440400, "clientip":"132.176.2.0", "request": "GET /french/news/11354.htm HTTP/1.0", "status": 200, "size": 3460, "is_active": true}"""
  23. },
  24. "fields": {
  25. "derived_request_object.is_active": [
  26. true
  27. ]
  28. }
  29. }
  30. ]
  31. }
  32. }