Neo4j 在组合语句上进行 Union 之后排序

问题描述 投票:0回答:1

我有两个 neo4j 密码查询。

一个:

MATCH (u:ClientUser {name: 'Karthick J [[email protected]]'})--(b)-[:BRAND_TAG_RELATESTOTHETOPICTAG|BUSINESSAREA_TAG_RELATESTOTHETOPICTAG|MARKET_TAG_RELATESTOTHETOPICTAG|SKILL_TAG_HASTHERELATEDTOPICTAG|INTEREST_TAG_HASTHERELATEDTOPICTAG]->(t:Tag)<-[:CONTENTPIECE_TAG_HASTHERELATEDCONTENTKEYWORD]-(p:ContentPiece)
WHERE NOT (u)-[:CLIENTUSER_CONTENTPIECE_ISTHEAUTHORFORTHECONTENTPIECE]-(p)
WITH u, b, p, t
ORDER BY p.publishedDate DESC
WITH u, p, collect(DISTINCT b.name) AS becauseYouWorkOn, collect(DISTINCT t.name) AS hasTag
RETURN DISTINCT{
becauseYouWorkOn: becauseYouWorkOn, 
recommendedArticleTitle: p.marcelDisplayName, 
recommendedArticleId: p.contentId,
publishDate: p.publishedDate,
hasTag: hasTag,
contentType: labels(p)
} AS result

二:

MATCH (u:ClientUser{name: 'Karthick J [[email protected]]'})-[:CLIENTUSER_BRAND_WORKSONTHEBRAND|CLIENTUSER_BUSINESSAREA_WORKSINTHEBUSINESSAREA|CLIENTUSER_MARKET_SPECIALIZESINTHEMARKET]->(b)<-[:POST_BUSINESSAREA_ISASSOCIATEDWITHTHEBUSINESSAREA|POST_BRAND_ISASSOCIATEDWITHTHEBRAND|POST_BRAND_ISASSOCIATEDWITHTHEADDITIONALBRANDS|POST_MARKET_ISASSOCIATEDWITHTHEMARKET]-(n)
WHERE NOT (u)-[:CLIENTUSER_POST_HASPUBLISHEDTHEPOST]->(n)
WITH u, b, n
ORDER BY n.publishedDate desc
WITH u, n, collect(DISTINCT b.name) as becauseYouWorkOn, collect(DISTINCT b.name) as hasTag
RETURN DISTINCT{
becauseYouWorkOn: becauseYouWorkOn, 
recommendedArticleTitle: n.title, 
recommendedArticleId: n.postId,
publishDate: n.publishedDate,
hasTag: hasTag,
contentType: labels(n)
} AS result

我想合并两个查询返回一个统一的列表,并且合并后的列表需要根据publishedDate进行排序。 我写的查询是:

// Query for ContentPiece
MATCH (u:ClientUser {name: 'Karthick J [[email protected]]'})--(b)-[:BRAND_TAG_RELATESTOTHETOPICTAG|BUSINESSAREA_TAG_RELATESTOTHETOPICTAG|MARKET_TAG_RELATESTOTHETOPICTAG|SKILL_TAG_HASTHERELATEDTOPICTAG|INTEREST_TAG_HASTHERELATEDTOPICTAG]->(t:Tag)<-[:CONTENTPIECE_TAG_HASTHERELATEDCONTENTKEYWORD]-(p:ContentPiece)
WHERE NOT (u)-[:CLIENTUSER_CONTENTPIECE_ISTHEAUTHORFORTHECONTENTPIECE]-(p)
WITH u, b, p, t
ORDER BY p.publishedDate DESC
WITH u, p, collect(DISTINCT b.name) AS becauseYouWorkOn, collect(DISTINCT t.name) AS hasTag, p.publishedDate AS date
RETURN {
  becauseYouWorkOn: becauseYouWorkOn, 
  recommendedArticleTitle: p.marcelDisplayName, 
  recommendedArticleId: p.contentId,
  publishDate: date,
  hasTag: hasTag,
  contentType: labels(p)
} AS result

UNION

// Query for Posts
MATCH (u:ClientUser {name: 'Karthick J [[email protected]]'})-[:CLIENTUSER_BRAND_WORKSONTHEBRAND|CLIENTUSER_BUSINESSAREA_WORKSINTHEBUSINESSAREA|CLIENTUSER_MARKET_SPECIALIZESINTHEMARKET]->(b)<-[:POST_BUSINESSAREA_ISASSOCIATEDWITHTHEBUSINESSAREA|POST_BRAND_ISASSOCIATEDWITHTHEBRAND|POST_BRAND_ISASSOCIATEDWITHTHEADDITIONALBRANDS|POST_MARKET_ISASSOCIATEDWITHTHEMARKET]-(n)
WHERE NOT (u)-[:CLIENTUSER_POST_HASPUBLISHEDTHEPOST]->(n)
WITH u, b, n
ORDER BY n.publishedDate DESC
WITH u, n, collect(DISTINCT b.name) AS becauseYouWorkOn, collect(DISTINCT b.name) AS hasTag, n.publishedDate AS date
RETURN {
  becauseYouWorkOn: becauseYouWorkOn, 
  recommendedArticleTitle: n.title, 
  recommendedArticleId: n.postId,
  publishDate: date,
  hasTag: hasTag,
  contentType: labels(n)
} AS result
ORDER BY result.publishDate DESC

上述查询的问题是,由于这是一个联合,它首先对第一个列表进行排序,然后对联合后的列表进行最后排序。 我想要做的是对组合列表、Post 和 ContentPiece 组合进行排序,然后按publishedDate 排序。非常感谢任何帮助。

neo4j graphql cypher neo4j-apoc neo4jclient
1个回答
0
投票

您可以使用CALL子查询来执行联合后处理

例如:

CALL {
    // Query for ContentPiece
    MATCH (u:ClientUser {name: 'Karthick J [[email protected]]'})--(b)-[:BRAND_TAG_RELATESTOTHETOPICTAG|BUSINESSAREA_TAG_RELATESTOTHETOPICTAG|MARKET_TAG_RELATESTOTHETOPICTAG|SKILL_TAG_HASTHERELATEDTOPICTAG|INTEREST_TAG_HASTHERELATEDTOPICTAG]->(t:Tag)<-[:CONTENTPIECE_TAG_HASTHERELATEDCONTENTKEYWORD]-(p:ContentPiece)
    WHERE NOT (u)-[:CLIENTUSER_CONTENTPIECE_ISTHEAUTHORFORTHECONTENTPIECE]-(p)
    WITH u, b, p, t
    ORDER BY p.publishedDate DESC
    WITH u, p, collect(DISTINCT b.name) AS becauseYouWorkOn, collect(DISTINCT t.name) AS hasTag, p.publishedDate AS date
    RETURN {
    becauseYouWorkOn: becauseYouWorkOn, 
    recommendedArticleTitle: p.marcelDisplayName, 
    recommendedArticleId: p.contentId,
    publishDate: date,
    hasTag: hasTag,
    contentType: labels(p)
    } AS result

    UNION

    // Query for Posts
    MATCH (u:ClientUser {name: 'Karthick J [[email protected]]'})-[:CLIENTUSER_BRAND_WORKSONTHEBRAND|CLIENTUSER_BUSINESSAREA_WORKSINTHEBUSINESSAREA|CLIENTUSER_MARKET_SPECIALIZESINTHEMARKET]->(b)<-[:POST_BUSINESSAREA_ISASSOCIATEDWITHTHEBUSINESSAREA|POST_BRAND_ISASSOCIATEDWITHTHEBRAND|POST_BRAND_ISASSOCIATEDWITHTHEADDITIONALBRANDS|POST_MARKET_ISASSOCIATEDWITHTHEMARKET]-(n)
    WHERE NOT (u)-[:CLIENTUSER_POST_HASPUBLISHEDTHEPOST]->(n)
    WITH u, b, n
    ORDER BY n.publishedDate DESC
    WITH u, n, collect(DISTINCT b.name) AS becauseYouWorkOn, collect(DISTINCT b.name) AS hasTag, n.publishedDate AS date
    RETURN {
    becauseYouWorkOn: becauseYouWorkOn, 
    recommendedArticleTitle: n.title, 
    recommendedArticleId: n.postId,
    publishDate: date,
    hasTag: hasTag,
    contentType: labels(n)
    } AS result
}
RETURN result
ORDER BY result.publishDate DESC
© www.soinside.com 2019 - 2024. All rights reserved.