BAEL-2958: Update elasticsearch demo project

This commit is contained in:
Michael Pratt 2019-06-12 13:00:24 +00:00
parent 83c500f977
commit e833ede836
4 changed files with 31 additions and 29 deletions

View File

@ -2,18 +2,18 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <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"> 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> <modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung.jest</groupId> <groupId>com.baeldung</groupId>
<artifactId>jest-demo</artifactId> <artifactId>elasticsearch</artifactId>
<version>0.0.1-SNAPSHOT</version> <version>0.0.1-SNAPSHOT</version>
<name>jest-demo</name> <name>elasticsearch</name>
<description>Demo project for Jest Client for Elasticsearch</description> <description>Demo project for Java Elasticsearch libraries</description>
<parent>
<properties> <groupId>com.baeldung</groupId>
<java.version>8</java.version> <artifactId>parent-modules</artifactId>
<maven.compiler.source>1.8</maven.compiler.source> <version>1.0.0-SNAPSHOT</version>
<maven.compiler.target>1.8</maven.compiler.target> <relativePath>../../</relativePath>
</properties> </parent>
<dependencies> <dependencies>

View File

@ -6,7 +6,7 @@ public class Employee {
String name; String name;
String title; String title;
List<String> skills; List<String> skills;
int years_of_service; int yearsOfService;
public String getName() { public String getName() {
return name; return name;
@ -32,11 +32,11 @@ public class Employee {
this.skills = skills; this.skills = skills;
} }
public int getYears_of_service() { public int getYearsOfService() {
return years_of_service; return yearsOfService;
} }
public void setYears_of_service(int years_of_service) { public void setYearsOfService(int yearsOfService) {
this.years_of_service = years_of_service; this.yearsOfService = yearsOfService;
} }
} }

View File

@ -2,8 +2,6 @@ package com.baeldung.jest;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.searchbox.client.JestClient; import io.searchbox.client.JestClient;
import io.searchbox.client.JestClientFactory; import io.searchbox.client.JestClientFactory;
import io.searchbox.client.JestResult; import io.searchbox.client.JestResult;
@ -15,8 +13,6 @@ import io.searchbox.indices.IndicesExists;
import io.searchbox.indices.aliases.AddAliasMapping; import io.searchbox.indices.aliases.AddAliasMapping;
import io.searchbox.indices.aliases.ModifyAliases; import io.searchbox.indices.aliases.ModifyAliases;
import io.searchbox.indices.aliases.RemoveAliasMapping; import io.searchbox.indices.aliases.RemoveAliasMapping;
import io.searchbox.indices.mapping.PutMapping;
import io.searchbox.indices.settings.GetSettings;
import java.io.IOException; import java.io.IOException;
import java.util.*; import java.util.*;
@ -50,20 +46,27 @@ public class JestDemoApplication {
"e") "e")
.build()) .build())
.build()); .build());
jestClient.execute(new ModifyAliases.Builder( JestResult jestResult = jestClient.execute(new ModifyAliases.Builder(
new RemoveAliasMapping.Builder( new RemoveAliasMapping.Builder(
"employees", "employees",
"e") "e")
.build()) .build())
.build()); .build());
if(jestResult.isSucceeded()) {
System.out.println("Success!");
}
else {
System.out.println(jestResult.getErrorMessage());
}
// Sample JSON for indexing // Sample JSON for indexing
// { // {
// "name": "Michael Pratt", // "name": "Michael Pratt",
// "title": "Java Developer", // "title": "Java Developer",
// "skills": ["java", "spring", "elasticsearch"], // "skills": ["java", "spring", "elasticsearch"],
// "years_of_service": 2 // "yearsOfService": 2
// } // }
// Index a document from String // Index a document from String
@ -71,7 +74,7 @@ public class JestDemoApplication {
JsonNode employeeJsonNode = mapper.createObjectNode() JsonNode employeeJsonNode = mapper.createObjectNode()
.put("name", "Michael Pratt") .put("name", "Michael Pratt")
.put("title", "Java Developer") .put("title", "Java Developer")
.put("years_of_service", 2) .put("yearsOfService", 2)
.set("skills", mapper.createArrayNode() .set("skills", mapper.createArrayNode()
.add("java") .add("java")
.add("spring") .add("spring")
@ -82,7 +85,7 @@ public class JestDemoApplication {
Map<String, Object> employeeHashMap = new LinkedHashMap<>(); Map<String, Object> employeeHashMap = new LinkedHashMap<>();
employeeHashMap.put("name", "Michael Pratt"); employeeHashMap.put("name", "Michael Pratt");
employeeHashMap.put("title", "Java Developer"); employeeHashMap.put("title", "Java Developer");
employeeHashMap.put("years_of_service", 2); employeeHashMap.put("yearsOfService", 2);
employeeHashMap.put("skills", Arrays.asList("java", "spring", "elasticsearch")); employeeHashMap.put("skills", Arrays.asList("java", "spring", "elasticsearch"));
jestClient.execute(new Index.Builder(employeeHashMap).index("employees").build()); jestClient.execute(new Index.Builder(employeeHashMap).index("employees").build());
@ -90,7 +93,7 @@ public class JestDemoApplication {
Employee employee = new Employee(); Employee employee = new Employee();
employee.setName("Michael Pratt"); employee.setName("Michael Pratt");
employee.setTitle("Java Developer"); employee.setTitle("Java Developer");
employee.setYears_of_service(2); employee.setYearsOfService(2);
employee.setSkills(Arrays.asList("java", "spring", "elasticsearch")); employee.setSkills(Arrays.asList("java", "spring", "elasticsearch"));
jestClient.execute(new Index.Builder(employee).index("employees").build()); jestClient.execute(new Index.Builder(employee).index("employees").build());
@ -116,7 +119,7 @@ public class JestDemoApplication {
}); });
// Update document // Update document
employee.setYears_of_service(3); employee.setYearsOfService(3);
jestClient.execute(new Update.Builder(employee).index("employees").id("1").build()); jestClient.execute(new Update.Builder(employee).index("employees").id("1").build());
// Delete documents // Delete documents
@ -126,13 +129,13 @@ public class JestDemoApplication {
Employee employeeObject1 = new Employee(); Employee employeeObject1 = new Employee();
employee.setName("John Smith"); employee.setName("John Smith");
employee.setTitle("Python Developer"); employee.setTitle("Python Developer");
employee.setYears_of_service(10); employee.setYearsOfService(10);
employee.setSkills(Arrays.asList("python")); employee.setSkills(Arrays.asList("python"));
Employee employeeObject2 = new Employee(); Employee employeeObject2 = new Employee();
employee.setName("Kate Smith"); employee.setName("Kate Smith");
employee.setTitle("Senior JavaScript Developer"); employee.setTitle("Senior JavaScript Developer");
employee.setYears_of_service(10); employee.setYearsOfService(10);
employee.setSkills(Arrays.asList("javascript", "angular")); employee.setSkills(Arrays.asList("javascript", "angular"));
jestClient.execute(new Bulk.Builder().defaultIndex("employees") jestClient.execute(new Bulk.Builder().defaultIndex("employees")
@ -144,7 +147,7 @@ public class JestDemoApplication {
Employee employeeObject3 = new Employee(); Employee employeeObject3 = new Employee();
employee.setName("Jane Doe"); employee.setName("Jane Doe");
employee.setTitle("Manager"); employee.setTitle("Manager");
employee.setYears_of_service(20); employee.setYearsOfService(20);
employee.setSkills(Arrays.asList("managing")); employee.setSkills(Arrays.asList("managing"));
jestClient.executeAsync( new Index.Builder(employeeObject3).build(), new JestResultHandler<JestResult>() { jestClient.executeAsync( new Index.Builder(employeeObject3).build(), new JestResultHandler<JestResult>() {