rawsh
rawsh

Reputation: 425

How to store text references in Neo4j

Suppose I have the following text:

text (ref 1) more text

I know where all the references are. How can I create neo4j nodes for all references and refer to them in text? As in seeing that there is the 10th reference in the fourth paragraph.

Upvotes: 0

Views: 80

Answers (1)

Jonathan March
Jonathan March

Reputation: 175

There are quite a few ways to accomplish what you're asking (inside or outside Neo4j). If you can get the text with references into Neo4j (and they're formatted like your question states), you can snip out the reference ids and create nodes as needed. Here's a basic example:

CREATE CONSTRAINT ON (r:Ref) ASSERT r.id IS UNIQUE;

  WITH 'text (ref 1) more (ref 1) text (ref 20)' AS txt
  WITH [x IN split(txt, '(ref ')[1..] | split(x, ')')[0]] AS ref_ids
UNWIND ref_ids AS id
  WITH DISTINCT id
 MERGE (:Ref {id:id})
RETURN count(id)

This will create Nodes in Neo4j that can be looked up by reference id in the text. You will obviously want to put more metadata on these, but that's highly dependent on your requirements.

Note: the MERGE will CREATE the node, but not if it already exists. This existence check requires a lookup operation, which will be an expensive scan without an index. Creating the CONSTRAINT first ensures we'll have an index for those lookups.

Upvotes: 1

Related Questions