indexingneo4jgraph-databases

Neo4j - Range index vs Text index when checking membership in list of only strings


Example query:

WITH ["sword", "Sword", "blade", "Blade", "katana", "Katana"] AS weapons
MATCH (s:Set)
WHERE s.name IN weapons
RETURN s

Neo4j documentation mentions that both Range index and Text index should be used when we check for list membership. Which one is actually better in the example above where the list contains only strings?

I could not find information in Neo4j documentation about which index should I should choose to speed up these queries.

What about a more compliacted example which uses any() with CONTAINS?:

WITH ["sword", "blade", "katana"] AS weapons
MATCH (s:Set)
WHERE ANY(piece IN weapons WHERE toLower(s.name) CONTAINS piece)
RETURN s

Solution

  • From the documentation

    Even though text indexes do support other text queries, ENDS WITH or CONTAINS queries are the only ones for which this index type provides an advantage over a range index.

    If you need CONTAINS, TEXT indexes are way faster than RANGE.

    That said, based on your query, TEXT indexes are case sensitive and trying to lowercase the value of properties for comparison will prevent the index usage.

    // doesn't use the index
    WHERE toLower(n.text) CONTAINS 'my-input'
    
    // uses the index
    WHERE n.text CONTAINS 'my-input'