How do I retrieve the nodes deleted after a cypher query? - java

So I'm deleting some nodes from a database, and I want to retrieve the nodes I've deleted so far so that I don't go looking for them later.
How do I get neo4j ids of the deleted nodes in java code?

You can return the ids of the nodes as you're deleting them:
MATCH (n:SomeLabel)
DELETE n
RETURN id(n)
However, as mentioned in the documentation, ids can be reused by Neo4j, so it's generally a bad idea to keep referencing them outside of a transaction (not sure if that's your use case).

Related

Spring Data Neo4j - Howto limit all queries to a subset of nodes (depending on the request header)

in our project we are using Spring Data Neo4j and have a lot of queries. When we want to update a node with new data, we do not actually update the node, instead we create a new node with a PREVIOUS relation to the current node. Furthermore we can make revisions. These are nodes with a CONTAINS relation to the nodes belonging to this revision.
Now the client passes in the request header an ID of a revision and the executed queries should then only contain the nodes which are either the newest (so no PREVIOUS relation exists there) or are part of the revision. We want to limit the queried nodes to a subset of nodes.
Now I don't want to change every query manually and I think that there is an easier way but unfortunately I haven't found it anywhere after a long search.
If your model looks like the one below and follows your description, this should do it.
Assuming a parameter ID
MATCH (r:Revision {ID: $id})
MATCH (n)
WHERE (n)<-[:CONTAINS]-(r)
OR NOT (n)-[:PREVIOUS]->()
RETURN n

Get all session-cached objects in Hibernate

I have a method which executes HQL using org.hibernate.Query.executeUpdate() and changes some rows in a database. If some of affected by the query rows were previously loaded into the current session (e.g. using Session.get()), they are now stale and need refreshing.
But I want that method to be independent of a previous work with the session, and not to track all loaded objects that might be affected in order to refresh them afterwards. Is it possible with Hibernate to retrieve and iterate through objects in the 1-level cache?
I've found the following solution for the issue, which works for me:
Map.Entry<Object,EntityEntry>[] entities = ((SessionImplementor)session).getPersistenceContext().reentrantSafeEntityEntries()

Do i have to remove from Index if i delete using Cypher?

Given i have node or, relation that is added to graph Indexes. When i delete the node / relation, do I also have to remove from Index? We delete within transactions from java API as well us using cypher
If you are using auto indexing then Neo4j will take care of removing it from the index. If you are using manual(or Explicit) indexing then I know there is a cypher way to delete the entire index but not sure if Neo4j supports removing individual entries from the manual index. You can check summarization on different Neo4j indexes here.
Docs on index manipulation with cypher.

How to delete whole child entities by ancestor?

I'd like to reduce the number of queries to delete entities in Google App Engine.
I already know how to delete them with ancestor as below sequences.
Set an ancestor to query and fetch them.
Convert entities to keys and delete them using keys.
I'd like to remove first step.
My expectation is deleting all entities by ancestor without fetching as below.
DELETE FROM DS1 WHERE ancestor is "PARENT"
Is it possible?
There's no way to delete entities like that. You will need to access them by Key and batch delete.
Though there's a query type that better suits your needs, that is keys-only query, as you appear to be querying for full entities to delete them.

Updating lucene index - the most popular way

I am updating lucene index onec a day. My strategy in general is:
Find all objects in DB that was modified since last index generation.
Create new tmp-index for these objects. (old index is stil available)
Remove all new indexed Documents (they are in tmp-index) from the old index using IndexWriter.deleteDocuments(Term)
Merge old index and tmp-index using IndexWriter.addIndexes(...)
I have found that in lucene wiki: There is no direct update procedure in Lucene...
I have found also that in lucene 4.1.0 doc: A document can be updated with updateDocument...
I have tried IndexWriter.updateDocument(Term, Document) but then performing search with filter I got NPE from one of my methods what not happens when I update index as describe in 1-4. Have anyone had a similar problem? How do you update your index?
What I do is basically this:
I keep a persistent IndexReader/Readers, this will keep the state that it has since it was created.
I start to delete and create all documents once again. I think I just do a deleteAll() and then recreate them (addDocument()).
I commit, which will activate all those changes.
I drop all IndexReaders that I have, so the next time the system request a Reader, it will create it and store it for subsequent requests.
The updateDocument is basically a delete/create, afaik.
You might want to use a SearcherManager to get new IndexSearchers as you update the index with a IndexWriter. I see no need for using a temporary index?

Categories

Resources