Skip to content
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
[[semantic-search-deployed-nlp-model]]
=== Tutorial: semantic search with a deployed model

++++
<titleabbrev>Semantic search with deployed model</titleabbrev>
++++

IMPORTANT: For the easiest way to perform semantic search in the {stack}, refer to the <<semantic-search-semantic-text, `semantic_text`>> end-to-end tutorial.

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


[discrete]
[[deployed-select-nlp-model]]
==== Select an NLP model

{es} offers the usage of a {ml-docs}/ml-nlp-model-ref.html#ml-nlp-model-ref-text-embedding[wide range of NLP models], 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.

To address this issue, Elastic provides a pre-trained representational model called {ml-docs}/ml-nlp-elser.html[Elastic Learned Sparse EncodeR (ELSER)].
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.

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.


[discrete]
[[deployed-deploy-nlp-model]]
==== Deploy the model

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

include::{es-ref-dir}/tab-widgets/semantic-search/deploy-nlp-model-widget.asciidoc[]


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

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.

include::{es-ref-dir}/tab-widgets/semantic-search/field-mappings-widget.asciidoc[]


[discrete]
[[deployed-generate-embeddings]]
==== Generate text 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,ingest pipeline>> with an <<inference-processor,inference processor>>.
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.

include::{es-ref-dir}/tab-widgets/semantic-search/generate-embeddings-widget.asciidoc[]

Now it is time to perform semantic search!


[discrete]
[[deployed-search]]
==== Search the data

Depending on the type of model you have deployed, you can query rank features with a <<query-dsl-sparse-vector-query, sparse vector>> query, or dense vectors with a kNN search.

include::{es-ref-dir}/tab-widgets/semantic-search/search-widget.asciidoc[]


[discrete]
[[deployed-hybrid-search]]
==== Beyond semantic search with hybrid search

In some situations, lexical search may perform better than semantic search.
For example, when searching for single words or IDs, like product numbers.

Combining semantic and lexical search into one hybrid search request using <<rrf,reciprocal rank fusion>> provides the best of both worlds.
Not only that, but hybrid search using reciprocal rank fusion {blog-ref}improving-information-retrieval-elastic-stack-hybrid[has been shown to perform better in general].

include::{es-ref-dir}/tab-widgets/semantic-search/hybrid-search-widget.asciidoc[]
108 changes: 35 additions & 73 deletions docs/reference/search/search-your-data/semantic-search.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ Embeddings are vectors that provide a numeric representation of a text.
Pieces of content with similar meaning have similar representations.
NLP models can be used in the {stack} various ways, you can:

* deploy models in {es}
* use the <<semantic-search-semantic-text, `semantic_text` workflow>> (recommended)
* use the <<semantic-search-inference, {infer} API workflow>>
* deploy models directly in {es}


