Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 25 additions & 27 deletions solutions/search/vector/dense-versus-sparse-ingest-pipelines.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,27 @@ products:

# Tutorial: Dense and sparse workflows using ingest pipelines [semantic-search-deployed-nlp-model]


::::{important}
* For the easiest way to perform semantic search in the {{stack}}, refer to the [`semantic_text`](../semantic-search/semantic-search-semantic-text.md) end-to-end tutorial.
* This tutorial was written before the [{{infer}} endpoint](https://www.elastic.co/docs/api/doc/elasticsearch/group/endpoint-inference) and [`semantic_text` field type](elasticsearch://reference/elasticsearch/mapping-reference/semantic-text.md) was introduced. Today we have simpler options for performing semantic search.

* For the easiest way to perform semantic search in the {{stack}}, refer to the [`semantic_text`](../semantic-search/semantic-search-semantic-text.md) end-to-end tutorial.
* This tutorial predates the [{{infer}} endpoint](https://www.elastic.co/docs/api/doc/elasticsearch/group/endpoint-inference) and the [`semantic_text` field type](elasticsearch://reference/elasticsearch/mapping-reference/semantic-text.md). Today there are simpler, higher-level options for semantic search than the ones outlined in this tutorial. The semantic text workflow is the recommended way to perform semantic search for most use cases.
::::


This guide shows you how to implement semantic search with models deployed in {{es}}: from selecting an NLP model, to writing queries.

**This guide shows how to implement semantic search in {{es}} with deployed NLP models: from selecting a model, to configuring ingest pipelines, to running queries.**

## Select an NLP model [deployed-select-nlp-model]

{{es}} offers the usage of a [wide range of NLP models](/explore-analyze/machine-learning/nlp/ml-nlp-model-ref.md#ml-nlp-model-ref-text-embedding), including both dense and sparse vector models. Your choice of the language model is critical for implementing semantic search successfully.

While it is possible to bring your own text embedding model, achieving good search results through model tuning is challenging. Selecting an appropriate model from our third-party model list is the first step. Training the model on your own data is essential to ensure better search results than using only BM25. However, the model training process requires a team of data scientists and ML experts, making it expensive and time-consuming.
{{es}} supports a [wide range of NLP models](/explore-analyze/machine-learning/nlp/ml-nlp-model-ref.md#ml-nlp-model-ref-text-embedding), including **dense** and **sparse** vector models. Choosing the right model is critical for good relevance.

To address this issue, Elastic provides a pre-trained representational model called [Elastic Learned Sparse EncodeR (ELSER)](/explore-analyze/machine-learning/nlp/ml-nlp-elser.md). ELSER, currently available only for English, is an out-of-domain sparse vector model that does not require fine-tuning. This adaptability makes it suitable for various NLP use cases out of the box. Unless you have a team of ML specialists, it is highly recommended to use the ELSER model.
While bringing your own text embedding model is possible, achieving strong results typically requires tuning and evaluation. Selecting a well-performing third-party model from our list is a good start. Training the model on your own data is essential to ensure better search results than using only BM25. However, the model training process requires a team of data scientists and ML experts, making it expensive and time-consuming.

In the case of sparse vector representation, the vectors mostly consist of zero values, with only a small subset containing non-zero values. This representation is commonly used for textual data. In the case of ELSER, each document in an index and the query text itself are represented by high-dimensional sparse vectors. Each non-zero element of the vector corresponds to a term in the model vocabulary. The ELSER vocabulary contains around 30000 terms, so the sparse vectors created by ELSER contain about 30000 values, the majority of which are zero. Effectively the ELSER model is replacing the terms in the original query with other terms that have been learnt to exist in the documents that best match the original search terms in a training dataset, and weights to control how important each is.
To lower the barrier, Elastic provides [**Elastic Learned Sparse EncodeR (ELSER)**](/explore-analyze/machine-learning/nlp/ml-nlp-elser.md), a pre-trained sparse vector model (English-only) that delivers strong out-of-the-box relevance without fine-tuning. This adaptability makes it suitable for various NLP use cases out of the box. Unless you have an ML team, we recommend starting with ELSER.

Sparse vectors are mostly zeros with a small number of non-zero values. This representation is commonly used for textual data. With ELSER, both documents and queries are represented as high-dimensional sparse vectors. Each non-zero element of the vector corresponds to a term in the model vocabulary. The ELSER vocabulary contains around 30000 terms, so the sparse vectors created by ELSER contain about 30000 values, the majority of which are zero. The ELSER model replaces the terms in the original query with other terms learned from the training dataset that appear in documents matching the original search terms, and assigns weights to control their importance.

## Deploy the model [deployed-deploy-nlp-model]
## Deploy the model in {{es}} [deployed-deploy-nlp-model]

After you decide which model you want to use for implementing semantic search, you need to deploy the model in {{es}}.
After you choose a model, deploy it in {{es}}.

:::::::{tab-set}

Expand All @@ -51,14 +47,15 @@ To deploy a third-party text embedding model, refer to [Deploy a text embedding

## Map a field for the text embeddings [deployed-field-mappings]

Before you start using the deployed model to generate embeddings based on your input text, you need to prepare your index mapping first. The mapping of the index depends on the type of model.
Before generating embeddings, prepare your **index mapping**. The mapping depends on whether you use a **sparse** (ELSER) or **dense** model.

:::::::{tab-set}

::::::{tab-item} ELSER
ELSER produces token-weight pairs as output from the input text and the query. The {{es}} [`sparse_vector`](elasticsearch://reference/elasticsearch/mapping-reference/sparse-vector.md) field type can store these token-weight pairs as numeric feature vectors. The index must have a field with the `sparse_vector` field type to index the tokens that ELSER generates.
ELSER outputs token-weight pairs. Use the [`sparse_vector`](elasticsearch://reference/elasticsearch/mapping-reference/sparse-vector.md) field to store them.

To create a mapping for your ELSER index, refer to the [Create the index mapping section](../semantic-search/semantic-search-elser-ingest-pipelines.md#elser-mappings) of the tutorial. The example shows how to create an index mapping for `my-index` that defines the `my_embeddings.tokens` field - which will contain the ELSER output - as a `sparse_vector` field.
To create a mapping for your ELSER index, refer to the [Create the index mapping section](../semantic-search/semantic-search-elser-ingest-pipelines.md#elser-mappings) of the tutorial.
Create a mapping with a `sparse_vector` field for the tokens and a `text` field for the source content. For example:

```console
PUT my-index
Expand All @@ -76,16 +73,16 @@ PUT my-index
}
```

1. The name of the field that will contain the tokens generated by ELSER.
1. The field that will contain ELSER-generated tokens.
2. The field that contains the tokens must be a `sparse_vector` field.
3. The name of the field from which to create the sparse vector representation. In this example, the name of the field is `my_text_field`.
3. The source field used to create the sparse vector representation. In this example, the name of the field is `my_text_field`.
4. The field type is `text` in this example.
::::::

::::::{tab-item} Dense vector models
The models compatible with {{es}} NLP generate dense vectors as output. The [`dense_vector`](elasticsearch://reference/elasticsearch/mapping-reference/dense-vector.md) field type is suitable for storing dense vectors of numeric values. The index must have a field with the `dense_vector` field type to index the embeddings that the supported third-party model that you selected generates. Keep in mind that the model produces embeddings with a certain number of dimensions. The `dense_vector` field must be configured with the same number of dimensions using the `dims` option. Refer to the respective model documentation to get information about the number of dimensions of the embeddings.
Dense vector models output numeric embeddings. Use the [`dense_vector`](elasticsearch://reference/elasticsearch/mapping-reference/dense-vector.md) field type for storing dense vectors that the supported third-party model you selected generates. Keep in mind that the model produces embeddings with a certain number of dimensions. The `dense_vector` field must be configured with the same number of dimensions using the `dims` option. Refer to the respective model documentation to get information about the number of dimensions of the embeddings.

To review a mapping of an index for an NLP model, refer to the mapping code snippet in the [Add the text embedding model to an ingest inference pipeline](/explore-analyze/machine-learning/nlp/ml-nlp-text-emb-vector-search-example.md#ex-text-emb-ingest) section of the tutorial. The example shows how to create an index mapping that defines the `my_embeddings.predicted_value` field - which will contain the model output - as a `dense_vector` field.
To review a mapping of an index for an NLP model, refer to the mapping code snippet in the [Add the text embedding model to an ingest inference pipeline](/explore-analyze/machine-learning/nlp/ml-nlp-text-emb-vector-search-example.md#ex-text-emb-ingest) section of the related tutorial. The example shows how to create an index mapping that defines the `my_embeddings.predicted_value` field - which will contain the model output - as a `dense_vector` field.

```console
PUT my-index
Expand All @@ -107,15 +104,15 @@ PUT my-index
1. The name of the field that will contain the embeddings generated by the model.
2. The field that contains the embeddings must be a `dense_vector` field.
3. The model produces embeddings with a certain number of dimensions. The `dense_vector` field must be configured with the same number of dimensions by the `dims` option. Refer to the respective model documentation to get information about the number of dimensions of the embeddings.
4. The name of the field from which to create the dense vector representation. In this example, the name of the field is `my_text_field`.
4. The source field used to create the dense vector representation. In this example, the name of the field is `my_text_field`.
5. The field type is `text` in this example.
::::::

:::::::

## Generate text embeddings [deployed-generate-embeddings]

Once you have created the mappings for the index, you can generate text embeddings from your input text. This can be done by using an [ingest pipeline](../../../manage-data/ingest/transform-enrich/ingest-pipelines.md) with an [inference processor](elasticsearch://reference/enrich-processor/inference-processor.md). The ingest pipeline processes the input data and indexes it into the destination index. At index time, the inference ingest processor uses the trained model to infer against the data ingested through the pipeline. After you created the ingest pipeline with the inference processor, you can ingest your data through it to generate the model output.
Use an [ingest pipeline](../../../manage-data/ingest/transform-enrich/ingest-pipelines.md) with an [inference processor](elasticsearch://reference/enrich-processor/inference-processor.md) to generate embeddings at ingest time. The ingest pipeline processes the input data and indexes it into the destination index. At index time, the inference ingest processor uses the trained model to infer against the data ingested through the pipeline. After you created the ingest pipeline with the inference processor, you can ingest your data through it to generate the model output.

:::::::{tab-set}

Expand Down Expand Up @@ -144,12 +141,11 @@ PUT _ingest/pipeline/my-text-embeddings-pipeline

1. Configuration object that defines the `input_field` for the {{infer}} process and the `output_field` that will contain the {{infer}} results.


To ingest data through the pipeline to generate tokens with ELSER, refer to the [Ingest the data through the {{infer}} ingest pipeline](/solutions/search/semantic-search/semantic-search-elser-ingest-pipelines.md#reindexing-data-elser) section of the tutorial. After you successfully ingested documents by using the pipeline, your index will contain the tokens generated by ELSER. Tokens are learned associations capturing relevance, they are not synonyms. To learn more about what tokens are, refer to [this page](/explore-analyze/machine-learning/nlp/ml-nlp-elser.md#elser-tokens).
::::::

::::::{tab-item} Dense vector models
This is how an ingest pipeline that uses a text embedding model is created:
Example ingest pipeline using a text embedding model:

```console
PUT _ingest/pipeline/my-text-embeddings-pipeline
Expand All @@ -172,15 +168,13 @@ PUT _ingest/pipeline/my-text-embeddings-pipeline
1. The model ID of the text embedding model you want to use.
2. The `field_map` object maps the input document field name (which is `my_text_field` in this example) to the name of the field that the model expects (which is always `text_field`).


To ingest data through the pipeline to generate text embeddings with your chosen model, refer to the [Add the text embedding model to an inference ingest pipeline](/explore-analyze/machine-learning/nlp/ml-nlp-text-emb-vector-search-example.md#ex-text-emb-ingest) section. The example shows how to create the pipeline with the inference processor and reindex your data through the pipeline. After you successfully ingested documents by using the pipeline, your index will contain the text embeddings generated by the model.
::::::

:::::::
Now it is time to perform semantic search!


## Search the data [deployed-search]
## Search the data with vector search [deployed-search]

Depending on the type of model you have deployed, you can query rank features with a [sparse vector](elasticsearch://reference/query-languages/query-dsl/query-dsl-sparse-vector-query.md) query, or dense vectors with a kNN search.

Expand All @@ -201,6 +195,7 @@ GET my-index/_search
}
}
```

::::::

::::::{tab-item} Dense vector models
Expand All @@ -222,6 +217,7 @@ GET my-index/_search
}
}
```

::::::

:::::::
Expand Down Expand Up @@ -268,6 +264,7 @@ GET my-index/_search
}
}
```

::::::

::::::{tab-item} Dense vector models
Expand Down Expand Up @@ -310,6 +307,7 @@ GET my-index/_search
}
}
```

::::::

:::::::
14 changes: 7 additions & 7 deletions solutions/search/vector/sparse-vector.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,29 @@ applies_to:
stack:
serverless:
---
# Sparse vector [sparse-vector-foo]
# Sparse vector search in {{es}} [sparse-vector-search]

When working with sparse vectors in {{es}}, you'll be using the Elastic learned sparse encoder (ELSER) model at index and query time to expand content with semantically related terms.
When working with sparse vectors in {{es}}, you'll use the [**Elastic Learned Sparse Encoder (ELSER)**](/explore-analyze/machine-learning/nlp/ml-nlp-elser.md) at index and query time to expand content into semantically related, weighted terms.

This approach preserves explainability while adding semantic understanding, with each document or query expanded into a set of weighted terms.

Sparse vector search with ELSER is ideal for:

- Enhanced keyword search
- Cases requiring explainable results
- Enhanced keyword search with semantic expansion
- Use cases requiring explainable results
- Domain-specific search
- Large-scale deployments

## Working with sparse vectors in Elasticsearch
## Working with sparse vectors in {{es}}

:::{tip}
Using the `semantic_text` field type provides automatic model management and sensible defaults. [Learn more](../semantic-search/semantic-search-semantic-text.md).
:::

Sparse vector search with ELSER expands both documents and queries into weighted terms. To use sparse vectors in {{es}}:

1. Index documents with ELSER
1. **Index documents with ELSER**
- Deploy and configure the ELSER model
- Use the `sparse_vector` field type
- See [this overview](../semantic-search.md#using-nlp-models) for implementation options
2. Query the index using [`sparse_vector` query](elasticsearch://reference/query-languages/query-dsl/query-dsl-sparse-vector-query.md).
2. **Query the index** using the [`sparse_vector` query](elasticsearch://reference/query-languages/query-dsl/query-dsl-sparse-vector-query.md).
Loading