cancel
Showing results for 
Search instead for 
Did you mean: 

How to return two results with one query

paolodipietro58
Graph Voyager

Neo4j Community Server 4.3.3 on Ubuntu 20.04

I have a question I cannot be able to give an answer.

I have the following query:

 MATCH (r:Recipe {uuid: '76e9cc6e-fb54-4678-b752-3f420a993653'})<-[d:CAN_BE_SUGGESTED_FOR]-(mention:Mention)
     OPTIONAL MATCH (mention)-[:IS_A]-(productType:ProductType)
     WITH d, mention, productType
        ORDER BY d.distance
    WITH d,mention,productType, collect(distinct productType.name) as PTN

return mention.name, productType.name,  PTN 

With this approach I have the following results, which is not what I need

 {
    "mention.name": "Santa Maddalena",
    "productType.name": "Fermo Rosso",
    "PTN": [
      "Fermo Rosso"
    ]
  },
  {
    "mention.name": "Classico",
    "productType.name": "Fermo Rosso",
    "PTN": [
      "Fermo Rosso"
    ]
  },
 
  {
    "mention.name": "Classico Chiaretto",
    "productType.name": "Fermo Rosato",
    "PTN": [
      "Fermo Rosato"
    ]
  },
  {
    "mention.name": "Nebbiolo(Spanna)",
    "productType.name": "Fermo Rosso",
    "PTN": [
      "Fermo Rosso"
    ]
  },
 {
    "mention.name": "Frizzante",
    "productType.name": "Mosso Rosso"
    "PTN": [
      "Mosso Rosso"
    ]
  }

What I need, Iis to have one result containing the following two sub Items:

The first:

 {
    "mention.name": "Santa Maddalena",
    "productType.name": "Fermo Rosso"
  },
  {
    "mention.name": "Classico",
    "productType.name": "Fermo Rosso"
  },
 
  {
    "mention.name": "Classico Chiaretto",
    "productType.name": "Fermo Rosato",
  },
  {
    "mention.name": "Nebbiolo(Spanna)",
    "productType.name": "Fermo Rosso"
  },
 {
    "mention.name": "Frizzante",
    "productType.name": "Mosso Rosso"
  ]

And the second

    "PTN": [
      "Fermo Rosso","Fermo Rosato","Mosso Rosso"
    ]

where the order of the PTN content is the same as in the first query result.

I hope to have been able to explain the problem.

Thank you

1 ACCEPTED SOLUTION

So you need a list of objects containing the mention.name and the productType.name, and separately a list for PTN? In that case, you will need to perform 2 collect() aggregations simultaneously, and also clear out any unnecessary grouping keys when you perform the aggregation.

Maybe something like this?

MATCH (r:Recipe {uuid: '76e9cc6e-fb54-4678-b752-3f420a993653'})<-[d:CAN_BE_SUGGESTED_FOR]-(mention:Mention)
OPTIONAL MATCH (mention)-[:IS_A]-(productType:ProductType)
WITH d, mention, productType
ORDER BY d.distance
WITH collect(mention {.name, `productType.name`:productType.name}) as mentions, collect(distinct productType.name) as PTN

RETURN mentions, PTN 

View solution in original post

1 REPLY 1

So you need a list of objects containing the mention.name and the productType.name, and separately a list for PTN? In that case, you will need to perform 2 collect() aggregations simultaneously, and also clear out any unnecessary grouping keys when you perform the aggregation.

Maybe something like this?

MATCH (r:Recipe {uuid: '76e9cc6e-fb54-4678-b752-3f420a993653'})<-[d:CAN_BE_SUGGESTED_FOR]-(mention:Mention)
OPTIONAL MATCH (mention)-[:IS_A]-(productType:ProductType)
WITH d, mention, productType
ORDER BY d.distance
WITH collect(mention {.name, `productType.name`:productType.name}) as mentions, collect(distinct productType.name) as PTN

RETURN mentions, PTN