[[semantic-search-diagram]]
Expand All @@ -20,95 +20,57 @@ image::images/search/vector-search-oversimplification.png[A simplified represent

At query time, {es} can use the same NLP model to convert a query into embeddings, enabling you to find documents with similar text embeddings.

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

IMPORTANT: For the easiest way to perform semantic search in the {stack}, refer to the <<semantic-search-semantic-text, `semantic_text`>> end-to-end tutorial.

[discrete]
[[semantic-search-select-nlp-model]]
=== Select an NLP model

{es} offers the usage of a
{ml-docs}/ml-nlp-model-ref.html#ml-nlp-model-ref-text-embedding[wide range of NLP models], 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.

To address this issue, Elastic provides a pre-trained representational model called {ml-docs}/ml-nlp-elser.html[Elastic Learned Sparse EncodeR (ELSER)].
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.

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.

[discrete]
[[semantic-search-deploy-nlp-model]]
=== Deploy the model

After you decide which model you want to use for implementing semantic search, you need to deploy the model in {es}.
[[using-nlp-models]]
=== Using NLP models

include::{es-ref-dir}/tab-widgets/semantic-search/deploy-nlp-model-widget.asciidoc[]
The easiest and recommended way to use NLP models in the {stack} is through the <<semantic-search-semantic-text, `semantic_text` workflow>>.
If you want to use ELSER for semantic search or already have a service you use, create an {infer} endpoint and an index mapping to start ingesting and querying data.
There is no need to define model-related settings and parameters, or to create {infer} ingest pipelines.
Refer to the <<put-inference-api, Create an {infer} endpoint API>> documentation for a list of supported services.

[discrete]
[[semantic-search-field-mappings]]
=== Map a field for the text embeddings
The <<semantic-search-inference, {infer} API workflow>> more complex but offers greater control over the {infer} endpoint configuration.
You need to create an {infer} endpoint, provide various model-related settings and parameters, define an index mapping, and set up an {infer} ingest pipeline with the appropriate settings.

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.
You can also deploy NLP models directly in {es}, which is the most complex way to perform semantic search in the {stack}.
You need to select an NLP model from the {ml-docs}/ml-nlp-model-ref.html#ml-nlp-model-ref-text-embedding[list of supported dense and sparse vector models], deploy it using the Eland client, create an index mapping, and set up a suitable ingest pipeline to start ingesting and querying data.

include::{es-ref-dir}/tab-widgets/semantic-search/field-mappings-widget.asciidoc[]

[discrete]
[[semantic-search-generate-embeddings]]
=== Generate text embeddings
[[using-query]]
=== Using the right query

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,ingest pipeline>> with an <<inference-processor,inference processor>>.
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.
Crafting the right query is crucial for semantic search.
The query type you should use depends first on whether you are using the recommended `semantic_text` workflow.
If not, it depends on the vector type in which your embeddings are stored.

include::{es-ref-dir}/tab-widgets/semantic-search/generate-embeddings-widget.asciidoc[]
[cols="3*", options="header"]
|=======================================================================================================================================================================================================
| Field to query | Query to use | Notes
| <<semantic-text,`semantic_text`>> | <<query-dsl-semantic-query,`semantic`>> . | The `semantic_text` field handles generating embeddings for you at index time and query time.
| <<sparse-vector,`sparse_vector`>> | <<query-dsl-sparse-vector-query,`sparse vector`>> | The `sparse_vector` query can generate query embeddings for you, but you can also provide your own. You are expected to provide embeddings at index time.
| <<dense-vector,`dense_vector`>> | <<query-dsl-knn-query,`knn`>> | The `knn` query can generate query embeddings for you, but you can also provide your own. You are expected to provide embeddings at index time.
|=======================================================================================================================================================================================================

Now it is time to perform semantic search!
If you want {es} to generate embeddings for you both index and query time, use the `semantic_text` field and the `semantic` query.
If you want to bring your own embeddings, store them in {es} and use the `sparse_vector` or `dense_vector` field type and the associated query depending on the NLP model you used for generating the embeddings.

[discrete]
[[semantic-search-search]]
=== Search the data

Depending on the type of model you have deployed, you can query rank features with a <<query-dsl-sparse-vector-query, sparse vector>> query, or dense vectors with a kNN search.

include::{es-ref-dir}/tab-widgets/semantic-search/search-widget.asciidoc[]

[discrete]
[[semantic-search-hybrid-search]]
=== Beyond semantic search with hybrid search

In some situations, lexical search may perform better than semantic search.
For example, when searching for single words or IDs, like product numbers.

Combining semantic and lexical search into one hybrid search request using
<<rrf,reciprocal rank fusion>> provides the best of both worlds.
Not only that, but hybrid search using reciprocal rank fusion {blog-ref}improving-information-retrieval-elastic-stack-hybrid[has been shown to perform better in general].
IMPORTANT: For the easiest way to perform semantic search in the {stack}, refer to the <<semantic-search-semantic-text, `semantic_text`>> end-to-end tutorial.

include::{es-ref-dir}/tab-widgets/semantic-search/hybrid-search-widget.asciidoc[]

[discrete]
[[semantic-search-read-more]]
=== Read more

* Tutorials:
** <<semantic-search-elser,Semantic search with ELSER>>
** <<semantic-search-semantic-text, Semantic search with `semantic_text`>>
** <<semantic-search-inference, Semantic search with the {infer} API>>
** <<semantic-search-elser,Semantic search with ELSER>> using the {infer} workflow
** <<semantic-search-deployed-nlp-model, Semantic search with a model deployed in {es}>>
** {ml-docs}/ml-nlp-text-emb-vector-search-example.html[Semantic search with the msmarco-MiniLM-L-12-v3 sentence-transformer model]
* Interactive examples:
** The https://github.com/elastic/elasticsearch-labs[`elasticsearch-labs`] repo contains a number of interactive semantic search examples in the form of executable Python notebooks, using the {es} Python client
* Blogs:
** {blog-ref}may-2023-launch-sparse-encoder-ai-model[Introducing Elastic Learned Sparse Encoder: Elastic's AI model for semantic search]
** {blog-ref}lexical-ai-powered-search-elastic-vector-database[How to get the best of lexical and AI-powered search with Elastic's vector database]
Expand All @@ -117,10 +79,10 @@ include::{es-ref-dir}/tab-widgets/semantic-search/hybrid-search-widget.asciidoc[
*** {blog-ref}improving-information-retrieval-elastic-stack-benchmarking-passage-retrieval[Part 2: Benchmarking passage retrieval]
*** {blog-ref}may-2023-launch-information-retrieval-elasticsearch-ai-model[Part 3: Introducing Elastic Learned Sparse Encoder, our new retrieval model]
*** {blog-ref}improving-information-retrieval-elastic-stack-hybrid[Part 4: Hybrid retrieval]
* Interactive examples:
** The https://github.com/elastic/elasticsearch-labs[`elasticsearch-labs`] repo contains a number of interactive semantic search examples in the form of executable Python notebooks, using the {es} Python client

include::semantic-search-elser.asciidoc[]

include::semantic-search-semantic-text.asciidoc[]
include::semantic-search-inference.asciidoc[]
include::semantic-search-elser.asciidoc[]
include::cohere-es.asciidoc[]
include::semantic-search-deploy-model.asciidoc[]