I'm working on a web project using the database neo4j. I want users to pick tags (separate nodes in the DB) and return content (also DB nodes) based on the tags selected.
I want to return to the user relevant tags based on the tags they've already selected.
The query I'm thinking of looks like this:
MATCH (tags:tag)-[:LINKED_WITH]-(content:content)-[:LINKED_WITH]-(previousTags:tag)
WHERE
//new tags must be connected to content that already selected tags are connected to
//for as many tags as the user has already selected
(previousTags.UID = {ID1} OR previousTags.UID = {ID2} OR previousTags.UID = {ID3})
//don't include tags that have already been selected in batch of new tags
//for as many tags as the user has already selected
AND NOT (tags.UID = {ID1} OR tags.UID = {ID2} OR tags.UID = {ID3})
RETURN tags.whatever
LIMIT 15
ODER BY tags.number_of_connections
Because the query depends on the number of tags the user has already selected, I have to use string building to write the query dynamically.
I have three questions about this:
Is there a way to accomplish this without string building?
Is cypher/neo4j meant to handle queries like this? (especially if the user has already selected 10-15 tags?)
Is there a better way to accomplish this?
Any insights would be greatly appreciated.
Remember to add an index for the tag.UID
.
create constraint on (t:tag) assert t.UID is unique
You can try to use previous.UID IN {selected_tags}
and pass an string array / collection but afaik that doesn't yet support index lookups. :(
So right now you'll have to stick to string building.
There is cypher-dsl for java and neo4j.rb also supports a cypher-dsl for ruby. Those might work for you, not sure if they support all you need for your query.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.