Neo4j. Динамический запрос с несколькими фильтрами на узлах - PullRequest
0 голосов
/ 19 ноября 2018

У меня есть такие узлы, и мне нужно применить фильтр к ним

{
  "value": "ma",
  "object_field_id": 2293
}

{
  "value": 2,
  "object_field_id": 2294
}

MATCH (:UserData {user_id:19})-[:CARD]-(c:Card)-[:FIELD_GROUP]-()-[:FIELD]-(fi:Field) 
WHERE fi.object_field_id IN [23,24] 
  AND (fi.value=~('(?iu).*149.*') 
  AND fi.value=~('(?iu).*gb.*')) 
RETURN c

но не работает с AND

Где моя ошибка?

Ответы [ 3 ]

0 голосов
/ 20 ноября 2018

Вот данные запроса для построения отношений и узлов:

    CREATE (a:UserData {user_id: 19})-[r:CARD]->(b:Card {name: "card"})
WITH count(*) as dummy

CREATE (a:FieldUserGroup {data_id: 1290})
WITH count(*) as dummy

CREATE (a:FieldUserGroup {data_id: 1280})
WITH count(*) as dummy

CREATE (a:FieldUserGroup {data_id: 1270})
WITH count(*) as dummy

MATCH (u:FieldUserGroup), (c:Card)  CREATE (c)-[:FIELD_GROUP]->(u)
WITH count(*) as dummy

create (f:Field {value: "jerry", object_field_id: 11})
WITH count(*) as dummy

create (f:Field {value: 2, object_field_id: 12})
WITH count(*) as dummy

create (f:Field {value: "tom", object_field_id: 13})
WITH count(*) as dummy

MATCH (f:FieldUserGroup {data_id: 1290}), (f2:Field) CREATE (f)-[:FIELD]->(f2)
WITH count(*) as dummy

CREATE (c:Field {value: "jack", object_field_id: 14})
WITH count(*) as dummy

CREATE (c:Field {value: 1234, object_field_id: 15})
WITH count(*) as dummy

CREATE (c:Field {value: "robin", object_field_id: 16})
WITH count(*) as dummy

MATCH (f:FieldUserGroup {data_id: 1280}), (c:Field {value: "jack", object_field_id: 14}) CREATE (f)-[:FIELD]->(c)
WITH count(*) as dummy

MATCH (f:FieldUserGroup {data_id: 1280}), (c:Field {value: 1234, object_field_id: 15}) CREATE (f)-[:FIELD]->(c)
WITH count(*) as dummy

MATCH (f:FieldUserGroup {data_id: 1280}), (c:Field {value: "robin", object_field_id: 16}) CREATE (f)-[:FIELD]->(c)
WITH count(*) as dummy

CREATE (c:Field {value: "zazaza", object_field_id: 17})
WITH count(*) as dummy

CREATE (c:Field {value: 6578970, object_field_id: 18})
WITH count(*) as dummy

CREATE (c:Field {value: "hamlet", object_field_id: 19})
WITH count(*) as dummy

MATCH (f:FieldUserGroup {data_id: 1270}), (c:Field {value: "zazaza", object_field_id: 17}) CREATE (f)-[:FIELD]->(c)
WITH count(*) as dummy

MATCH (f:FieldUserGroup {data_id: 1270}), (c:Field {value: 6578970, object_field_id: 18}) CREATE (f)-[:FIELD]->(c)
WITH count(*) as dummy

MATCH (f:FieldUserGroup {data_id: 1270}), (c:Field {value: "hamlet", object_field_id: 19}) CREATE (f)-[:FIELD]->(c)
WITH count(*) as dummy

CREATE (r:card {name: "card2"})
WITH count(*) as dummy

MATCH (r:card {name: "card2"}), (a:UserData {user_id: 19}) CREATE (a)-[:CARD]->(r)
WITH count(*) as dummy

CREATE (a:FieldUserGroup {data_id: 1260})
WITH count(*) as dummy

CREATE (a:FieldUserGroup {data_id: 1250})
WITH count(*) as dummy

CREATE (a:FieldUserGroup {data_id: 1240})
WITH count(*) as dummy

MATCH (f:FieldUserGroup {data_id: 1260}), (r:card {name: "card2"}) CREATE (r)-[:CARD]->(f)
WITH count(*) as dummy

MATCH (f:FieldUserGroup {data_id: 1250}), (r:card {name: "card2"}) CREATE (r)-[:CARD]->(f)
WITH count(*) as dummy

MATCH (f:FieldUserGroup {data_id: 1240}), (r:card {name: "card2"}) CREATE (r)-[:CARD]->(f)
WITH count(*) as dummy

