AI Hints
Home›Elasticsearch Tutorial›Semantic Search and Vectors
Elasticsearch · Lesson 99 of 120

dense_vector

Store the vectors.

⏱ 2 min read▶ Video at 1:33:05🎓 Free
Watch this lesson in the Elasticsearch courseWatch on YouTube ↗Full Elasticsearch course

Elasticsearch stores vectors in a field type called dense vector. You tell it how many numbers, and how to measure similarity. It then builds a fast index for finding the nearest vectors.

  • dense_vector
  • dims
  • similarity

A tiny vector field

Here, a vector field with just three dimensions, so we can read it. Similarity is cosine. We index two documents with vectors. Run it. Both are stored, and ready for vector search.

Dev Tools Consoletext
PUT notes?filter_path=acknowledged
{ "mappings": { "properties": {
    "text":      { "type": "text" },
    "embedding": { "type": "dense_vector", "dims": 3, "similarity": "cosine" } } } }

POST notes/_bulk?refresh=true&filter_path=errors
{ "index": { "_id": 1 } }
{ "text": "rain jacket", "embedding": [0.9, 0.1, 0.2] }
{ "index": { "_id": 2 } }
{ "text": "camp stove", "embedding": [0.1, 0.8, 0.5] }
Output
# PUT notes 200 OK
{"acknowledged": true}

# POST notes/_bulk 200 OK
{"errors": false}

Index real embeddings

For real data, we embed each product's name and description in Python, and bulk index the vectors. Run it. Forty nine products, each with three hundred and eighty four numbers.

embed_index.pypython
from elasticsearch import Elasticsearch
from sentence_transformers import SentenceTransformer

es = Elasticsearch("http://localhost:9200")
model = SentenceTransformer("all-MiniLM-L6-v2", device="cpu")
from elasticsearch import helpers
import json

products = json.load(open("products.json"))
vecs = model.encode([p["name"] + ". " + p["description"] for p in products])
actions = ({"_index": "products", "_id": p["id"], **p, "embedding": v.tolist()}
           for p, v in zip(products, vecs))
