You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: solutions/search/vector/bring-own-vectors.md
+59-39Lines changed: 59 additions & 39 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,36 +7,50 @@ applies_to:
7
7
serverless:
8
8
products:
9
9
- id: elasticsearch
10
+
description: An introduction to vectors and knn search in Elasticsearch.
10
11
---
11
12
12
13
# Bring your own dense vectors [bring-your-own-vectors]
13
14
15
+
{{es}} enables you store and search mathematical representations of your content called _embeddings_ or _vectors_, which help machines understand and process your data more effectively.
16
+
There are two types of representation (_dense_ and _sparse_), which are suited to different types of queries and use cases (for example, finding similar images and content or storing expanded terms and weights).
14
17
15
-
This tutorial demonstrates how to index documents that already have dense vector embeddings into {{es}}. You’ll also learn the syntax for searching these documents using a `knn` query.
18
+
In this introduction to [vector search](/solutions/search/vector.md), you'll store and search for dense vectors.
19
+
You'll also learn the syntax for searching these documents using a [k-nearest neighbour](/solutions/search/vector/knn.md) (kNN) query.
16
20
17
-
You’ll find links at the end of this tutorial for more information about deploying a text embedding model in {{es}}, so you can generate embeddings for queries on the fly.
21
+
## Prerequisites
18
22
19
-
::::{tip}
20
-
This is an advanced use case. Refer to [Semantic search](../semantic-search.md) for an overview of your options for semantic search with {{es}}.
23
+
- If you're using {{es-serverless}}, create a project that is optimized for vectors. To add the sample data, you must have a `developer` or `admin` predefined role or an equivalent custom role.
24
+
- If you're using {{ech}} or a self-managed cluster, start {{es}} and {{kib}}. The simplest method to complete the steps in this guide is to log in with a user that has the `superuser` built-in role.
25
+
26
+
To learn about role-based access control, check out [](/deploy-manage/users-roles/cluster-or-deployment-auth/user-roles.md).
21
27
22
-
::::
28
+
## Create a vector database
23
29
30
+
When you create vectors (or _vectorize_ your data), you convert complex and nuanced content (such as text, videos, images, or audio) into multidimensional numerical representations.
31
+
They must be stored in specialized data structures designed to ensure efficient similarity search and speedy vector distance calculations.
24
32
25
-
## Step 1: Create an index with `dense_vector` mapping [bring-your-own-vectors-create-index]
33
+
In this quide, you'll use documents that already have dense vector embeddings.
34
+
To deploy a vector embedding model in {{es}} and generate vectors while ingesting and searching your data, refer to the links in [Learn more](#bring-your-own-vectors-learn-more).
26
35
27
-
Each document in our simple dataset will have:
36
+
::::{tip}
37
+
This is an advanced use case that uses the `dense_vector` field type. Refer to [](/solutions/search/semantic-search.md) for an overview of your options for semantic search with {{es}}.
38
+
To learn about the differences between semantic search and vector search, go to [](/solutions/search/ai-search/ai-search.md).
39
+
::::
28
40
29
-
* A review: stored in a `review_text` field
30
-
* An embedding of that review: stored in a `review_vector`field
41
+
:::::{stepper}
42
+
::::{step} Create an index with dense vector field mappings
31
43
32
-
* The `review_vector` field is defined as a [`dense_vector`](elasticsearch://reference/elasticsearch/mapping-reference/dense-vector.md) data type.
44
+
Each document in our simple data set will have:
33
45
46
+
* A review: stored in a `review_text` field
47
+
* An embedding of that review: stored in a `review_vector` field, which is defined as a [`dense_vector`](elasticsearch://reference/elasticsearch/mapping-reference/dense-vector.md) data type.
34
48
35
-
::::{tip}
49
+
:::{tip}
36
50
The `dense_vector` type automatically uses `int8_hnsw` quantization by default to reduce the memory footprint required when searching float vectors. Learn more about balancing performance and accuracy in [Dense vector quantization](elasticsearch://reference/elasticsearch/mapping-reference/dense-vector.md#dense-vector-quantization).
51
+
:::
37
52
38
-
::::
39
-
53
+
The following API request defines the `review_text` and `review_vector` fields:
40
54
41
55
```console
42
56
PUT /amazon-reviews
@@ -57,18 +71,17 @@ PUT /amazon-reviews
57
71
}
58
72
```
59
73
60
-
1. The `dims` parameter must match the length of the embedding vector. Here we’re using a simple 8-dimensional embedding for readability. If not specified, `dims` will be dynamically calculated based on the first indexed document.
74
+
1. The `dims` parameter must match the length of the embedding vector. If not specified, `dims` will be dynamically calculated based on the first indexed document.
61
75
2. The `index` parameter is set to `true` to enable the use of the `knn` query.
62
76
3. The `similarity` parameter defines the similarity function used to compare the query vector to the document vectors. `cosine` is the default similarity function for `dense_vector` fields in {{es}}.
63
77
78
+
Here we're using an 8-dimensional embedding for readability.
79
+
The vectors that neural network models work with can have several hundreds or even thousands of dimensions that represent a point in a multi-dimensional space.
80
+
Each vector dimension represents a _feature_ or a characteristic of the unstructured data.
81
+
::::
82
+
::::{step} Add documents with embeddings
64
83
65
-
66
-
## Step 2: Index documents with embeddings [bring-your-own-vectors-index-documents]
67
-
68
-
69
-
### Index a single document [_index_a_single_document]
70
-
71
-
First, index a single document to understand the document structure.
84
+
First, index a single document to understand the document structure:
72
85
73
86
```console
74
87
PUT /amazon-reviews/_doc/1
@@ -80,13 +93,8 @@ PUT /amazon-reviews/_doc/1
80
93
81
94
1. The size of the `review_vector` array is 8, matching the `dims` count specified in the mapping.
82
95
83
-
84
-
85
-
### Bulk index multiple documents [_bulk_index_multiple_documents]
86
-
87
-
In a production scenario, you’ll want to index many documents at once using the [`_bulk` endpoint](https://www.elastic.co/docs/api/doc/elasticsearch/operation/operation-bulk).
88
-
89
-
Here’s an example of indexing multiple documents in a single `_bulk` request.
96
+
In a production scenario, you'll want to index many documents at once using the [`_bulk` endpoint]({{es-apis}}operation/operation-bulk).
97
+
Here's an example of indexing multiple documents in a single `_bulk` request:
90
98
91
99
```console
92
100
POST /_bulk
@@ -100,10 +108,14 @@ POST /_bulk
100
108
{ "review_text": "This product has ruined my life and the lives of my family and friends.", "review_vector": [0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1] }
101
109
```
102
110
111
+
::::
112
+
:::::
103
113
104
-
## Step 3: Search documents with embeddings[bring-your-own-vectors-search-documents]
114
+
## Test vector search[bring-your-own-vectors-search-documents]
105
115
106
-
Now you can query these document vectors using a [`knn` retriever](https://www.elastic.co/docs/api/doc/elasticsearch/operation/operation-search#operation-search-body-application-json-retriever). `knn` is a type of vector search, which finds the `k` most similar documents to a query vector. Here we’re simply using a raw vector for the query text, for demonstration purposes.
116
+
Now you can query these document vectors using a [`knn` retriever](elasticsearch://reference/elasticsearch/rest-apis/retrievers.md#knn-retriever).
117
+
`knn` is a type of vector search, which finds the `k` most similar documents to a query vector.
118
+
Here we're using a raw vector for the query text for demonstration purposes:
107
119
108
120
```console
109
121
POST /amazon-reviews/_search
@@ -119,23 +131,31 @@ POST /amazon-reviews/_search
119
131
}
120
132
```
121
133
122
-
1.In this simple example, we’re sending a raw vector as the query text. In a real-world scenario, you’ll need to generate vectors for queries using an embedding model.
134
+
1.A raw vector serves as the query text in this example. In a real-world scenario, you'll need to generate vectors for queries using an embedding model.
123
135
2. The `k` parameter specifies the number of results to return.
124
136
3. The `num_candidates` parameter is optional. It limits the number of candidates returned by the search node. This can improve performance and reduce costs.
125
137
138
+
## Next steps
126
139
140
+
If you want to try a similar set of steps from an {{es}} client, check out the guided index workflow:
127
141
128
-
## Learn more [bring-your-own-vectors-learn-more]
142
+
- If you're using Elasticsearch Serverless, go to **{{es}} > Home**, select the vector search workflow, and **Create a vector optimized index**.
143
+
- If you're using {{ech}} or a self-managed cluster, go to **Elasticsearch > Home** and click **Create API index**. Select the vector search workflow.
129
144
130
-
In this simple example, we’re sending a raw vector for the query text. In a real-world scenario you won’t know the query text ahead of time. You’ll need to generate query vectors, on the fly, using the same embedding model that generated the document vectors.
145
+
When you finish your tests and no longer need the sample data set, delete the index:
131
146
132
-
For this you’ll need to deploy a text embedding model in {{es}} and use the [`query_vector_builder` parameter](elasticsearch://reference/query-languages/query-dsl/query-dsl-knn-query.md#knn-query-top-level-parameters). Alternatively, you can generate vectors client-side and send them directly with the search request.
133
-
134
-
Learn how to [use a deployed text embedding model](dense-versus-sparse-ingest-pipelines.md) for semantic search.
147
+
```console
148
+
DELETE /amazon-reviews
149
+
```
135
150
136
-
::::{tip}
137
-
If you’re just getting started with vector search in {{es}}, refer to [Semantic search](../semantic-search.md).
151
+
## Learn more [bring-your-own-vectors-learn-more]
138
152
139
-
::::
153
+
In these simple examples, we're sending a raw vector for the query text.
154
+
In a real-world scenario you won't know the query text ahead of time.
155
+
You'll need to generate query vectors, on the fly, using the same embedding model that generated the document vectors.
156
+
For this you'll need to deploy a text embedding model in {{es}} and use the [`query_vector_builder` parameter](elasticsearch://reference/query-languages/query-dsl/query-dsl-knn-query.md#knn-query-top-level-parameters).
157
+
Alternatively, you can generate vectors client-side and send them directly with the search request.
140
158
159
+
For an example of using pipelines to generate text embeddings, check out [](/solutions/search/vector/dense-versus-sparse-ingest-pipelines.md).
141
160
161
+
To learn about more search options, such as semantic, full-text, and hybrid, go to [](/solutions/search/search-approaches.md).
0 commit comments