Java API for Graph

Does the current Java ES 2.3 API support graph or we need to use something like JEST ?

Hi Sharad,
Below is a demo which obviously depends on core elasticsearch resources and the jar from the xpack plugin for graph:

package org.elasticsearch;

import java.net.InetAddress;
import java.util.Collection;

import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.graph.GraphPlugin;
import org.elasticsearch.graph.action.GraphExploreAction;
import org.elasticsearch.graph.action.GraphExploreRequestBuilder;
import org.elasticsearch.graph.action.GraphExploreResponse;
import org.elasticsearch.graph.action.Hop;
import org.elasticsearch.graph.action.Vertex;
import org.elasticsearch.index.query.QueryBuilders;

/**
 * Demo graph client - find elastic meetup attendees and what other meetup groups they frequent
 */
public class GraphDemoClient {

	public static void main(String[] args) throws Exception {
	
		TransportClient client = TransportClient.builder()
				.settings(Settings.builder()
					.put("cluster.name", "elasticsearch")			    
					.build())
				.addPlugin(GraphPlugin.class)
				.build()
				.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));

	
		int requiredNumberOfSightings=1;
	
		GraphExploreRequestBuilder grb = new GraphExploreRequestBuilder(client, GraphExploreAction.INSTANCE).setIndices("meetuprsvps");
		Hop hop1 = grb.createNextHop(QueryBuilders.termQuery("_all", "elasticsearch"));
		// elasticsearch meetup attendees
		hop1.addVertexRequest("member_id").size(10).minDocCount(requiredNumberOfSightings); 
		// groups attended by elastic attendees
		grb.createNextHop(null).addVertexRequest("group_id").size(10).minDocCount(requiredNumberOfSightings); 
	
		GraphExploreResponse response = grb.get();
		Collection<Vertex> vertices = response.getVertices();
	
		System.out.println("==Members===");
		for (Vertex vertex : vertices) {
			if(vertex.getField().equals("member_id")){
				System.out.println(vertex.getTerm());        		
			}
		}
	
		System.out.println("==Groups===");
		for (Vertex vertex : vertices) {
			if(vertex.getField().equals("group_id")){
				System.out.println(vertex.getTerm());        		
			}
		}        

		client.close();

	}

}
1 Like

Hi Mark,

Can you please help me with the Maven details for the graph plugin as I couldn't find it in the maven repo?

-Sharad

The graph jar you need is in the graph plugin zip. This zip should be unzipped into the "plugins" directory of elasticsearch as part of installation.

if you add the following repository in your pom.xml you should be able to get the dependency properly managed :

<repositories>
    <repository>
        <id>elastic-plugins</id>
        <name>elastic plugins</name>
        <url>https://maven.elasticsearch.org/releases/</url>
    </repository>
</repositories>



<dependency>
    <groupId>org.elasticsearch.plugin</groupId>
    <artifactId>graph</artifactId>
    <version>2.3.3</version>
</dependency>

Hi,
I have trawled through the elastic site and google looking for the Java API to use for ElasticSearch Graph. I notice your snippet above but would to have a link to the documentation or a tutorial.
Appreciated

Hello Sir,
I am new in elasticSearch. I use your program for creating the graph in elasticSearch and I want to see this graph in Kibana. Can you please help me for this. and how can I use queries on this graph like - shortest path query?

Please do not hijack this Java API question with a different topic (UI questions).
I'll answer your questions here but if you want to continue the discussion please open a new question topic.

If you want to view Graphs in Kibana install the x-pack plugin and here is an example of how to configure the UI.

That would be done outside of elasticsearch/kibana currently. We are working on an export-to-GraphML feature which would allow you to use tools like Gephi which offer various Graph algos e.g. shortest-path, centrality measures, community detection etc wrapped up in a UI. If you just want to write code that does graph analysis consider using tools like Python's NetworkX library - here is an example calling the Graph API and performing betweenness centrality measures using networkX.

If you want to discuss any of the above further please open a new topic so we can keep this one focused on the Java API.

1 Like