Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
rlippolis opened this issue Nov 18, 2021 · 4 comments
Assignees
Labels
type: enhancement A general enhancement

Comments

@rlippolis
Copy link
Contributor

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

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Nov 18, 2021
@michael-simons
Copy link
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.

@rlippolis
Copy link
Contributor Author

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.

@michael-simons michael-simons self-assigned this Nov 18, 2021
@michael-simons michael-simons added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels Nov 18, 2021
@michael-simons michael-simons added this to the 6.0.16 (2020.0.16) milestone Nov 18, 2021
michael-simons added a commit that referenced this issue Nov 18, 2021
michael-simons added a commit that referenced this issue Nov 18, 2021
michael-simons added a commit that referenced this issue Nov 18, 2021
michael-simons added a commit that referenced this issue Nov 18, 2021
@michael-simons
Copy link
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.

@rlippolis
Copy link
Contributor Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

3 participants