Handling Synonyms in Neo4j Full Text Search

When a user search for boat, it doesn't yield results containing yacht and ship, don't go any further, I explain everything you need to know in this blog post

3 Likes

Nice post indeed.
I used graph-based synonyms for one customer in the past. Basically in the graph we store (:Word)-[:HAS_SYNONYM]->(:Word). When resolving synonyms a cypher query resolves them. Eat your own graph food.

Yes, I go over that technique in my Nodes 19 talk : https://www.bigmarker.com/neo4j/full-text-search-tips-to-be-or-not-to-be?bmid=953a7644b875

Here are the queries used

// Show Synonyms
MATCH p=(n:Syn)-[:ALIAS]->(o) RETURN p

// How to build synonyms
WITH '"lucene"' AS query
UNWIND split(replace(query, "\"", ""), " ") AS token
MATCH (n:Syn {value: token})-[:ALIAS]-(a)
RETURN replace(query, token, a.value)


// Generate synonyms query
WITH '"lucene"' AS query
UNWIND split(replace(query, "\"", ""), " ") AS token
MATCH (n:Syn {value: token})-[:ALIAS]-(a)
WITH collect(replace(query, token, a.value)) + query AS coll
WITH [x IN coll | "(" + x + ")"] AS coll
RETURN apoc.text.join(coll, " OR ")

// Make search with generated synonyms query
WITH '"lucene"' AS query
UNWIND split(replace(query, "\"", ""), " ") AS token
MATCH (n:Syn {value: token})-[:ALIAS]-(a)
WITH collect(replace(query, token, a.value)) + query AS coll
WITH [x IN coll | "(" + x + ")"] AS coll
WITH apoc.text.join(coll, " OR ") AS finalQuery
CALL db.index.fulltext.queryNodes('nodes19', 'topic: ' + finalQuery)
YIELD node, score
RETURN node, score
1 Like

Thanks for submitting!

I've added a tag that allows your blog to be displayed on the community home page!

1 Like