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"
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>com.baeldung.jest</groupId>
<artifactId>jest-demo</artifactId>
<groupId>com.baeldung</groupId>
<artifactId>elasticsearch</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>jest-demo</name>
<description>Demo project for Jest Client for Elasticsearch</description>
<name>elasticsearch</name>
<description>Demo project for Java Elasticsearch libraries</description>
<properties>
<java.version>8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../../</relativePath>
</parent>
<dependencies>

View File

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

View File

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