Search code examples
neo4jcypherneo4jclient

Neo4j Query returns multiple records after first time


I'm using Neo4j Client with ASP.NET MVC, I have this query that shows the user events within a range of dates, when i run this query for the first time it returns the events correctly but when i call my ajax to run the event again query gives me the twice as much records as the first time. I've traced the query line to line but everything seems to be working fine, i have to say I'm using Union for the first time could look at my query and tell me if something is wrong?

var q = new CypherFluentQuery(client) as ICypherFluentQuery;
q = q.Match("(y:Year)--(m:Month)--(d:Day)--(h:Hour)--(sr:ServiceRequest)-[" + Relations.EXPERT_ASSIGN_TO_SQ.ToString() + "]-(u:User)")
     .Where("y.value >= {param}").WithParam("param", dt1.Year)
     .AndWhere("m.value >= {param2}").WithParam("param2", dt1.Month)
     .AndWhere("d.value >= {param4}").WithParam("param4", dt1.Day)
     .AndWhere("u.Id = {param10}").WithParam("param10", Id)
     .With("y,m,d,h,sr,u")
     .Match("(sr)-[:" + Relations.SC_HAS_SQ.ToString() + "]-(c2:ServiceCategory)-[:" + Relations.SC_IS_SC_CHILD.ToString() + "*]-(c3:ServiceCategory)")
     .ReturnDistinct((y, m, d, h, sr, c2, c3) => new
     {
         Year = y.As<RootTree>().value,
         Month = m.As<RootTree>().value,
         Day = d.As<RootTree>().value,
         Hour = h.As<RootTree>().value,
         serviceRequest = sr.As<ServiceRequest>(),
         ServiceCategory = c2.As<OnDemandService>(),
         ServiceParent = c3.As<OnDemandService>()

     })
     .UnionAll()
     .Match("(yy:Year)--(mm:Month)--(dd:Day)--(hh:Hour)--(sr2:ServiceRequest)-[" + Relations.EXPERT_ASSIGN_TO_SQ.ToString() + "]-(u)")
     .Where("yy.value <= {param7}").WithParam("param7", dt2.Year)
     .AndWhere("mm.value >= {param8}").WithParam("param8", dt2.Month)
     .AndWhere("dd.value >= {param9}").WithParam("param9", dt2.Day)
     .With("yy,mm,dd,hh,sr2,u")
     .Match("(sr2)-[:" + Relations.SC_HAS_SQ.ToString() + "]-(c2:ServiceCategory)-[:" + Relations.SC_IS_SC_CHILD.ToString() + "*]-(c3:ServiceCategory)");

var query = q.ReturnDistinct((yy, mm, dd, hh, sr2, c2, c3) => new
{
    Year = yy.As<RootTree>().value,
    Month = mm.As<RootTree>().value,
    Day = dd.As<RootTree>().value,
    Hour = hh.As<RootTree>().value,
    serviceRequest = sr2.As<ServiceRequest>(),
    ServiceCategory = c2.As<OnDemandService>(),
    ServiceParent = c3.As<OnDemandService>()
}).Results.ToList();

The Traced Query:

MATCH (y:Year)--(m:Month)--(d:Day)--(h:Hour)--(sr:ServiceRequest)-[EXPERT_ASSIGN_TO_SQ]-(u:User)
WHERE y.value >= 2017
AND m.value >= 5
AND d.value >= 16
AND u.Id = "c0dc7cbe-5626-4012-a5ea-72c1cfce2461"
WITH y,m,d,h,sr,u
MATCH (sr)-[:SC_HAS_SQ]-(c2:ServiceCategory)-[:SC_IS_SC_CHILD*]-(c3:ServiceCategory)
RETURN distinct y.value AS Year, m.value AS Month, d.value AS Day, h.value AS Hour, sr AS serviceRequest, c2 AS ServiceCategory, c3 AS ServiceParent
UNION ALL
MATCH (yy:Year)--(mm:Month)--(dd:Day)--(hh:Hour)--(sr2:ServiceRequest)-[EXPERT_ASSIGN_TO_SQ]-(u)
WHERE yy.value <= 2017
AND mm.value >= 5
AND dd.value >= 9
WITH yy,mm,dd,hh,sr2,u
MATCH (sr2)-[:SC_HAS_SQ]-(c2:ServiceCategory)-[:SC_IS_SC_CHILD*]-(c3:ServiceCategory)
RETURN distinct yy.value AS Year, mm.value AS Month, dd.value AS Day, hh.value AS Hour, sr2 AS serviceRequest, c2 AS ServiceCategory, c3 AS ServiceParent

Update

I've ran the query on neo4j itself for multiple times it returns correctly, this only happens when query runs within my application using neo4jClient


Solution

  • It was quit simple actually it was because i used union all i changed it to union and it worked.