print(helpers.bulk(es, actions, refresh=True)[0], "products with vectors")
Output
49 products with vectors
Also uses _setup.http
PUT products
{"mappings":{"properties":{"embedding":{"type":"dense_vector","dims":384,"similarity":"cosine"}}}}
Also uses products.json
[
{
"id": 1,
"name": "Basecamp 2-Person Tent",
"brand": "TrailCo",
"category": "Tents",
"price": 237.99,
"rating": 4.4,
"reviews": 77,
"in_stock": true,
"colors": [
"green"
],
"tags": [
"camping",
"summer"
],
"description": "A durable tent that sets up in minutes and keeps you dry in heavy rain.",
"added": "2024-02-08",
"weight_g": 1252
},
{
"id": 2,
"name": "Basecamp 3-Person Tent",
"brand": "NorthPeak",
"category": "Tents",
"price": 288.99,
"rating": 3.4,
"reviews": 437,
"in_stock": false,
"colors": [
"black",
"blue",
"red"
],
"tags": [
"camping",
"summer"
],
"description": "A roomy, lightweight tent with two doors and space for all your gear.",
"added": "2025-02-10",
"weight_g": 1103
},
{
"id": 3,
"name": "Basecamp 4-Person Tent",
"brand": "Ridgeline",
"category": "Tents",
"price": 538.99,
"rating": 4.7,
"reviews": 299,
"in_stock": true,
"colors": [
"black",
"red",
"green"
],
"tags": [
"backpacking",
"camping"
],
"description": "A roomy, waterproof tent with two doors and space for all your gear.",
"added": "2025-08-07",
"weight_g": 3516
},
{
"id": 4,
"name": "Basecamp Family Tent",
"brand": "Ridgeline",
"category": "Tents",
"price": 175.99,
"rating": 4.5,
"reviews": 580,
"in_stock": false,
"colors": [
"orange"
],
"tags": [
"climbing",
"trail"
],
"description": "A lightweight tent with strong poles for windy nights on the mountain.",
"added": "2025-04-21",
"weight_g": 3298
},
{
"id": 5,
"name": "Ultralight Solo Tent",
"brand": "Alpina",
"category": "Tents",
"price": 513.99,
"rating": 3.6,
"reviews": 187,
"in_stock": true,
"colors": [
"black"
],
"tags": [
"summer",
"trail"
],
"description": "A compact tent with strong poles for windy nights on the mountain.",
"added": "2024-12-17",
"weight_g": 3887
},
{
"id": 6,
"name": "Ultralight 2-Person Tent",
"brand": "Summit",
"category": "Tents",
"price": 295.99,
"rating": 4.1,
"reviews": 171,
"in_stock": true,
"colors": [
"orange"
],
"tags": [
"camping",
"climbing"
],
"description": "A roomy, lightweight tent with two doors and space for all your gear.",
"added": "2024-03-20",
"weight_g": 4031
},
{
"id": 7,
"name": "Storm Dome Shelter",
"brand": "Alpina",
"category": "Tents",
"price": 345.99,
"rating": 4.5,
"reviews": 611,
"in_stock": true,
"colors": [
"black",
"yellow"
],
"tags": [
"summer",
"winter"
],
"description": "A roomy, compact tent with two doors and space for all your gear.",
"added": "2025-11-11",
"weight_g": 1166
},
{
"id": 8,
"name": "Storm Tunnel Shelter",
"brand": "Granite",
"category": "Tents",
"price": 116.99,
"rating": 4.2,
"reviews": 700,
"in_stock": true,
"colors": [
"grey",
"orange"
],
"tags": [
"camping",
"travel"
],
"description": "A breathable tent with strong poles for windy nights on the mountain.",
"added": "2024-12-29",
"weight_g": 1588
},
{
"id": 9,
"name": "Arctic Down Sleeping Bag",
"brand": "Summit",
"category": "Sleeping Bags",
"price": 262.99,
"rating": 3.6,
"reviews": 297,
"in_stock": false,
"colors": [
"orange"
],
"tags": [
"climbing",
"trail"
],
"description": "A soft, rugged sleeping bag that packs down small for long hikes.",
"added": "2024-03-23",
"weight_g": 940
},
{
"id": 10,
"name": "Alpine Down Sleeping Bag",
"brand": "NorthPeak",
"category": "Sleeping Bags",
"price": 206.99,
"rating": 4.7,
"reviews": 887,
"in_stock": true,
"colors": [
"orange",
"green",
"yellow"
],
"tags": [
"hiking",
"winter"
],
"description": "A breathable sleeping bag that keeps you warm on cold nights.",
"added": "2024-06-29",
"weight_g": 909
},
{
"id": 11,
"name": "Summer Down Sleeping Bag",
"brand": "Summit",
"category": "Sleeping Bags",
"price": 130.99,
"rating": 4.1,
"reviews": 606,
"in_stock": true,
"colors": [
"black",
"blue"
],
"tags": [
"climbing",
"summer"
],
"description": "A soft, warm sleeping bag that packs down small for long hikes.",
"added": "2025-09-16",
"weight_g": 1759
},
{
"id": 12,
"name": "Trail Synthetic Sleeping Bag",
"brand": "Granite",
"category": "Sleeping Bags",
"price": 160.99,
"rating": 4.7,
"reviews": 635,
"in_stock": true,
"colors": [
"black",
"orange",
"red"
],
"tags": [
"climbing",
"winter"
],
"description": "A soft, durable sleeping bag that packs down small for long hikes.",
"added": "2025-02-07",
"weight_g": 812
},
{
"id": 13,
"name": "Camp Synthetic Sleeping Bag",
"brand": "Summit",
"category": "Sleeping Bags",
"price": 217.99,
"rating": 3.5,
"reviews": 216,
"in_stock": true,
"colors": [
"green"
],
"tags": [
"camping",
"trail"
],
"description": "An ultralight sleeping bag that keeps you warm on cold nights.",
"added": "2025-08-03",
"weight_g": 909
},
{
"id": 14,
"name": "Daypack 22L Hiking Backpack",
"brand": "Ridgeline",
"category": "Backpacks",
"price": 173.99,
"rating": 3.2,
"reviews": 898,
"in_stock": true,
"colors": [
"blue",
"grey"
],
"tags": [
"backpacking",
"summer"
],
"description": "A compact backpack with a rain cover and lots of pockets.",
"added": "2025-01-07",
"weight_g": 1471
},
{
"id": 15,
"name": "Trek 45L Hiking Backpack",
"brand": "TrailCo",
"category": "Backpacks",
"price": 69.99,
"rating": 4.1,
"reviews": 322,
"in_stock": false,
"colors": [
"grey"
],
"tags": [
"trail",
"travel"
],
"description": "A comfortable, rugged pack, big enough for a weekend of hiking.",
"added": "2025-05-05",
"weight_g": 2197
},
{
"id": 16,
"name": "Expedition 65L Hiking Backpack",
"brand": "NorthPeak",
"category": "Backpacks",
"price": 212.99,
"rating": 4.9,
"reviews": 543,
"in_stock": true,
"colors": [
"red",
"black",
"yellow"
],
"tags": [
"summer",
"travel"
],
"description": "A waterproof backpack with a padded hip belt for long days on the trail.",
"added": "2024-09-24",
"weight_g": 1561
},
{
"id": 17,
"name": "Carry-On 35L Travel Pack",
"brand": "Alpina",
"category": "Backpacks",
"price": 130.99,
"rating": 4.6,
"reviews": 548,
"in_stock": true,
"colors": [
"green",
"grey",
"blue"
],
"tags": [
"climbing",
"winter"
],
"description": "A durable backpack with a padded hip belt for long days on the trail.",
"added": "2025-02-14",
"weight_g": 2015
},
{
"id": 18,
"name": "Rolltop 28L Travel Pack",
"brand": "Ridgeline",
"category": "Backpacks",
"price": 239.99,
"rating": 4.1,
"reviews": 751,
"in_stock": false,
"colors": [
"yellow"
],
"tags": [
"summer",
"winter"
],
"description": "A comfortable, warm pack, big enough for a weekend of hiking.",
"added": "2024-07-17",
"weight_g": 1918
},
{
"id": 19,
"name": "Storm Rain Jacket",
"brand": "TrailCo",
"category": "Jackets",
"price": 230.99,
"rating": 4.7,
"reviews": 743,
"in_stock": true,
"colors": [
"black",
"blue"
],
"tags": [
"hiking",
"trail"
],
"description": "A warm, compact jacket that packs into its own pocket.",
"added": "2024-07-20",
"weight_g": 545
},
{
"id": 20,
"name": "Breeze Rain Jacket",
"brand": "TrailCo",
"category": "Jackets",
"price": 110.99,
"rating": 4.8,
"reviews": 355,
"in_stock": true,
"colors": [
"yellow"
],
"tags": [
"hiking",
"winter"
],
"description": "A waterproof jacket for cold mornings and windy ridges.",
"added": "2024-07-23",
"weight_g": 689
},
{
"id": 21,
"name": "Cloudburst Rain Jacket",
"brand": "Granite",
"category": "Jackets",
"price": 315.99,
"rating": 3.8,
"reviews": 823,
"in_stock": true,
"colors": [
"orange",
"yellow",
"grey"
],
"tags": [
"hiking",
"travel"
],
"description": "An ultralight jacket that keeps out wind and rain.",
"added": "2024-06-23",
"weight_g": 330
},
{
"id": 22,
"name": "Puffer Down Jacket",
"brand": "Granite",
"category": "Jackets",
"price": 57.99,
"rating": 3.5,
"reviews": 849,
"in_stock": true,
"colors": [
"grey",
"green"
],
"tags": [
"backpacking",
"summer"
],
"description": "A rugged jacket for cold mornings and windy ridges.",
"added": "2024-05-14",
"weight_g": 221
},
{
"id": 23,
"name": "Hooded Down Jacket",
"brand": "Ridgeline",
"category": "Jackets",
"price": 53.99,
"rating": 4.5,
"reviews": 145,
"in_stock": true,
"colors": [
"yellow"
],
"tags": [
"camping",
"winter"
],
"description": "A warm, lightweight jacket that packs into its own pocket.",
"added": "2024-08-05",
"weight_g": 499
},
{
"id": 24,
"name": "Grid Fleece Jacket",
"brand": "Alpina",
"category": "Jackets",
"price": 199.99,
"rating": 4.2,
"reviews": 857,
"in_stock": false,
"colors": [
"green",
"orange",
"red"
],
"tags": [
"climbing",
"trail"
],
"description": "A compact jacket for cold mornings and windy ridges.",
"added": "2024-05-13",
"weight_g": 744
},
{
"id": 25,
"name": "Classic Fleece Jacket",
"brand": "TrailCo",
"category": "Jackets",
"price": 94.99,
"rating": 4.6,
"reviews": 626,
"in_stock": false,
"colors": [
"blue"
],
"tags": [
"backpacking",
"winter"
],
"description": "A waterproof jacket for cold mornings and windy ridges.",
"added": "2024-05-03",
"weight_g": 769
},
{
"id": 26,
"name": "Trail Hiking Boots",
"brand": "Summit",
"category": "Footwear",
"price": 50.99,
"rating": 4.8,
"reviews": 61,
"in_stock": true,
"colors": [
"black",
"yellow"
],
"tags": [
"summer",
"trail"
],
"description": "Rugged boots with a grippy sole for rocky trails.",
"added": "2024-03-05",
"weight_g": 1207
},
{
"id": 27,
"name": "Summit Hiking Boots",
"brand": "Granite",
"category": "Footwear",
"price": 100.99,
"rating": 3.7,
"reviews": 523,
"in_stock": true,
"colors": [
"red",
"blue"
],
"tags": [
"summer",
"trail"
],
"description": "Warm boots with a grippy sole for rocky trails.",
"added": "2025-04-03",
"weight_g": 580
},
{
"id": 28,
"name": "Waterproof Hiking Boots",
"brand": "TrailCo",
"category": "Footwear",
"price": 118.99,
"rating": 3.8,
"reviews": 690,
"in_stock": true,
"colors": [
"blue"
],
"tags": [
"climbing",
"summer"
],
"description": "Ultralight boots with a grippy sole for rocky trails.",
"added": "2024-06-07",
"weight_g": 1049
},
{
"id": 29,
"name": "Swift Trail Running Shoes",
"brand": "TrailCo",
"category": "Footwear",
"price": 66.99,
"rating": 3.6,
"reviews": 99,
"in_stock": true,
"colors": [
"blue",
"grey"
],
"tags": [
"hiking",
"winter"
],
"description": "Comfortable, durable footwear with a cushioned sole for long walks.",
"added": "2025-03-17",
"weight_g": 1355
},
{
"id": 30,
"name": "Grip Trail Running Shoes",
"brand": "NorthPeak",
"category": "Footwear",
"price": 115.99,
"rating": 3.8,
"reviews": 97,
"in_stock": true,
"colors": [
"green"
],
"tags": [
"trail",
"winter"
],
"description": "Comfortable, ultralight footwear with a cushioned sole for long walks.",
"added": "2024-01-19",
"weight_g": 1087
},
{
"id": 31,
"name": "River Camp Sandals",
"brand": "Ridgeline",
"category": "Footwear",
"price": 101.99,
"rating": 4.9,
"reviews": 118,
"in_stock": true,
"colors": [
"black"
],
"tags": [
"backpacking",
"hiking"
],
"description": "Light, breathable shoes for fast hiking and trail running.",
"added": "2024-02-10",
"weight_g": 671
},
{
"id": 32,
"name": "Easy Camp Sandals",
"brand": "TrailCo",
"category": "Footwear",
"price": 90.99,
"rating": 4.7,
"reviews": 695,
"in_stock": true,
"colors": [
"orange",
"blue"
],
"tags": [
"trail",
"travel"
],
"description": "Light, durable shoes for fast hiking and trail running.",
"added": "2024-04-01",
"weight_g": 871
},
{
"id": 33,
"name": "Pocket Camping Stove",
"brand": "TrailCo",
"category": "Cooking",
"price": 28.99,
"rating": 4.8,
"reviews": 278,
"in_stock": true,
"colors": [
"black",
"green",
"yellow"
],
"tags": [
"camping",
"winter"
],
"description": "A durable kit, made for cooking on the trail.",
"added": "2024-05-04",
"weight_g": 554
},
{
"id": 34,
"name": "Twin Burner Camping Stove",
"brand": "Alpina",
"category": "Cooking",
"price": 20.99,
"rating": 4.3,
"reviews": 47,
"in_stock": true,
"colors": [
"black"
],
"tags": [
"backpacking",
"trail"
],
"description": "An ultralight stove that boils water fast at camp.",
"added": "2024-07-04",
"weight_g": 296
},
{
"id": 35,
"name": "Titanium Cook Set",
"brand": "Ridgeline",
"category": "Cooking",
"price": 168.99,
"rating": 4.6,
"reviews": 299,
"in_stock": true,
"colors": [
"blue",
"green",
"grey"
],
"tags": [
"backpacking",
"camping"
],
"description": "A breathable stove that boils water fast at camp.",
"added": "2024-01-16",
"weight_g": 108
},
{
"id": 36,
"name": "Nonstick Cook Set",
"brand": "Ridgeline",
"category": "Cooking",
"price": 136.99,
"rating": 4.1,
"reviews": 460,
"in_stock": false,
"colors": [
"orange",
"grey",
"yellow"
],
"tags": [
"climbing",
"summer"
],
"description": "A warm kit, made for cooking on the trail.",
"added": "2024-08-08",
"weight_g": 325
},
{
"id": 37,
"name": "Squeeze Water Filter",
"brand": "TrailCo",
"category": "Cooking",
"price": 73.99,
"rating": 5.0,
"reviews": 58,
"in_stock": true,
"colors": [
"black"
],
"tags": [
"summer",
"winter"
],
"description": "A durable stove that boils water fast at camp.",
"added": "2024-02-26",
"weight_g": 176
},
{
"id": 38,
"name": "Gravity Water Filter",
"brand": "Ridgeline",
"category": "Cooking",
"price": 125.99,
"rating": 4.4,
"reviews": 291,
"in_stock": true,
"colors": [
"green",
"black",
"orange"
],
"tags": [
"backpacking",
"hiking"
],
"description": "An ultralight kit, made for cooking on the trail.",
"added": "2025-04-01",
"weight_g": 93
},
{
"id": 39,
"name": "Bright 400 Headlamp",
"brand": "Ridgeline",
"category": "Lighting",
"price": 32.99,
"rating": 3.8,
"reviews": 38,
"in_stock": true,
"colors": [
"blue",
"green"
],
"tags": [
"backpacking",
"camping"
],
"description": "A bright, compact light that runs for hours on one charge.",
"added": "2025-01-25",
"weight_g": 102
},
{
"id": 40,
"name": "Mini 200 Headlamp",
"brand": "NorthPeak",
"category": "Lighting",
"price": 48.99,
"rating": 4.1,
"reviews": 8,
"in_stock": false,
"colors": [
"blue"
],
"tags": [
"climbing",
"summer"
],
"description": "A warm light for night hikes and setting up camp in the dark.",
"added": "2025-02-07",
"weight_g": 71
},
{
"id": 41,
"name": "Rechargeable 600 Headlamp",
"brand": "Summit",
"category": "Lighting",
"price": 35.99,
"rating": 4.3,
"reviews": 544,
"in_stock": true,
"colors": [
"grey"
],
"tags": [
"climbing",
"trail"
],
"description": "A bright, warm light that runs for hours on one charge.",
"added": "2025-05-21",
"weight_g": 136
},
{
"id": 42,
"name": "Solar Camp Lantern",
"brand": "Summit",
"category": "Lighting",
"price": 33.99,
"rating": 4.7,
"reviews": 735,
"in_stock": true,
"colors": [
"orange",
"grey",
"red"
],
"tags": [
"backpacking",
"summer"
],
"description": "A durable lamp that lights up the whole tent.",
"added": "2025-08-05",
"weight_g": 68
},
{
"id": 43,
"name": "LED Camp Lantern",
"brand": "Summit",
"category": "Lighting",
"price": 75.99,
"rating": 3.3,
"reviews": 139,
"in_stock": true,
"colors": [
"orange"
],
"tags": [
"summer",
"trail"
],
"description": "A warm light for night hikes and setting up camp in the dark.",
"added": "2025-10-04",
"weight_g": 69
},
{
"id": 44,
"name": "Sport Climbing Harness",
"brand": "TrailCo",
"category": "Climbing",
"price": 173.99,
"rating": 3.7,
"reviews": 470,
"in_stock": true,
"colors": [
"red",
"yellow",
"black"
],
"tags": [
"hiking",
"travel"
],
"description": "A warm choice for your first season of climbing.",
"added": "2025-04-30",
"weight_g": 1072
},
{
"id": 45,
"name": "Alpine Climbing Harness",
"brand": "NorthPeak",
"category": "Climbing",
"price": 215.99,
"rating": 4.5,
"reviews": 213,
"in_stock": true,
"colors": [
"orange",
"yellow",
"grey"
],
"tags": [
"hiking",
"winter"
],
"description": "A breathable choice for your first season of climbing.",
"added": "2024-10-21",
"weight_g": 3181
},
{
"id": 46,
"name": "Dynamic 60m Climbing Rope",
"brand": "Summit",
"category": "Climbing",
"price": 39.99,
"rating": 4.3,
"reviews": 342,
"in_stock": true,
"colors": [
"grey",
"green",
"red"
],
"tags": [
"backpacking",
"camping"
],
"description": "Strong, warm climbing gear, tested to safety standards.",
"added": "2024-03-03",
"weight_g": 2029
},
{
"id": 47,
"name": "Dry 70m Climbing Rope",
"brand": "Granite",
"category": "Climbing",
"price": 90.99,
"rating": 3.6,
"reviews": 504,
"in_stock": true,
"colors": [
"green",
"orange",
"grey"
],
"tags": [
"climbing",
"trail"
],
"description": "Lightweight gear for sport climbing and long alpine routes.",
"added": "2025-07-16",
"weight_g": 856
},
{
"id": 48,
"name": "Locking Carabiner Set",
"brand": "TrailCo",
"category": "Climbing",
"price": 100.99,
"rating": 3.2,
"reviews": 472,
"in_stock": false,
"colors": [
"orange",
"green",
"yellow"
],
"tags": [
"hiking",
"winter"
],
"description": "Lightweight gear for sport climbing and long alpine routes.",
"added": "2025-08-18",
"weight_g": 409
},
{
"id": 49,
"name": "Wiregate Carabiner Set",
"brand": "Alpina",
"category": "Climbing",
"price": 61.99,
"rating": 3.4,
"reviews": 842,
"in_stock": true,
"colors": [
"black",
"grey"
],
"tags": [
"hiking",
"travel"
],
"description": "Strong, breathable climbing gear, tested to safety standards.",
"added": "2025-05-12",
"weight_g": 1654
}
]

Summary

Dense vector fields store embeddings, with their dimensions and a similarity measure. Next, finding the nearest vectors with k NN.

Key takeaways
  • dense_vector
  • dims: 384
  • cosine
Next up: kNN Search — nearest neighbours