BAEL-2958: Move Jest demo and remove maven wrapper files
This commit is contained in:
29
persistence-modules/elasticsearch/jest/.gitignore
vendored
Normal file
29
persistence-modules/elasticsearch/jest/.gitignore
vendored
Normal file
@@ -0,0 +1,29 @@
|
||||
HELP.md
|
||||
/target/
|
||||
!.mvn/wrapper/maven-wrapper.jar
|
||||
|
||||
### STS ###
|
||||
.apt_generated
|
||||
.classpath
|
||||
.factorypath
|
||||
.project
|
||||
.settings
|
||||
.springBeans
|
||||
.sts4-cache
|
||||
|
||||
### IntelliJ IDEA ###
|
||||
.idea
|
||||
*.iws
|
||||
*.iml
|
||||
*.ipr
|
||||
|
||||
### NetBeans ###
|
||||
/nbproject/private/
|
||||
/nbbuild/
|
||||
/dist/
|
||||
/nbdist/
|
||||
/.nb-gradle/
|
||||
/build/
|
||||
|
||||
### VS Code ###
|
||||
.vscode/
|
||||
53
persistence-modules/elasticsearch/jest/pom.xml
Normal file
53
persistence-modules/elasticsearch/jest/pom.xml
Normal file
@@ -0,0 +1,53 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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>
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.1.5.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
<groupId>com.baeldung.jest</groupId>
|
||||
<artifactId>jest-demo</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>jest-demo</name>
|
||||
<description>Demo project for Spring Boot</description>
|
||||
|
||||
<properties>
|
||||
<java.version>8</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.searchbox</groupId>
|
||||
<artifactId>jest</artifactId>
|
||||
<version>6.3.1</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
<version>2.9.6</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,42 @@
|
||||
package com.baeldung.jest;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Employee {
|
||||
String name;
|
||||
String title;
|
||||
List<String> skills;
|
||||
int years_of_service;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public List<String> getSkills() {
|
||||
return skills;
|
||||
}
|
||||
|
||||
public void setSkills(List<String> skills) {
|
||||
this.skills = skills;
|
||||
}
|
||||
|
||||
public int getYears_of_service() {
|
||||
return years_of_service;
|
||||
}
|
||||
|
||||
public void setYears_of_service(int years_of_service) {
|
||||
this.years_of_service = years_of_service;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.baeldung.jest;
|
||||
|
||||
import io.searchbox.client.JestClient;
|
||||
import io.searchbox.client.JestClientFactory;
|
||||
import io.searchbox.client.config.HttpClientConfig;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class JestClientConfiguration {
|
||||
|
||||
@Value("${elastic.url:http://localhost:9200}")
|
||||
private String elasticUrl;
|
||||
|
||||
/**
|
||||
* Create a JEST client bean.
|
||||
* @return JestClient
|
||||
*/
|
||||
@Bean
|
||||
public JestClient jestClient()
|
||||
{
|
||||
JestClientFactory factory = new JestClientFactory();
|
||||
factory.setHttpClientConfig(
|
||||
new HttpClientConfig.Builder(elasticUrl)
|
||||
.multiThreaded(true)
|
||||
.defaultMaxTotalConnectionPerRoute(2)
|
||||
.maxTotalConnection(20)
|
||||
.build());
|
||||
return factory.getObject();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,163 @@
|
||||
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.JestResult;
|
||||
import io.searchbox.client.JestResultHandler;
|
||||
import io.searchbox.core.*;
|
||||
import io.searchbox.indices.CreateIndex;
|
||||
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 org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
|
||||
@SpringBootApplication
|
||||
public class JestDemoApplication {
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
ApplicationContext context = SpringApplication.run(JestDemoApplication.class, args);
|
||||
|
||||
// Demo the JestClient
|
||||
JestClient jestClient = context.getBean(JestClient.class);
|
||||
|
||||
// Check an index
|
||||
JestResult result = jestClient.execute(new IndicesExists.Builder("employees").build());
|
||||
if(!result.isSucceeded()) {
|
||||
System.out.println(result.getErrorMessage());
|
||||
}
|
||||
|
||||
// Create an index
|
||||
jestClient.execute(new CreateIndex.Builder("employees").build());
|
||||
|
||||
// Create an index with options
|
||||
Map<String, Object> settings = new HashMap<>();
|
||||
settings.put("number_of_shards", 11);
|
||||
settings.put("number_of_replicas", 2);
|
||||
jestClient.execute(new CreateIndex.Builder("employees").settings(settings).build());
|
||||
|
||||
// Create an alias, then remove it
|
||||
jestClient.execute(new ModifyAliases.Builder(
|
||||
new AddAliasMapping.Builder(
|
||||
"employees",
|
||||
"e")
|
||||
.build())
|
||||
.build());
|
||||
jestClient.execute(new ModifyAliases.Builder(
|
||||
new RemoveAliasMapping.Builder(
|
||||
"employees",
|
||||
"e")
|
||||
.build())
|
||||
.build());
|
||||
|
||||
// Sample JSON for indexing
|
||||
|
||||
// {
|
||||
// "name": "Michael Pratt",
|
||||
// "title": "Java Developer",
|
||||
// "skills": ["java", "spring", "elasticsearch"],
|
||||
// "years_of_service": 2
|
||||
// }
|
||||
|
||||
// Index a document from String
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
JsonNode employeeJsonNode = mapper.createObjectNode()
|
||||
.put("name", "Michael Pratt")
|
||||
.put("title", "Java Developer")
|
||||
.put("years_of_service", 2)
|
||||
.set("skills", mapper.createArrayNode()
|
||||
.add("java")
|
||||
.add("spring")
|
||||
.add("elasticsearch"));
|
||||
jestClient.execute(new Index.Builder(employeeJsonNode.toString()).index("employees").build());
|
||||
|
||||
// Index a document from Map
|
||||
Map<String, Object> employeeHashMap = new LinkedHashMap<>();
|
||||
employeeHashMap.put("name", "Michael Pratt");
|
||||
employeeHashMap.put("title", "Java Developer");
|
||||
employeeHashMap.put("years_of_service", 2);
|
||||
employeeHashMap.put("skills", Arrays.asList("java", "spring", "elasticsearch"));
|
||||
jestClient.execute(new Index.Builder(employeeHashMap).index("employees").build());
|
||||
|
||||
// Index a document from POJO
|
||||
Employee employee = new Employee();
|
||||
employee.setName("Michael Pratt");
|
||||
employee.setTitle("Java Developer");
|
||||
employee.setYears_of_service(2);
|
||||
employee.setSkills(Arrays.asList("java", "spring", "elasticsearch"));
|
||||
jestClient.execute(new Index.Builder(employee).index("employees").build());
|
||||
|
||||
// Read document by ID
|
||||
Employee getResult = jestClient.execute(new Get.Builder("employees", "1").build()).getSourceAsObject(Employee.class);
|
||||
|
||||
// Search documents
|
||||
String search = "{\n" +
|
||||
" \"query\": {\n" +
|
||||
" \"bool\": {\n" +
|
||||
" \"must\": [\n" +
|
||||
" { \"match\": { \"name\": \"Michael Pratt\" }}\n" +
|
||||
" ]\n" +
|
||||
" }\n" +
|
||||
" }\n" +
|
||||
"}";
|
||||
List<SearchResult.Hit<Employee, Void>> searchResults =
|
||||
jestClient.execute(new Search.Builder(search).build())
|
||||
.getHits(Employee.class);
|
||||
|
||||
searchResults.forEach(hit -> {
|
||||
System.out.println(String.format("Document %s has score %s", hit.id, hit.score));
|
||||
});
|
||||
|
||||
// Update document
|
||||
employee.setYears_of_service(3);
|
||||
jestClient.execute(new Update.Builder(employee).index("employees").id("1").build());
|
||||
|
||||
// Delete documents
|
||||
jestClient.execute(new Delete.Builder("2") .index("employees") .build());
|
||||
|
||||
// Bulk operations
|
||||
Employee employeeObject1 = new Employee();
|
||||
employee.setName("John Smith");
|
||||
employee.setTitle("Python Developer");
|
||||
employee.setYears_of_service(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.setSkills(Arrays.asList("javascript", "angular"));
|
||||
|
||||
jestClient.execute(new Bulk.Builder().defaultIndex("employees")
|
||||
.addAction(new Index.Builder(employeeObject1).build())
|
||||
.addAction(new Index.Builder(employeeObject2).build())
|
||||
.addAction(new Delete.Builder("3").build()) .build());
|
||||
|
||||
// Async operations
|
||||
Employee employeeObject3 = new Employee();
|
||||
employee.setName("Jane Doe");
|
||||
employee.setTitle("Manager");
|
||||
employee.setYears_of_service(20);
|
||||
employee.setSkills(Arrays.asList("managing"));
|
||||
|
||||
jestClient.executeAsync( new Index.Builder(employeeObject3).build(), new JestResultHandler<JestResult>() {
|
||||
@Override public void completed(JestResult result) {
|
||||
// handle result
|
||||
}
|
||||
@Override public void failed(Exception ex) {
|
||||
// handle exception
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.jest;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class JestDemoApplicationTests {
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user