The CREATE INDEX <indexName> command is not idempotent and will cause an error if the given index already exists. I'm new to neo4j, and can't find a predicate that avoids this error. I've tried various permutations of ANY(...), and they all barf at "db.indexes()".
Since CREATE INDEX ... fails if the index exists and DROP INDEX ... fails if it doesn't, I don't know how to write a .cypher file that creates the index only if needed.
A short form might be something like CREATE INDEX indexName FOR (c:SomeLabel) ON (c.someProperty) IF NOT EXISTS, but of course that short form doesn't exist.
Is there some way to do this with a predicate, subquery or some such expression?
As of Neo4j 4.1.3, a new index creation syntax has been introduced to do just that
CREATE INDEX myIndex IF NOT EXISTS FOR (t:Test) ON (t.id)
Indexes for search performance
You can use the apoc.schema.node.indexExists function to check whether an index exists before creating it.
For example, this query will create the :Foo(id) index if it does not already exist:
WITH 1 AS ignored
WHERE NOT apoc.schema.node.indexExists('Foo', ['id'])
CALL db.createIndex('index_name', ['Foo'], ['id'], 'native-btree-1.0') YIELD name, labels, properties
RETURN name, labels, properties
For some reason, the Cypher planner currently is not able to parse the normal CREATE INDEX index_name ... syntax after the above WHERE clause, so this query uses the db.createIndex procedure instead.
There is also a much more powerful APOC procedure, apoc.schema.assert, but it may be overkill for your requirements.
By default, the command is ignored if the index exists.
Can you test the following?
CREATE (n:Car {id: 1});
Added 1 label, created 1 node, set 1 property, completed after 23 ms.
CREATE INDEX ON :Car (id);
1st execution: Added 1 index, completed after 6 ms.
2nd execution : (no changes, no records)
I tried both suggestions, and neither solves my issue. I don't have time to discover, through trial-and-error, how to install APOC in my environment.
The first line of mbh86's answer is inaccurate, at least in my system. The command is not ignored, it fails with an error. So if anything else is in the same cypher script, it will fail.
The best I can do is apparently to wrap the CREATE INDEX in a command-line string, run that string from either a bash or python script, run it, and check the return code from the calling program.
I appreciate the effort of both commentators, and I didn't want to leave either hanging.
What happens if you replay a CREATE INDEX ON command on a Neo4J database that already has this index.
Does Neo4J simply ignore that command?
The first time you will execute it, Neo4j will :
Create the index
Create a background job to populate it
As a query stat, tell you that an index is created
At the second attempt, you will have :
No error
No impact on the existed index
An empty stat for the query (ie. no new index created)
Cheers
the 2nd invocation of the create index will run, produce no error, but also not report the index alredy exists and in the end you will still only have 1 index
for testing and development reasons we reindex our data from a rails app by deleting an index and recreate with mapping and import existing documents.
But after recreating the index, elasticsearch returns other results than expected and before recreating. If we restart the elasticsearch instance, the results as expected.
This is how we recreate the index.
Tire.index indexname do
delete
create _mappings
import _objects
refresh
end
We also checked the search query directly via curl on elastic search, but we got not the expected result. After restarting the elastic search daemon, same query returns expected data.
What have to be done or what is expected of an elasticsearch instance to return correct data after recreating an index with the same name without restarting? We also tried creating new indexes with timestamp names and aliasing the index name to these index, but with same results.
thanks in advance
I followed the following post to test facebook friends of friends in Neo4j 2.0.1
http://blog.neo4j.org/2013/06/fun-with-facebook-in-neo4j_19.html
I am able to create the nodes successfully.. Auto Indexing is enabled
Here is the create node statement - create (n{name:'User 123', type:'Facebook'});
This works fine
When I create the relationships, I am getting this notification: "Nothing was created and No data Returned"
Here is the create Relationship statement
start n1=node:node_auto_index(name='User 123'),n2=node:node_auto_index(name='User XYZ') CREATE n1-[:IS_A_FRIEND_OF]->n2;
Any help is very much appreciated. I am new to neo4j and trying to get my hands dirty by learning some stuff.
Neo4j 2.0 has a new feature called schema indexes. For most use cases it's beneficial to use schema indexing instead of autoindexing.
For your example, I'd move the value of the type property to become a label.
First, create the index for property name based on label Facebook:
CREATE INDEX ON :Facebook(name)
The CREATE looks like:
CREATE (n:Facebook {name:'User 123'})
For creating the relationships use:
MATCH (n1:Facebook {name:'User 123'}),n2=(n2:Facebook {name:'User XYZ'})
CREATE n1-[:IS_A_FRIEND_OF]->n2
You might also look into Neo4j 2.0's new MERGE statement.
I have a neo4j db with node_auto_indexing and relationship_auto_indexing at true and node_keys_indexable and relationship_keys_indexable with some keys.
I use neo4j 1.9.3 ans SDN 2.3.1.RELEASE
When I try to delete nodes using neoTemplate neo4jTemplate.delete(t); I get a read only index exception.
I try to disable auto index like that :
neo4jTemplate.getGraphDatabaseService().index().getNodeAutoIndexer().stopAutoIndexingProperty("myprop");
or
neo4jTemplate.getGraphDatabaseService().index().getNodeAutoIndexer().setEnabled(false);
don't work.
Any idea ?