Skip to content

Database - Constraints, Nodes and Relationships  #22

@KazChe

Description

@KazChe

(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

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions