0

I have created the following Neo4j APOC trigger which adds node properties into the manual index:

CALL apoc.trigger.add('CREATE_NODES_TRIGGER', "UNWIND {createdNodes} AS n 
WITH n WHERE labels(n)[1] IN ['Title','Name','Character'] 
CALL apoc.index.addNodeByName(labels(n)[1], n, keys(n)) 
RETURN true", {phase:'before'})

When I perform the following query:

CALL apoc.index.nodes('Character','name:An*') YIELD node AS c, weight 
RETURN c.name, weight LIMIT 10

it correctly returns the data:

c.name  weight
"Annette Porter"    1.0
"Anke Bauernfeind"  1.0
"Angel's Henchman"  1.0
"Anna Slovatzka Marshall"   1.0
"Andrew Horvath"    1.0
"Angela Somes"  1.0
"An-Soo 'Alison' Kim"   1.0
"Annette Holman"    1.0
"Anita Wilcox"  1.0
"Anna Cerdick"  1.0

but in case of lowercased query name:an*:

CALL apoc.index.nodes('Character','name:an*') YIELD node AS c, weight 
RETURN c.name, weight LIMIT 10

the result is empty.

I think I should lowercase the property values before adding them to the index. Am I right?

If so, could you please help to improve the trigger in order to lowercase the property values:

CALL apoc.trigger.add('CREATE_NODES_TRIGGER', "UNWIND {createdNodes} AS n 
WITH n WHERE labels(n)[1] IN ['Title','Name','Character'] 
CALL apoc.index.addNodeByName(labels(n)[1], n, keys(n)) 
RETURN true", {phase:'before'})

Or please suggest another way how to solve this issue. Thanks!

UPDATED

I found the following property in neo4j.conf:

apoc.autoIndex.enabled=true

and added all nodes to the index:

CALL apoc.index.addAllNodes('movies',{
  Title: ["primaryTitle", "originalTitle"],
  Name:  ["primaryName"],
  Character: ["name"]}, {autoUpdate:true})

So, right now I have a question: will apoc.autoIndex.enabled=true only track and reflect changes on the already added nodes to the index(with apoc.index.addAllNodes) or it will also add the newly created Title, Name and Character nodes to the index also?

alexanoid
  • 24,051
  • 54
  • 210
  • 410
  • can you check your index definition? it should be created as "fulltext" index which also contains `case_insensitive: true` – Michael Hunger Dec 07 '18 at 12:50
  • also the labels(n) order is not guaranteed, in general something like `n:Title OR n:Name OR n:Character` is better than the IN check with a string array – Michael Hunger Dec 07 '18 at 12:51
  • Thanks ! This is the index config: `{ "type": "exact", "provider": "lucene"}` How can I change it to fulltext ? – alexanoid Dec 07 '18 at 13:33
  • And how in case of `n:Title OR n:Name OR n:Character` can I change the following code `apoc.index.addNodeByName(labels(n)[1], n, keys(n))`.. especially the following part `labels(n)[1]` ? – alexanoid Dec 07 '18 at 13:37
  • Also, please check my updated question. I found the following property `apoc.autoIndex.enabled=true` and looks like this is the simplest way to go... but I'm not sure or will it automatically add the newly created nodes to the index also? Or do I need to use a separate trigger for new nodes and manually add them to this index? – alexanoid Dec 07 '18 at 14:05
  • I verified - `apoc.autoIndex.enabled=true` perfectly works for new nodes also. I think this is exactly what I need. – alexanoid Dec 07 '18 at 21:26

0 Answers0