Example #1
0
 public function discoveryQuery(NodeInterface $input)
 {
     $query = 'MATCH (n) WHERE id(n) = {id}
     MATCH (n)-[:CONTRIBUTED_TO]->(repo)<-[:CONTRIBUTED_TO]-(reco)
     RETURN reco, count(*) as score';
     return Statement::create($query, ['id' => $input->identity()]);
 }
 /**
  * @param string      $statement
  * @param array       $parameters
  * @param null|string $tag
  *
  * @return \GraphAware\Common\Result\Result
  */
 public function run($statement, array $parameters = array(), $tag = null)
 {
     if (!$this->driverTransaction->isOpen() && !in_array($this->driverTransaction->status(), ['COMMITED', 'ROLLED_BACK'])) {
         $this->driverTransaction->begin();
     }
     return $this->driverTransaction->run(Statement::create($statement, $parameters, $tag));
 }
 public function blacklistQuery(NodeInterface $input)
 {
     $query = 'MATCH (input) WHERE id(input) = {inputId}
     MATCH (input)-[:RATED]->(movie)
     RETURN movie as item';
     return Statement::create($query, ['inputId' => $input->identity()]);
 }
 public function buildStatement($fromId, $direction, $type, $identifier)
 {
     switch ($direction) {
         case 'INCOMING':
             $pattern = '<-[%s:%s]-';
             break;
         case 'OUTGOING':
             $pattern = '-[%s:%s]->';
             break;
         case 'BOTH':
             $pattern = '-[%s:%s]->';
             break;
         default:
             throw new \LogicException(sprintf('Unsupported relationship direction "%s"', $direction));
     }
     $relationshipPattern = sprintf($pattern, $identifier, $type);
     $query = 'MATCH (start) WHERE id(start) = {id}
     MATCH (start)' . $relationshipPattern . '(end)';
     if ($this->relationshipMetadata->hasOrderBy()) {
         $query .= ' WITH end ORDER BY end.' . $this->relationshipMetadata->getOrderByPropery() . ' ' . $this->relationshipMetadata->getOrder();
     }
     $query .= ' RETURN end';
     //print_r($query);
     return Statement::create($query, ['id' => $fromId]);
 }
Example #5
0
 public function discoveryQuery(Node $input, Context $context) : StatementInterface
 {
     $query = 'MATCH (n) WHERE id(n) = {id}
     MATCH (n)-[:FRIEND]->(friend)-[:FRIEND]->(reco)
     WHERE NOT (n)-[:FRIEND]->(reco)
     RETURN reco, count(*) as score';
     return Statement::prepare($query, ['id' => $input->identity()]);
 }
Example #6
0
 public function discoveryQuery(Node $input, Context $context)
 {
     $query = 'MATCH (input:User) WHERE id(input) = {id}
     MATCH (input)-[:RATED]->(m)<-[:RATED]-(o)
     WITH distinct o
     MATCH (o)-[:RATED]->(reco)
     RETURN distinct reco LIMIT 500';
     return Statement::create($query, ['id' => $input->identity()]);
 }
 public function discoveryQuery(NodeInterface $input)
 {
     $query = 'MATCH (input) WHERE id(input) = {id}
     MATCH (input)<-[:FOLLOWS]-(follower)-[:FOLLOWS]->(reco)
     WHERE size((follower)-[:FOLLOWS]->()) < {max_follows}
     RETURN reco, count(*) as score
     LIMIT 100';
     return Statement::create($query, ['id' => $input->identity(), 'max_follows' => 200]);
 }
 public final function buildQuery(NodeInterface $input, Recommendation $recommendation)
 {
     $relationshipPatterns = [Direction::BOTH => array('-[:%s]-', '-[:%s]-'), Direction::INCOMING => array('<-[:%s]-', '-[:%s]->'), Direction::OUTGOING => array('-[:%s]->', '<-[:%s]-')];
     $relPattern = sprintf($relationshipPatterns[$this->relationshipDirection()][0], $this->relationshipType());
     $inversedRelPattern = sprintf($relationshipPatterns[$this->relationshipDirection()][1], $this->relationshipType());
     $query = 'MATCH (input) WHERE id(input) = {inputId}, (item) WHERE id(item) = {itemId}
     MATCH (input)' . $relPattern . '(shared)' . $inversedRelPattern . '(item)
     RETURN shared as sharedThing';
     return Statement::create($query, ['inputId' => $input->identity(), 'itemId' => $recommendation->item()->identity()]);
 }
