Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/core_docs/docs/integrations/vectorstores/neo4jvector.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ import ExistingGraphExample from "@examples/indexes/vector_stores/neo4j_vector/n

<CodeBlock language="typescript">{ExistingGraphExample}</CodeBlock>

### Metadata filtering

import MetadataExample from "@examples/indexes/vector_stores/neo4j_vector/neo4j_vector_metadata.ts";

<CodeBlock language="typescript">{MetadataExample}</CodeBlock>

# Disclaimer ⚠️

_Security note_: Make sure that the database connection uses credentials
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { OpenAIEmbeddings } from "@langchain/openai";
import { Neo4jVectorStore } from "@langchain/community/vectorstores/neo4j_vector";

/**
* `similaritySearch` Method with Metadata Filtering:
*
* Description:
* This method facilitates advanced similarity searches within a Neo4j vector index, leveraging both text embeddings and metadata attributes.
* The third parameter, `filter`, allows for the specification of metadata-based conditions that pre-filter the nodes before performing the similarity search.
* This approach enhances the search precision by allowing users to query based on complex metadata criteria alongside textual similarity.
* Metadata filtering also support the following operators:
*
* $eq: Equal
* $ne: Not Equal
* $lt: Less than
* $lte: Less than or equal
* $gt: Greater than
* $gte: Greater than or equal
* $in: In a list of values
* $nin: Not in a list of values
* $between: Between two values
* $like: Text contains value
* $ilike: lowered text contains value
*
* The filter supports a range of query operations such as equality checks, range queries, and compound conditions (using logical operators like $and, $or).
* This makes it highly adaptable to varied use cases requiring detailed and specific retrieval of documents based on both content and contextual information.
*
* Note:
* Effective use of this method requires a well-structured Neo4j database where nodes are enriched with both text and metadata properties.
* The method is particularly useful in scenarios where the integration of text analysis with detailed metadata querying is crucial, such as in content recommendation systems, detailed archival searches, or any application where contextual relevance is key.
*/

// Configuration object for Neo4j connection and other related settings
const config = {
url: "bolt://localhost:7687", // URL for the Neo4j instance
username: "neo4j", // Username for Neo4j authentication
password: "pleaseletmein", // Password for Neo4j authentication
indexName: "vector", // Name of the vector index
keywordIndexName: "keyword", // Name of the keyword index if using hybrid search
searchType: "vector" as const, // Type of search (e.g., vector, hybrid)
nodeLabel: "Chunk", // Label for the nodes in the graph
textNodeProperty: "text", // Property of the node containing text
embeddingNodeProperty: "embedding", // Property of the node containing embedding
};

const documents = [
{ pageContent: "what's this", metadata: { a: 2 } },
{ pageContent: "Cat drinks milk", metadata: { a: 1 } },
];

const neo4jVectorIndex = await Neo4jVectorStore.fromDocuments(
documents,
new OpenAIEmbeddings(),
config
);

const filter = { a: { $eq: 1 } };
const results = await neo4jVectorIndex.similaritySearch("water", 1, { filter });

console.log(results);

/*
[ Document { pageContent: 'Cat drinks milk', metadata: { a: 1 } } ]
*/

await neo4jVectorIndex.close();