CREATE (c:Field {value: "some value", object_field_id: 20})
WITH count(*) as dummy

CREATE (c:Field {value: "more value", object_field_id: 21})
WITH count(*) as dummy

CREATE (c:Field {value: "this is insane!", object_field_id: 22})
WITH count(*) as dummy

MATCH (f:FieldUserGroup {data_id: 1260}), (c:Field {object_field_id: 20}) CREATE (f)-[:FIELD]->(c)
WITH count(*) as dummy

MATCH (f:FieldUserGroup {data_id: 1260}), (c:Field {object_field_id: 21}) CREATE (f)-[:FIELD]->(c)
WITH count(*) as dummy

MATCH (f:FieldUserGroup {data_id: 1260}), (c:Field {object_field_id: 22}) CREATE (f)-[:FIELD]->(c)
WITH count(*) as dummy

CREATE (c:Field {value: 14948576, object_field_id: 23})
WITH count(*) as dummy

CREATE (c:Field {value: "GB", object_field_id: 24})
WITH count(*) as dummy

CREATE (c:Field {value: 1, object_field_id: 25})
WITH count(*) as dummy

MATCH (f:FieldUserGroup {data_id: 1250}), (c:Field {object_field_id: 23}) CREATE (f)-[:FIELD]->(c)
WITH count(*) as dummy

MATCH (f:FieldUserGroup {data_id: 1250}), (c:Field {object_field_id: 24}) CREATE (f)-[:FIELD]->(c)
WITH count(*) as dummy

MATCH (f:FieldUserGroup {data_id: 1250}), (c:Field {object_field_id: 25}) CREATE (f)-[:FIELD]->(c)
WITH count(*) as dummy

CREATE (c:Field {value: 2, object_field_id: 26})
WITH count(*) as dummy

CREATE (c:Field {value: "Marky", object_field_id: 27})
WITH count(*) as dummy

CREATE (c:Field {value: "Tom Born", object_field_id: 28})
WITH count(*) as dummy

MATCH (f:FieldUserGroup {data_id: 1240}), (c:Field {object_field_id: 26}) CREATE (f)-[:FIELD]->(c)
WITH count(*) as dummy

MATCH (f:FieldUserGroup {data_id: 1240}), (c:Field {object_field_id: 27}) CREATE (f)-[:FIELD]->(c)
WITH count(*) as dummy

MATCH (f:FieldUserGroup {data_id: 1240}), (c:Field {object_field_id: 28}) CREATE (f)-[:FIELD]->(c)
WITH count(*) as dummy
0 голосов
/ 20 ноября 2018

UPD:

Вот запрос, который работает, но только в одну сторону, от FieldUserGroup

MATCH (c:Card)-[:FIELD_GROUP]-()-[:FIELD]-(fi:Field) where (any(prop in keys(fi) where fi[prop] =~ '(?iu).*tom.*'))
return c
0 голосов
/ 20 ноября 2018

Я очистил ваши запросы Cypher для создания базы данных, надеюсь, я не допустил ошибок.Вы заметите, что я удалил все WITH count(*) as dummy, так как они не нужны и вызывают только ошибки.Для операторов, которые имеют MATCH и CREATE, им нужно было WITH.Я переупорядочил и сгруппировал подобные утверждения так, чтобы все они могли быть выполнены в одном утверждении, например, заканчиваться на ;.

CREATE (:UserData {user_id: 19});

MATCH (n:UserData {user_id: 19})
WITH n
CREATE (n)-[:CARD]->(:Card {name: "card"});

CREATE (:FieldUserGroup {data_id: 1290})
CREATE (:FieldUserGroup {data_id: 1280})
CREATE (:FieldUserGroup {data_id: 1270});

MATCH (f1:FieldUserGroup {data_id: 1290}),
      (f2:FieldUserGroup {data_id: 1280}),
      (f3:FieldUserGroup {data_id: 1270}),
      (c1:Card {name: "card"})
WITH f1,f2,f3,c1
CREATE (c1)-[:FIELD_GROUP]->(f1)
CREATE (c1)-[:FIELD_GROUP]->(f2)
CREATE (c1)-[:FIELD_GROUP]->(f3);

CREATE (:Field {value: "jerry", object_field_id: 11})
CREATE (:Field {value: 2, object_field_id: 12})
CREATE (:Field {value: "tom", object_field_id: 13})
CREATE (:Field {value: "jack", object_field_id: 14})
CREATE (:Field {value: 1234, object_field_id: 15})
CREATE (:Field {value: "robin", object_field_id: 16})
CREATE (:Field {value: "zazaza", object_field_id: 17})
CREATE (:Field {value: 6578970, object_field_id: 18})
CREATE (:Field {value: "hamlet", object_field_id: 19});