Example #9
0
 public function discoveryQuery(NodeInterface $input)
 {
     $query = 'MATCH (input:User) WHERE id(input) = {id}
     MATCH (input)-[:RATED]->(movie)<-[:RATED]-(other)
     WITH distinct other
     MATCH (other)-[:RATED]->(reco)
     RETURN reco, count(*) as score
     ORDER BY score DESC
     LIMIT 200';
     return Statement::create($query, ['id' => $input->identity()]);
 }
 public function buildQuery(NodeInterface $input, Recommendations $recommendations)
 {
     $ids = [];
     foreach ($recommendations->getItems() as $recommendation) {
         $ids[] = $recommendation->item()->identity();
     }
     $query = 'UNWIND {ids} as id
     MATCH (n) WHERE id(n) = id
     RETURN id, size((n)<-[:FOLLOWS]-()) as followersCount';
     return Statement::create($query, ['ids' => $ids]);
 }
Example #11
0
 public function buildQuery(Node $input, Recommendations $recommendations)
 {
     $query = 'UNWIND {ids} as id
     MATCH (n) WHERE id(n) = id
     MATCH (n)<-[r:RATED]-(u)
     RETURN id(n) as id, sum(r.rating) as score';
     $ids = [];
     foreach ($recommendations->getItems() as $item) {
         $ids[] = $item->item()->identity();
     }
     return Statement::create($query, ['ids' => $ids]);
 }
 public function discoveryQuery(Node $input, Context $context)
 {
     $query = 'MATCH (input) WHERE id(input) = {id}
     MATCH (input)-[r:RATED]->(movie)-[:HAS_GENRE]->(genre)
     WITH distinct genre, sum(r.rating) as score
     ORDER BY score DESC
     LIMIT 15
     MATCH (genre)<-[:HAS_GENRE]-(reco)
     RETURN reco
     LIMIT 200';
     return Statement::create($query, ['id' => $input->identity()]);
 }
