DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • The Beginner's Guide To Understanding Graph Databases
  • How Milvus Realizes the Delete Function
  • See What's New in Neo4j 4.0
  • Lessons from Migrating an Oracle Database to AWS RDS

Trending

  • Breaking Bottlenecks: Applying the Theory of Constraints to Software Development
  • Unlocking the Benefits of a Private API in AWS API Gateway
  • Top Book Picks for Site Reliability Engineers
  • Integrating Security as Code: A Necessity for DevSecOps
  1. DZone
  2. Data Engineering
  3. Databases
  4. Intro to Querying Neo4j Using OGM

Intro to Querying Neo4j Using OGM

Let's take a look at how to add examples of querying the data that was loaded in the previous article.

By 
Scott Sosna user avatar
Scott Sosna
DZone Core CORE ·
Aug. 15, 18 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
9.1K Views

Join the DZone community and get the full member experience.

Join For Free

Overview

Neo4j Object-Graph Mapping, or Neo4j OGM, is a library for modifying and querying Neo4j databases without directly using Cypher.

Conceptually similar to Java Persistence API for relational databases, OGM annotations are added to plain-old Java objects, identifying them as Neo4j nodes or relationships. New objects for nodes or relationships are created and added to the Neo4j session, which OGM persists by creating and then executing the appropriate Cypher statements.

Using OGM to manipulate your Neo4j data gives you compile-time checks of nodes, relationships, labels, and properties that you don't have when working directly with Cypher but still allows your data model to evolve naturally as other NoSQL databases.

Prerequisites

  • Neo4j Server. Either Community or Enterprise, this intro tested with v3.3.1.
  • Neo4j OGM Libraries. Latest version today is v3.1.0, accessible via Maven, Gradle, and Ivy.

Sample Project

This blog extends my Introduction to Neo4J OGM by adding examples of querying the data previously loaded.

Single Filter

A single filter is a conditional used to evaluate nodes or relationships to return as the results in a query, identical to a SQL WHERE clause.

A filter is composed of a property name, a , and - if the application for boolean operation provided - a comparison value.

The session object defines methods for applying the filter against a specific node or relationship type.

In the example project, the filter is querying for a specific birth year.

    private Iterable<Person> queryByFilter (int birthYear,
                                            Session session) {

        //  Create an OGM filter for the birthYear property.
        Filter filter = new Filter ("birthYear", ComparisonOperator.EQUALS, birthYear);

        //  Load all Persons with the given birth year.
        return session.loadAll (Person.class, filter);
    }

Composite Filter

A composite filter contains one or more filters connected by a boolean relationship.  The contained filters may be either a single filter or another composite filter, in this case, a SQL WHERE clause surrounded by parentheses.

In the example, the composite filter is querying for a specific birth year or for a Person's name greater than a starting letter provided.

    private Iterable<Person> queryByMultipleFilters (int birthYear,
                                                     String startingLetter,
                                                     Session session) {

        //  Filter either by the birth year or name greater than the starting letter
        Filters composite = new Filters();
        Filter filter = new Filter ("birthYear", ComparisonOperator.EQUALS, birthYear);
        composite.add(filter);
        filter = new Filter ("name", ComparisonOperator.GREATER_THAN, startingLetter);
        filter.setBooleanOperator(BooleanOperator.OR);
        composite.add(filter);

        //  Load all Persons which match the composite filter.
        return session.loadAll (Person.class, composite);
    }

A composite filter composed of other composite filters represent multiple levels of parentheses for more complicated comparisons.

Cypher Queries

When your query needs are more complicated than can be represented by filters, you can define the specific Cypher statement and provide the parameters to substitute into the statement.

In this example, the query returns the Person nodes for those married to the target of the relationship, as specified by name.

    private Iterable<Person> queryByCypher (String marriedTo,
                                            Session session) {

        //  Create/load a map to hold the parameter
        Map<String, Object> params = new HashMap<>(1);
        params.put ("name", marriedTo);

        //  Execute query and return the other side of the married relationship
        String cypher = "MATCH (w:Person)-[:MARRIED]->(h:Person {name:$name}) RETURN w";
        return session.query (Person.class, cypher, params);
    }

Instead of hard-coding the specific name being queried on, the $name parameter is substituted at runtime with whatever value has been put into the parameter map.  Parameters allow the Cypher statement to be reused without having to do a bunch of string building for each time called.

And similar to relational databases, using substitution parameters allow the Cypher query to be parsed once and the execution plan cached, leading to faster execution times.

Conclusion

Granted, the examples are very simple, but hopefully, you can apply this to your own OGM projects and query and get the data returned as objects.

The complete demo project can be downloaded from here.

Neo4j Filter (software) Relational database

Opinions expressed by DZone contributors are their own.

Related

  • The Beginner's Guide To Understanding Graph Databases
  • How Milvus Realizes the Delete Function
  • See What's New in Neo4j 4.0
  • Lessons from Migrating an Oracle Database to AWS RDS

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!