# Java API for Graph

**URL:** https://discuss.elastic.co/t/java-api-for-graph/48076
**Category:** Elasticsearch
**Tags:** elastic-stack-graph
**Created:** [April 21, 2016, 4:34pm UTC](https://discuss.elastic.co/t/java-api-for-graph/48076 "2016-04-21T16:34:56Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![sharad\_khandelwal1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sharad_khandelwal1/32/48680_2.png) [@sharad\_khandelwal1](https://discuss.elastic.co/u/sharad_khandelwal1)
#### Post date: [April 21, 2016, 4:34pm UTC](https://discuss.elastic.co/t/java-api-for-graph/48076/1 "2016-04-21T16:34:56Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)
#### Post date: [April 22, 2016, 8:35am UTC](https://discuss.elastic.co/t/java-api-for-graph/48076/2 "2016-04-22T08:35:56Z")

</div>

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();

	}

}
```

---

<div class="post-metadata">

### Author: ![sharad\_khandelwal1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sharad_khandelwal1/32/48680_2.png) [@sharad\_khandelwal1](https://discuss.elastic.co/u/sharad_khandelwal1)
#### Post date: [April 23, 2016, 1:16pm UTC](https://discuss.elastic.co/t/java-api-for-graph/48076/3 "2016-04-23T13:16:54Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)
#### Post date: [April 24, 2016, 7:16am UTC](https://discuss.elastic.co/t/java-api-for-graph/48076/4 "2016-04-24T07:16:35Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![hubz](https://avatars.discourse-cdn.com/v4/letter/h/e79b87/32.png) [@hubz](https://discuss.elastic.co/u/hubz)
#### Post date: [July 17, 2016, 4:30pm UTC](https://discuss.elastic.co/t/java-api-for-graph/48076/5 "2016-07-17T16:30:45Z")

</div>

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>
```

---

<div class="post-metadata">

### Author: ![David\_she](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/david_she/32/48792_2.png) [@David\_she](https://discuss.elastic.co/u/David_she)
#### Post date: [November 11, 2016, 5:13am UTC](https://discuss.elastic.co/t/java-api-for-graph/48076/6 "2016-11-11T05:13:45Z")

</div>

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

---

<div class="post-metadata">

### Author: ![ahr12](https://avatars.discourse-cdn.com/v4/letter/a/9fc29f/32.png) [@ahr12](https://discuss.elastic.co/u/ahr12)
#### Post date: [February 7, 2017, 6:06am UTC](https://discuss.elastic.co/t/java-api-for-graph/48076/7 "2017-02-07T06:06:44Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)
#### Post date: [February 7, 2017, 9:48am UTC](https://discuss.elastic.co/t/java-api-for-graph/48076/8 "2017-02-07T09:48:24Z")

</div>

> [@ahr12](#):
>
> I want to see this graph in Kibana. Can you please help me for this

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](https://www.youtube.com/watch?v=ZMmod1Hg92s) is an example of how to configure the UI.

> [@ahr12](#):
>
> how can I use queries on this graph like - shortest path query?

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](https://gist.github.com/markharwood/b8ca11b3ee58223db7cfbdcf6f5ac6bd) 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.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [July 6, 2017, 1:42pm UTC](https://discuss.elastic.co/t/java-api-for-graph/48076/9 "2017-07-06T13:42:16Z")

</div>


