Search code examples
javaindexingnumericneo4jbatch-insert

Numeric index during batch insertion in Neo4j


I'm using batch insertion of nodes and relationships into Neo4j graph database. Everything works, including the index on multiple properties ([String] name, [int] id). However, when I try to query the index on property "id" by range, it returns no results.

The problem, as I derived it from the non-batch example, is that I can't supply numeric ValueContext to the BatchInserterIndex like so:

Map<String, Object> properties = new HashMap<String, Object>(2);

properties.put("name", urs.getString(1));

// I can do this:
properties.put("id", urs.getInt(2));

// But not this (throws an "invalid type" exception):
// properties.put("id", new ValueContext( urs.getInt(2) ).indexNumeric());

long node_id = inserter.createNode(properties);
index.add(node_id, properties);

I haven't found any documentation regarding numeric indexing during batch insertion.

The query code is as follows:

IndexManager index = gdb.index();
Index<Node> people = index.forNodes("people");
IndexHits<Node> hits = people.query(
    QueryContext.numericRange("id", min_id, max_id)
  );

Is it at all possible to add numeric index in a batch insertion operation, so that I can then query values by range?

Thanks.


Edit

What I was doing wrong is I tried to pass the same property map to createNode() and index.add(). The former was crashing, because it doesn't need ValueContext and doesn't understand it. So, be sure to pass different property maps to these methods and include ValueContext-ed numeric value in the one meant for index.add:

Long value = 1L;

long node_id = inserter.createNode(
  MapUtil.map("id", value, "other_prop", other_value));

index.add(node_id,
  MapUtil.map("id", ValueContext.numeric( value ), "other_prop", other_value));

Solution

  • Which version of neo4j are you using? It's working in the latest release