Trim processor

The trim processor is used to remove leading and trailing white space characters from a specified field.

The following is the syntax for the trim processor:

  1. {
  2. "trim": {
  3. "field": "field_to_trim",
  4. "target_field": "trimmed_field"
  5. }
  6. }

copy

Configuration parameters

The following table lists the required and optional parameters for the trim processor.

Parameter | Required/Optional | Description | |———–|———–|———–| field | Required | The field containing the text to be trimmed. target_field | Required | The field in which the trimmed text is stored. If not specified, then the field is updated in-place. ignore_missing | Optional | Specifies whether the processor should ignore documents that do not contain the specified field. If set to true, then the processor ignores missing values in the field and leaves the target_field unchanged. Default is false. description | Optional | A brief description of the processor. if | Optional | A condition for running the processor. ignore_failure | Optional | Specifies whether the processor continues execution even if it encounters an error. If set to true, then failures are ignored. Default is false. on_failure | Optional | A list of processors to run if the processor fails. tag | Optional | An identifier tag for the processor. Useful for debugging in order to distinguish between processors of the same type.

Using the processor

Follow these steps to use the processor in a pipeline.

Step 1: Create a pipeline

The following query creates a pipeline named trim_pipeline that uses the trim processor to remove leading and trailing white space from the raw_text field and store the trimmed text in the trimmed_text field:

  1. PUT _ingest/pipeline/trim_pipeline
  2. {
  3. "description": "Trim leading and trailing white space",
  4. "processors": [
  5. {
  6. "trim": {
  7. "field": "raw_text",
  8. "target_field": "trimmed_text"
  9. }
  10. }
  11. ]
  12. }

copy

Step 2 (Optional): Test the pipeline

It is recommended that you test your pipeline before you ingest documents.

To test the pipeline, run the following query:

  1. POST _ingest/pipeline/trim_pipeline/_simulate
  2. {
  3. "docs": [
  4. {
  5. "_source": {
  6. "raw_text": " Hello, world! "
  7. }
  8. }
  9. ]
  10. }

copy

Response

The following example response confirms that the pipeline is working as expected:

  1. {
  2. "docs": [
  3. {
  4. "doc": {
  5. "_index": "_index",
  6. "_id": "_id",
  7. "_source": {
  8. "raw_text": " Hello, world! ",
  9. "trimmed_text": "Hello, world!"
  10. },
  11. "_ingest": {
  12. "timestamp": "2024-04-26T20:58:17.418006805Z"
  13. }
  14. }
  15. }
  16. ]
  17. }

Step 3: Ingest a document

The following query ingests a document into an index named testindex1:

  1. PUT testindex1/_doc/1?pipeline=trim_pipeline
  2. {
  3. "message": " This is a test document. "
  4. }

copy

Response

The request indexes the document into the index testindex1 and indexes all documents with the raw_text field, which is processed by the trim_pipeline, to populate the trimmed_text field, as shown in the following response:

  1. "_index": "testindex1",
  2. "_id": "1",
  3. "_version": 68,
  4. "result": "updated",
  5. "_shards": {
  6. "total": 2,
  7. "successful": 1,
  8. "failed": 0
  9. },
  10. "_seq_no": 70,
  11. "_primary_term": 47
  12. }

copy

Step 4 (Optional): Retrieve the document

To retrieve the document, run the following query:

  1. GET testindex1/_doc/1

copy

The response includes the trimmed_text field with the leading and trailing white space removed:

  1. {
  2. "_index": "testindex1",
  3. "_id": "1",
  4. "_version": 69,
  5. "_seq_no": 71,
  6. "_primary_term": 47,
  7. "found": true,
  8. "_source": {
  9. "raw_text": " This is a test document. ",
  10. "trimmed_text": "This is a test document."
  11. }
  12. }