Hello everyone, I'm pretty new to Neo4j, thank you in advance for any help
Let me try to explain my problem here:
I have stories that have many tags, each tag has one element (an element might be a company, location, ...)
then I have newsletters, each newsletter has many interests, and every interest have one or more (has_many) elements
the element has newsletters and tags end of relationship mapped
to build a newsletter with simple interests (simple means each interest has only one element), the performance right now is really fast and the query is pretty easy:
newsletter.interests.element.tags.story.as(:s).where('s.published_date > ?', Date.yesterday) works fine and fast
(it maps to the graph query bellow)
Gbrief#interests#elements#tags#story
MATCH (gbrief363386)
WHERE (ID(gbrief363386) = {ID_gbrief363386})
MATCH (gbrief363386)-[rel1:`interested_in`]->(node3:`Ginterest`)
MATCH (node3)-[rel2:`interested`]->(node4:`Gelement`)
MATCH (node4)<-[rel3:`element_tag`]-(node5:`GstoryTag`)
MATCH (node5)-[rel4:`storytag`]->(s:`Gstory`)
WHERE (s.published_date > {question_mark_param})
RETURN s | {:question_mark_param=>Tue, 12 Nov 2019 21:14:45 UTC +00:00, :ID_gbrief363386=>363386}
but that is not good enough, if an interest has more than one element, all elements must match to the tagged elements to match a story
for example, one interest has elements A and C
storyA is tagged with A and B
storyB is tagged with A, B and C
only storyB should match that interest
and I'm using this code:
newsletter.interests.query_as(:int).match('(int)-[intelem:interested
]->(elem:Gelement
)').with('int,collect(distinct elem) as ielements').match('(elem)<-[rel3:element_tag
]-(node5:GstoryTag
),(node5)-[rel4:storytag
]->(s:Gstory
),(s)<-[rel5:storytag
]-(node7:GstoryTag
),(node7)-[rel6:element_tag
]->(selem:Gelement
)').with('ielements, s, collect(distinct selem) as selements').where('all(e in ielements where e in selements)').where('s.published_date > ?', dt).pluck(:s)
(it maps to the graph query bellow)
Gbrief#interests
MATCH (gbrief363386)
WHERE (ID(gbrief363386) = {ID_gbrief363386})
MATCH
(gbrief363386)-[rel1:`interested_in`]->(int:`Ginterest`),
(int)-[intelem:`interested`]->(elem:`Gelement`)
WITH int,collect(distinct elem) as ielements
MATCH (elem)<-[rel3:`element_tag`]-(node5:`GstoryTag`),(node5)-[rel4:`storytag`]->(s:`Gstory`),(s)<-[rel5:`storytag`]-(node7:`GstoryTag`),(node7)-[rel6:`element_tag`]->(selem:`Gelement`)
WITH ielements, s, collect(distinct selem) as selements
WHERE
(all(e in ielements where e in selements)) AND
(s.published_date > {question_mark_param})
RETURN s | {:question_mark_param=>Tue, 12 Nov 2019 21:14:45 UTC +00:00, :ID_gbrief363386=>363386}
and this is really, really slow
any ideas on how I can improve this? changing the mapping or the query will work
Thank youvery much