Elasticsearch
ElasticsearchStore #
Bases: BasePydanticVectorStore
Elasticsearch vector store.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
index_name |
str
|
Name of the Elasticsearch index. |
required |
es_client |
Optional[Any]
|
Optional. Pre-existing AsyncElasticsearch client. |
None
|
es_url |
Optional[str]
|
Optional. Elasticsearch URL. |
None
|
es_cloud_id |
Optional[str]
|
Optional. Elasticsearch cloud ID. |
None
|
es_api_key |
Optional[str]
|
Optional. Elasticsearch API key. |
None
|
es_user |
Optional[str]
|
Optional. Elasticsearch username. |
None
|
es_password |
Optional[str]
|
Optional. Elasticsearch password. |
None
|
text_field |
str
|
Optional. Name of the Elasticsearch field that stores the text. |
'content'
|
vector_field |
str
|
Optional. Name of the Elasticsearch field that stores the embedding. |
'embedding'
|
batch_size |
int
|
Optional. Batch size for bulk indexing. Defaults to 200. |
200
|
distance_strategy |
Optional[DISTANCE_STRATEGIES]
|
Optional. Distance strategy to use for similarity search. Defaults to "COSINE". |
'COSINE'
|
retrieval_strategy |
Optional[AsyncRetrievalStrategy]
|
Retrieval strategy to use. AsyncBM25Strategy / AsyncSparseVectorStrategy / AsyncDenseVectorStrategy / AsyncRetrievalStrategy. Defaults to AsyncDenseVectorStrategy. |
None
|
Raises:
Type | Description |
---|---|
ConnectionError
|
If AsyncElasticsearch client cannot connect to Elasticsearch. |
ValueError
|
If neither es_client nor es_url nor es_cloud_id is provided. |
Examples:
pip install llama-index-vector-stores-elasticsearch
from llama_index.vector_stores import ElasticsearchStore
# Additional setup for ElasticsearchStore class
index_name = "my_index"
es_url = "http://localhost:9200"
es_cloud_id = "<cloud-id>" # Found within the deployment page
es_user = "elastic"
es_password = "<password>" # Provided when creating deployment or can be reset
es_api_key = "<api-key>" # Create an API key within Kibana (Security -> API Keys)
# Connecting to ElasticsearchStore locally
es_local = ElasticsearchStore(
index_name=index_name,
es_url=es_url,
)
# Connecting to Elastic Cloud with username and password
es_cloud_user_pass = ElasticsearchStore(
index_name=index_name,
es_cloud_id=es_cloud_id,
es_user=es_user,
es_password=es_password,
)
# Connecting to Elastic Cloud with API Key
es_cloud_api_key = ElasticsearchStore(
index_name=index_name,
es_cloud_id=es_cloud_id,
es_api_key=es_api_key,
)
Source code in llama-index-integrations/vector_stores/llama-index-vector-stores-elasticsearch/llama_index/vector_stores/elasticsearch/base.py
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 |
|
add #
add(nodes: List[BaseNode], *, create_index_if_not_exists: bool = True, **add_kwargs: Any) -> List[str]
Add nodes to Elasticsearch index.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
nodes |
List[BaseNode]
|
List of nodes with embeddings. |
required |
create_index_if_not_exists |
bool
|
Optional. Whether to create the Elasticsearch index if it doesn't already exist. Defaults to True. |
True
|
Returns:
Type | Description |
---|---|
List[str]
|
List of node IDs that were added to the index. |
Raises:
Type | Description |
---|---|
ImportError
|
If elasticsearch['async'] python package is not installed. |
BulkIndexError
|
If AsyncElasticsearch async_bulk indexing fails. |
Source code in llama-index-integrations/vector_stores/llama-index-vector-stores-elasticsearch/llama_index/vector_stores/elasticsearch/base.py
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 |
|
async_add
async
#
async_add(nodes: List[BaseNode], *, create_index_if_not_exists: bool = True, **add_kwargs: Any) -> List[str]
Asynchronous method to add nodes to Elasticsearch index.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
nodes |
List[BaseNode]
|
List of nodes with embeddings. |
required |
create_index_if_not_exists |
bool
|
Optional. Whether to create the AsyncElasticsearch index if it doesn't already exist. Defaults to True. |
True
|
Returns:
Type | Description |
---|---|
List[str]
|
List of node IDs that were added to the index. |
Raises:
Type | Description |
---|---|
ImportError
|
If elasticsearch python package is not installed. |
BulkIndexError
|
If AsyncElasticsearch async_bulk indexing fails. |
Source code in llama-index-integrations/vector_stores/llama-index-vector-stores-elasticsearch/llama_index/vector_stores/elasticsearch/base.py
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 |
|
delete #
delete(ref_doc_id: str, **delete_kwargs: Any) -> None
Delete node from Elasticsearch index.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ref_doc_id |
str
|
ID of the node to delete. |
required |
delete_kwargs |
Any
|
Optional. Additional arguments to pass to Elasticsearch delete_by_query. |
{}
|
Raises:
Type | Description |
---|---|
Exception
|
If Elasticsearch delete_by_query fails. |
Source code in llama-index-integrations/vector_stores/llama-index-vector-stores-elasticsearch/llama_index/vector_stores/elasticsearch/base.py
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 |
|
adelete
async
#
adelete(ref_doc_id: str, **delete_kwargs: Any) -> None
Async delete node from Elasticsearch index.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ref_doc_id |
str
|
ID of the node to delete. |
required |
delete_kwargs |
Any
|
Optional. Additional arguments to pass to AsyncElasticsearch delete_by_query. |
{}
|
Raises:
Type | Description |
---|---|
Exception
|
If AsyncElasticsearch delete_by_query fails. |
Source code in llama-index-integrations/vector_stores/llama-index-vector-stores-elasticsearch/llama_index/vector_stores/elasticsearch/base.py
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 |
|
query #
query(query: VectorStoreQuery, custom_query: Optional[Callable[[Dict, Union[VectorStoreQuery, None]], Dict]] = None, es_filter: Optional[List[Dict]] = None, **kwargs: Any) -> VectorStoreQueryResult
Query index for top k most similar nodes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
query_embedding |
List[float]
|
query embedding |
required |
custom_query |
Optional[Callable[[Dict, Union[VectorStoreQuery, None]], Dict]]
|
Optional. custom query function that takes in the es query body and returns a modified query body. This can be used to add additional query parameters to the Elasticsearch query. |
None
|
es_filter |
Optional[List[Dict]]
|
Optional. Elasticsearch filter to apply to the query. If filter is provided in the query, this filter will be ignored. |
None
|
Returns:
Name | Type | Description |
---|---|---|
VectorStoreQueryResult |
VectorStoreQueryResult
|
Result of the query. |
Raises:
Type | Description |
---|---|
Exception
|
If Elasticsearch query fails. |
Source code in llama-index-integrations/vector_stores/llama-index-vector-stores-elasticsearch/llama_index/vector_stores/elasticsearch/base.py
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 |
|
aquery
async
#
aquery(query: VectorStoreQuery, custom_query: Optional[Callable[[Dict, Union[VectorStoreQuery, None]], Dict]] = None, es_filter: Optional[List[Dict]] = None, **kwargs: Any) -> VectorStoreQueryResult
Asynchronous query index for top k most similar nodes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
query_embedding |
VectorStoreQuery
|
query embedding |
required |
custom_query |
Optional[Callable[[Dict, Union[VectorStoreQuery, None]], Dict]]
|
Optional. custom query function that takes in the es query body and returns a modified query body. This can be used to add additional query parameters to the AsyncElasticsearch query. |
None
|
es_filter |
Optional[List[Dict]]
|
Optional. AsyncElasticsearch filter to apply to the query. If filter is provided in the query, this filter will be ignored. |
None
|
Returns:
Name | Type | Description |
---|---|---|
VectorStoreQueryResult |
VectorStoreQueryResult
|
Result of the query. |
Raises:
Type | Description |
---|---|
Exception
|
If AsyncElasticsearch query fails. |
Source code in llama-index-integrations/vector_stores/llama-index-vector-stores-elasticsearch/llama_index/vector_stores/elasticsearch/base.py
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 |
|