JAVA API and doco for Graph

graph

(David She) #1

Hi,
I have trawled through the elastic site and google looking for
the Java API to use for ElasticSearch Graph. Also any sample code or doco would be ideal.
Appreciated


(Mark Harwood) #2

Hi David,

Installing dependencies is as per the Watcher docs [1].
In addition (assuming 5.0) you will need the x-pack-5.0.0.jar on your classpath which is found in your plugins directory once you have installed x-pack[2].
Here is the "Hello World" example of Graph client code:

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.index.query.QueryBuilders;
import org.elasticsearch.xpack.XPackClient;
import org.elasticsearch.xpack.client.PreBuiltXPackTransportClient;
import org.elasticsearch.xpack.graph.action.GraphExploreAction;
import org.elasticsearch.xpack.graph.action.GraphExploreRequestBuilder;

import org.elasticsearch.xpack.graph.action.GraphExploreResponse;
import org.elasticsearch.xpack.graph.action.Hop;
import org.elasticsearch.xpack.graph.action.Vertex;

public class GraphDemoClient {

	public static void main(String[] args) throws Exception {

		TransportClient client = new PreBuiltXPackTransportClient(Settings.EMPTY)
				.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));

		XPackClient xpackClient = new XPackClient(client);

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

	}

}

To run this I found I also needed runtime dependencies of log4j and the netty artefacts in the "modules" directory of my elasticsearch installation.

Hope this helps
Mark

[1] https://www.elastic.co/guide/en/x-pack/current/api-java.html
[2] https://www.elastic.co/guide/en/x-pack/current/installing-xpack.html


(David She) #3

Cheers


(system) #4

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.