Last active
October 29, 2017 02:35
-
-
Save markharwood/f9e955f52908129ef364446cfec0a65e to your computer and use it in GitHub Desktop.
HelloWorld Graph client example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
curl -XPUT "http://localhost:9200/meetuprsvps" -d' | |
{ | |
"settings": { | |
"index.number_of_replicas": 0, | |
"index.number_of_shards": 1 | |
}, | |
"mappings": { | |
"rsvp": { | |
"properties": { | |
"member_id": { | |
"type": "keyword" | |
}, | |
"group_id": { | |
"type": "keyword" | |
} | |
} | |
} | |
} | |
}' | |
curl -XPOST "http://localhost:9200/meetuprsvps/rsvp" -d' | |
{ | |
"group_id": "elasticsearch", | |
"member_id":"mark" | |
}' | |
curl -XPOST "http://localhost:9200/meetuprsvps/rsvp" -d' | |
{ | |
"group_id": "elasticsearch", | |
"member_id":"mark" | |
}' | |
curl -XPOST "http://localhost:9200/meetuprsvps/rsvp" -d' | |
{ | |
"group_id": "elasticsearch", | |
"member_id":"mark" | |
}' | |
curl -XPOST "http://localhost:9200/meetuprsvps/rsvp" -d' | |
{ | |
"group_id": "lucene", | |
"member_id":"mark" | |
}' | |
curl -XPOST "http://localhost:9200/meetuprsvps/rsvp" -d' | |
{ | |
"group_id": "elasticsearch", | |
"member_id":"clinton" | |
}' | |
curl -XPOST "http://localhost:9200/meetuprsvps/rsvp" -d' | |
{ | |
"group_id": "spencer", | |
"member_id":"angular" | |
}' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
curl -XGET "http://localhost:9200/meetuprsvps/_xpack/_graph/_explore" -d' | |
{ | |
"query": { | |
"term": { | |
"_all": "elasticsearch" | |
} | |
}, | |
"vertices": [ | |
{ | |
"field": "member_id", | |
"min_doc_count": 1 | |
} | |
], | |
"connections": { | |
"vertices": [ | |
{ | |
"field": "group_id" | |
} | |
] | |
} | |
}' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package XPackMavenClient.XPackMavenClient; | |
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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>5.2DemoXPackMavenClient</groupId> | |
<artifactId>5.2DemoXPackMavenClient</artifactId> | |
<version>0.0.1-SNAPSHOT</version> | |
<packaging>jar</packaging> | |
<name>5.2DemoXPackMavenClient</name> | |
<url>http://maven.apache.org</url> | |
<repositories> | |
<!-- add the elasticsearch repo --> | |
<repository> | |
<id>elasticsearch-releases</id> | |
<url>https://artifacts.elastic.co/maven</url> | |
<releases> | |
<enabled>true</enabled> | |
</releases> | |
<snapshots> | |
<enabled>false</enabled> | |
</snapshots> | |
</repository> | |
</repositories> | |
<properties> | |
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
</properties> | |
<dependencies> | |
<dependency> | |
<groupId>org.elasticsearch.client</groupId> | |
<artifactId>x-pack-transport</artifactId> | |
<version>5.2.0</version> | |
</dependency> | |
<dependency> | |
<groupId>org.apache.logging.log4j</groupId> | |
<artifactId>log4j-api</artifactId> | |
<version>2.7</version> | |
</dependency> | |
</dependencies> | |
</project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment