public Long getTypedNodeTokenCount(String type, String pattern, Boolean sensitive) { Long frequency = (long) 0; try ( Transaction ignored = database.beginTx(); ) { IndexManager index = database.index(); Index<Node> nodeIndex = index.forNodes(type); for (Node match : nodeIndex.get("label", pattern)) { if (!sensitive || ((String) match.getProperty("label")).equals(pattern)) { Object count = match.getProperty("token_count"); if (count instanceof Integer) frequency = frequency + new Long((Integer) count); else if (count instanceof Long) frequency = frequency + (Long) count; } } } return frequency; }
protected Response getMetadataLabel(final String label) { StreamingOutput stream = new StreamingOutput() { @Override public void write( OutputStream os ) throws IOException, WebApplicationException { try ( Transaction tx = database.beginTx() ) { JsonGenerator jg = objectMapper.getFactory().createGenerator(os, JsonEncoding.UTF8); IndexManager index = database.index(); Index<Node> metadata = index.forNodes("Metadatum"); jg.writeStartArray(); for ( Node metadatum : metadata.query( "label:"+label ) ) { executor.writeField(metadatum, jg); } jg.writeEndArray(); jg.flush(); tx.success(); } } }; return Response.ok().entity( stream ).type( MediaType.APPLICATION_JSON ).build(); }
protected Response updateMetadataLabel(final String label, final String property, final String value) { StreamingOutput stream = new StreamingOutput() { @Override public void write( OutputStream os ) throws IOException, WebApplicationException { try ( Transaction tx = database.beginTx() ) { JsonGenerator jg = objectMapper.getFactory().createGenerator(os, JsonEncoding.UTF8); IndexManager index = database.index(); Index<Node> metadata = index.forNodes("Metadatum"); for ( Node metadatum : metadata.query( "label:"+label ) ) { if (property.equals("explorable") || property.equals("searchable")) metadatum.setProperty(property, Boolean.valueOf(value)); else metadatum.setProperty(property, value); } jg.writeString("Updated "+label); jg.flush(); tx.success(); } } }; return Response.ok().entity( stream ).type( MediaType.APPLICATION_JSON ).build(); }
protected Response getMetadataGroupKey(final String group, final String key) { StreamingOutput stream = new StreamingOutput() { @Override public void write( OutputStream os ) throws IOException, WebApplicationException { try ( Transaction tx = database.beginTx() ) { JsonGenerator jg = objectMapper.getFactory().createGenerator(os, JsonEncoding.UTF8); IndexManager index = database.index(); Index<Node> metadata = index.forNodes("Metadatum"); jg.writeStartArray(); for ( Node metadatum : metadata.query( "group:"+group+" AND key:"+key ) ) { executor.writeField(metadatum, jg); } jg.writeEndArray(); jg.flush(); tx.success(); } } }; return Response.ok().entity( stream ).type( MediaType.APPLICATION_JSON ).build(); }
@Test public void deleteIndex() { GraphDatabaseService graphDb = new TestGraphDatabaseFactory().newImpermanentDatabase(); try ( Transaction tx = graphDb.beginTx() ) { // START SNIPPET: delete IndexManager index = graphDb.index(); Index<Node> actors = index.forNodes( "actors" ); actors.delete(); // END SNIPPET: delete tx.success(); } assertFalse( indexExists( graphDb ) ); graphDb.shutdown(); }
@Test public void fulltext() { // START SNIPPET: fulltext IndexManager index = graphDb.index(); Index<Node> fulltextMovies = index.forNodes( "movies-fulltext", MapUtil.stringMap( IndexManager.PROVIDER, "lucene", "type", "fulltext" ) ); // END SNIPPET: fulltext Index<Node> movies = index.forNodes( "movies" ); Node theMatrix = movies.get( "title", "The Matrix" ).getSingle(); Node theMatrixReloaded = movies.get( "title", "The Matrix Reloaded" ).getSingle(); // START SNIPPET: fulltext fulltextMovies.add( theMatrix, "title", "The Matrix" ); fulltextMovies.add( theMatrixReloaded, "title", "The Matrix Reloaded" ); // search in the fulltext index Node found = fulltextMovies.query( "title", "reloAdEd" ).getSingle(); // END SNIPPET: fulltext assertEquals( theMatrixReloaded, found ); }
private void makeSureAdditionsCanBeRemoved( boolean restartTx ) { Index<Node> index = nodeIndex( LuceneIndexImplementation.EXACT_CONFIG ); String key = "name"; String value = "Mattias"; assertNull( index.get( key, value ).getSingle() ); Node node = graphDb.createNode(); index.add( node, key, value ); if ( restartTx ) { restartTx(); } assertEquals( node, index.get( key, value ).getSingle() ); index.remove( node, key, value ); assertNull( index.get( key, value ).getSingle() ); restartTx(); assertNull( index.get( key, value ).getSingle() ); node.delete(); index.delete(); }
private void makeSureSomeAdditionsCanBeRemoved( boolean restartTx ) { Index<Node> index = nodeIndex( LuceneIndexImplementation.EXACT_CONFIG ); String key1 = "name"; String key2 = "title"; String value1 = "Mattias"; assertNull( index.get( key1, value1 ).getSingle() ); assertNull( index.get( key2, value1 ).getSingle() ); Node node = graphDb.createNode(); Node node2 = graphDb.createNode(); index.add( node, key1, value1 ); index.add( node, key2, value1 ); index.add( node2, key1, value1 ); if ( restartTx ) { restartTx(); } index.remove( node, key1, value1 ); index.remove( node, key2, value1 ); assertEquals( node2, index.get( key1, value1 ).getSingle() ); assertNull( index.get( key2, value1 ).getSingle() ); assertEquals( node2, index.get( key1, value1 ).getSingle() ); assertNull( index.get( key2, value1 ).getSingle() ); node.delete(); index.delete(); }
@Test public void makeSureWildcardQueriesCanBeAsked() { Index<Node> index = nodeIndex( LuceneIndexImplementation.EXACT_CONFIG ); String key = "name"; String value1 = "neo4j"; String value2 = "nescafe"; Node node1 = graphDb.createNode(); Node node2 = graphDb.createNode(); index.add( node1, key, value1 ); index.add( node2, key, value2 ); for ( int i = 0; i < 2; i++ ) { assertThat( index.query( key, "neo*" ), contains( node1 ) ); assertThat( index.query( key, "n?o4j" ), contains( node1 ) ); assertThat( index.query( key, "ne*" ), contains( node1, node2 ) ); assertThat( index.query( key + ":neo4j" ), contains( node1 ) ); assertThat( index.query( key + ":neo*" ), contains( node1 ) ); assertThat( index.query( key + ":n?o4j" ), contains( node1 ) ); assertThat( index.query( key + ":ne*" ), contains( node1, node2 ) ); restartTx(); } index.delete(); }
@Test public void testIndexNumberAsString() { Index<Node> index = nodeIndex( LuceneIndexImplementation.EXACT_CONFIG ); Node node1 = graphDb.createNode(); index.add( node1, "key", 10 ); for ( int i = 0; i < 2; i++ ) { assertEquals( node1, index.get( "key", 10 ).getSingle() ); assertEquals( node1, index.get( "key", "10" ).getSingle() ); assertEquals( node1, index.query( "key", 10 ).getSingle() ); assertEquals( node1, index.query( "key", "10" ).getSingle() ); restartTx(); } }
@Test public void makeSureFulltextConfigIsCaseInsensitiveByDefault() { Index<Node> index = nodeIndex( LuceneIndexImplementation.FULLTEXT_CONFIG ); Node node = graphDb.createNode(); String key = "name"; String value = "Mattias Persson"; index.add( node, key, value ); for ( int i = 0; i < 2; i++ ) { assertThat( index.query( "name", "[A TO Z]" ), contains( node ) ); assertThat( index.query( "name", "[a TO z]" ), contains( node ) ); assertThat( index.query( "name", "Mattias" ), contains( node ) ); assertThat( index.query( "name", "mattias" ), contains( node ) ); assertThat( index.query( "name", "Matt*" ), contains( node ) ); assertThat( index.query( "name", "matt*" ), contains( node ) ); restartTx(); } }
@Test public void makeSureFulltextIndexCanBeCaseSensitive() { Index<Node> index = nodeIndex( MapUtil.stringMap( new HashMap<>( LuceneIndexImplementation.FULLTEXT_CONFIG ), "to_lower_case", "false" ) ); Node node = graphDb.createNode(); String key = "name"; String value = "Mattias Persson"; index.add( node, key, value ); for ( int i = 0; i < 2; i++ ) { assertThat( index.query( "name", "[A TO Z]" ), contains( node ) ); assertThat( index.query( "name", "[a TO z]" ), isEmpty() ); assertThat( index.query( "name", "Matt*" ), contains( node ) ); assertThat( index.query( "name", "matt*" ), isEmpty() ); assertThat( index.query( "name", "Persson" ), contains( node ) ); assertThat( index.query( "name", "persson" ), isEmpty() ); restartTx(); } }
@Test public void testScoring() { Index<Node> index = nodeIndex( LuceneIndexImplementation.FULLTEXT_CONFIG ); Node node1 = graphDb.createNode(); Node node2 = graphDb.createNode(); String key = "text"; // Where the heck did I get this sentence from? index.add( node1, key, "a time where no one was really awake" ); index.add( node2, key, "once upon a time there was" ); restartTx(); IndexHits<Node> hits = index.query( key, new QueryContext( "once upon a time was" ).sort( Sort.RELEVANCE ) ); Node hit1 = hits.next(); float score1 = hits.currentScore(); Node hit2 = hits.next(); float score2 = hits.currentScore(); assertEquals( node2, hit1 ); assertEquals( node1, hit2 ); assertTrue( "Score 1 (" + score1 + ") should have been higher than score 2 (" + score2 + ")", score1 > score2 ); }
@Test public void testCombinedHitsSizeProblem() { Index<Node> index = nodeIndex( LuceneIndexImplementation.EXACT_CONFIG ); Node node1 = graphDb.createNode(); Node node2 = graphDb.createNode(); Node node3 = graphDb.createNode(); String key = "key"; String value = "value"; index.add( node1, key, value ); index.add( node2, key, value ); restartTx(); index.add( node3, key, value ); IndexHits<Node> hits = index.get( key, value ); assertEquals( 3, hits.size() ); }
@Test public void testSortingWithTopHitsInPartCommittedPartLocal() { Index<Node> index = nodeIndex( LuceneIndexImplementation.FULLTEXT_CONFIG ); Node first = graphDb.createNode(); Node second = graphDb.createNode(); Node third = graphDb.createNode(); Node fourth = graphDb.createNode(); String key = "key"; index.add( third, key, "ccc" ); index.add( second, key, "bbb" ); restartTx(); index.add( fourth, key, "ddd" ); index.add( first, key, "aaa" ); assertContainsInOrder( index.query( key, new QueryContext( "*" ).sort( key ) ), first, second, third, fourth ); assertContainsInOrder( index.query( key, new QueryContext( "*" ).sort( key ).top( 2 ) ), first, second ); }
@Test public void shouldNotFindValueDeletedInSameTx() { Index<Node> nodeIndex = graphDb.index().forNodes( "size-after-removal" ); Node node = graphDb.createNode(); nodeIndex.add( node, "key", "value" ); restartTx(); nodeIndex.remove( node ); for ( int i = 0; i < 2; i++ ) { IndexHits<Node> hits = nodeIndex.get( "key", "value" ); assertEquals( 0, hits.size() ); assertNull( hits.getSingle() ); hits.close(); restartTx(); } }
@Test public void updateIndex() throws Exception { String TEXT = "text"; String NUMERIC = "numeric"; String TEXT_1 = "text_1"; Index<Node> index = nodeIndex( LuceneIndexImplementation.EXACT_CONFIG ); Node n = graphDb.createNode(); index.add(n, NUMERIC, new ValueContext(5).indexNumeric()); index.add(n, TEXT, "text"); index.add(n, TEXT_1, "text"); commitTx(); beginTx(); assertNotNull( index.query( QueryContext.numericRange( NUMERIC, 5, 5, true, true ) ).getSingle() ); assertNotNull( index.get( TEXT_1, "text" ).getSingle() ); index.remove( n, TEXT, "text" ); index.add( n, TEXT, "text 1" ); commitTx(); beginTx(); assertNotNull( index.get(TEXT_1, "text").getSingle() ); assertNotNull( index.query(QueryContext.numericRange(NUMERIC, 5, 5, true, true)).getSingle() ); }
@Ignore( "an issue that should be fixed at some point" ) @Test( expected = NotFoundException.class ) public void shouldNotBeAbleToIndexNodeThatIsNotCommitted() throws Exception { Index<Node> index = nodeIndex( LuceneIndexImplementation.EXACT_CONFIG ); Node node = graphDb.createNode(); String key = "noob"; String value = "Johan"; WorkThread thread = new WorkThread( "other thread", index, graphDb, node ); thread.beginTransaction(); try { thread.add( node, key, value ); } finally { thread.rollback(); } }
@Test public void putIfAbsentSingleThreaded() { Index<Node> index = nodeIndex( LuceneIndexImplementation.EXACT_CONFIG ); Node node = graphDb.createNode(); String key = "name"; String value = "Mattias"; String value2 = "Persson"; assertNull( index.putIfAbsent( node, key, value ) ); assertEquals( node, index.get( key, value ).getSingle() ); assertNotNull( index.putIfAbsent( node, key, value ) ); assertNull( index.putIfAbsent( node, key, value2 ) ); assertNotNull( index.putIfAbsent( node, key, value2 ) ); restartTx(); assertNotNull( index.putIfAbsent( node, key, value ) ); assertNotNull( index.putIfAbsent( node, key, value2 ) ); assertEquals( node, index.get( key, value ).getSingle() ); }
@Test public void putIfAbsentShouldntBlockIfNotAbsent() throws Exception { Index<Node> index = nodeIndex( LuceneIndexImplementation.EXACT_CONFIG ); Node node = graphDb.createNode(); String key = "key"; String value = "value"; index.add( node, key, value ); restartTx(); WorkThread otherThread = new WorkThread( "other thread", index, graphDb, node ); otherThread.beginTransaction(); // Should not grab lock index.putIfAbsent( node, key, value ); // Should be able to complete right away assertNotNull( otherThread.putIfAbsent( node, key, value ).get() ); otherThread.commit(); commitTx(); otherThread.close(); }
@Test public void getOrCreateMultiThreaded() throws Exception { Index<Node> index = nodeIndex( LuceneIndexImplementation.EXACT_CONFIG ); String key = "name"; String value = "Mattias"; WorkThread t1 = new WorkThread( "t1", index, graphDb, null ); WorkThread t2 = new WorkThread( "t2", index, graphDb, null ); t1.beginTransaction(); t2.beginTransaction(); Node node = t2.getOrCreate( key, value, 0 ).get(); assertNotNull( node ); assertEquals( 0, t2.getProperty( node, key ) ); Future<Node> futurePut = t1.getOrCreate( key, value, 1 ); t1.waitUntilWaiting(); t2.commit(); assertEquals( node, futurePut.get() ); assertEquals( 0, t1.getProperty( node, key ) ); t1.commit(); assertEquals( node, index.get( key, value ).getSingle() ); t1.close(); t2.close(); }
@Test public void shouldBeAbleToQueryAllMatchingDocsAfterRemovingWithWildcard() throws Exception { // GIVEN Index<Node> index = nodeIndex( EXACT_CONFIG ); Node node1 = graphDb.createNode(); index.add( node1, "name", "Mattias" ); finishTx( true ); beginTx(); // WHEN index.remove( node1, "name" ); Set<Node> nodes = asSet( (Iterable<Node>) index.query( "*:*" ) ); // THEN assertEquals( asSet(), nodes ); }
@Test public void canReadAndUpgradeOldIndexStoreFormat() throws Exception { File path = new File( "target/var/old-index-store" ); Neo4jTestCase.deleteFileOrDirectory( path ); startDatabase( path ).shutdown(); InputStream stream = getClass().getClassLoader().getResourceAsStream( "old-index.db" ); writeFile( stream, new File( path, "index.db" ) ); GraphDatabaseService db = startDatabase( path ); try ( Transaction ignore = db.beginTx() ) { assertTrue( db.index().existsForNodes( "indexOne" ) ); Index<Node> indexOne = db.index().forNodes( "indexOne" ); verifyConfiguration( db, indexOne, LuceneIndexImplementation.EXACT_CONFIG ); assertTrue( db.index().existsForNodes( "indexTwo" ) ); Index<Node> indexTwo = db.index().forNodes( "indexTwo" ); verifyConfiguration( db, indexTwo, LuceneIndexImplementation.FULLTEXT_CONFIG ); assertTrue( db.index().existsForRelationships( "indexThree" ) ); Index<Relationship> indexThree = db.index().forRelationships( "indexThree" ); verifyConfiguration( db, indexThree, LuceneIndexImplementation.EXACT_CONFIG ); } db.shutdown(); }
@Test public void canDeleteIndexEvenIfEntitiesAreFoundToBeAbandonedInTheSameTx() throws Exception { // create and index a node Index<Node> nodeIndex = graphDb.index().forNodes( "index" ); Node node = graphDb.createNode(); nodeIndex.add( node, "key", "value" ); // make sure to commit the creation of the entry restartTx(); // delete the node to abandon the index entry node.delete(); restartTx(); // iterate over all nodes indexed with the key to discover abandoned for ( @SuppressWarnings( "unused" ) Node hit : nodeIndex.get( "key", "value" ) ) { ; } nodeIndex.delete(); restartTx(); }
public static void main( String[] args ) { GraphDatabaseService db = new TestGraphDatabaseFactory().newEmbeddedDatabase( args[0] ); try ( Transaction tx = db.beginTx() ) { Index<Relationship> index = db.index().forRelationships( "myIndex" ); Node node = db.createNode(); Relationship relationship = db.createNode().createRelationshipTo( node, DynamicRelationshipType.withName( "KNOWS" ) ); index.add( relationship, "key", "value" ); tx.success(); } db.shutdown(); System.exit( 0 ); }
public static void main( String[] args ) { GraphDatabaseService db = new TestGraphDatabaseFactory().newEmbeddedDatabase( args[0] ); Index<Node> index; Index<Node> index2; try ( Transaction tx = db.beginTx() ) { index = db.index().forNodes( "index" ); index2 = db.index().forNodes( "index2" ); Node node = db.createNode(); index.add( node, "key", "value" ); tx.success(); } try ( Transaction tx = db.beginTx() ) { index.delete(); index2.add( db.createNode(), "key", "value" ); tx.success(); } db.shutdown(); System.exit( 0 ); }
@GET @Path("/tags/{label}") public Response getPosTagByLabel(@PathParam("label") final String label) { StreamingOutput stream = new StreamingOutput() { @Override public void write( OutputStream os ) throws IOException, WebApplicationException { try ( Transaction ignored = database.beginTx() ) { JsonGenerator jg = objectMapper.getFactory().createGenerator(os, JsonEncoding.UTF8); IndexManager index = database.index(); Index<Node> postags = index.forNodes("PosTag"); IndexHits<Node> hits = postags.get( "label", label ); Node postag = hits.getSingle(); jg.writeStartObject(); for (String field : postag.getPropertyKeys()) { Object value = postag.getProperty(field); if (field.contains("_count") && value instanceof Integer) jg.writeNumberField(field, (Integer) value); else if (field.contains("_count") && value instanceof Long) jg.writeNumberField(field, (Long) value); else jg.writeStringField(field, (String) postag.getProperty(field)); } jg.writeEndObject(); jg.flush(); jg.close(); } } }; return Response.ok().entity( stream ).type( MediaType.APPLICATION_JSON ).build(); }
@GET @Path("/heads/{label}") public Response getPosHeadByLabel(@PathParam("label") final String label) { StreamingOutput stream = new StreamingOutput() { @Override public void write( OutputStream os ) throws IOException, WebApplicationException { try ( Transaction ignored = database.beginTx() ) { JsonGenerator jg = objectMapper.getFactory().createGenerator(os, JsonEncoding.UTF8); IndexManager index = database.index(); Index<Node> postags = index.forNodes("PosHead"); IndexHits<Node> hits = postags.get( "label", label ); Node poshead = hits.getSingle(); jg.writeStartObject(); for (String field : poshead.getPropertyKeys()) { Object value = poshead.getProperty(field); if (field.contains("_count") && value instanceof Integer) jg.writeNumberField(field, (Integer) value); else if (field.contains("_count") && value instanceof Long) jg.writeNumberField(field, (Long) value); else jg.writeStringField(field, (String) poshead.getProperty(field)); } jg.writeEndObject(); jg.flush(); jg.close(); } } }; return Response.ok().entity( stream ).type( MediaType.APPLICATION_JSON ).build(); }
protected Response getMetadataGroupKeyValueDocuments(final String group, final String key, final String value) { StreamingOutput stream = new StreamingOutput() { @Override public void write( OutputStream os ) throws IOException, WebApplicationException { try ( Transaction tx = database.beginTx() ) { JsonGenerator jg = objectMapper.getFactory().createGenerator(os, JsonEncoding.UTF8); IndexManager index = database.index(); Index<Relationship> metadataValues = index.forRelationships("HAS_METADATUM"); List<String> documentsSeen = new ArrayList<String>(); jg.writeStartArray(); System.out.println("VALUE: "+value); for (Relationship hasMetadatum : metadataValues.get("value", value)) { Node metadatum = hasMetadatum.getEndNode(); String mGroup = (String) metadatum.getProperty("group"); String mKey = (String) metadatum.getProperty("key"); if (mGroup.equals(group) && mKey.equals(key)) { Node document = hasMetadatum.getStartNode(); String xmlid = (String) document.getProperty("xmlid"); if (!documentsSeen.contains(xmlid)) { jg.writeString(xmlid); documentsSeen.add(xmlid); } } } jg.writeEndArray(); jg.flush(); tx.success(); } } }; return Response.ok().entity( stream ).type( MediaType.APPLICATION_JSON ).build(); }
private void assertIsLuceneIndex( GraphDatabaseService db, Index<T> index ) { Map<String, String> config = db.index().getConfiguration( index ); if ( !config.get( IndexManager.PROVIDER ).equals( "lucene" ) ) // Not so hard coded please { throw new IllegalArgumentException( index + " isn't a Lucene index" ); } }
@Test public void addSomeThings() { // START SNIPPET: add Index<Node> persons = graphDb.index().forNodes( "persons" ); Node morpheus = graphDb.createNode(); Node trinity = graphDb.createNode(); Node neo = graphDb.createNode(); persons.add( morpheus, "name", "Morpheus" ); persons.add( morpheus, "rank", "Captain" ); persons.add( trinity, "name", "Trinity" ); persons.add( neo, "name", "Neo" ); persons.add( neo, "title", "The One" ); // END SNIPPET: add }
@Test public void doSomeGets() { Index<Node> persons = graphDb.index().forNodes( "persons" ); // START SNIPPET: get Node morpheus = persons.get( "name", "Morpheus" ).getSingle(); // END SNIPPET: get assertNotNull( morpheus ); }
@Test public void doSomeQueries() { Index<Node> persons = graphDb.index().forNodes( "persons" ); // START SNIPPET: query for ( Node person : persons.query( "name", "*e*" ) ) { // It will get Morpheus and Neo } Node neo = persons.query( "name:*e* AND title:\"The One\"" ).getSingle(); // END SNIPPET: query assertNotNull( neo ); }
@Test public void update() { IndexManager index = graphDb.index(); Index<Node> actors = index.forNodes( "actors" ); // START SNIPPET: update // create a node with a property // so we have something to update later on Node fishburn = graphDb.createNode(); fishburn.setProperty( "name", "Fishburn" ); // index it actors.add( fishburn, "name", fishburn.getProperty( "name" ) ); // END SNIPPET: update Node node = actors.get( "name", "Fishburn" ).getSingle(); assertEquals( fishburn, node ); // START SNIPPET: update // update the index entry // when the property value changes actors.remove( fishburn, "name", fishburn.getProperty( "name" ) ); fishburn.setProperty( "name", "Laurence Fishburn" ); actors.add( fishburn, "name", fishburn.getProperty( "name" ) ); // END SNIPPET: update node = actors.get( "name", "Fishburn" ).getSingle(); assertEquals( null, node ); node = actors.get( "name", "Laurence Fishburn" ).getSingle(); assertEquals( fishburn, node ); }
@Test public void doGetForNodes() { Index<Node> actors = graphDb.index().forNodes( "actors" ); // START SNIPPET: getSingleNode IndexHits<Node> hits = actors.get( "name", "Keanu Reeves" ); Node reeves = hits.getSingle(); // END SNIPPET: getSingleNode assertEquals( "Keanu Reeves", reeves.getProperty( "name" ) ); }
@Test public void batchInsert() throws Exception { Neo4jTestCase.deleteFileOrDirectory( new File( "target/neo4jdb-batchinsert" ) ); // START SNIPPET: batchInsert BatchInserter inserter = BatchInserters.inserter( "target/neo4jdb-batchinsert" ); BatchInserterIndexProvider indexProvider = new LuceneBatchInserterIndexProvider( inserter ); BatchInserterIndex actors = indexProvider.nodeIndex( "actors", MapUtil.stringMap( "type", "exact" ) ); actors.setCacheCapacity( "name", 100000 ); Map<String, Object> properties = MapUtil.map( "name", "Keanu Reeves" ); long node = inserter.createNode( properties ); actors.add( node, properties ); //make the changes visible for reading, use this sparsely, requires IO! actors.flush(); // Make sure to shut down the index provider as well indexProvider.shutdown(); inserter.shutdown(); // END SNIPPET: batchInsert GraphDatabaseService db = new TestGraphDatabaseFactory().newEmbeddedDatabase( "target/neo4jdb-batchinsert" ); try ( Transaction tx = db.beginTx() ) { Index<Node> index = db.index().forNodes( "actors" ); Node reeves = index.get( "name", "Keanu Reeves" ).next(); assertEquals( node, reeves.getId() ); } db.shutdown(); }
@Test public void testNumericValues() { BatchInserterIndexProvider provider = new LuceneBatchInserterIndexProviderNewImpl( inserter ); BatchInserterIndex index = provider.nodeIndex( "mine", EXACT_CONFIG ); long node1 = inserter.createNode( null ); index.add( node1, map( "number", numeric( 45 ) ) ); long node2 = inserter.createNode( null ); index.add( node2, map( "number", numeric( 21 ) ) ); index.flush(); assertContains( index.query( "number", newIntRange( "number", 21, 50, true, true ) ), node1, node2 ); provider.shutdown(); switchToGraphDatabaseService(); try ( Transaction transaction = db.beginTx() ) { Node n1 = db.getNodeById( node1 ); db.getNodeById( node2 ); Index<Node> idx = db.index().forNodes( "mine" ); assertContains( idx.query( "number", newIntRange( "number", 21, 45, false, true ) ), n1 ); transaction.success(); } }
@Test public void indexCreationConfigRaceCondition() throws Exception { // Since this is a probability test and not a precise test run do the run // a couple of times to be sure. for ( int run = 0; run < 10; run++ ) { final int r = run; final CountDownLatch latch = new CountDownLatch( 1 ); ExecutorService executor = newCachedThreadPool(); for ( int thread = 0; thread < 10; thread++ ) { executor.submit( new Runnable() { @Override public void run() { try ( Transaction tx = db.beginTx() ) { latch.await(); Index<Node> index = db.index().forNodes( "index" + r ); Node node = db.createNode(); index.add( node, "name", "Name" ); tx.success(); } catch ( InterruptedException e ) { Thread.interrupted(); } } } ); } latch.countDown(); executor.shutdown(); executor.awaitTermination( 10, TimeUnit.SECONDS ); verifyThatIndexCreationTransactionIsTheFirstOne(); } }