MATCH (f1:FieldUserGroup {data_id: 1290}), 
      (c11:Field {value: "jerry"}),
      (c12:Field {value: 2}),
      (c13:Field {value: "tom"}),
      (f2:FieldUserGroup {data_id: 1280}),
      (c21:Field {value: "jack"}), 
      (c22:Field {value: 1234}),
      (c23:Field {value: "robin"}),
      (f3:FieldUserGroup {data_id: 1270}), 
      (c31:Field {value: "zazaza"}),
      (c32:Field {value: 6578970}),
      (c33:Field {value: "hamlet"})
WITH f1,f2,f3,c11,c12,c13,c21,c22,c23,c31,c32,c33
CREATE (f1)-[:FIELD]->(c11)
CREATE (f1)-[:FIELD]->(c12)
CREATE (f1)-[:FIELD]->(c13)
CREATE (f2)-[:FIELD]->(c21)
CREATE (f2)-[:FIELD]->(c22)
CREATE (f2)-[:FIELD]->(c23)
CREATE (f3)-[:FIELD]->(c31)
CREATE (f3)-[:FIELD]->(c32)
CREATE (f3)-[:FIELD]->(c33);


MATCH (n:UserData {user_id: 19})
WITH n
CREATE (n)-[:CARD]->(:Card {name: "card2"});

CREATE (:FieldUserGroup {data_id: 1240})
CREATE (:FieldUserGroup {data_id: 1250})
CREATE (:FieldUserGroup {data_id: 1260});

MATCH (f1:FieldUserGroup {data_id: 1240}),
      (f2:FieldUserGroup {data_id: 1250}),
      (f3:FieldUserGroup {data_id: 1260}),
      (c1:Card {name: "card2"})
WITH f1,f2,f3,c1
CREATE (c1)-[:FIELD_GROUP]->(f1)
CREATE (c1)-[:FIELD_GROUP]->(f2)
CREATE (c1)-[:FIELD_GROUP]->(f3);

CREATE (:Field {value: "some value", object_field_id: 20})
CREATE (:Field {value: "more value", object_field_id: 21})
CREATE (:Field {value: "this is insane!", object_field_id: 22})
CREATE (:Field {value: 14948576, object_field_id: 23})
CREATE (:Field {value: "GB", object_field_id: 24})
CREATE (:Field {value: 1, object_field_id: 25})
CREATE (:Field {value: 2, object_field_id: 26})
CREATE (:Field {value: "Marky", object_field_id: 27})
CREATE (:Field {value: "Tom Born", object_field_id: 28});

MATCH (f1:FieldUserGroup {data_id: 1240}), 
      (c11:Field {value: 2, object_field_id: 26}),
      (c12:Field {value: "Marky"}),
      (c13:Field {value: "Tom Born"}),    
      (f2:FieldUserGroup {data_id: 1250}),
      (c21:Field {value: 14948576}), 
      (c22:Field {value: "GB"}),
      (c23:Field {value: 1}),     
      (f3:FieldUserGroup {data_id: 1260}), 
      (c31:Field {value: "some value"}),
      (c32:Field {value: "more value"}),
      (c33:Field {value: "this is insane!"})
WITH f1,f2,f3,c11,c12,c13,c21,c22,c23,c31,c32,c33
CREATE (f1)-[:FIELD]->(c11)
CREATE (f1)-[:FIELD]->(c12)
CREATE (f1)-[:FIELD]->(c13)
CREATE (f2)-[:FIELD]->(c21)
CREATE (f2)-[:FIELD]->(c22)
CREATE (f2)-[:FIELD]->(c23)
CREATE (f3)-[:FIELD]->(c31)
CREATE (f3)-[:FIELD]->(c32)
CREATE (f3)-[:FIELD]->(c33);

enter image description here

Исходный запрос

MATCH (:UserData {user_id:19})-[:CARD]-(c:Card)-[:FIELD_GROUP]-()-[:FIELD]-(fi:Field) 
WHERE fi.object_field_id IN [23,24] 
  AND (fi.value=~('(?iu).*149.*') 
  AND fi.value=~('(?iu).*gb.*')) 
RETURN c

работает правильно, когда не возвращает записей.Это связано с тем, что нет узлов, соответствующих этой части запроса

MATCH (fi:Field) 
    WHERE fi.value=~('(?iu).*149.*')  
    RETURN fi;

, которая также не возвращает записей.Предложение AND работает как надо.

...