Skip to content

Repository query method with 'contains' on list property generates incorrect cypher query #2444

Closed

Description

@rlippolis

Given a graph with the following node:

CREATE (testNode:TestNode{id: 1, items: ['item 1', 'item 2', 'item 3'], description: 'desc 1'})

If I want to write a repository query method to find the nodes which contain a given item in their items list property, I expect the following to work:

interface TestNodeRepository: Neo4jRepository<TestNode, Long> {
    fun findByItemsContains(item: String): TestNode?
}

However, executing this method yields no results, because it generates the following cypher query:

MATCH (n:`TestNode`) WHERE n.items CONTAINS $item RETURN n{.description, .id, .items, __nodeLabels__: labels(n), __internalNeo4jId__: id(n)}

So it uses the CONTAINS keyword, which in cypher is used for partial String matching. I would have expected it to create the following query:

MATCH (n:`TestNode`) WHERE $item IN n.items RETURN n{.description, .id, .items, __nodeLabels__: labels(n), __internalNeo4jId__: id(n)}

My first thought was that maybe I should modify my query method to use In instead of Contains, but that only works the other way around (matching a property against a list of possible items), e.g.: fun findByDescriptionIn(descriptions: List<String>): TestNode?, which correctly generates a cypher query using the IN keyword.

Am I missing something (not so) obvious here, or is this indeed a bug?

See example project with failing test case here: https://github.com/rlippolis/spring-data-neo4j-listcontains-test

Activity

  1. michael-simons commented on Nov 18, 2021

    @michael-simons
    Collaborator

    Hey. This is an interesting one, takes for taking the time to create it, much appreciated

    In Spring Data commons CONTAINS is usually defined as the String Op you described and how we implemented it.
    Can you share the domain class(es), please?
    It might be worth considering that if we find a list on the entity to generate an actual IN condition.

    Things that interest me:

    • Are you using a custom converter?
    • Or an array of Strings?

    Thanks in advance.

  2. rlippolis commented on Nov 18, 2021

    @rlippolis
    ContributorAuthor

    Hey Michael, thanks for your quick response.

    See the domain class in my sample project: https://github.com/rlippolis/spring-data-neo4j-listcontains-test/blob/main/src/main/kotlin/com/example/neo4jlistcontainstest/TestNode.kt

    I'm not using any custom converters. The property currently is a List<String>. I've tested it with a Set as well as with an Array, same result.

  3. added 4 commits that reference this issue on Nov 18, 2021
  4. michael-simons commented on Nov 18, 2021

    @michael-simons
    Collaborator

    Thank you, @rlippolis Thanks to the Cypher-DSL based builder, this was an easy fix / improvement. Thanks again for reporting it and providing the reproducer.

  5. rlippolis commented on Nov 18, 2021

    @rlippolis
    ContributorAuthor

    Awesome, thanks for the quick response and fix, @michael-simons! 👍

  6. added 4 commits that reference this issue on Nov 25, 2021
  7. added a commit that references this issue on Aug 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions