|
15 | 15 | from __future__ import annotations
|
16 | 16 |
|
17 | 17 | import logging
|
18 |
| -from typing import Literal, Optional |
| 18 | +from typing import List, Literal, Optional |
19 | 19 |
|
20 | 20 | import neo4j
|
21 | 21 | from pydantic import ValidationError
|
@@ -469,3 +469,108 @@ async def async_upsert_vector_on_relationship(
|
469 | 469 | raise Neo4jInsertionError(
|
470 | 470 | f"Upserting vector to Neo4j failed: {e.message}"
|
471 | 471 | ) from e
|
| 472 | + |
| 473 | + |
| 474 | +def _sort_by_index_name( |
| 475 | + records: List[neo4j.Record], index_name: str |
| 476 | +) -> List[neo4j.Record]: |
| 477 | + """ |
| 478 | + Sorts the provided list of dictionaries containing index information so |
| 479 | + that any item whose 'name' key matches the given 'index_name' appears at |
| 480 | + the front of the list. |
| 481 | +
|
| 482 | + Args: |
| 483 | + records (List[Dict[str, Any]]): The list of records containing index |
| 484 | + information to sort. |
| 485 | + index_name (str): The index name to match against the 'name' key of |
| 486 | + each dictionary. |
| 487 | +
|
| 488 | + Returns: |
| 489 | + List[Dict[str, Any]]: A newly sorted list with items matching |
| 490 | + 'index_name' placed first. |
| 491 | + """ |
| 492 | + return sorted(records, key=lambda x: x.get("name") != index_name) |
| 493 | + |
| 494 | + |
| 495 | +def retrieve_vector_index_info( |
| 496 | + driver: neo4j.Driver, index_name: str, label_or_type: str, embedding_property: str |
| 497 | +) -> Optional[neo4j.Record]: |
| 498 | + """ |
| 499 | + Check if a vector index exists in a Neo4j database and return its |
| 500 | + information. If no matching index is found, returns None. |
| 501 | +
|
| 502 | + Args: |
| 503 | + driver (neo4j.Driver): Neo4j Python driver instance. |
| 504 | + index_name (str): The name of the index to look up. |
| 505 | + label_or_type (str): The label (for nodes) or type (for relationships) |
| 506 | + of the index. |
| 507 | + embedding_property (str): The name of the property containing the |
| 508 | + embeddings. |
| 509 | +
|
| 510 | + Returns: |
| 511 | + Optional[Dict[str, Any]]: |
| 512 | + A dictionary containing the first matching index's information if found, |
| 513 | + or None otherwise. |
| 514 | + """ |
| 515 | + result = driver.execute_query( |
| 516 | + query_=( |
| 517 | + "SHOW INDEXES YIELD name, type, entityType, labelsOrTypes, " |
| 518 | + "properties, options WHERE type = 'VECTOR' AND (name = $index_name " |
| 519 | + "OR (labelsOrTypes[0] = $label_or_type AND " |
| 520 | + "properties[0] = $embedding_property)) " |
| 521 | + "RETURN name, type, entityType, labelsOrTypes, properties, options" |
| 522 | + ), |
| 523 | + parameters_={ |
| 524 | + "index_name": index_name, |
| 525 | + "label_or_type": label_or_type, |
| 526 | + "embedding_property": embedding_property, |
| 527 | + }, |
| 528 | + ) |
| 529 | + index_information = _sort_by_index_name(result.records, index_name) |
| 530 | + if len(index_information) > 0: |
| 531 | + return index_information[0] |
| 532 | + else: |
| 533 | + return None |
| 534 | + |
| 535 | + |
| 536 | +def retrieve_fulltext_index_info( |
| 537 | + driver: neo4j.Driver, |
| 538 | + index_name: str, |
| 539 | + label_or_type: str, |
| 540 | + text_properties: List[str] = [], |
| 541 | +) -> Optional[neo4j.Record]: |
| 542 | + """ |
| 543 | + Check if a full text index exists in a Neo4j database and return its |
| 544 | + information. If no matching index is found, returns None. |
| 545 | +
|
| 546 | + Args: |
| 547 | + driver (neo4j.Driver): Neo4j Python driver instance. |
| 548 | + index_name (str): The name of the index to look up. |
| 549 | + label_or_type (str): The label (for nodes) or type (for relationships) |
| 550 | + of the index. |
| 551 | + text_properties (List[str]): The names of the text properties indexed. |
| 552 | +
|
| 553 | + Returns: |
| 554 | + Optional[Dict[str, Any]]: |
| 555 | + A dictionary containing the first matching index's information if found, |
| 556 | + or None otherwise. |
| 557 | + """ |
| 558 | + result = driver.execute_query( |
| 559 | + query_=( |
| 560 | + "SHOW INDEXES YIELD name, type, entityType, labelsOrTypes, properties, options " |
| 561 | + "WHERE type = 'FULLTEXT' AND (name = $index_name " |
| 562 | + "OR (labelsOrTypes = [$label_or_type] AND " |
| 563 | + "properties = $text_properties)) " |
| 564 | + "RETURN name, type, entityType, labelsOrTypes, properties, options" |
| 565 | + ), |
| 566 | + parameters_={ |
| 567 | + "index_name": index_name, |
| 568 | + "label_or_type": label_or_type, |
| 569 | + "text_properties": text_properties, |
| 570 | + }, |
| 571 | + ) |
| 572 | + index_information = _sort_by_index_name(result.records, index_name) |
| 573 | + if len(index_information) > 0: |
| 574 | + return index_information[0] |
| 575 | + else: |
| 576 | + return None |
0 commit comments