Neo4j display subgraph based on multiple paths - java
I want to display a subgraph in Neo4j(COMMUNITY EDITION on localhost) based on multiple paths. The paths are the result of a custom traversalDescription() with a special evaluate(Path path). The intention was to ignore a special sequence of relationships and nodes(details to sequence). As far as i know its not possible in a cypher query.
The result looks like this:
(268911)
(268911)<--[REL1,151]--(276650)
(268911)<--[REL1,151]--(276650)<--[REL2,715]--(276651)
(268911)<--[REL1,151]--(276650)<--[REL2,715]--(276651)<--[REL1,34]--(259461)
(268911)<--[REL1,151]--(276650)<--[REL2,715]--(276651)<--[REL1,34]--(259461)<--[REL2,749]--(259466)
(268911)<--[REL1,151]--(276650)<--[REL2,715]--(276651)<--[REL1,34]--(259461)<--[REL2,749]--(259466)<--[REL1,230]--(281)
(268911)<--[REL1,151]--(276650)<--[REL2,715]--(276651)<--[REL1,34]--(259461)<--[REL2,749]--(259466)<--[REL1,230]--(281)--[REL2,2923]-->(278)
(268911)<--[REL1,151]--(276650)<--[REL2,715]--(276651)<--[REL1,34]--(259461)<--[REL2,749]--(259466)<--[REL1,230]--(281)--[REL2,2923]-->(278)<--[REL2,1034]--(9)
(268911)<--[REL1,151]--(276650)<--[REL2,715]--(276651)<--[REL1,34]--(259461)<--[REL2,749]--(259466)<--[REL1,230]--(281)--[REL2,2923]-->(278)<--[REL2,1034]--(9)--[REL2,2943]-->(6)
(268911)<--[REL1,151]--(276650)<--[REL2,715]--(276651)<--[REL1,34]--(259461)<--[REL2,749]--(259466)<--[REL1,230]--(281)--[REL2,2923]-->(278)<--[REL2,1034]--(9)--[REL2,1040]-->(396685)
(268911)<--[REL1,151]--(276650)<--[REL2,715]--(276651)<--[REL1,34]--(259461)<--[REL2,749]--(259466)<--[REL1,230]--(281)--[REL2,2923]-->(278)<--[REL2,1034]--(9)--[REL2,1040]-->(396685)<--[REL2,3047]--(396687)
(268911)<--[REL1,151]--(276650)<--[REL2,715]--(276651)<--[REL1,34]--(259461)<--[REL2,559]--(259465)
(268911)<--[REL1,151]--(276650)<--[REL2,715]--(276651)<--[REL1,34]--(259461)<--[REL2,559]--(259465)<--[REL1,294]--(257)
(268911)<--[REL1,151]--(276650)<--[REL2,715]--(276651)<--[REL1,34]--(259461)<--[REL2,559]--(259465)<--[REL1,294]--(257)--[REL2,2959]-->(255)
(268911)<--[REL1,151]--(276650)<--[REL2,715]--(276651)<--[REL1,34]--(259461)<--[REL2,559]--(259465)<--[REL1,294]--(257)--[REL2,2959]-->(255)<--[REL2,379]--(142)
(268911)<--[REL1,151]--(276650)<--[REL2,715]--(276651)<--[REL1,34]--(259461)<--[REL2,559]--(259465)<--[REL1,294]--(257)--[REL2,2959]-->(255)<--[REL2,379]--(142)--[REL2,2892]-->(139)
(268911)<--[REL1,151]--(276650)<--[REL2,715]--(276651)<--[REL1,34]--(259461)<--[REL2,559]--(259465)<--[REL1,294]--(257)--[REL2,2959]-->(255)<--[REL2,379]--(142)--[REL2,626]-->(396840)
(268911)<--[REL1,151]--(276650)<--[REL2,715]--(276651)<--[REL1,34]--(259461)<--[REL2,559]--(259465)<--[REL1,294]--(257)--[REL2,2959]-->(255)<--[REL2,379]--(142)--[REL2,626]-->(396840)<--[REL2,2988]--(396843)
(268911)<--[REL1,151]--(276650)<--[REL2,711]--(276653)
(268911)<--[REL1,151]--(276650)<--[REL2,711]--(276653)<--[REL1,74]--(259462)
(268911)<--[REL1,151]--(276650)<--[REL2,711]--(276653)<--[REL1,74]--(259462)<--[REL2,777]--(259469)
(268911)<--[REL1,151]--(276650)<--[REL2,711]--(276653)<--[REL1,74]--(259462)<--[REL2,777]--(259469)<--[REL1,12]--(283)
(268911)<--[REL1,151]--(276650)<--[REL2,711]--(276653)<--[REL1,74]--(259462)<--[REL2,777]--(259469)<--[REL1,12]--(283)--[REL2,2922]-->(279)
(268911)<--[REL1,151]--(276650)<--[REL2,711]--(276653)<--[REL1,74]--(259462)<--[REL2,777]--(259469)<--[REL1,12]--(283)--[REL2,2922]-->(279)<--[REL2,1032]--(11)
(268911)<--[REL1,151]--(276650)<--[REL2,711]--(276653)<--[REL1,74]--(259462)<--[REL2,777]--(259469)<--[REL1,12]--(283)--[REL2,2922]-->(279)<--[REL2,1032]--(11)--[REL2,2942]-->(8)
(268911)<--[REL1,151]--(276650)<--[REL2,711]--(276653)<--[REL1,74]--(259462)<--[REL2,777]--(259469)<--[REL1,12]--(283)--[REL2,2922]-->(279)<--[REL2,1032]--(11)--[REL2,1039]-->(396683)
(268911)<--[REL1,151]--(276650)<--[REL2,711]--(276653)<--[REL1,74]--(259462)<--[REL2,777]--(259469)<--[REL1,12]--(283)--[REL2,2922]-->(279)<--[REL2,1032]--(11)--[REL2,1039]-->(396683)<--[REL2,3009]--(396684)
(268911)<--[REL1,151]--(276650)<--[REL2,711]--(276653)<--[REL1,74]--(259462)<--[REL2,558]--(259464)
(268911)<--[REL1,151]--(276650)<--[REL2,711]--(276653)<--[REL1,74]--(259462)<--[REL2,558]--(259464)<--[REL1,147]--(259)
(268911)<--[REL1,151]--(276650)<--[REL2,711]--(276653)<--[REL1,74]--(259462)<--[REL2,558]--(259464)<--[REL1,147]--(259)--[REL2,2958]-->(258)
(268911)<--[REL1,151]--(276650)<--[REL2,711]--(276653)<--[REL1,74]--(259462)<--[REL2,558]--(259464)<--[REL1,147]--(259)--[REL2,2958]-->(258)<--[REL2,378]--(143)
(268911)<--[REL1,151]--(276650)<--[REL2,711]--(276653)<--[REL1,74]--(259462)<--[REL2,558]--(259464)<--[REL1,147]--(259)--[REL2,2958]-->(258)<--[REL2,378]--(143)--[REL2,2891]-->(140)
(268911)<--[REL1,151]--(276650)<--[REL2,711]--(276653)<--[REL1,74]--(259462)<--[REL2,558]--(259464)<--[REL1,147]--(259)--[REL2,2958]-->(258)<--[REL2,378]--(143)--[REL2,625]-->(396864)
(268911)<--[REL1,151]--(276650)<--[REL2,711]--(276653)<--[REL1,74]--(259462)<--[REL2,558]--(259464)<--[REL1,147]--(259)--[REL2,2958]-->(258)<--[REL2,378]--(143)--[REL2,625]-->(396864)<--[REL2,3088]--(396867)
Its basically a subgraph starting from one node showing all possible paths(ignoring the sequence). But how to display this in neo4j? Is it possible to use my traverser in neo4j as standard traverser? I want to avoid to pick every single node and query all nodes(possibly tons of nodes).
This solves the problem(for small amount of nodes, copy paste query into webui):
String query1=null,query2=null;
int ascii =65;
try ( Transaction tx = graphDb.beginTx() )
{
Traverser traverser = td.traverse(graphDb.getNodeById(id));
for ( Path path : traverser)
{
System.out.println(path.toString());
if(ascii == 65)
{
query1= "MATCH ("+Character.toString((char) ascii)+")";
query2= " WHERE id("+Character.toString((char) ascii)+")="+path.endNode().getId();
}
else
{
query1+= ",("+Character.toString((char) ascii)+")";
query2+= " AND id("+Character.toString((char) ascii)+")="+path.endNode().getId();
}
if(ascii==90)
ascii=96;
ascii++;
}
tx.success();
}
System.out.println(query1+query2+" RETURN *");
but is there any other solution?
Related
Neo4j ExecutionEngine does not return valid results
Trying to use a similar example from the sample code found here My sample function is: void query() { String nodeResult = ""; String rows = ""; String resultString; String columnsString; System.out.println("In query"); // START SNIPPET: execute ExecutionEngine engine = new ExecutionEngine( graphDb ); ExecutionResult result; try ( Transaction ignored = graphDb.beginTx() ) { result = engine.execute( "start n=node(*) where n.Name =~ '.*79.*' return n, n.Name" ); // END SNIPPET: execute // START SNIPPET: items Iterator<Node> n_column = result.columnAs( "n" ); for ( Node node : IteratorUtil.asIterable( n_column ) ) { // note: we're grabbing the name property from the node, // not from the n.name in this case. nodeResult = node + ": " + node.getProperty( "Name" ); System.out.println("In for loop"); System.out.println(nodeResult); } // END SNIPPET: items // START SNIPPET: columns List<String> columns = result.columns(); // END SNIPPET: columns // the result is now empty, get a new one result = engine.execute( "start n=node(*) where n.Name =~ '.*79.*' return n, n.Name" ); // START SNIPPET: rows for ( Map<String, Object> row : result ) { for ( Entry<String, Object> column : row.entrySet() ) { rows += column.getKey() + ": " + column.getValue() + "; "; System.out.println("nested"); } rows += "\n"; } // END SNIPPET: rows resultString = engine.execute( "start n=node(*) where n.Name =~ '.*79.*' return n.Name" ).dumpToString(); columnsString = columns.toString(); System.out.println(rows); System.out.println(resultString); System.out.println(columnsString); System.out.println("leaving"); } } When I run this in the web console I get many results (as there are multiple nodes that have an attribute of Name that contains the pattern 79. Yet running this code returns no results. The debug print statements 'in loop' and 'nested' never print either. Thus this must mean there are not results found in the Iterator, yet that doesn't make sense. And yes, I already checked and made sure that the graphDb variable is the same as the path for the web console. I have other code earlier that uses the same variable to write to the database. EDIT - More info If I place the contents of query in the same function that creates my data, I get the correct results. If I run the query by itself it returns nothing. It's almost as the query works only in the instance where I add the data and not if I come back to the database cold in a separate instance. EDIT2 - Here is a snippet of code that shows the bigger context of how it is being called and sharing the same DBHandle package ContextEngine; import ContextEngine.NeoHandle; import java.util.LinkedList; /* * Class to handle streaming data from any coded source */ public class Streamer { private NeoHandle myHandle; private String contextType; Streamer() { } public void openStream(String contextType) { myHandle = new NeoHandle(); myHandle.createDb(); } public void streamInput(String dataLine) { Context context = new Context(); /* * get database instance * write to database * check for errors * report errors & success */ System.out.println(dataLine); //apply rules to data (make ContextRules do this, send type and string of data) ContextRules contextRules = new ContextRules(); context = contextRules.processContextRules("Calls", dataLine); //write data (using linked list from contextRules) NeoProcessor processor = new NeoProcessor(myHandle); processor.processContextData(context); } public void runQuery() { NeoProcessor processor = new NeoProcessor(myHandle); processor.query(); } public void closeStream() { /* * close database instance */ myHandle.shutDown(); } } Now, if I call streamInput AND query in in the same instance (parent calls) the query returns results. If I only call query and do not enter ANY data in that instance (yet web console shows data for same query) I get nothing. Why would I have to create the Nodes and enter them into the database at runtime just to return a valid query. Shouldn't I ALWAYS get the same results with such a query?
You mention that you are using the Neo4j Browser, which comes with Neo4j. However, the example you posted is for Neo4j Embedded, which is the in-process version of Neo4j. Are you sure you are talking to the same database when you try your query in the Browser? In order to talk to Neo4j Server from Java, I'd recommend looking at the Neo4j JDBC driver, which has good support for connecting to the Neo4j server from Java. http://www.neo4j.org/develop/tools/jdbc You can set up a simple connection by adding the Neo4j JDBC jar to your classpath, available here: https://github.com/neo4j-contrib/neo4j-jdbc/releases Then just use Neo4j as any JDBC driver: Connection conn = DriverManager.getConnection("jdbc:neo4j://localhost:7474/"); ResultSet rs = conn.executeQuery("start n=node({id}) return id(n) as id", map("id", id)); while(rs.next()) { System.out.println(rs.getLong("id")); } Refer to the JDBC documentation for more advanced usage. To answer your question on why the data is not durably stored, it may be one of many reasons. I would attempt to incrementally scale back the complexity of the code to try and locate the culprit. For instance, until you've found your problem, do these one at a time: Instead of looping through the result, print it using System.out.println(result.dumpToString()); Instead of the regex query, try just MATCH (n) RETURN n, to return all data in the database Make sure the data you are seeing in the browser is not "old" data inserted earlier on, but really is an insert from your latest run of the Java program. You can verify this by deleting the data via the browser before running the Java program using MATCH (n) OPTIONAL MATCH (n)-[r]->() DELETE n,r; Make sure you are actually working against the same database directories. You can verify this by leaving the server running. If you can still start your java program, unless your Java program is using the Neo4j REST Bindings, you are not using the same directory. Two Neo4j databases cannot run against the same database directory simultaneously.
Neo4j : Retrieving All Nodes and Relationship connected to a Node in Neo4j Rest OR through Cypher
I want to Retrieve all nodes and relationship connected to a node. I Tried to do this in two ways: 1st Through Neo4j REST API i Tried this URI traverserUri = new URI( startNode.toString() + "/traverse/node" ); WebResource resource = Client.create() .resource( traverserUri ); String jsonTraverserPayload = t.toJson(); ClientResponse response = resource.accept( MediaType.APPLICATION_JSON ) .type( MediaType.APPLICATION_JSON ) .entity( jsonTraverserPayload ) .post( ClientResponse.class ); System.out.println( String.format( "POST [%s] to [%s], status code [%d], returned data: " + System.getProperty( "line.separator" ) + "%s", jsonTraverserPayload, traverserUri, response.getStatus(), response.getEntity( String.class ) ) ); response.close(); And get Following Response : [ { "outgoing_relationships" : "http://localhost:7474/db/data/node/82/relationships/out", "data" : { "band" : "The Clash", "name" : "Joe Strummer" }, "traverse" : "http://localhost:7474/db/data/node/82/traverse/{returnType}", "all_typed_relationships" : "http://localhost:7474/db/data/node/82/relationships/all/{-list|&|types}", "property" : "http://localhost:7474/db/data/node/82/properties/{key}", "all_relationships" : "http://localhost:7474/db/data/node/82/relationships/all", "self" : "http://localhost:7474/db/data/node/82", "properties" : "http://localhost:7474/db/data/node/82/properties", "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/82/relationships/out/{-list|&|types}", "incoming_relationships" : "http://localhost:7474/db/data/node/82/relationships/in", "incoming_typed_relationships" : "http://localhost:7474/db/data/node/82/relationships/in/{-list|&|types}", "create_relationship" : "http://localhost:7474/db/data/node/82/relationships" }, { "outgoing_relationships" : "http://localhost:7474/db/data/node/83/relationships/out", "data" : { }] But the problem is if i want to see the relationship of this node again i will have to hit the link "http://localhost:7474/db/data/node/82/relationships/all" Cant we get Data in which Node and its relationship are shown directly instead of link to relationship without hitting the link again???? 2nd thing I have tried to do is to get this from cypher query : START a=node(3) MATCH (a)-[:KNOWS]->(b)-[:KNOWS]->(c)-[:KNOWS]->(d) RETURN a,b,c,d But this also didn't work because at (b) and (c) there will be multiple values as a result for which i will have to iterate and write another query Cant we get this done in single query because i have so many connected relationship that it is getting hard to iterate again and again. Any Help would be Appreaciated.
It's easy to get all nodes connected to a given node with Cypher START a=node(3) MATCH (a)-[:KNOWS*]->(d) RETURN distinct d But if you have large number of connected nodes and deep connections, you might not get a good performance. If you know the bounds of the connections, specify it explicitly in the query would be helpful for performance, START a=node(3) MATCH (a)-[:KNOWS*1..3]->(d) RETURN Distinct d
Regarding the question about multiple nodes, or duplicate nodes. I understand what you mean. Here is something I did with such a query to weed out duplicates. More about if a KNOWS b which KNOWS c, but c is really a. Kind of like that. We can use something like WHERE NOT start player=node({0}) match player-[:player.active*2..2]-friendsOfFriends, where not(player-[:player.active]-friendsOfFriends) and player <> friendsOfFriends return distinct friendsOfFriends order by friendsOfFriends.username asc
If you make your query MATCH (a)-[r1:KNOWS]->(b)-[r2:KNOWS]->(c)-[r3:KNOWS]->(d) RETURN a,r1,b,r2,c,r3,d; The r(x) will return the respective details regarding the relationship. There will be a "row" for each path that matches the query. If you define your deserializer so it recognizes the r(x) and constructs a relationship rather than an entity then you should be able to do it all in one query.
How to run query on Apache Jackrabbit, explain with example
I am using Apache Jackrabbit as a database. In my case, root node has numbers of child nodes(only at depth 1). All child node has unique name, i.e., some Integer. Each child Node have some properties that I have used further. My task I have to take top 10 nodes whose keys(integer values) are minimum. My thinking To achieve above goal, I make a query that sorts the keys of all child nodes, and pick top 10. Then by using that keys, I get all corresponding nodes, and after working, delete all that key/value pairs. For that I searched a lot on the internet how to run the query. Can you please tell me how to run query on apache jackrabit. It is good, if you explain with example. Edit no. 1 public class JackRabbit { public static void main(String[] args) throws Exception { try { Repository repository = JcrUtils.getRepository("http://localhost:4502/crx/server"); javax.jcr.Session session = repository.login(new SimpleCredentials("admin", "admin".toCharArray())); Node root = session.getRootNode(); // Obtain the query manager for the session via the workspace ... javax.jcr.query.QueryManager queryManager = session.getWorkspace().getQueryManager(); // Create a query object ... String expression = "select * from nt:base where name= '12345' "; javax.jcr.query.Query query = queryManager.createQuery(expression, javax.jcr.query.Query.JCR_SQL2); // Execute the query and get the results ... javax.jcr.query.QueryResult result = query.execute(); session.logout(); } catch (Exception e) { e.printStackTrace(); } } } Exception javax.jcr.query.InvalidQueryException: Query: select * from nt:(*)base where name= '12345'; expected: <end> at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:525) at org.apache.jackrabbit.spi2dav.ExceptionConverter.generate(ExceptionConverter.java:69) at org.apache.jackrabbit.spi2dav.ExceptionConverter.generate(ExceptionConverter.java:51) at org.apache.jackrabbit.spi2dav.ExceptionConverter.generate(ExceptionConverter.java:45) at org.apache.jackrabbit.spi2dav.RepositoryServiceImpl.executeQuery(RepositoryServiceImpl.java:2004) at org.apache.jackrabbit.jcr2spi.WorkspaceManager.executeQuery(WorkspaceManager.java:349) at org.apache.jackrabbit.jcr2spi.query.QueryImpl.execute(QueryImpl.java:149) at jackrabbit.JackRabbit.main(JackRabbit.java:36) I want to write a query of below scenereo Here nodes having integer value have some properties. I want to sort these nodes by their integer values, and extract top 50 nodes for further processing. Help me in that.
You should quote your node type name in JCR-SQL2: select * from [nt:base] This is one of the main differences between JCR-SQL and JCR-SQL2. Besides, name is a dynamic operand taking a selector argument. So a better way to write your query would be this: select * from [nt:base] as b where name(b) = '12345'
You have different ways of executing your queries depending on the query language you want to use. Take a look at this code for some simple query using only the API and not SQL like string queries. You can take a look at JBoss Modeshape documentation for examples too since it is another JCR 2.0 implementation.
I hope this will help you to execute the query: public FolderListReturn listFolder(String parentNode, String userid,String password) { System.out.println("getting folders and files from = "+parentNode+" of user : "+userid); SessionWrapper sessions =JcrRepositoryUtils.login(userid, password); Session jcrsession = sessions.getSession(); Assert.notNull(name); FolderListReturn folderList1 = new FolderListReturn(); ArrayOfFolders folders = new ArrayOfFolders(); try { javax.jcr.query.QueryManager queryManager; queryManager = jcrsession.getWorkspace().getQueryManager(); String expression = "select * from [nt:folder] AS s WHERE ISCHILDNODE(s,'"+name+"')and CONTAINS(s.[edms:owner],'*"+userid+"*') ORDER BY s.["+Config.EDMS_Sorting_Parameter+"] ASC"; javax.jcr.query.Query query = queryManager.createQuery(expression, javax.jcr.query.Query.JCR_SQL2); javax.jcr.query.QueryResult result = query.execute(); for (NodeIterator nit = result.getNodes(); nit.hasNext();) { Node node = nit.nextNode(); Folder folder = new Folder(); folder=setProperties(node,folder,userid,password,jcrsession,name); folders.getFolderList().add(folder); } folderList1.setFolderListResult(folders); folderList1.setSuccess(true); } catch (Exception e) { e.printStackTrace(); }finally{ //JcrRepositoryUtils.logout(sessionId); } return folderList1; }
Exception using QueryEngine inside a Transaction
I'm using the neo4j 1.9.M01 version with the java-rest-binding 1.8.M07, and I have a problem with this code that aims to get a node from a neo4j database with the property "URL" that is "ARREL", using the Query language via rest. The problems seems to happens only inside a transaction, throwing an exception, but otherwise works well : RestGraphDatabase graphDb = new RestGraphDatabase("http://localhost:7474/db/data"); RestCypherQueryEngine queryEngine = new RestCypherQueryEngine(graphDb.getRestAPI()); Node nodearrel = null; Transaction tx0 = gds.beginTx(); try{ final String queryStringarrel = ("START n=node(*) WHERE n.URL =~{URL} RETURN n"); QueryResult<Map<String, Object>> retornar = queryEngine.query(queryStringarrel, MapUtil.map("URL","ARREL")); for (Map<String,Object> row : retornar) { nodearrel = (Node)row.get("n"); System.out.println("Arrel: "+nodearrel.getProperty("URL")+" id : "+nodearrel.getId()); } tx0.success(); } (...) But an exception happens: *exception tx0: Error reading as JSON '' * every execution at the line that returns the QueryResult object. I also have tried to do it with the ExecutionEngine (between a transaction): ExecutionEngine engine = new ExecutionEngine( graphDb ); String ARREL = "ARREL"; ExecutionResult result = engine.execute("START n=node(*) WHERE n.URL =~{"+ARREL+"} RETURN n"); Iterator<Node> n_column = result.columnAs("n"); Node arrelat = (Node) n_column.next(); for ( Node node : IteratorUtil.asIterable( n_column ) ) (...) But it also fails at the *n_column.next()* returning a null object that throws an exception. The problem is that I need to use the transactions to optimize the queries due if not it take too much time processing all the queries that I need to do. Should I try to join several operations to the query, to avoid using the transactions?
try to add single quotes at: START n=node(*) WHERE n.URL =~ '{URL}' RETURN n
Can you update your java-rest-binding to the latest version (1.8) ? In between we had a version that automatically applied REST-batch-operations to places with transaction semantics. So the transactions you see are not real transactions but just recording your operations to be executed as batch-rest-operations on tx.success/finish Execute the queries within the transaction, but only access the results after the tx is finished. Then your results will be there. This is for instance useful to send many cypher queries in one go to the server and have the results available all in one go afterwards. And yes #ulkas use parameters but not like that: START n=node(*) WHERE n.URL =~ {URL} RETURN n params: { "URL" : "http://your.url" } No quotes neccessary when using params, just like SQL prepared statements.
Elasticsearch - Assigning Shards
I have recently discovered Elasticsearch and I decided to have a play. Unfortunately I am having trouble with adding indexes. The code used to add an index is as follows and runs every time a new index is attempted to be added: public void index ( String index, String type, String id, String json ){ Node node = null; try{ node = nodeBuilder().node(); Client client = node.client(); IndexResponse response = client.prepareIndex( index, type, id ) .setSource( json ) .execute() .actionGet(); } catch ( Exception e ){ Logger.error( e, " Error indexing JSON file: " + json ); } finally { if( node != null) node.close(); } } No indexes appear to be added and my Cluster helath is currently red (as one of the shards is red), but I have no idea how to resolve this. I am receiveing confirmation that my index is being added each time but they do not show up when searched or in es-admin. All help or ideas are greatly appreciated.
When starting a Node, one of the common settings to consider is if it should hold data or not. In other words, should indices and shards be allocated to it. Many times we would like to have the clients just be clients, without shards being allocated to them [1]. If you want to set up your client as being a non-data client (no shards) try setting it up like so by replacing this: node = nodeBuilder().node(); with this: node = nodeBuilder().client(true).node(); [1] http://www.elasticsearch.org/guide/reference/java-api/client.html