Example #13
0
 /**
  * Run a Cypher statement against the default database or the database specified.
  *
  * @param $query
  * @param null|array  $parameters
  * @param null|string $tag
  * @param null|string $connectionAlias
  *
  * @return \GraphAware\Common\Result\Result|null
  *
  * @throws \GraphAware\Neo4j\Client\Exception\Neo4jExceptionInterface
  */
 public function run($query, $parameters = null, $tag = null, $connectionAlias = null)
 {
     $connection = $this->connectionManager->getConnection($connectionAlias);
     $params = null !== $parameters ? $parameters : array();
     $statement = Statement::create($query, $params, $tag);
     $this->eventDispatcher->dispatch(Neo4jClientEvents::NEO4J_PRE_RUN, new PreRunEvent(array($statement)));
     try {
         $result = $connection->run($query, $parameters, $tag);
         $this->eventDispatcher->dispatch(Neo4jClientEvents::NEO4J_POST_RUN, new PostRunEvent(ResultCollection::withResult($result)));
     } catch (Neo4jException $e) {
         $event = new FailureEvent($e);
         $this->eventDispatcher->dispatch(Neo4jClientEvents::NEO4J_ON_FAILURE, $event);
         if ($event->shouldThrowException()) {
             throw $e;
         }
         return;
     }
     return $result;
 }
 public function buildStatement($fromId, $direction, $type, $identifier)
 {
     $type = $this->relationshipEntityMetadata->getType();
     $identifier = 'rel_' . $this->relationshipMetadata->getPropertyName() . '_' . $this->relationshipEntityMetadata->getType();
     switch ($direction) {
         case 'INCOMING':
             $pattern = '<-[%s:%s]-';
             break;
         case 'OUTGOING':
             $pattern = '-[%s:%s]->';
             break;
         case 'BOTH':
             $pattern = '-[%s:%s]->';
             break;
         default:
             throw new \LogicException(sprintf('Unsupported relationship direction "%s"', $direction));
     }
     $relationshipPattern = sprintf($pattern, $identifier, $type);
     $query = 'MATCH (start) WHERE id(start) = {id}
     MATCH (start)' . $relationshipPattern . '(end)';
     if ($this->relationshipMetadata->hasOrderBy()) {
         $orderProperty = $this->relationshipMetadata->getOrderByPropery();
         $split = explode('.', $orderProperty);
         $reMetadata = $this->relationshipEntityMetadata;
         if (count($split) > 1) {
             $reName = $split[0];
             $v = $split[1];
             if ($reMetadata->getStartNodePropertyName() === $reName || $reMetadata->getEndNodePropertyName() === $reName) {
                 $orderProperty = $this->relationshipMetadata->getPropertyName() . '.' . $v;
             }
         } else {
             if (null !== $reMetadata->getPropertyMetadata($this->relationshipMetadata->getOrderByPropery())) {
                 $orderProperty = $identifier . '.' . $this->relationshipMetadata->getOrderByPropery();
             }
         }
         $query .= ' WITH end, ' . $identifier . ' ORDER BY ' . $orderProperty . ' ' . $this->relationshipMetadata->getOrder();
     }
     $query .= ' RETURN CASE count(' . $identifier . ') WHEN 0 THEN [] ELSE collect({start:startNode(' . $identifier . '), end:endNode(' . $identifier . '), rel:' . $identifier . '}) END AS ' . $identifier;
     //print_r($query);
     return Statement::create($query, ['id' => $fromId]);
 }
 public function getDeleteRelationshipQuery($entityIdA, $entityIdB, RelationshipMetadata $relationship)
 {
     $relString = '';
     switch ($relationship->getDirection()) {
         case 'OUTGOING':
             $relString = '-[r:%s]->';
             break;
         case 'INCOMING':
             $relString = '<-[r:%s]-';
             break;
         case 'BOTH':
             $relString = '-[r:%s]-';
             break;
         default:
             throw new \InvalidArgumentException(sprintf('Direction "%s" is not valid', $relationship->getDirection()));
     }
     $relStringPart = sprintf($relString, $relationship->getType());
     $query = 'MATCH (a), (b) WHERE id(a) = {ida} AND id(b) = {idb}
     MATCH (a)' . $relStringPart . '(b)
     DELETE r';
     return Statement::create($query, ['ida' => $entityIdA, 'idb' => $entityIdB]);
 }
 public function discoveryQuery(Node $input, Context $context) : StatementInterface
 {
     $query = "MATCH (n) WHERE id(n) <> {inputId} RETURN n";
     return Statement::create($query, ['inputId' => $input->identity()]);
 }
 public function getDeleteQuery($object)
 {
     $query = 'MATCH (n) WHERE id(n) = {id} DELETE n';
     $id = $this->classMetadata->getIdValue($object);
     return Statement::create($query, ['id' => $id]);
 }
Example #18
0
 /**
  * {@inheritdoc}
  */
 public function push($query, array $parameters = array(), $tag = null)
 {
     $this->statements[] = Statement::create($query, $parameters, $tag);
 }
Example #19
0
 public function blacklistQuery(Node $input)
 {
     $query = 'MATCH (n) WHERE n.name = "Zoe" RETURN n as item';
     return Statement::prepare($query, ['id' => $input->identity()]);
 }
 public function getDeleteQuery($entity)
 {
     $id = $this->classMetadata->getIdValue($entity);
     $query = 'START rel=rel(' . $id . ') DELETE rel';
     return Statement::create($query);
 }
Example #21
0
 /**
  * @param $query
  * @param array|null $parameters
  * @param array|null $tag
  */
 public function addPreflight($query, $parameters = null, $tag = null)
 {
     $params = null !== $parameters ? $parameters : array();
     $this->preflights[] = Statement::create($query, $params, $tag);
 }
Example #22
0
 public function buildQuery(NodeInterface $input, Recommendation $recommendation)
 {
     $query = 'MATCH (item) WHERE id(item) = {itemId}
     RETURN size((item)<-[:RATED]-()) as ratings';
     return Statement::create($query, ['itemId' => $recommendation->item()->identity()]);
 }
 public function discoveryQuery(Node $input, Context $context) : StatementInterface
 {
     $query = "MATCH (n) WHERE id(n) <> {input}\n        RETURN n LIMIT {limit}";
     return Statement::create($query, ['input' => $input->identity(), 'limit' => 300]);
 }