HTML strip processor

The html_strip processor removes HTML tags from string fields in incoming documents. This processor is useful when indexing data from webpages or other sources that may contain HTML markup. HTML tags are replaced with newline characters (\n).

The following is the syntax for the html_strip processor:

  1. {
  2. "html_strip": {
  3. "field": "webpage"
  4. }
  5. }

copy

Configuration parameters

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

ParameterRequired/OptionalDescription
fieldRequiredThe string field from which to remove HTML tags.
target_fieldOptionalThe field that receives the plain text version after stripping HTML tags. If not specified, then the field is updated in-place.
ignore_missingOptionalSpecifies whether the processor should ignore documents that do not contain the specified field. Default is false.
descriptionOptionalA description of the processor’s purpose or configuration.
ifOptionalSpecifies to conditionally execute the processor.
ignore_failureOptionalSpecifies to ignore processor failures. See Handling pipeline failures.
on_failureOptionalSpecifies a list of processors to run if the processor fails during execution. These processors are executed in the order they are specified. See Handling pipeline failures.
tagOptionalAn 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 strip-html-pipeline that uses the html_strip processor to remove HTML tags from the description field and store the processed value in a new field named cleaned_description:

  1. PUT _ingest/pipeline/strip-html-pipeline
  2. {
  3. "description": "A pipeline to strip HTML from description field",
  4. "processors": [
  5. {
  6. "html_strip": {
  7. "field": "description",
  8. "target_field": "cleaned_description"
  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/strip-html-pipeline/_simulate
  2. {
  3. "docs": [
  4. {
  5. "_source": {
  6. "description": "This is a <b>test</b> description with <i>some</i> HTML tags."
  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. "description": "This is a <b>test</b> description with <i>some</i> HTML tags.",
  9. "cleaned_description": "This is a test description with some HTML tags."
  10. },
  11. "_ingest": {
  12. "timestamp": "2024-05-22T21:46:11.227974965Z"
  13. }
  14. }
  15. }
  16. ]
  17. }

copy

Step 3: Ingest a document

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

  1. PUT products/_doc/1?pipeline=strip-html-pipeline
  2. {
  3. "name": "Product 1",
  4. "description": "This is a <b>test</b> product with <i>some</i> HTML tags."
  5. }

copy

Response

The response shows that the request has indexed the document into the index products and will index all documents with the description field containing HTML tags while storing the plain text version in the cleaned_description field:

  1. {
  2. "_index": "products",
  3. "_id": "1",
  4. "_version": 1,
  5. "result": "created",
  6. "_shards": {
  7. "total": 2,
  8. "successful": 1,
  9. "failed": 0
  10. },
  11. "_seq_no": 0,
  12. "_primary_term": 1
  13. }

copy

Step 4 (Optional): Retrieve the document

To retrieve the document, run the following query:

  1. GET products/_doc/1

copy

Response

The response includes both the original description field and the cleaned_description field with HTML tags removed:

  1. {
  2. "_index": "products",
  3. "_id": "1",
  4. "_version": 1,
  5. "_seq_no": 0,
  6. "_primary_term": 1,
  7. "found": true,
  8. "_source": {
  9. "cleaned_description": "This is a test product with some HTML tags.",
  10. "name": "Product 1",
  11. "description": "This is a <b>test</b> product with <i>some</i> HTML tags."
  12. }
  13. }