Sparse encoding processor

The sparse_encoding processor is used to generate a sparse vector/token and weights from text fields for neural sparse search using sparse retrieval.

PREREQUISITE
Before using the sparse_encoding processor, you must set up a machine learning (ML) model. For more information, see Choosing a model.

The following is the syntax for the sparse_encoding processor:

  1. {
  2. "sparse_encoding": {
  3. "model_id": "<model_id>",
  4. "field_map": {
  5. "<input_field>": "<vector_field>"
  6. }
  7. }
  8. }

copy

Configuration parameters

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

ParameterData typeRequired/OptionalDescription
model_idStringRequiredThe ID of the model that will be used to generate the embeddings. The model must be deployed in OpenSearch before it can be used in neural search. For more information, see Using custom models within OpenSearch and Neural sparse search.
field_mapObjectRequiredContains key-value pairs that specify the mapping of a text field to a rank_features field.
field_map.<input_field>StringRequiredThe name of the field from which to obtain text for generating vector embeddings.
field_map.<vector_field>StringRequiredThe name of the vector field in which to store the generated vector embeddings.
descriptionStringOptionalA brief description of the processor.
tagStringOptionalAn identifier tag for the processor. Useful for debugging to distinguish between processors of the same type.
batch_sizeIntegerOptionalSpecifies the number of documents to be batched and processed each time. Default is 1.

Using the processor

Follow these steps to use the processor in a pipeline. You must provide a model ID when creating the processor. For more information, see Using custom models within OpenSearch.

Step 1: Create a pipeline.

The following example request creates an ingest pipeline where the text from passage_text will be converted into text embeddings and the embeddings will be stored in passage_embedding:

  1. PUT /_ingest/pipeline/nlp-ingest-pipeline
  2. {
  3. "description": "A sparse encoding ingest pipeline",
  4. "processors": [
  5. {
  6. "sparse_encoding": {
  7. "model_id": "aP2Q8ooBpBj3wT4HVS8a",
  8. "field_map": {
  9. "passage_text": "passage_embedding"
  10. }
  11. }
  12. }
  13. ]
  14. }

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/nlp-ingest-pipeline/_simulate
  2. {
  3. "docs": [
  4. {
  5. "_index": "testindex1",
  6. "_id": "1",
  7. "_source":{
  8. "passage_text": "hello world"
  9. }
  10. }
  11. ]
  12. }

copy

Response

The response confirms that in addition to the passage_text field, the processor has generated text embeddings in the passage_embedding field:

  1. {
  2. "docs" : [
  3. {
  4. "doc" : {
  5. "_index" : "testindex1",
  6. "_id" : "1",
  7. "_source" : {
  8. "passage_embedding" : {
  9. "!" : 0.8708904,
  10. "door" : 0.8587369,
  11. "hi" : 2.3929274,
  12. "worlds" : 2.7839446,
  13. "yes" : 0.75845814,
  14. "##world" : 2.5432441,
  15. "born" : 0.2682308,
  16. "nothing" : 0.8625516,
  17. "goodbye" : 0.17146169,
  18. "greeting" : 0.96817183,
  19. "birth" : 1.2788506,
  20. "come" : 0.1623208,
  21. "global" : 0.4371151,
  22. "it" : 0.42951578,
  23. "life" : 1.5750692,
  24. "thanks" : 0.26481047,
  25. "world" : 4.7300377,
  26. "tiny" : 0.5462298,
  27. "earth" : 2.6555297,
  28. "universe" : 2.0308156,
  29. "worldwide" : 1.3903781,
  30. "hello" : 6.696973,
  31. "so" : 0.20279501,
  32. "?" : 0.67785245
  33. },
  34. "passage_text" : "hello world"
  35. },
  36. "_ingest" : {
  37. "timestamp" : "2023-10-11T22:35:53.654650086Z"
  38. }
  39. }
  40. }
  41. ]
  42. }

Once you have created an ingest pipeline, you need to create an index for ingestion and ingest documents into the index. To learn more, see Step 2: Create an index for ingestion and Step 3: Ingest documents into the index of Neural sparse search.


Next steps