Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Database - Constraints, Nodes and Relationships #22

Open
KazChe opened this issue Nov 11, 2024 · 0 comments
Open

Database - Constraints, Nodes and Relationships #22

KazChe opened this issue Nov 11, 2024 · 0 comments
Assignees
Labels

Comments

@KazChe
Copy link
Owner

KazChe commented Nov 11, 2024

(keep adjusting)

  1. Create Constraints and Indexes:
// Unique constraints
CREATE CONSTRAINT content_id IF NOT EXISTS
FOR (c:Content) REQUIRE c.id IS UNIQUE;

CREATE CONSTRAINT category_name IF NOT EXISTS
FOR (c:Category) REQUIRE c.name IS UNIQUE;

CREATE CONSTRAINT subcategory_name IF NOT EXISTS
FOR (s:SubCategory) REQUIRE s.name IS UNIQUE;

CREATE CONSTRAINT tag_name IF NOT EXISTS
FOR (t:Tag) REQUIRE t.name IS UNIQUE;

// Vector index for semantic search
CREATE VECTOR INDEX content_embedding IF NOT EXISTS
FOR (c:Content)
ON (c.textEmbedding)
OPTIONS {
  indexConfig: {
    `vector.dimensions`: 768,
    `vector.similarity_function`: 'cosine'
  }
};

2, Create Content with Relationships:

// Create or match existing category
MERGE (cat:Category {name: $categoryName})

// Create or match existing subcategory
WITH cat
MERGE (subcat:SubCategory {name: $subcategoryName})
MERGE (subcat)-[:CHILD_OF]->(cat)

// Create content node with embedding
WITH cat, subcat
CREATE (c:Content {
    id: randomUUID(),
    text: $text,
    textEmbedding: $embedding,
    createdAt: datetime(),
    updatedAt: datetime()
})
MERGE (c)-[:BELONGS_TO]->(cat)
MERGE (c)-[:SUBCATEGORIZED_AS]->(subcat)

// Create tags and relationships
WITH c
UNWIND $tags as tagName
MERGE (t:Tag {name: tagName})
MERGE (c)-[:TAGGED_WITH]->(t)
  1. Semantic Search Query to test with
// Find similar content using vector similarity
MATCH (c:Content)
WHERE c.textEmbedding IS NOT NULL
WITH c, vector.similarity(c.textEmbedding, $searchEmbedding) as score
WHERE score > 0.7  // Adjustable threshold
RETURN c.text, score, 
[(c)-[:BELONGS_TO]->(cat) | cat.name] as categories,
[(c)-[:TAGGED_WITH]->(t) | t.name] as tags
ORDER BY score DESC
LIMIT 5
@KazChe KazChe added the task label Nov 11, 2024
@KazChe KazChe self-assigned this Nov 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant