match nodes on property, and include relationships between those nodes in the result












0















I have the following graph model to represent a microservice architecture



(:Team {space})-[:OPEX]->(:Service)-[:USES]->[:Service]




  • a Team belongs to a space (a department)

  • a Team has operation ownership (=OPEX) over several Services

  • Services have USES relationships between them


My query in natural language:




  • find all the Teams in a certain space, and via the OPEX relationship find all their Services

  • also include all USES relationships between these services in the result


I'm having problems expressing this query in an elegant Cypher query. I came up with the following, but there must be an easier and more natural way to express it.



MATCH (t1:Team {space:"shopping"})-[o1:OPEX]->(s1:Service),
(t2:Team {space:"shopping"})-[o2:OPEX]->(s2:Service),
(s3:Service)-[u:USES]->(s4:Service)
WHERE s3.name=s1.name AND s4.name=s2.name
RETURN t1, o1, s1, u, s2, o2, t2


Can someone point me into a better direction?










share|improve this question





























    0















    I have the following graph model to represent a microservice architecture



    (:Team {space})-[:OPEX]->(:Service)-[:USES]->[:Service]




    • a Team belongs to a space (a department)

    • a Team has operation ownership (=OPEX) over several Services

    • Services have USES relationships between them


    My query in natural language:




    • find all the Teams in a certain space, and via the OPEX relationship find all their Services

    • also include all USES relationships between these services in the result


    I'm having problems expressing this query in an elegant Cypher query. I came up with the following, but there must be an easier and more natural way to express it.



    MATCH (t1:Team {space:"shopping"})-[o1:OPEX]->(s1:Service),
    (t2:Team {space:"shopping"})-[o2:OPEX]->(s2:Service),
    (s3:Service)-[u:USES]->(s4:Service)
    WHERE s3.name=s1.name AND s4.name=s2.name
    RETURN t1, o1, s1, u, s2, o2, t2


    Can someone point me into a better direction?










    share|improve this question



























      0












      0








      0








      I have the following graph model to represent a microservice architecture



      (:Team {space})-[:OPEX]->(:Service)-[:USES]->[:Service]




      • a Team belongs to a space (a department)

      • a Team has operation ownership (=OPEX) over several Services

      • Services have USES relationships between them


      My query in natural language:




      • find all the Teams in a certain space, and via the OPEX relationship find all their Services

      • also include all USES relationships between these services in the result


      I'm having problems expressing this query in an elegant Cypher query. I came up with the following, but there must be an easier and more natural way to express it.



      MATCH (t1:Team {space:"shopping"})-[o1:OPEX]->(s1:Service),
      (t2:Team {space:"shopping"})-[o2:OPEX]->(s2:Service),
      (s3:Service)-[u:USES]->(s4:Service)
      WHERE s3.name=s1.name AND s4.name=s2.name
      RETURN t1, o1, s1, u, s2, o2, t2


      Can someone point me into a better direction?










      share|improve this question
















      I have the following graph model to represent a microservice architecture



      (:Team {space})-[:OPEX]->(:Service)-[:USES]->[:Service]




      • a Team belongs to a space (a department)

      • a Team has operation ownership (=OPEX) over several Services

      • Services have USES relationships between them


      My query in natural language:




      • find all the Teams in a certain space, and via the OPEX relationship find all their Services

      • also include all USES relationships between these services in the result


      I'm having problems expressing this query in an elegant Cypher query. I came up with the following, but there must be an easier and more natural way to express it.



      MATCH (t1:Team {space:"shopping"})-[o1:OPEX]->(s1:Service),
      (t2:Team {space:"shopping"})-[o2:OPEX]->(s2:Service),
      (s3:Service)-[u:USES]->(s4:Service)
      WHERE s3.name=s1.name AND s4.name=s2.name
      RETURN t1, o1, s1, u, s2, o2, t2


      Can someone point me into a better direction?







      cypher






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 21 '18 at 13:40







      Kristel

















      asked Nov 21 '18 at 11:10









      KristelKristel

      2817




      2817
























          1 Answer
          1






          active

          oldest

          votes


















          1














          You should reuse variables that refer to the same instance, and you can use IN and COLLECT to find patterns in a set of nodes.



          Something like this



          MATCH (:Team {space:"shopping"})-[:OPEX]->(s:Service)
          WITH COLLECT(s) as services
          MATCH (t:Team {space:"shopping"})-[o:OPEX]->(s1:Service)
          OPTIONAL MATCH (s1:Service)-[u:USES]->(s2:Service)
          WHERE s1 in services AND s2 in services
          RETURN t, o, s1, u, s2





          share|improve this answer
























          • Thnx Tezra! This query is definitely is more Cypher-like than my original SQL-like query.

            – Kristel
            Nov 22 '18 at 10:09











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53410860%2fmatch-nodes-on-property-and-include-relationships-between-those-nodes-in-the-re%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          You should reuse variables that refer to the same instance, and you can use IN and COLLECT to find patterns in a set of nodes.



          Something like this



          MATCH (:Team {space:"shopping"})-[:OPEX]->(s:Service)
          WITH COLLECT(s) as services
          MATCH (t:Team {space:"shopping"})-[o:OPEX]->(s1:Service)
          OPTIONAL MATCH (s1:Service)-[u:USES]->(s2:Service)
          WHERE s1 in services AND s2 in services
          RETURN t, o, s1, u, s2





          share|improve this answer
























          • Thnx Tezra! This query is definitely is more Cypher-like than my original SQL-like query.

            – Kristel
            Nov 22 '18 at 10:09
















          1














          You should reuse variables that refer to the same instance, and you can use IN and COLLECT to find patterns in a set of nodes.



          Something like this



          MATCH (:Team {space:"shopping"})-[:OPEX]->(s:Service)
          WITH COLLECT(s) as services
          MATCH (t:Team {space:"shopping"})-[o:OPEX]->(s1:Service)
          OPTIONAL MATCH (s1:Service)-[u:USES]->(s2:Service)
          WHERE s1 in services AND s2 in services
          RETURN t, o, s1, u, s2





          share|improve this answer
























          • Thnx Tezra! This query is definitely is more Cypher-like than my original SQL-like query.

            – Kristel
            Nov 22 '18 at 10:09














          1












          1








          1







          You should reuse variables that refer to the same instance, and you can use IN and COLLECT to find patterns in a set of nodes.



          Something like this



          MATCH (:Team {space:"shopping"})-[:OPEX]->(s:Service)
          WITH COLLECT(s) as services
          MATCH (t:Team {space:"shopping"})-[o:OPEX]->(s1:Service)
          OPTIONAL MATCH (s1:Service)-[u:USES]->(s2:Service)
          WHERE s1 in services AND s2 in services
          RETURN t, o, s1, u, s2





          share|improve this answer













          You should reuse variables that refer to the same instance, and you can use IN and COLLECT to find patterns in a set of nodes.



          Something like this



          MATCH (:Team {space:"shopping"})-[:OPEX]->(s:Service)
          WITH COLLECT(s) as services
          MATCH (t:Team {space:"shopping"})-[o:OPEX]->(s1:Service)
          OPTIONAL MATCH (s1:Service)-[u:USES]->(s2:Service)
          WHERE s1 in services AND s2 in services
          RETURN t, o, s1, u, s2






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 '18 at 19:07









          TezraTezra

          5,07821143




          5,07821143













          • Thnx Tezra! This query is definitely is more Cypher-like than my original SQL-like query.

            – Kristel
            Nov 22 '18 at 10:09



















          • Thnx Tezra! This query is definitely is more Cypher-like than my original SQL-like query.

            – Kristel
            Nov 22 '18 at 10:09

















          Thnx Tezra! This query is definitely is more Cypher-like than my original SQL-like query.

          – Kristel
          Nov 22 '18 at 10:09





          Thnx Tezra! This query is definitely is more Cypher-like than my original SQL-like query.

          – Kristel
          Nov 22 '18 at 10:09




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53410860%2fmatch-nodes-on-property-and-include-relationships-between-those-nodes-in-the-re%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          MongoDB - Not Authorized To Execute Command

          in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

          How to fix TextFormField cause rebuild widget in Flutter