Compare commits
71 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 67f6db932d | |||
| 9fddc28bdb | |||
| 16d7f10f97 | |||
| e6c125acda | |||
| cc6899703b | |||
| 971a72e7b8 | |||
| 033b3fbf55 | |||
| b9c8c2ef7e | |||
| 37c78e4289 | |||
| 0122f61ec0 | |||
| b775357524 | |||
| c9fe8a29b9 | |||
| c444bbd65f | |||
| 9c1b001da1 | |||
| ea234c7b68 | |||
| 42af6e375c | |||
| 5dfd05992f | |||
| 64cf9566d9 | |||
| 2c857178f4 | |||
| f087d5aac3 | |||
| fbcb76f8ad | |||
| ba06741c93 | |||
| a228629c7d | |||
| 6fd688f3a2 | |||
| 7d85f0bdd8 | |||
| c7534fa8b9 | |||
| fe9e0b5d0c | |||
| d26d01bab1 | |||
| 026be264fe | |||
| fd1ba5869d | |||
| 7f3035dab5 | |||
| a5c4867684 | |||
| 572cc7ffea | |||
| 4e7bcac5f5 | |||
| 4628908e84 | |||
| 922f4b1760 | |||
| 4614c62bb5 | |||
| 063020f8b3 | |||
| 0c98c419c9 | |||
| 3f085b2675 | |||
| 42aeb48b43 | |||
| 449910b4f7 | |||
| 2e99f5b2e0 | |||
| 9b9136d852 | |||
| a266d7c46a | |||
| c045a8ae29 | |||
| 11c87a1251 | |||
| c9e9bf757e | |||
| db8d89aeff | |||
| 0ed14e7caa | |||
| d0658affc3 | |||
| 7c10128cec | |||
| 6e3535d68d | |||
| 121b47e1be | |||
| 3abe6d9c1b | |||
| d43b44ba9c | |||
| a715d2836d | |||
| a69b95867a | |||
| d424195c8b | |||
| 44d1614a20 | |||
| 7b6823cc31 | |||
| 2e43c7800b | |||
| 59968e8118 | |||
| a5934442bf | |||
| 89afa819f3 | |||
| 5561a5b1ae | |||
| 23a5071ee5 | |||
| d1324a26db | |||
| 34e3e8f5da | |||
| 1571b9b4e2 | |||
| 78e7dd2764 |
@@ -26,11 +26,3 @@ target
|
||||
/zap.env
|
||||
/localdocker.env
|
||||
.localdocker-env
|
||||
|
||||
build/
|
||||
node_modules
|
||||
node
|
||||
package.json
|
||||
package-lock.json
|
||||
|
||||
.mvn/.gradle-enterprise
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<extensions>
|
||||
<extension>
|
||||
<groupId>com.gradle</groupId>
|
||||
<artifactId>gradle-enterprise-maven-extension</artifactId>
|
||||
<version>1.19.2</version>
|
||||
</extension>
|
||||
<extension>
|
||||
<groupId>com.gradle</groupId>
|
||||
<artifactId>common-custom-user-data-maven-extension</artifactId>
|
||||
<version>1.12.4</version>
|
||||
</extension>
|
||||
</extensions>
|
||||
@@ -1,31 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
|
||||
<gradleEnterprise
|
||||
xmlns="https://www.gradle.com/gradle-enterprise-maven" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="https://www.gradle.com/gradle-enterprise-maven https://www.gradle.com/schema/gradle-enterprise-maven.xsd">
|
||||
<server>
|
||||
<url>https://ge.spring.io</url>
|
||||
</server>
|
||||
<buildScan>
|
||||
<backgroundBuildScanUpload>false</backgroundBuildScanUpload>
|
||||
<captureGoalInputFiles>true</captureGoalInputFiles>
|
||||
<publishIfAuthenticated>true</publishIfAuthenticated>
|
||||
<obfuscation>
|
||||
<ipAddresses>#{{'0.0.0.0'}}</ipAddresses>
|
||||
</obfuscation>
|
||||
</buildScan>
|
||||
<buildCache>
|
||||
<local>
|
||||
<enabled>true</enabled>
|
||||
</local>
|
||||
<remote>
|
||||
<server>
|
||||
<credentials>
|
||||
<username>${env.GRADLE_ENTERPRISE_CACHE_USERNAME}</username>
|
||||
<password>${env.GRADLE_ENTERPRISE_CACHE_PASSWORD}</password>
|
||||
</credentials>
|
||||
</server>
|
||||
<enabled>true</enabled>
|
||||
<storeEnabled>#{env['GRADLE_ENTERPRISE_CACHE_USERNAME'] != null and env['GRADLE_ENTERPRISE_CACHE_PASSWORD'] != null}</storeEnabled>
|
||||
</remote>
|
||||
</buildCache>
|
||||
</gradleEnterprise>
|
||||
+1
-1
@@ -1,3 +1,3 @@
|
||||
#Thu Dec 14 08:34:15 CET 2023
|
||||
#Thu Dec 14 08:37:40 CET 2023
|
||||
wrapperUrl=https\://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar
|
||||
distributionUrl=https\://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.6/apache-maven-3.9.6-bin.zip
|
||||
|
||||
@@ -8,3 +8,7 @@ Do not submit a Pull Request before having created an issue and having discussed
|
||||
== Running the test locally
|
||||
|
||||
In order to run the tests locally with `./mvnw test` you need to have docker running because Spring Data Elasticsearch uses https://www.testcontainers.org/[Testcontainers] to start a local running Elasticsearch instance.
|
||||
|
||||
== Class names of the test classes
|
||||
|
||||
Test classes that do depend on the client have either `ERHLC` (when using the deprecated Elasticsearch `RestHighLevelClient`) or `ELC` (the new `ElasticsearchClient`) in their name.
|
||||
|
||||
Vendored
+1
-10
@@ -9,7 +9,7 @@ pipeline {
|
||||
|
||||
triggers {
|
||||
pollSCM 'H/10 * * * *'
|
||||
upstream(upstreamProjects: "spring-data-commons/3.2.x", threshold: hudson.model.Result.SUCCESS)
|
||||
upstream(upstreamProjects: "spring-data-commons/3.1.x", threshold: hudson.model.Result.SUCCESS)
|
||||
}
|
||||
|
||||
options {
|
||||
@@ -33,8 +33,6 @@ pipeline {
|
||||
|
||||
environment {
|
||||
ARTIFACTORY = credentials("${p['artifactory.credentials']}")
|
||||
DEVELOCITY_CACHE = credentials("${p['develocity.cache.credentials']}")
|
||||
DEVELOCITY_ACCESS_KEY = credentials("${p['develocity.access-key']}")
|
||||
}
|
||||
|
||||
steps {
|
||||
@@ -63,8 +61,6 @@ pipeline {
|
||||
options { timeout(time: 30, unit: 'MINUTES') }
|
||||
environment {
|
||||
ARTIFACTORY = credentials("${p['artifactory.credentials']}")
|
||||
DEVELOCITY_CACHE = credentials("${p['develocity.cache.credentials']}")
|
||||
DEVELOCITY_ACCESS_KEY = credentials("${p['develocity.access-key']}")
|
||||
}
|
||||
steps {
|
||||
script {
|
||||
@@ -92,16 +88,11 @@ pipeline {
|
||||
options { timeout(time: 20, unit: 'MINUTES') }
|
||||
environment {
|
||||
ARTIFACTORY = credentials("${p['artifactory.credentials']}")
|
||||
DEVELOCITY_CACHE = credentials("${p['develocity.cache.credentials']}")
|
||||
DEVELOCITY_ACCESS_KEY = credentials("${p['develocity.access-key']}")
|
||||
}
|
||||
steps {
|
||||
script {
|
||||
docker.image(p['docker.java.main.image']).inside(p['docker.java.inside.basic']) {
|
||||
sh 'MAVEN_OPTS="-Duser.name=' + "${p['jenkins.user.name']}" + ' -Duser.home=/tmp/jenkins-home" ' +
|
||||
"DEVELOCITY_CACHE_USERNAME=${DEVELOCITY_CACHE_USR} " +
|
||||
"DEVELOCITY_CACHE_PASSWORD=${DEVELOCITY_CACHE_PSW} " +
|
||||
"GRADLE_ENTERPRISE_ACCESS_KEY=${DEVELOCITY_ACCESS_KEY} " +
|
||||
"./mvnw -s settings.xml -Pci,artifactory -Dmaven.repo.local=/tmp/jenkins-home/.m2/spring-data-elasticsearch-non-root " +
|
||||
"-Dartifactory.server=${p['artifactory.url']} " +
|
||||
"-Dartifactory.username=${ARTIFACTORY_USR} " +
|
||||
|
||||
+50
-7
@@ -1,17 +1,17 @@
|
||||
image:https://spring.io/badges/spring-data-elasticsearch/ga.svg[Spring Data Elasticsearch,link=https://projects.spring.io/spring-data-elasticsearch#quick-start] image:https://spring.io/badges/spring-data-elasticsearch/snapshot.svg[Spring Data Elasticsearch,link=https://projects.spring.io/spring-data-elasticsearch#quick-start]
|
||||
|
||||
= Spring Data for Elasticsearch image:https://jenkins.spring.io/buildStatus/icon?job=spring-data-elasticsearch%2Fmain&subject=Build[link=https://jenkins.spring.io/view/SpringData/job/spring-data-elasticsearch/] https://gitter.im/spring-projects/spring-data[image:https://badges.gitter.im/spring-projects/spring-data.svg[Gitter]] image:https://img.shields.io/badge/Revved%20up%20by-Gradle%20Enterprise-06A0CE?logo=Gradle&labelColor=02303A["Revved up by Gradle Enterprise", link="https://ge.spring.io/scans?search.rootProjectNames=Spring Data Elasticsearch"]
|
||||
= Spring Data for Elasticsearch image:https://jenkins.spring.io/buildStatus/icon?job=spring-data-elasticsearch%2Fmain&subject=Build[link=https://jenkins.spring.io/view/SpringData/job/spring-data-elasticsearch/] https://gitter.im/spring-projects/spring-data[image:https://badges.gitter.im/spring-projects/spring-data.svg[Gitter]]
|
||||
|
||||
The primary goal of the https://projects.spring.io/spring-data[Spring Data] project is to make it easier to build Spring-powered applications that use new data access technologies such as non-relational databases, map-reduce frameworks, and cloud based data services.
|
||||
|
||||
The Spring Data Elasticsearch project provides integration with the https://www.elastic.co/[Elasticsearch] search engine.
|
||||
Key functional areas of Spring Data Elasticsearch are a POJO centric model for interacting with Elasticsearch Documents and easily writing a Repository style data access layer.
|
||||
Key functional areas of Spring Data Elasticsearch are a POJO centric model for interacting with a Elasticsearch Documents and easily writing a Repository style data access layer.
|
||||
|
||||
This project is lead and maintained by the community.
|
||||
|
||||
== Features
|
||||
|
||||
* Spring configuration support using Java based `@Configuration` classes or an XML namespace for an ES client instances.
|
||||
* Spring configuration support using Java based `@Configuration` classes or an XML namespace for a ES clients instances.
|
||||
* `ElasticsearchOperations` class and implementations that increases productivity performing common ES operations.
|
||||
Includes integrated object mapping between documents and POJOs.
|
||||
* Feature Rich Object Mapping integrated with Spring’s Conversion Service
|
||||
@@ -19,6 +19,28 @@ Includes integrated object mapping between documents and POJOs.
|
||||
* Automatic implementation of `Repository` interfaces including support for custom search methods.
|
||||
* CDI support for repositories
|
||||
|
||||
== About Elasticsearch versions and clients
|
||||
|
||||
=== Elasticsearch 7.17 client libraries
|
||||
|
||||
At the end of 2021 Elasticsearch with version 7.17 released the new version of their Java client and deprecated the `RestHighLevelCLient` which was the default way to access Elasticsearch up to then.
|
||||
|
||||
Spring Data Elasticsearch will in version 4.4 offer the possibility to optionally use the new client as an alternative to the existing setup using the `RestHighLevelCLient`.
|
||||
The default client that is used still is the `RestHighLevelCLient`, first because the integration of the new client is not yet complete, the new client still has features missing and bugs which will hopefully be resolved soon.
|
||||
Second, and more important, the new Elasticsearch client forces users to switch from using `javax.json.spi.JsonProvider` to `jakarta.json.spi.JsonProvider`.
|
||||
Spring Data Elasticsearch cannot enforce this switch; Spring Boot will switch to `jakarta` with version 3 and then it's safe for Spring Data Elasticsearch to switch to the new client.
|
||||
|
||||
So for version 4.4 Spring Data Elasticsearch will keep using the `RestHighLevelCLient` in version 7.17.x (as long as this will be available).
|
||||
|
||||
=== Elasticsearch 8 client libraries
|
||||
|
||||
In Elasticsearch 8, the `RestHighLevelCLient` has been removed.
|
||||
This means that a switch to this client version can only be done with the next major upgrade which will be Spring Data Elasticsearch 5, based on Spring Data 3, used by Spring Boot 3, based on Spring 6 and Java 17.
|
||||
|
||||
=== Elasticsearch 8 cluster
|
||||
|
||||
It should be possible to use the Elasticsearch 7 client to access a cluster running version 8 by setting the appropriate compatibility headers (see the documentation at https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#elasticsearch.clients.configuration). but I encountered and heard of cases where the response from the server is not parseable by the client although the headers are set, so use with care.
|
||||
|
||||
== Code of Conduct
|
||||
|
||||
This project is governed by the https://github.com/spring-projects/.github/blob/e3cc2ff230d8f1dca06535aa6b5a4a23815861d4/CODE_OF_CONDUCT.md[Spring Code of Conduct].
|
||||
@@ -64,7 +86,25 @@ public class MyService {
|
||||
|
||||
=== Using the RestClient
|
||||
|
||||
Please check the [official documentation](https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#elasticsearch.clients.configuration).
|
||||
Provide a configuration like this:
|
||||
|
||||
[source,java]
|
||||
----
|
||||
@Configuration
|
||||
public class RestClientConfig extends AbstractElasticsearchConfiguration {
|
||||
|
||||
@Override
|
||||
@Bean
|
||||
public RestHighLevelClient elasticsearchClient() {
|
||||
|
||||
final ClientConfiguration clientConfiguration = ClientConfiguration.builder()
|
||||
.connectedTo("localhost:9200")
|
||||
.build();
|
||||
|
||||
return RestClients.create(clientConfiguration).rest();
|
||||
}
|
||||
}
|
||||
----
|
||||
|
||||
=== Maven configuration
|
||||
|
||||
@@ -79,6 +119,9 @@ Add the Maven dependency:
|
||||
</dependency>
|
||||
----
|
||||
|
||||
// NOTE: since Github does not support include directives, the content of
|
||||
// the src/main/asciidoc/reference/preface.adoc file is duplicated here
|
||||
// Always change both files!
|
||||
**Compatibility Matrix**
|
||||
|
||||
The compatibility between Spring Data Elasticsearch, Elasticsearch client drivers and Spring Boot versions can be found in the https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#preface.versions[reference documentation].
|
||||
@@ -137,7 +180,7 @@ If you want to raise an issue, please follow the recommendations below:
|
||||
|
||||
* Before you log a bug, please search the
|
||||
https://github.com/spring-projects/spring-data-elasticsearch/issues[issue tracker] to see if someone has already reported the problem.
|
||||
* If the issue doesn't already exist, https://github.com/spring-projects/spring-data-elasticsearch/issues/new[create a new issue].
|
||||
* If the issue doesn’t already exist, https://github.com/spring-projects/spring-data-elasticsearch/issues/new[create a new issue].
|
||||
* Please provide as much information as possible with the issue report, we like to know the version of Spring Data Elasticsearch that you are using and JVM version.
|
||||
* If you need to paste code, or include a stack trace use Markdown +++```+++ escapes before and after your text.
|
||||
* If possible try to create a test-case or project that replicates the issue.
|
||||
@@ -168,10 +211,10 @@ Building the documentation builds also the project without running tests.
|
||||
|
||||
[source,bash]
|
||||
----
|
||||
$ ./mvnw clean install -Pantora
|
||||
$ ./mvnw clean install -Pdistribute
|
||||
----
|
||||
|
||||
The generated documentation is available from `target/antora/site/index.html`.
|
||||
The generated documentation is available from `target/site/reference/html/index.html`.
|
||||
|
||||
== Examples
|
||||
|
||||
|
||||
@@ -18,3 +18,24 @@ is run. There must be _docker_ running, as the integration tests use docker to s
|
||||
|
||||
Integration tests are tests that have the Junit5 Tag `@Tag("integration-test")` on the test class. Normally this should not be set explicitly, but the annotation `@SpringIntegrationTest` should be used. This not only marks the test as integration test, but integrates an automatic setup of an Elasticsearch Testcontainer and integrate this with Spring, so
|
||||
that the required Beans can be automatically injected. Check _src/test/java/org/springframework/data/elasticsearch/JUnit5SampleRestClientBasedTests.java_ as a reference setup
|
||||
|
||||
== Mutation testing
|
||||
|
||||
The pom includes a plugin dependency to run mutation tests using [pitest](https://pitest.org/). These tests must be explicitly configured and run, they are not included in the normal build steps. Before pitest can run, a normal `./mvnw test` must be executed. The configuration excludes integration tests, only unit tests are considered.
|
||||
|
||||
|
||||
pitest can be run directly from the commandline
|
||||
----
|
||||
./mvnw org.pitest:pitest-maven:mutationCoverage
|
||||
----
|
||||
This will output an html report to _target/pit-reports/YYYYMMDDHHMI_.
|
||||
|
||||
To speed-up repeated analysis of the same codebase set the withHistory parameter to true.
|
||||
----
|
||||
./mvnw -DwithHistory org.pitest:pitest-maven:mutationCoverage
|
||||
----
|
||||
|
||||
The classes to test are defined either in the pom.xml or can be set on the commandline:
|
||||
----
|
||||
./mvnw -DwithHistory org.pitest:pitest-maven:mutationCoverage -DtargetClasses="org.springframework.data.elasticsearch.support.*"
|
||||
----
|
||||
|
||||
+1
-6
@@ -2,12 +2,7 @@
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
export DEVELOCITY_CACHE_USERNAME=${DEVELOCITY_CACHE_USR}
|
||||
export DEVELOCITY_CACHE_PASSWORD=${DEVELOCITY_CACHE_PSW}
|
||||
export JENKINS_USER=${JENKINS_USER_NAME}
|
||||
|
||||
# The environment variable to configure access key is still GRADLE_ENTERPRISE_ACCESS_KEY
|
||||
export GRADLE_ENTERPRISE_ACCESS_KEY=${DEVELOCITY_ACCESS_KEY}
|
||||
|
||||
MAVEN_OPTS="-Duser.name=${JENKINS_USER} -Duser.home=/tmp/jenkins-home" \
|
||||
./mvnw -s settings.xml clean -Dscan=false -Dmaven.repo.local=/tmp/jenkins-home/.m2/spring-data-elasticsearch
|
||||
./mvnw -s settings.xml clean -Dmaven.repo.local=/tmp/jenkins-home/.m2/spring-data-elasticsearch
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# Java versions
|
||||
java.main.tag=17.0.9_9-jdk-focal
|
||||
java.next.tag=21.0.1_12-jdk-jammy
|
||||
java.next.tag=20-jdk-jammy
|
||||
|
||||
# Docker container images - standard
|
||||
docker.java.main.image=harbor-repo.vmware.com/dockerhub-proxy-cache/library/eclipse-temurin:${java.main.tag}
|
||||
@@ -10,7 +10,6 @@ docker.java.next.image=harbor-repo.vmware.com/dockerhub-proxy-cache/library/ecli
|
||||
docker.mongodb.4.4.version=4.4.25
|
||||
docker.mongodb.5.0.version=5.0.21
|
||||
docker.mongodb.6.0.version=6.0.10
|
||||
docker.mongodb.7.0.version=7.0.2
|
||||
|
||||
# Supported versions of Redis
|
||||
docker.redis.6.version=6.2.13
|
||||
@@ -28,6 +27,4 @@ docker.credentials=hub.docker.com-springbuildmaster
|
||||
artifactory.credentials=02bd1690-b54f-4c9f-819d-a77cb7a9822c
|
||||
artifactory.url=https://repo.spring.io
|
||||
artifactory.repository.snapshot=libs-snapshot-local
|
||||
develocity.cache.credentials=gradle_enterprise_cache_user
|
||||
develocity.access-key=gradle_enterprise_secret_access_key
|
||||
jenkins.user.name=spring-builds+jenkins
|
||||
|
||||
@@ -5,13 +5,8 @@ set -euo pipefail
|
||||
mkdir -p /tmp/jenkins-home/.m2/spring-data-elasticsearch
|
||||
chown -R 1001:1001 .
|
||||
|
||||
export DEVELOCITY_CACHE_USERNAME=${DEVELOCITY_CACHE_USR}
|
||||
export DEVELOCITY_CACHE_PASSWORD=${DEVELOCITY_CACHE_PSW}
|
||||
export JENKINS_USER=${JENKINS_USER_NAME}
|
||||
|
||||
# The environment variable to configure access key is still GRADLE_ENTERPRISE_ACCESS_KEY
|
||||
export GRADLE_ENTERPRISE_ACCESS_KEY=${DEVELOCITY_ACCESS_KEY}
|
||||
|
||||
MAVEN_OPTS="-Duser.name=${JENKINS_USER} -Duser.home=/tmp/jenkins-home" \
|
||||
./mvnw -s settings.xml \
|
||||
-P${PROFILE} clean dependency:list verify -Dsort -U -B -Dmaven.repo.local=/tmp/jenkins-home/.m2/spring-data-elasticsearch
|
||||
|
||||
@@ -5,12 +5,12 @@
|
||||
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-elasticsearch</artifactId>
|
||||
<version>5.2.2</version>
|
||||
<version>5.1.8</version>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.data.build</groupId>
|
||||
<artifactId>spring-data-parent</artifactId>
|
||||
<version>3.2.2</version>
|
||||
<version>3.1.8</version>
|
||||
</parent>
|
||||
|
||||
<name>Spring Data Elasticsearch</name>
|
||||
@@ -18,17 +18,22 @@
|
||||
<url>https://github.com/spring-projects/spring-data-elasticsearch</url>
|
||||
|
||||
<properties>
|
||||
<springdata.commons>3.2.2</springdata.commons>
|
||||
<springdata.commons>3.1.8</springdata.commons>
|
||||
|
||||
<!-- version of the ElasticsearchClient -->
|
||||
<elasticsearch-java>8.11.3</elasticsearch-java>
|
||||
<!-- version of the RestHighLevelClient -->
|
||||
<elasticsearch-rhlc>7.17.15</elasticsearch-rhlc>
|
||||
<!-- version of the new ElasticsearchClient -->
|
||||
<elasticsearch-java>8.7.1</elasticsearch-java>
|
||||
|
||||
<log4j>2.18.0</log4j>
|
||||
<!-- netty dependency can be removed once the WebClient code is gone -->
|
||||
<netty>4.1.90.Final</netty>
|
||||
|
||||
<blockhound-junit>1.0.8.RELEASE</blockhound-junit>
|
||||
<hoverfly>0.14.4</hoverfly>
|
||||
<log4j>2.18.0</log4j>
|
||||
<jsonassert>1.5.1</jsonassert>
|
||||
<testcontainers>1.18.0</testcontainers>
|
||||
<wiremock>2.35.1</wiremock>
|
||||
<wiremock>2.35.0</wiremock>
|
||||
|
||||
<java-module-name>spring.data.elasticsearch</java-module-name>
|
||||
|
||||
@@ -88,6 +93,18 @@
|
||||
<url>https://github.com/spring-projects/spring-data-elasticsearch/issues</url>
|
||||
</issueManagement>
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>io.netty</groupId>
|
||||
<artifactId>netty-bom</artifactId>
|
||||
<version>${netty}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<!-- Spring -->
|
||||
@@ -115,12 +132,33 @@
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.projectreactor.netty</groupId>
|
||||
<artifactId>reactor-netty-http</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.projectreactor</groupId>
|
||||
<artifactId>reactor-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- optional Elasticsearch RestHighLevelClient, deprecated in SDE 5.0 -->
|
||||
<dependency>
|
||||
<groupId>org.elasticsearch.client</groupId>
|
||||
<artifactId>elasticsearch-rest-high-level-client</artifactId>
|
||||
<version>${elasticsearch-rhlc}</version>
|
||||
<optional>true</optional>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>commons-logging</groupId>
|
||||
<artifactId>commons-logging</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<!-- new Elasticsearch client, needs the low-level rest client and json api -->
|
||||
<dependency>
|
||||
<groupId>co.elastic.clients</groupId>
|
||||
<artifactId>elasticsearch-java</artifactId>
|
||||
@@ -166,6 +204,7 @@
|
||||
<dependency>
|
||||
<groupId>jakarta.enterprise</groupId>
|
||||
<artifactId>jakarta.enterprise.cdi-api</artifactId>
|
||||
<version>3.0.1</version>
|
||||
<scope>provided</scope>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
@@ -180,33 +219,25 @@
|
||||
<dependency>
|
||||
<groupId>org.apache.openwebbeans</groupId>
|
||||
<artifactId>openwebbeans-se</artifactId>
|
||||
<classifier>jakarta</classifier>
|
||||
<version>${webbeans}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- Kotlin extension -->
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
<artifactId>kotlin-stdlib</artifactId>
|
||||
<optional>true</optional>
|
||||
<groupId>org.apache.openwebbeans</groupId>
|
||||
<artifactId>openwebbeans-spi</artifactId>
|
||||
<classifier>jakarta</classifier>
|
||||
<version>${webbeans}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
<artifactId>kotlin-reflect</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.kotlinx</groupId>
|
||||
<artifactId>kotlinx-coroutines-core</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.kotlinx</groupId>
|
||||
<artifactId>kotlinx-coroutines-reactor</artifactId>
|
||||
<optional>true</optional>
|
||||
<groupId>org.apache.openwebbeans</groupId>
|
||||
<artifactId>openwebbeans-impl</artifactId>
|
||||
<classifier>jakarta</classifier>
|
||||
<version>${webbeans}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- Test -->
|
||||
@@ -222,13 +253,6 @@
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.kotlinx</groupId>
|
||||
<artifactId>kotlinx-coroutines-test</artifactId>
|
||||
<scope>test</scope>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>log4j-over-slf4j</artifactId>
|
||||
@@ -316,14 +340,6 @@
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<!--we need Murmur3Hash in a test, before 5.2 we had it from the old Elasticsearch dependency -->
|
||||
<dependency>
|
||||
<groupId>commons-codec</groupId>
|
||||
<artifactId>commons-codec</artifactId>
|
||||
<version>1.15</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
@@ -392,17 +408,8 @@
|
||||
<artifactId>maven-assembly-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<annotationProcessorPaths>
|
||||
<path>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
<artifactId>log4j-core</artifactId>
|
||||
<version>${log4j}</version>
|
||||
</path>
|
||||
</annotationProcessorPaths>
|
||||
</configuration>
|
||||
<groupId>org.asciidoctor</groupId>
|
||||
<artifactId>asciidoctor-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
@@ -454,30 +461,6 @@
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
|
||||
<profile>
|
||||
<id>antora-process-resources</id>
|
||||
<build>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/antora/resources/antora-resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
</build>
|
||||
</profile>
|
||||
|
||||
<profile>
|
||||
<id>antora</id>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>io.spring.maven.antora</groupId>
|
||||
<artifactId>antora-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
</profiles>
|
||||
|
||||
<repositories>
|
||||
|
||||
@@ -1,42 +0,0 @@
|
||||
# PACKAGES antora@3.2.0-alpha.2 @antora/atlas-extension:1.0.0-alpha.1 @antora/collector-extension@1.0.0-alpha.3 @springio/antora-extensions@1.1.0-alpha.2 @asciidoctor/tabs@1.0.0-alpha.12 @opendevise/antora-release-line-extension@1.0.0-alpha.2
|
||||
#
|
||||
# The purpose of this Antora playbook is to build the docs in the current branch.
|
||||
antora:
|
||||
extensions:
|
||||
- '@antora/collector-extension'
|
||||
- require: '@springio/antora-extensions/root-component-extension'
|
||||
root_component_name: 'data-elasticsearch'
|
||||
site:
|
||||
title: Spring Data Elasticsearch
|
||||
url: https://docs.spring.io/spring-data-elasticsearch/reference/
|
||||
content:
|
||||
sources:
|
||||
- url: ./../../..
|
||||
branches: HEAD
|
||||
start_path: src/main/antora
|
||||
worktrees: true
|
||||
- url: https://github.com/spring-projects/spring-data-commons
|
||||
# Refname matching:
|
||||
# https://docs.antora.org/antora/latest/playbook/content-refname-matching/
|
||||
branches: [ main, 3.2.x ]
|
||||
start_path: src/main/antora
|
||||
asciidoc:
|
||||
attributes:
|
||||
page-pagination: ''
|
||||
hide-uri-scheme: '@'
|
||||
tabs-sync-option: '@'
|
||||
chomp: 'all'
|
||||
extensions:
|
||||
- '@asciidoctor/tabs'
|
||||
- '@springio/asciidoctor-extensions'
|
||||
sourcemap: true
|
||||
urls:
|
||||
latest_version_segment: ''
|
||||
runtime:
|
||||
log:
|
||||
failure_level: warn
|
||||
format: pretty
|
||||
ui:
|
||||
bundle:
|
||||
url: https://github.com/spring-io/antora-ui-spring/releases/download/v0.3.5/ui-bundle.zip
|
||||
snapshot: true
|
||||
@@ -1,12 +0,0 @@
|
||||
name: data-elasticsearch
|
||||
version: true
|
||||
title: Spring Data Elasticsearch
|
||||
nav:
|
||||
- modules/ROOT/nav.adoc
|
||||
ext:
|
||||
collector:
|
||||
- run:
|
||||
command: ./mvnw validate process-resources -am -Pantora-process-resources
|
||||
local: true
|
||||
scan:
|
||||
dir: target/classes/
|
||||
@@ -1,42 +0,0 @@
|
||||
* xref:index.adoc[Overview]
|
||||
** xref:commons/upgrade.adoc[]
|
||||
** xref:migration-guides.adoc[]
|
||||
*** xref:migration-guides/migration-guide-3.2-4.0.adoc[]
|
||||
*** xref:migration-guides/migration-guide-4.0-4.1.adoc[]
|
||||
*** xref:migration-guides/migration-guide-4.1-4.2.adoc[]
|
||||
*** xref:migration-guides/migration-guide-4.2-4.3.adoc[]
|
||||
*** xref:migration-guides/migration-guide-4.3-4.4.adoc[]
|
||||
*** xref:migration-guides/migration-guide-4.4-5.0.adoc[]
|
||||
*** xref:migration-guides/migration-guide-5.0-5.1.adoc[]
|
||||
*** xref:migration-guides/migration-guide-5.1-5.2.adoc[]
|
||||
|
||||
|
||||
* xref:elasticsearch.adoc[]
|
||||
** xref:elasticsearch/clients.adoc[]
|
||||
** xref:elasticsearch/object-mapping.adoc[]
|
||||
** xref:elasticsearch/template.adoc[]
|
||||
** xref:elasticsearch/reactive-template.adoc[]
|
||||
** xref:elasticsearch/entity-callbacks.adoc[]
|
||||
** xref:elasticsearch/auditing.adoc[]
|
||||
** xref:elasticsearch/join-types.adoc[]
|
||||
** xref:elasticsearch/routing.adoc[]
|
||||
** xref:elasticsearch/misc.adoc[]
|
||||
** xref:elasticsearch/scripted-and-runtime-fields.adoc[]
|
||||
|
||||
* xref:repositories.adoc[]
|
||||
** xref:repositories/core-concepts.adoc[]
|
||||
** xref:repositories/definition.adoc[]
|
||||
** xref:elasticsearch/repositories/elasticsearch-repositories.adoc[]
|
||||
** xref:elasticsearch/repositories/reactive-elasticsearch-repositories.adoc[]
|
||||
** xref:repositories/create-instances.adoc[]
|
||||
** xref:repositories/query-methods-details.adoc[]
|
||||
** xref:elasticsearch/repositories/elasticsearch-repository-queries.adoc[]
|
||||
** xref:repositories/projections.adoc[]
|
||||
** xref:repositories/custom-implementations.adoc[]
|
||||
** xref:repositories/core-domain-events.adoc[]
|
||||
** xref:repositories/null-handling.adoc[]
|
||||
** xref:elasticsearch/repositories/cdi-integration.adoc[]
|
||||
** xref:repositories/query-keywords-reference.adoc[]
|
||||
** xref:repositories/query-return-types-reference.adoc[]
|
||||
|
||||
* https://github.com/spring-projects/spring-data-commons/wiki[Wiki]
|
||||
@@ -1 +0,0 @@
|
||||
include::{commons}@data-commons::page$upgrade.adoc[]
|
||||
@@ -1,16 +0,0 @@
|
||||
[[elasticsearch.core]]
|
||||
= Elasticsearch Support
|
||||
:page-section-summary-toc: 1
|
||||
|
||||
Spring Data support for Elasticsearch contains a wide range of features:
|
||||
|
||||
* Spring configuration support for various xref:elasticsearch/clients.adoc[Elasticsearch clients].
|
||||
* The xref:elasticsearch/template.adoc[`ElasticsearchTemplate` and `ReactiveElasticsearchTemplate`] helper classes that provide object mapping between ES index operations and POJOs.
|
||||
* xref:elasticsearch/template.adoc#exception-translation[Exception translation] into Spring's portable {springDocsUrl}data-access.html#dao-exceptions[Data Access Exception Hierarchy].
|
||||
* Feature rich xref:elasticsearch/object-mapping.adoc[object mapping] integrated with _Spring's_ {springDocsUrl}core.html#core-convert[Conversion Service].
|
||||
* xref:elasticsearch/object-mapping.adoc#elasticsearch.mapping.meta-model.annotations[Annotation-based mapping] metadata that is extensible to support other metadata formats.
|
||||
* Java-based xref:elasticsearch/template.adoc#cassandra.template.query[query, criteria, and update DSLs].
|
||||
* Automatic implementation of xref:repositories.adoc[imperative and reactive `Repository` interfaces] including support for xref:repositories/custom-implementations.adoc[custom query methods].
|
||||
|
||||
For most data-oriented tasks, you can use the `[Reactive]ElasticsearchTemplate` or the `Repository` support, both of which use the rich object-mapping functionality.
|
||||
Spring Data Elasticsearch uses consistent naming conventions on objects in various APIs to those found in the DataStax Java Driver so that they are familiar and so that you can map your existing knowledge onto the Spring APIs.
|
||||
@@ -1,35 +0,0 @@
|
||||
[[elasticsearch.cdi]]
|
||||
= CDI Integration
|
||||
|
||||
The Spring Data Elasticsearch repositories can also be set up using CDI functionality.
|
||||
|
||||
.Spring Data Elasticsearch repositories using CDI
|
||||
====
|
||||
[source,java]
|
||||
----
|
||||
class ElasticsearchTemplateProducer {
|
||||
|
||||
@Produces
|
||||
@ApplicationScoped
|
||||
public ElasticsearchOperations createElasticsearchTemplate() {
|
||||
// ... <1>
|
||||
}
|
||||
}
|
||||
|
||||
class ProductService {
|
||||
|
||||
private ProductRepository repository; <2>
|
||||
public Page<Product> findAvailableBookByName(String name, Pageable pageable) {
|
||||
return repository.findByAvailableTrueAndNameStartingWith(name, pageable);
|
||||
}
|
||||
@Inject
|
||||
public void setRepository(ProductRepository repository) {
|
||||
this.repository = repository;
|
||||
}
|
||||
}
|
||||
----
|
||||
|
||||
<1> Create a component by using the same calls as are used in the xref:elasticsearch/template.adoc[Elasticsearch Operations] chapter.
|
||||
<2> Let the CDI framework inject the Repository into your class.
|
||||
|
||||
====
|
||||
@@ -1,228 +0,0 @@
|
||||
[[elasticsearch.misc.scripted-and-runtime-fields]]
|
||||
= Scripted and runtime fields
|
||||
|
||||
Spring Data Elasticsearch supports scripted fields and runtime fields.
|
||||
Please refer to the Elasticsearch documentation about scripting (https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting.html) and runtime fields (https://www.elastic.co/guide/en/elasticsearch/reference/8.9/runtime.html) for detailed information about this.
|
||||
In the context of Spring Data Elasticsearch you can use
|
||||
|
||||
* scripted fields that are used to return fields that are calculated on the result documents and added to the returned document.
|
||||
* runtime fields that are calculated on the stored documents and can be used in a query and/or be returned in the search result.
|
||||
|
||||
The following code snippets will show what you can do (these show imperative code, but the reactive implementation works similar).
|
||||
|
||||
[[the-person-entity]]
|
||||
== The person entity
|
||||
|
||||
The enity that is used in these examples is a `Person` entity.
|
||||
This entity has a `birthDate` and an `age` property.
|
||||
Whereas the birthdate is fix, the age depends on the time when a query is issued and needs to be calculated dynamically.
|
||||
|
||||
====
|
||||
[source,java]
|
||||
----
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.elasticsearch.annotations.DateFormat;
|
||||
import org.springframework.data.elasticsearch.annotations.Document;
|
||||
import org.springframework.data.elasticsearch.annotations.Field;
|
||||
import org.springframework.data.elasticsearch.annotations.ScriptedField;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
import static org.springframework.data.elasticsearch.annotations.FieldType.*;
|
||||
|
||||
import java.lang.Integer;
|
||||
|
||||
@Document(indexName = "persons")
|
||||
public record Person(
|
||||
@Id
|
||||
@Nullable
|
||||
String id,
|
||||
@Field(type = Text)
|
||||
String lastName,
|
||||
@Field(type = Text)
|
||||
String firstName,
|
||||
@Field(type = Keyword)
|
||||
String gender,
|
||||
@Field(type = Date, format = DateFormat.basic_date)
|
||||
LocalDate birthDate,
|
||||
@Nullable
|
||||
@ScriptedField Integer age <.>
|
||||
) {
|
||||
public Person(String id,String lastName, String firstName, String gender, String birthDate) {
|
||||
this(id, <.>
|
||||
lastName,
|
||||
firstName,
|
||||
LocalDate.parse(birthDate, DateTimeFormatter.ISO_LOCAL_DATE),
|
||||
gender,
|
||||
null);
|
||||
}
|
||||
}
|
||||
|
||||
----
|
||||
|
||||
<.> the `age` property will be calculated and filled in search results.
|
||||
<.> a convenience constructor to set up the test data.
|
||||
====
|
||||
|
||||
Note that the `age` property is annotated with `@ScriptedField`.
|
||||
This inhibits the writing of a corresponding entry in the index mapping and marks the property as a target to put a calculated field from a search response.
|
||||
|
||||
[[the-repository-interface]]
|
||||
== The repository interface
|
||||
|
||||
The repository used in this example:
|
||||
|
||||
====
|
||||
[source,java]
|
||||
----
|
||||
public interface PersonRepository extends ElasticsearchRepository<Person, String> {
|
||||
|
||||
SearchHits<Person> findAllBy(ScriptedField scriptedField);
|
||||
|
||||
SearchHits<Person> findByGenderAndAgeLessThanEqual(String gender, Integer age, RuntimeField runtimeField);
|
||||
}
|
||||
|
||||
----
|
||||
====
|
||||
|
||||
[[the-service-class]]
|
||||
== The service class
|
||||
|
||||
The service class has a repository injected and an `ElasticsearchOperations` instance to show several ways of populating and using the `age` property.
|
||||
We show the code split up in different pieces to put the explanations in
|
||||
|
||||
====
|
||||
[source,java]
|
||||
----
|
||||
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
|
||||
import org.springframework.data.elasticsearch.core.SearchHits;
|
||||
import org.springframework.data.elasticsearch.core.query.Criteria;
|
||||
import org.springframework.data.elasticsearch.core.query.CriteriaQuery;
|
||||
import org.springframework.data.elasticsearch.core.query.FetchSourceFilter;
|
||||
import org.springframework.data.elasticsearch.core.query.RuntimeField;
|
||||
import org.springframework.data.elasticsearch.core.query.ScriptData;
|
||||
import org.springframework.data.elasticsearch.core.query.ScriptType;
|
||||
import org.springframework.data.elasticsearch.core.query.ScriptedField;
|
||||
import org.springframework.data.elasticsearch.core.query.StringQuery;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class PersonService {
|
||||
private final ElasticsearchOperations operations;
|
||||
private final PersonRepository repository;
|
||||
|
||||
public PersonService(ElasticsearchOperations operations, SaRPersonRepository repository) {
|
||||
this.operations = operations;
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
public void save() { <.>
|
||||
List<Person> persons = List.of(
|
||||
new Person("1", "Smith", "Mary", "f", "1987-05-03"),
|
||||
new Person("2", "Smith", "Joshua", "m", "1982-11-17"),
|
||||
new Person("3", "Smith", "Joanna", "f", "2018-03-27"),
|
||||
new Person("4", "Smith", "Alex", "m", "2020-08-01"),
|
||||
new Person("5", "McNeill", "Fiona", "f", "1989-04-07"),
|
||||
new Person("6", "McNeill", "Michael", "m", "1984-10-20"),
|
||||
new Person("7", "McNeill", "Geraldine", "f", "2020-03-02"),
|
||||
new Person("8", "McNeill", "Patrick", "m", "2022-07-04"));
|
||||
|
||||
repository.saveAll(persons);
|
||||
}
|
||||
----
|
||||
|
||||
<.> a utility method to store some data in Elasticsearch.
|
||||
====
|
||||
|
||||
[[scripted-fields]]
|
||||
=== Scripted fields
|
||||
|
||||
The next piece shows how to use a scripted field to calculate and return the age of the persons.
|
||||
Scripted fields can only add something to the returned data, the age cannot be used in the query (see runtime fields for that).
|
||||
|
||||
====
|
||||
[source,java]
|
||||
----
|
||||
public SearchHits<Person> findAllWithAge() {
|
||||
|
||||
var scriptedField = ScriptedField.of("age", <.>
|
||||
ScriptData.of(b -> b
|
||||
.withType(ScriptType.INLINE)
|
||||
.withScript("""
|
||||
Instant currentDate = Instant.ofEpochMilli(new Date().getTime());
|
||||
Instant startDate = doc['birth-date'].value.toInstant();
|
||||
return (ChronoUnit.DAYS.between(startDate, currentDate) / 365);
|
||||
""")));
|
||||
|
||||
// version 1: use a direct query
|
||||
var query = new StringQuery("""
|
||||
{ "match_all": {} }
|
||||
""");
|
||||
query.addScriptedField(scriptedField); <.>
|
||||
query.addSourceFilter(FetchSourceFilter.of(b -> b.withIncludes("*"))); <.>
|
||||
|
||||
var result1 = operations.search(query, Person.class); <.>
|
||||
|
||||
// version 2: use the repository
|
||||
var result2 = repository.findAllBy(scriptedField); <.>
|
||||
|
||||
return result1;
|
||||
}
|
||||
----
|
||||
|
||||
<.> define the `ScriptedField` that calculates the age of a person.
|
||||
<.> when using a `Query`, add the scripted field to the query.
|
||||
<.> when adding a scripted field to a `Query`, an additional source filter is needed to also retrieve the _normal_ fields from the document source.
|
||||
<.> get the data where the `Person` entities now have the values set in their `age` property.
|
||||
<.> when using the repository, all that needs to be done is adding the scripted field as method parameter.
|
||||
====
|
||||
|
||||
[[runtime-fields]]
|
||||
=== Runtime fields
|
||||
|
||||
When using runtime fields, the calculated value can be used in the query itself.
|
||||
In the following code this is used to run a query for a given gender and maximum age of persons:
|
||||
|
||||
====
|
||||
[source,java]
|
||||
----
|
||||
public SearchHits<Person> findWithGenderAndMaxAge(String gender, Integer maxAge) {
|
||||
|
||||
var runtimeField = new RuntimeField("age", "long", """ <.>
|
||||
Instant currentDate = Instant.ofEpochMilli(new Date().getTime());
|
||||
Instant startDate = doc['birth-date'].value.toInstant();
|
||||
emit (ChronoUnit.DAYS.between(startDate, currentDate) / 365);
|
||||
""");
|
||||
|
||||
// variant 1 : use a direct query
|
||||
var query = CriteriaQuery.builder(Criteria
|
||||
.where("gender").is(gender)
|
||||
.and("age").lessThanEqual(maxAge))
|
||||
.withRuntimeFields(List.of(runtimeField)) <.>
|
||||
.withFields("age") <.>
|
||||
.withSourceFilter(FetchSourceFilter.of(b -> b.withIncludes("*"))) <.>
|
||||
.build();
|
||||
|
||||
var result1 = operations.search(query, Person.class); <.>
|
||||
|
||||
// variant 2: use the repository <.>
|
||||
var result2 = repository.findByGenderAndAgeLessThanEqual(gender, maxAge, runtimeField);
|
||||
|
||||
return result1;
|
||||
}
|
||||
}
|
||||
----
|
||||
|
||||
<.> define the runtime field that calculates the age of a person. // see https://asciidoctor.org/docs/user-manual/#builtin-attributes for builtin attributes.
|
||||
<.> when using `Query`, add the runtime field.
|
||||
<.> when adding a scripted field to a `Query`, an additional field parameter is needed to have the calculated value returned.
|
||||
<.> when adding a scripted field to a `Query`, an additional source filter is needed to also retrieve the _normal_ fields from the document source.
|
||||
<.> get the data filtered with the query and where the returned entites have the age property set.
|
||||
<.> when using the repository, all that needs to be done is adding the runtime field as method parameter.
|
||||
====
|
||||
|
||||
In addition to define a runtime fields on a query, they can also be defined in the index by setting the `runtimeFieldsPath` property of the `@Mapping` annotation to point to a JSON file that contains the runtime field definitions.
|
||||
@@ -1,23 +0,0 @@
|
||||
[[preface.versions]]
|
||||
= Versions
|
||||
|
||||
The following table shows the Elasticsearch and Spring versions that are used by Spring Data release trains and the version of Spring Data Elasticsearch included in that.
|
||||
|
||||
[cols="^,^,^,^",options="header"]
|
||||
|===
|
||||
| Spring Data Release Train | Spring Data Elasticsearch | Elasticsearch | Spring Framework
|
||||
| 2023.1 (Vaughan) | 5.2.x | 8.11.3 | 6.1.x
|
||||
| 2023.0 (Ullmann) | 5.1.x | 8.7.1 | 6.0.x
|
||||
| 2022.0 (Turing) | 5.0.xfootnote:oom[Out of maintenance] | 8.5.3 | 6.0.x
|
||||
| 2021.2 (Raj) | 4.4.xfootnote:oom[] | 7.17.3 | 5.3.x
|
||||
| 2021.1 (Q) | 4.3.xfootnote:oom[] | 7.15.2 | 5.3.x
|
||||
| 2021.0 (Pascal) | 4.2.xfootnote:oom[] | 7.12.0 | 5.3.x
|
||||
| 2020.0 (Ockham) | 4.1.xfootnote:oom[] | 7.9.3 | 5.3.2
|
||||
| Neumann | 4.0.xfootnote:oom[] | 7.6.2 | 5.2.12
|
||||
| Moore | 3.2.xfootnote:oom[] |6.8.12 | 5.2.12
|
||||
| Lovelace | 3.1.xfootnote:oom[] | 6.2.2 | 5.1.19
|
||||
| Kay | 3.0.xfootnote:oom[] | 5.5.0 | 5.0.13
|
||||
| Ingalls | 2.1.xfootnote:oom[] | 2.4.0 | 4.3.25
|
||||
|===
|
||||
|
||||
Support for upcoming versions of Elasticsearch is being tracked and general compatibility should be given assuming the usage of the xref:elasticsearch/template.adoc[ElasticsearchOperations interface].
|
||||
@@ -1,22 +0,0 @@
|
||||
[[spring-data-elasticsearch-reference-documentation]]
|
||||
= Spring Data Elasticsearch
|
||||
:revnumber: {version}
|
||||
:revdate: {localdate}
|
||||
:feature-scroll: true
|
||||
|
||||
_Spring Data Elasticsearch provides repository support for the Elasticsearch database.
|
||||
It eases development of applications with a consistent programming model that need to access Elasticsearch data sources._
|
||||
|
||||
[horizontal]
|
||||
xref:elasticsearch/versions.adoc[Versions] :: Version Compatibility Matrix
|
||||
xref:elasticsearch/clients.adoc[Clients] :: Elasticsearch Client Configuration
|
||||
xref:elasticsearch.adoc[Elasticsearch] :: Elasticsearch support
|
||||
xref:repositories.adoc[Repositories] :: Elasticsearch Repositories
|
||||
xref:migration-guides.adoc[Migration] :: Migration Guides
|
||||
https://github.com/spring-projects/spring-data-commons/wiki[Wiki] :: What's New, Upgrade Notes, Supported Versions, additional cross-version information.
|
||||
|
||||
BioMed Central Development Team; Oliver Drotbohm; Greg Turnquist; Christoph Strobl; Peter-Josef Meisch
|
||||
|
||||
(C) 2008-{copyright-year} VMware, Inc.
|
||||
|
||||
Copies of this document may be made for your own use and for distribution to others, provided that you do not charge any fee for such copies and further provided that each copy contains this Copyright Notice, whether distributed in print or electronically.
|
||||
@@ -1,8 +0,0 @@
|
||||
[[elasticsearch.migration]]
|
||||
= Migration Guides
|
||||
:page-section-summary-toc: 1
|
||||
|
||||
This section contains version-specific migration guides explaining how to upgrade across versions.
|
||||
|
||||
|
||||
|
||||
@@ -1,40 +0,0 @@
|
||||
[[elasticsearch-migration-guide-5.1-5.2]]
|
||||
= Upgrading from 5.1.x to 5.2.x
|
||||
|
||||
This section describes breaking changes from version 5.1.x to 5.2.x and how removed features can be replaced by new introduced features.
|
||||
|
||||
[[elasticsearch-migration-guide-5.1-5.2.breaking-changes]]
|
||||
== Breaking Changes
|
||||
|
||||
[[bulk-failures]]
|
||||
=== Bulk failures
|
||||
In the `org.springframework.data.elasticsearch.BulkFailureException` class, the return type of the `getFailedDocuments` is changed from `Map<String, String>`
|
||||
to `Map<String, FailureDetails>`, which allows to get additional details about failure reasons.
|
||||
|
||||
The definition of the `FailureDetails` class (inner to `BulkFailureException`):
|
||||
[source,java]
|
||||
public record FailureDetails(Integer status, String errorMessage) {
|
||||
}
|
||||
|
||||
[[scripted-and-runtime-fields]]
|
||||
=== scripted and runtime fields
|
||||
|
||||
The classes `org.springframework.data.elasticsearch.core.RuntimeField` and `org.springframework.data.elasticsearch.core.query.ScriptType` have been moved to the subpackage `org.springframework.data.elasticsearch.core.query`.
|
||||
|
||||
The `type` parameter of the `ScriptData` constructor is not nullable any longer.
|
||||
|
||||
[[elasticsearch-migration-guide-5.1-5.2.deprecations]]
|
||||
== Deprecations
|
||||
|
||||
[[removal-of-deprecated-code]]
|
||||
=== Removal of deprecated code
|
||||
|
||||
* All the code using the old deprecated `RestHighLevelClient` has been removed.
|
||||
The default Elasticsearch client used since version 5.0 is the (not so) new Elasticsearch Java client.
|
||||
* The `org.springframework.data.elasticsearch.client.ClientLogger` class has been removed.
|
||||
This logger was configured with the `org.springframework.data.elasticsearch.client.WIRE` setting, but was not working with all clients.
|
||||
From version 5 on, use the trace logger available in the Elasticsearch Java client, see xref:elasticsearch/clients.adoc#elasticsearch.clients.logging[Client Logging].
|
||||
* The method `org.springframework.data.elasticsearch.core.ElasticsearchOperations.stringIdRepresentation(Object)` has been removed, use the `convertId(Object)` method defined in the same interface instead.
|
||||
* The class `org.springframework.data.elasticsearch.core.Range` has been removed, use `org.springframework.data.domain.Range` instead.
|
||||
* The methods `org.springframework.data.elasticsearch.core.query.IndexQuery.getParentId() and `setParentId(String)` have been removed, they weren't used anymore and were no-ops.
|
||||
It has been removed from the `org.springframework.data.elasticsearch.core.query.IndexQuery` class as well.
|
||||
@@ -1,8 +0,0 @@
|
||||
[[elasticsearch.repositories]]
|
||||
= Repositories
|
||||
:page-section-summary-toc: 1
|
||||
|
||||
This chapter explains the basic foundations of Spring Data repositories and Elasticsearch specifics.
|
||||
Before continuing to the Elasticsearch specifics, make sure you have a sound understanding of the basic concepts.
|
||||
|
||||
The goal of the Spring Data repository abstraction is to significantly reduce the amount of boilerplate code required to implement data access layers for various persistence stores.
|
||||
@@ -1,4 +0,0 @@
|
||||
include::{commons}@data-commons::page$repositories/core-concepts.adoc[]
|
||||
|
||||
[[elasticsearch.entity-persistence.state-detection-strategies]]
|
||||
include::{commons}@data-commons::page$is-new-state-detection.adoc[leveloffset=+1]
|
||||
@@ -1 +0,0 @@
|
||||
include::{commons}@data-commons::page$repositories/core-domain-events.adoc[]
|
||||
@@ -1 +0,0 @@
|
||||
include::{commons}@data-commons::page$repositories/core-extensions.adoc[]
|
||||
@@ -1 +0,0 @@
|
||||
include::{commons}@data-commons::page$repositories/create-instances.adoc[]
|
||||
@@ -1 +0,0 @@
|
||||
include::{commons}@data-commons::page$repositories/custom-implementations.adoc[]
|
||||
@@ -1 +0,0 @@
|
||||
include::{commons}@data-commons::page$repositories/definition.adoc[]
|
||||
@@ -1 +0,0 @@
|
||||
include::{commons}@data-commons::page$repositories/null-handling.adoc[]
|
||||
@@ -1,4 +0,0 @@
|
||||
[[elasticsearch.projections]]
|
||||
= Projections
|
||||
|
||||
include::{commons}@data-commons::page$repositories/projections.adoc[leveloffset=+1]
|
||||
@@ -1 +0,0 @@
|
||||
include::{commons}@data-commons::page$repositories/query-keywords-reference.adoc[]
|
||||
@@ -1 +0,0 @@
|
||||
include::{commons}@data-commons::page$repositories/query-methods-details.adoc[]
|
||||
@@ -1 +0,0 @@
|
||||
include::{commons}@data-commons::page$repositories/query-return-types-reference.adoc[]
|
||||
@@ -1,21 +0,0 @@
|
||||
version: ${antora-component.version}
|
||||
prerelease: ${antora-component.prerelease}
|
||||
|
||||
asciidoc:
|
||||
attributes:
|
||||
copyright-year: ${current.year}
|
||||
version: ${project.version}
|
||||
springversionshort: ${spring.short}
|
||||
springversion: ${spring}
|
||||
attribute-missing: 'warn'
|
||||
commons: ${springdata.commons.docs}
|
||||
include-xml-namespaces: false
|
||||
spring-data-commons-docs-url: https://docs.spring.io/spring-data/commons/reference
|
||||
spring-data-commons-javadoc-base: https://docs.spring.io/spring-data/commons/docs/${springdata.commons}/api/
|
||||
springdocsurl: https://docs.spring.io/spring-framework/reference/{springversionshort}
|
||||
springjavadocurl: https://docs.spring.io/spring-framework/docs/${spring}/javadoc-api
|
||||
spring-framework-docs: '{springdocsurl}'
|
||||
spring-framework-javadoc: '{springjavadocurl}'
|
||||
springhateoasversion: ${spring-hateoas}
|
||||
releasetrainversion: ${releasetrain}
|
||||
store: Elasticsearch
|
||||
@@ -0,0 +1,52 @@
|
||||
= Spring Data Elasticsearch - Reference Documentation
|
||||
BioMed Central Development Team; Oliver Drotbohm; Greg Turnquist; Christoph Strobl; Peter-Josef Meisch
|
||||
:revnumber: {version}
|
||||
:revdate: {localdate}
|
||||
ifdef::backend-epub3[:front-cover-image: image:epub-cover.png[Front Cover,1050,1600]]
|
||||
:spring-data-commons-docs: ../../../../spring-data-commons/src/main/asciidoc
|
||||
|
||||
(C) 2013-2024 The original author(s).
|
||||
|
||||
NOTE: Copies of this document may be made for your own use and for distribution to others, provided that you do not charge any fee for such copies and further provided that each copy contains this Copyright Notice, whether distributed in print or electronically.
|
||||
|
||||
toc::[]
|
||||
|
||||
include::preface.adoc[]
|
||||
include::{spring-data-commons-docs}/upgrade.adoc[leveloffset=+1]
|
||||
|
||||
|
||||
:leveloffset: +1
|
||||
include::{spring-data-commons-docs}/repositories.adoc[]
|
||||
:leveloffset: -1
|
||||
|
||||
[[reference]]
|
||||
= Reference Documentation
|
||||
|
||||
:leveloffset: +1
|
||||
include::reference/elasticsearch-clients.adoc[]
|
||||
include::reference/elasticsearch-object-mapping.adoc[]
|
||||
include::reference/elasticsearch-operations.adoc[]
|
||||
|
||||
include::reference/elasticsearch-repositories.adoc[]
|
||||
|
||||
include::{spring-data-commons-docs}/auditing.adoc[]
|
||||
include::reference/elasticsearch-auditing.adoc[]
|
||||
|
||||
include::{spring-data-commons-docs}/entity-callbacks.adoc[]
|
||||
include::reference/elasticsearch-entity-callbacks.adoc[leveloffset=+1]
|
||||
|
||||
include::reference/elasticsearch-join-types.adoc[]
|
||||
include::reference/elasticsearch-routing.adoc[]
|
||||
include::reference/elasticsearch-misc.adoc[]
|
||||
:leveloffset: -1
|
||||
|
||||
[[appendix]]
|
||||
= Appendix
|
||||
:numbered!:
|
||||
:leveloffset: +1
|
||||
include::{spring-data-commons-docs}/repository-namespace-reference.adoc[]
|
||||
include::{spring-data-commons-docs}/repository-populator-namespace-reference.adoc[]
|
||||
include::{spring-data-commons-docs}/repository-query-keywords-reference.adoc[]
|
||||
include::{spring-data-commons-docs}/repository-query-return-types-reference.adoc[]
|
||||
include::reference/migration-guides.adoc[]
|
||||
:leveloffset: -1
|
||||
@@ -0,0 +1,54 @@
|
||||
[[preface]]
|
||||
= Preface
|
||||
|
||||
The Spring Data Elasticsearch project applies core Spring concepts to the development of solutions using the Elasticsearch Search Engine.
|
||||
It provides:
|
||||
|
||||
* _Templates_ as a high-level abstraction for storing, searching, sorting documents and building aggregations.
|
||||
* _Repositories_ which for example enable the user to express queries by defining interfaces having customized method names (for basic information about repositories see <<repositories>>).
|
||||
|
||||
You will notice similarities to the Spring data solr and mongodb support in the Spring Framework.
|
||||
|
||||
include::reference/elasticsearch-new.adoc[leveloffset=+1]
|
||||
|
||||
[[preface.metadata]]
|
||||
== Project Metadata
|
||||
|
||||
* Version Control - https://github.com/spring-projects/spring-data-elasticsearch
|
||||
* API Documentation - https://docs.spring.io/spring-data/elasticsearch/docs/current/api/
|
||||
* Bugtracker - https://github.com/spring-projects/spring-data-elasticsearch/issues
|
||||
* Release repository - https://repo1.maven.org/maven2/
|
||||
* Milestone repository - https://repo.spring.io/milestone/
|
||||
* Snapshot repository - https://repo.spring.io/snapshot/
|
||||
|
||||
[[preface.requirements]]
|
||||
== Requirements
|
||||
|
||||
Requires an installation of https://www.elastic.co/products/elasticsearch[Elasticsearch].
|
||||
|
||||
[[preface.versions]]
|
||||
=== Versions
|
||||
|
||||
The following table shows the Elasticsearch versions that are used by Spring Data release trains and version of
|
||||
Spring Data Elasticsearch included in that, as well as the Spring Boot versions referring to that particular Spring
|
||||
Data release train. The Elasticsearch version given shows with which client libraries Spring Data Elasticsearch was
|
||||
built and tested.
|
||||
|
||||
[cols="^,^,^,^,^",options="header"]
|
||||
|===
|
||||
| Spring Data Release Train | Spring Data Elasticsearch | Elasticsearch | Spring Framework | Spring Boot
|
||||
| 2023.0 (Ullmann) | 5.1.x | 8.7.1 | 6.0.x | 3.1.x
|
||||
| 2022.0 (Turing) | 5.0.x | 8.5.3 | 6.0.x | 3.0.x
|
||||
| 2021.2 (Raj) | 4.4.xfootnote:oom[Out of maintenance] | 7.17.3 | 5.3.x | 2.7.x
|
||||
| 2021.1 (Q) | 4.3.xfootnote:oom[] | 7.15.2 | 5.3.x | 2.6.x
|
||||
| 2021.0 (Pascal) | 4.2.xfootnote:oom[] | 7.12.0 | 5.3.x | 2.5.x
|
||||
| 2020.0 (Ockham) | 4.1.xfootnote:oom[] | 7.9.3 | 5.3.2 | 2.4.x
|
||||
| Neumann | 4.0.xfootnote:oom[] | 7.6.2 | 5.2.12 |2.3.x
|
||||
| Moore | 3.2.xfootnote:oom[] |6.8.12 | 5.2.12| 2.2.x
|
||||
| Lovelace | 3.1.xfootnote:oom[] | 6.2.2 | 5.1.19 |2.1.x
|
||||
| Kay | 3.0.xfootnote:oom[] | 5.5.0 | 5.0.13 | 2.0.x
|
||||
| Ingalls | 2.1.xfootnote:oom[] | 2.4.0 | 4.3.25 | 1.5.x
|
||||
|===
|
||||
|
||||
Support for upcoming versions of Elasticsearch is being tracked and general compatibility should be given assuming
|
||||
the usage of the <<elasticsearch.operations,ElasticsearchOperations interface>>.
|
||||
+3
-3
@@ -1,8 +1,8 @@
|
||||
[[elasticsearch.auditing]]
|
||||
= Elasticsearch Auditing
|
||||
== Elasticsearch Auditing
|
||||
|
||||
[[elasticsearch.auditing.preparing]]
|
||||
== Preparing entities
|
||||
=== Preparing entities
|
||||
|
||||
In order for the auditing code to be able to decide whether an entity instance is new, the entity must implement the `Persistable<ID>` interface which is defined as follows:
|
||||
|
||||
@@ -56,7 +56,7 @@ public class Person implements Persistable<Long> {
|
||||
<.> an object is new if it either has no `id` or none of fields containing creation attributes are set.
|
||||
|
||||
[[elasticsearch.auditing.activating]]
|
||||
== Activating auditing
|
||||
=== Activating auditing
|
||||
|
||||
After the entities have been set up and providing the `AuditorAware` - or `ReactiveAuditorAware` - the Auditing must be activated by setting the `@EnableElasticsearchAuditing` on a configuration class:
|
||||
|
||||
+144
-31
@@ -3,8 +3,10 @@
|
||||
|
||||
This chapter illustrates configuration and usage of supported Elasticsearch client implementations.
|
||||
|
||||
Spring Data Elasticsearch operates upon an Elasticsearch client (provided by Elasticsearch client libraries) that is connected to a single Elasticsearch node or a cluster.
|
||||
Although the Elasticsearch Client can be used directly to work with the cluster, applications using Spring Data Elasticsearch normally use the higher level abstractions of xref:elasticsearch/template.adoc[Elasticsearch Operations] and xref:elasticsearch/repositories/elasticsearch-repositories.adoc[Elasticsearch Repositories].
|
||||
Spring Data Elasticsearch operates upon an Elasticsearch client (provided by Elasticsearch client libraries) that is
|
||||
connected to a single Elasticsearch node or a cluster.
|
||||
Although the Elasticsearch Client can be used directly to work with the cluster, applications using Spring Data
|
||||
Elasticsearch normally use the higher level abstractions of <<elasticsearch.operations>> and <<elasticsearch.repositories>>.
|
||||
|
||||
[[elasticsearch.clients.restclient]]
|
||||
== Imperative Rest Client
|
||||
@@ -21,41 +23,33 @@ public class MyClientConfig extends ElasticsearchConfiguration {
|
||||
|
||||
@Override
|
||||
public ClientConfiguration clientConfiguration() {
|
||||
return ClientConfiguration.builder() <.>
|
||||
.connectedTo("localhost:9200")
|
||||
return ClientConfiguration.builder() <.>
|
||||
.connectedTo("localhost:9200")
|
||||
.build();
|
||||
}
|
||||
}
|
||||
----
|
||||
|
||||
<.> for a detailed description of the builder methods see xref:elasticsearch/clients.adoc#elasticsearch.clients.configuration[Client Configuration]
|
||||
<.> for a detailed description of the builder methods see <<elasticsearch.clients.configuration>>
|
||||
====
|
||||
|
||||
The `ElasticsearchConfiguration` class allows further configuration by overriding for example the `jsonpMapper()` or `transportOptions()` methods.
|
||||
|
||||
|
||||
The following beans can then be injected in other Spring components:
|
||||
|
||||
====
|
||||
[source,java]
|
||||
----
|
||||
import org.springframework.beans.factory.annotation.Autowired;@Autowired
|
||||
@Autowired
|
||||
ElasticsearchOperations operations; <.>
|
||||
|
||||
@Autowired
|
||||
@Autowired
|
||||
ElasticsearchClient elasticsearchClient; <.>
|
||||
|
||||
@Autowired
|
||||
RestClient restClient; <.>
|
||||
|
||||
@Autowired
|
||||
JsonpMapper jsonpMapper; <.>
|
||||
----
|
||||
|
||||
<.> an implementation of `ElasticsearchOperations`
|
||||
<.> the `co.elastic.clients.elasticsearch.ElasticsearchClient` that is used.
|
||||
<.> the low level `RestClient` from the Elasticsearch libraries
|
||||
<.> the `JsonpMapper` user by the Elasticsearch `Transport`
|
||||
====
|
||||
|
||||
Basically one should just use the `ElasticsearchOperations` to interact with the Elasticsearch cluster.
|
||||
@@ -77,17 +71,14 @@ public class MyClientConfig extends ReactiveElasticsearchConfiguration {
|
||||
@Override
|
||||
public ClientConfiguration clientConfiguration() {
|
||||
return ClientConfiguration.builder() <.>
|
||||
.connectedTo("localhost:9200")
|
||||
.connectedTo("localhost:9200")
|
||||
.build();
|
||||
}
|
||||
}
|
||||
----
|
||||
|
||||
<.> for a detailed description of the builder methods see xref:elasticsearch/clients.adoc#elasticsearch.clients.configuration[Client Configuration]
|
||||
<.> for a detailed description of the builder methods see <<elasticsearch.clients.configuration>>
|
||||
====
|
||||
|
||||
The `ReactiveElasticsearchConfiguration` class allows further configuration by overriding for example the `jsonpMapper()` or `transportOptions()` methods.
|
||||
|
||||
The following beans can then be injected in other Spring components:
|
||||
|
||||
====
|
||||
@@ -96,14 +87,11 @@ The following beans can then be injected in other Spring components:
|
||||
@Autowired
|
||||
ReactiveElasticsearchOperations operations; <.>
|
||||
|
||||
@Autowired
|
||||
@Autowired
|
||||
ReactiveElasticsearchClient elasticsearchClient; <.>
|
||||
|
||||
@Autowired
|
||||
RestClient restClient; <.>
|
||||
|
||||
@Autowired
|
||||
JsonpMapper jsonpMapper; <.>
|
||||
----
|
||||
|
||||
the following can be injected:
|
||||
@@ -112,12 +100,103 @@ the following can be injected:
|
||||
<.> the `org.springframework.data.elasticsearch.client.elc.ReactiveElasticsearchClient` that is used.
|
||||
This is a reactive implementation based on the Elasticsearch client implementation.
|
||||
<.> the low level `RestClient` from the Elasticsearch libraries
|
||||
<.> the `JsonpMapper` user by the Elasticsearch `Transport`
|
||||
====
|
||||
|
||||
Basically one should just use the `ReactiveElasticsearchOperations` to interact with the Elasticsearch cluster.
|
||||
When using repositories, this instance is used under the hood as well.
|
||||
|
||||
[[elasticsearch.clients.resthighlevelclient]]
|
||||
== High Level REST Client (deprecated)
|
||||
|
||||
[CAUTION]
|
||||
====
|
||||
The Elasticsearch Java RestHighLevelClient is deprecated, but still can be configured like shown (make sure to read
|
||||
<<elasticsearch-migration-guide-4.4-5.0.old-client>> as well).
|
||||
|
||||
It should only be used to access an Elasticsearch cluster running version 7, even with the compatibility headers set
|
||||
there are cases where the `RestHighLevelClient` cannot read the responses sent from a version 8 cluster.
|
||||
====
|
||||
|
||||
.RestHighLevelClient
|
||||
====
|
||||
[source,java]
|
||||
----
|
||||
import org.springframework.data.elasticsearch.client.erhlc.AbstractElasticsearchConfiguration;
|
||||
|
||||
@Configuration
|
||||
public class RestClientConfig extends AbstractElasticsearchConfiguration {
|
||||
|
||||
@Override
|
||||
@Bean
|
||||
public RestHighLevelClient elasticsearchClient() {
|
||||
|
||||
final ClientConfiguration clientConfiguration = ClientConfiguration.builder() <1>
|
||||
.connectedTo("localhost:9200")
|
||||
.build();
|
||||
|
||||
return RestClients.create(clientConfiguration).rest(); <2>
|
||||
}
|
||||
}
|
||||
|
||||
// ...
|
||||
|
||||
@Autowired
|
||||
RestHighLevelClient highLevelClient;
|
||||
|
||||
RestClient lowLevelClient = highLevelClient.lowLevelClient(); <3>
|
||||
----
|
||||
|
||||
<1> Use the builder to provide cluster addresses, set default `HttpHeaders` or enable SSL.
|
||||
<2> Create the RestHighLevelClient.
|
||||
<3> It is also possible to obtain the `lowLevelRest()` client.
|
||||
====
|
||||
|
||||
[[elasticsearch.clients.reactive]]
|
||||
== Reactive Client (deprecated)
|
||||
|
||||
The `org.springframework.data.elasticsearch.client.erhlc.ReactiveElasticsearchClient` is a non official driver based on `WebClient`.
|
||||
It uses the request/response objects provided by the Elasticsearch core project.
|
||||
Calls are directly operated on the reactive stack, **not** wrapping async (thread pool bound) responses into reactive types.
|
||||
|
||||
[CAUTION]
|
||||
====
|
||||
This was the first reactive implementation Spring Data Elasticsearch provided, but now is deprecated in favour
|
||||
of the `org.springframework.data.elasticsearch.client.elc.ReactiveElasticsearchClient`
|
||||
which uses the functionality offered by the new Elasticsearch client libraries.
|
||||
====
|
||||
|
||||
.Reactive REST Client (deprecated)
|
||||
====
|
||||
[source,java]
|
||||
----
|
||||
import org.springframework.data.elasticsearch.client.erhlc.AbstractReactiveElasticsearchConfiguration;
|
||||
|
||||
@Configuration
|
||||
public class ReactiveRestClientConfig extends AbstractReactiveElasticsearchConfiguration {
|
||||
|
||||
@Override
|
||||
@Bean
|
||||
public ReactiveElasticsearchClient reactiveElasticsearchClient() {
|
||||
final ClientConfiguration clientConfiguration = ClientConfiguration.builder() <.>
|
||||
.connectedTo("localhost:9200") //
|
||||
.build();
|
||||
return ReactiveRestClients.create(clientConfiguration);
|
||||
|
||||
}
|
||||
}
|
||||
// ...
|
||||
|
||||
Mono<IndexResponse> response = client.index(request ->
|
||||
|
||||
request.index("spring-data")
|
||||
.id(randomID())
|
||||
.source(singletonMap("feature", "reactive-client"));
|
||||
);
|
||||
----
|
||||
|
||||
<.> Use the builder to provide cluster addresses, set default `HttpHeaders` or enable SSL.
|
||||
====
|
||||
|
||||
[[elasticsearch.clients.configuration]]
|
||||
== Client Configuration
|
||||
|
||||
@@ -161,7 +240,7 @@ ClientConfiguration clientConfiguration = ClientConfiguration.builder()
|
||||
|
||||
<.> Define default headers, if they need to be customized
|
||||
<.> Use the builder to provide cluster addresses, set default `HttpHeaders` or enable SSL.
|
||||
<.> Optionally enable SSL.There exist overloads of this function that can take a `SSLContext` or as an alternative the fingerprint of the certificate as it is output by Elasticsearch 8 on startup.
|
||||
<.> Optionally enable SSL. There exist overloads of this function that can take a `SSLContext` or as an alternative the fingerprint of the certificate as it is output by Elasticsearch 8 on startup.
|
||||
<.> Optionally set a proxy.
|
||||
<.> Optionally set a path prefix, mostly used when different clusters a behind some reverse proxy.
|
||||
<.> Set the connection timeout.
|
||||
@@ -169,7 +248,8 @@ ClientConfiguration clientConfiguration = ClientConfiguration.builder()
|
||||
<.> Optionally set headers.
|
||||
<.> Add basic authentication.
|
||||
<.> A `Supplier<HttpHeaders>` function can be specified which is called every time before a request is sent to Elasticsearch - here, as an example, the current time is written in a header.
|
||||
<.> a function to configure the created client (see xref:elasticsearch/clients.adoc#elasticsearch.clients.configuration.callbacks[Client configuration callbacks]), can be added multiple times.
|
||||
<.> a function to configure the created client (see <<elasticsearch.clients.configuration.callbacks>>), can be added
|
||||
multiple times.
|
||||
====
|
||||
|
||||
IMPORTANT: Adding a Header supplier as shown in above example allows to inject headers that may change over the time, like authentication JWT tokens.
|
||||
@@ -178,8 +258,8 @@ If this is used in the reactive setup, the supplier function *must not* block!
|
||||
[[elasticsearch.clients.configuration.callbacks]]
|
||||
=== Client configuration callbacks
|
||||
|
||||
The `ClientConfiguration` class offers the most common parameters to configure the client.
|
||||
In the case this is not enough, the user can add callback functions by using the `withClientConfigurer(ClientConfigurationCallback<?>)` method.
|
||||
The `ClientConfiguration` class offers the most common parameters to configure the client. In the case this is not
|
||||
enough, the user can add callback functions by using the `withClientConfigurer(ClientConfigurationCallback<?>)` method.
|
||||
|
||||
The following callbacks are provided:
|
||||
|
||||
@@ -218,11 +298,44 @@ ClientConfiguration.builder()
|
||||
----
|
||||
====
|
||||
|
||||
[[elasticsearch.clients.configuration.headers]]
|
||||
=== Elasticsearch 7 compatibility headers
|
||||
|
||||
When using the deprecated `RestHighLevelClient` and accessing an Elasticsearch cluster that is running on version 8, it is necessary to set the compatibility headers
|
||||
https://www.elastic.co/guide/en/elasticsearch/reference/8.0/rest-api-compatibility.html[see Elasticsearch
|
||||
documentation].
|
||||
|
||||
For the imperative client this must be done by setting the default headers, for the reactive code this must be done using a header supplier:
|
||||
|
||||
CAUTION: Even when these headers are set, there are cases where the response returned from the cluster cannot be
|
||||
parsed with the client. This is not an error in Spring Data Elasticsearch.
|
||||
|
||||
====
|
||||
[source,java]
|
||||
----
|
||||
|
||||
HttpHeaders compatibilityHeaders = new HttpHeaders();
|
||||
compatibilityHeaders.add("Accept", "application/vnd.elasticsearch+json;compatible-with=7");
|
||||
compatibilityHeaders.add("Content-Type", "application/vnd.elasticsearch+json;"
|
||||
+ "compatible-with=7");
|
||||
|
||||
ClientConfiguration clientConfiguration = ClientConfiguration.builder()
|
||||
.connectedTo("localhost:9200")
|
||||
.withProxy("localhost:8080")
|
||||
.withBasicAuth("elastic","hcraescitsale")
|
||||
.withDefaultHeaders(compatibilityHeaders) // this variant for imperative code
|
||||
.withHeaders(() -> compatibilityHeaders) // this variant for reactive code
|
||||
.build();
|
||||
|
||||
----
|
||||
====
|
||||
|
||||
[[elasticsearch.clients.logging]]
|
||||
== Client Logging
|
||||
|
||||
To see what is actually sent to and received from the server `Request` / `Response` logging on the transport level needs to be turned on as outlined in the snippet below.
|
||||
This can be enabled in the Elasticsearch client by setting the level of the `tracer` package to "trace" (see
|
||||
To see what is actually sent to and received from the server `Request` / `Response` logging on the transport level
|
||||
needs to be turned on as outlined in the snippet below. This can be enabled in the Elasticsearch client by setting
|
||||
the level of the `tracer` package to "trace" (see
|
||||
https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/java-rest-low-usage-logging.html)
|
||||
|
||||
.Enable transport layer logging
|
||||
+1
-3
@@ -1,7 +1,5 @@
|
||||
include::{commons}@data-commons::page$entity-callbacks.adoc[]
|
||||
|
||||
[[elasticsearch.entity-callbacks]]
|
||||
== Store specific EntityCallbacks
|
||||
= Elasticsearch EntityCallbacks
|
||||
|
||||
Spring Data Elasticsearch uses the `EntityCallback` API internally for its auditing support and reacts on the following callbacks:
|
||||
|
||||
+3
-3
@@ -112,7 +112,7 @@ public class Statement {
|
||||
}
|
||||
}
|
||||
----
|
||||
<.> for routing related info see xref:elasticsearch/routing.adoc[Routing values]
|
||||
<.> for routing related info see <<elasticsearch.routing>>
|
||||
<.> a question can have answers and comments
|
||||
<.> an answer can have votes
|
||||
<.> the `JoinField` property is used to combine the name (_question_, _answer_, _comment_ or _vote_) of the relation with the parent id.
|
||||
@@ -208,13 +208,13 @@ void init() {
|
||||
<2> the first answer to the question
|
||||
<3> the second answer
|
||||
<4> a comment to the question
|
||||
<5> a vote for the first answer, this needs to have the routing set to the weather document, see xref:elasticsearch/routing.adoc[Routing values].
|
||||
<5> a vote for the first answer, this needs to have the routing set to the weather document, see <<elasticsearch.routing>>.
|
||||
====
|
||||
|
||||
[[elasticsearch.jointype.retrieving]]
|
||||
== Retrieving data
|
||||
|
||||
Currently native queries must be used to query the data, so there is no support from standard repository methods. xref:repositories/custom-implementations.adoc[] can be used instead.
|
||||
Currently native queries must be used to query the data, so there is no support from standard repository methods. <<repositories.custom-implementations>> can be used instead.
|
||||
|
||||
The following code shows as an example how to retrieve all entries that have a _vote_ (which must be _answers_, because only answers can have a vote) using an `ElasticsearchOperations` instance:
|
||||
|
||||
+7
-16
@@ -6,12 +6,9 @@ This section describes breaking changes from version 3.2.x to 4.0.x and how remo
|
||||
[[elasticsearch-migration-guide-3.2-4.0.jackson-removal]]
|
||||
== Removal of the used Jackson Mapper
|
||||
|
||||
One of the changes in version 4.0.x is that Spring Data Elasticsearch does not use the Jackson Mapper anymore to map an entity to the JSON representation needed for Elasticsearch (see xref:elasticsearch/object-mapping.adoc[Elasticsearch Object Mapping]).
|
||||
In version 3.2.x the Jackson Mapper was the default that was used.
|
||||
It was possible to switch to the meta-model based converter (named `ElasticsearchEntityMapper`) by explicitly configuring it (xref:elasticsearch/object-mapping.adoc#elasticsearch.mapping.meta-model[Meta Model Object Mapping]).
|
||||
One of the changes in version 4.0.x is that Spring Data Elasticsearch does not use the Jackson Mapper anymore to map an entity to the JSON representation needed for Elasticsearch (see <<elasticsearch.mapping>>). In version 3.2.x the Jackson Mapper was the default that was used. It was possible to switch to the meta-model based converter (named `ElasticsearchEntityMapper`) by explicitly configuring it (<<elasticsearch.mapping.meta-model>>).
|
||||
|
||||
In version 4.0.x the meta-model based converter is the only one that is available and does not need to be configured explicitly.
|
||||
If you had a custom configuration to enable the meta-model converter by providing a bean like this:
|
||||
In version 4.0.x the meta-model based converter is the only one that is available and does not need to be configured explicitly. If you had a custom configuration to enable the meta-model converter by providing a bean like this:
|
||||
|
||||
[source,java]
|
||||
----
|
||||
@@ -31,16 +28,13 @@ public EntityMapper entityMapper() {
|
||||
You now have to remove this bean, the `ElasticsearchEntityMapper` interface has been removed.
|
||||
|
||||
.Entity configuration
|
||||
Some users had custom Jackson annotations on the entity class, for example in order to define a custom name for the mapped document in Elasticsearch or to configure date conversions.
|
||||
These are not taken into account anymore.
|
||||
The needed functionality is now provided with Spring Data Elasticsearch's `@Field` annotation.
|
||||
Please see xref:elasticsearch/object-mapping.adoc#elasticsearch.mapping.meta-model.annotations[Mapping Annotation Overview] for detailed information.
|
||||
Some users had custom Jackson annotations on the entity class, for example in order to define a custom name for the mapped document in Elasticsearch or to configure date conversions. These are not taken into account anymore. The needed functionality is now provided with Spring Data Elasticsearch's `@Field` annotation. Please see <<elasticsearch.mapping.meta-model.annotations>> for detailed information.
|
||||
|
||||
[[elasticsearch-migration-guide-3.2-4.0.implicit-index-name]]
|
||||
== Removal of implicit index name from query objects
|
||||
|
||||
In 3.2.x the different query classes like `IndexQuery` or `SearchQuery` had properties that were taking the index name or index names that they were operating upon.If these were not set, the passed in entity was inspected to retrieve the index name that was set in the `@Document` annotation. +
|
||||
In 4.0.x the index name(s) must now be provided in an additional parameter of type `IndexCoordinates`.By separating this, it now is possible to use one query object against different indices.
|
||||
In 3.2.x the different query classes like `IndexQuery` or `SearchQuery` had properties that were taking the index name or index names that they were operating upon. If these were not set, the passed in entity was inspected to retrieve the index name that was set in the `@Document` annotation. +
|
||||
In 4.0.x the index name(s) must now be provided in an additional parameter of type `IndexCoordinates`. By separating this, it now is possible to use one query object against different indices.
|
||||
|
||||
So for example the following code:
|
||||
|
||||
@@ -136,9 +130,6 @@ Mapping types were removed from Elasticsearch 7, they still exist as deprecated
|
||||
|
||||
* The `SearchQuery` interface has been merged into it's base interface `Query`, so it's occurrences can just be replaced with `Query`.
|
||||
|
||||
* The method `org.springframework.data.elasticsearch.core.ElasticsearchOperations.query(SearchQuery query, ResultsExtractor<T> resultsExtractor);` and the `org.springframework.data.elasticsearch.core.ResultsExtractor` interface have been removed.
|
||||
These could be used to parse the result from Elasticsearch for cases in which the response mapping done with the Jackson based mapper was not enough.
|
||||
Since version 4.0, there are the new xref:elasticsearch/template.adoc#elasticsearch.operations.searchresulttypes[Search Result Types] to return the information from an Elasticsearch response, so there is no need to expose this low level functionality.
|
||||
* The method `org.springframework.data.elasticsearch.core.ElasticsearchOperations.query(SearchQuery query, ResultsExtractor<T> resultsExtractor);` and the `org.springframework.data.elasticsearch.core.ResultsExtractor` interface have been removed. These could be used to parse the result from Elasticsearch for cases in which the response mapping done with the Jackson based mapper was not enough. Since version 4.0, there are the new <<elasticsearch.operations.searchresulttypes>> to return the information from an Elasticsearch response, so there is no need to expose this low level functionality.
|
||||
|
||||
* The low level methods `startScroll`, `continueScroll` and `clearScroll` have been removed from the `ElasticsearchOperations` interface.
|
||||
For low level scroll API access, there now are `searchScrollStart`, `searchScrollContinue` and `searchScrollClear` methods on the `ElasticsearchRestTemplate` class.
|
||||
* The low level methods `startScroll`, `continueScroll` and `clearScroll` have been removed from the `ElasticsearchOperations` interface. For low level scroll API access, there now are `searchScrollStart`, `searchScrollContinue` and `searchScrollClear` methods on the `ElasticsearchRestTemplate` class.
|
||||
+1
-1
@@ -54,7 +54,7 @@ If a refresh policy is set, then it will be used by the repositories as well.
|
||||
[[elasticsearch-migration-guide-4.1-4.2.breaking-changes.refresh-policy.configuration]]
|
||||
==== Refresh configuration
|
||||
|
||||
When configuring Spring Data Elasticsearch like described in xref:elasticsearch/clients.adoc[Elasticsearch Clients] by using `ElasticsearchConfigurationSupport`, `AbstractElasticsearchConfiguration` or `AbstractReactiveElasticsearchConfiguration` the refresh policy will be initialized to `null`.
|
||||
When configuring Spring Data Elasticsearch like described in <<elasticsearch.clients>> by using `ElasticsearchConfigurationSupport`, `AbstractElasticsearchConfiguration` or `AbstractReactiveElasticsearchConfiguration` the refresh policy will be initialized to `null`.
|
||||
Previously the reactive code initialized this to `IMMEDIATE`, now reactive and non-reactive code show the same behaviour.
|
||||
|
||||
[[elasticsearch-migration-guide-4.1-4.2.breaking-changes.method-return-types]]
|
||||
+1
-1
@@ -15,7 +15,7 @@ For the user that means, that some enum classes that were used are replaced by e
|
||||
|
||||
Places where classes are used that cannot easily be replaced, this usage is marked as deprecated, we are working on replacements.
|
||||
|
||||
Check the sections on xref:migration-guides/migration-guide-4.2-4.3.adoc#elasticsearch-migration-guide-4.2-4.3.deprecations[Deprecations] and xref:migration-guides/migration-guide-4.2-4.3.adoc#elasticsearch-migration-guide-4.2-4.3.breaking-changes[Breaking Changes] for further details.
|
||||
Check the sections on <<elasticsearch-migration-guide-4.2-4.3.deprecations>> and <<elasticsearch-migration-guide-4.2-4.3.breaking-changes>> for further details.
|
||||
====
|
||||
|
||||
[[elasticsearch-migration-guide-4.2-4.3.deprecations]]
|
||||
+2
-3
@@ -18,7 +18,6 @@ As there now are multiple implementations using different client libraries the `
|
||||
[[elasticsearch-migration-guide-4.3-4.4.breaking-changes.1]]
|
||||
=== Removal of deprecated classes
|
||||
|
||||
[[org-springframework-data-elasticsearch-core-elasticsearchtemplate-has-been-removed]]
|
||||
==== `org.springframework.data.elasticsearch.core.ElasticsearchTemplate` has been removed
|
||||
|
||||
As of version 4.4 Spring Data Elasticsearch does not use the `TransportClient` from Elasticsearch anymore (which itself is deprecated since Elasticsearch 7.0).
|
||||
@@ -39,7 +38,7 @@ The `ReactiveElasticsearchTemplate`, when created directly or by Spring Boot con
|
||||
This could cause performance issues on heavy indexing and was different than the default behaviour of Elasticsearch.
|
||||
This has been changed to that now the default refresh policy is NONE.
|
||||
When the
|
||||
`ReactiveElasticsearchTemplate` was provided by using the configuration like described in xref:elasticsearch/clients.adoc#elasticsearch.clients.reactiverestclient[Reactive REST Client] the default refresh policy already was set to NONE.
|
||||
`ReactiveElasticsearchTemplate` was provided by using the configuration like described in <<elasticsearch.clients.reactive>> the default refresh policy already was set to NONE.
|
||||
|
||||
[[elasticsearch-migration-guide-4.3-4.4.new-clients]]
|
||||
== New Elasticsearch client
|
||||
@@ -76,7 +75,7 @@ public class SpringdataElasticTestApplication {
|
||||
====
|
||||
|
||||
Remove Spring Data Elasticsearch related properties from your application configuration.
|
||||
If Spring Data Elasticsearch was configured using a programmatic configuration (see xref:elasticsearch/clients.adoc[Elasticsearch Clients]), remove these beans from the Spring application context.
|
||||
If Spring Data Elasticsearch was configured using a programmatic configuration (see <<elasticsearch.clients>>), remove these beans from the Spring application context.
|
||||
|
||||
[[elasticsearch-migration-guide-4.3-4.4.new-clients.how-to.dependencies]]
|
||||
==== Add dependencies
|
||||
+4
-3
@@ -6,15 +6,16 @@ This section describes breaking changes from version 4.4.x to 5.0.x and how remo
|
||||
[[elasticsearch-migration-guide-4.4-4.5.deprecations]]
|
||||
== Deprecations
|
||||
|
||||
[[custom-trace-level-logging]]
|
||||
=== Custom trace level logging
|
||||
|
||||
Logging by setting the property `logging.level.org.springframework.data.elasticsearch.client.WIRE=trace` is deprecated now, the Elasticsearch `RestClient` provides a better solution that can be activated by setting the logging level of the `tracer` package to "trace".
|
||||
Logging by setting the property `logging.level.org.springframework.data.elasticsearch.client.WIRE=trace` is
|
||||
deprecated now, the Elasticsearch `RestClient` provides a better solution that can be activated by setting the
|
||||
logging level of the `tracer` package to "trace".
|
||||
|
||||
[[elasticsearch-migration-guide-4.4-4.5.deprecations.package]]
|
||||
=== `org.springframework.data.elasticsearch.client.erhlc` package
|
||||
|
||||
See xref:migration-guides/migration-guide-4.4-5.0.adoc#elasticsearch-migration-guide-4.4-5.0.breaking-changes-packages[Package changes], all classes in this package have been deprecated, as the default client implementations to use are the ones based on the new Java Client from Elasticsearch, see xref:migration-guides/migration-guide-4.4-5.0.adoc#elasticsearch-migration-guide-4.4-5.0.new-clients[New Elasticsearch client]
|
||||
See <<elasticsearch-migration-guide-4.4-5.0.breaking-changes-packages>>, all classes in this package have been deprecated, as the default client implementations to use are the ones based on the new Java Client from Elasticsearch, see <<elasticsearch-migration-guide-4.4-5.0.new-clients>>
|
||||
|
||||
[[elasticsearch-migration-guide-4.4-4.5.deprecations.code]]
|
||||
=== Removal of deprecated code
|
||||
+1
-2
@@ -18,10 +18,9 @@ Adaptions are necessary when this enum was used at other places than as a proper
|
||||
[[elasticsearch-migration-guide-5.0-5.1.deprecations]]
|
||||
== Deprecations
|
||||
|
||||
[[template-functions]]
|
||||
=== template functions
|
||||
|
||||
The functions in the `IndexOperations` and `ReactiverIndexOperations` to manage index templates that were introduced in Spring Data Elasticsearch 4.1
|
||||
have been deprecated. They were using the old Elasticsearch API that was deprecated in Elasticsearch version 7.8.
|
||||
|
||||
Please use the new functions that are based on the composable index template API instead.
|
||||
Please use the new functions that are based on the compsable index template API instead.
|
||||
+7
-38
@@ -2,7 +2,7 @@
|
||||
= Miscellaneous Elasticsearch Operation Support
|
||||
|
||||
This chapter covers additional support for Elasticsearch operations that cannot be directly accessed via the repository interface.
|
||||
It is recommended to add those operations as custom implementation as described in xref:repositories/custom-implementations.adoc[] .
|
||||
It is recommended to add those operations as custom implementation as described in <<repositories.custom-implementations>> .
|
||||
|
||||
[[elasticsearc.misc.index.settings]]
|
||||
== Index settings
|
||||
@@ -50,7 +50,7 @@ class Entity {
|
||||
[[elasticsearch.misc.mappings]]
|
||||
== Index Mapping
|
||||
|
||||
When Spring Data Elasticsearch creates the index mapping with the `IndexOperations.createMapping()` methods, it uses the annotations described in xref:elasticsearch/object-mapping.adoc#elasticsearch.mapping.meta-model.annotations[Mapping Annotation Overview], especially the `@Field` annotation.
|
||||
When Spring Data Elasticsearch creates the index mapping with the `IndexOperations.createMapping()` methods, it uses the annotations described in <<elasticsearch.mapping.meta-model.annotations>>, especially the `@Field` annotation.
|
||||
In addition to that it is possible to add the `@Mapping` annotation to a class.
|
||||
This annotation has the following properties:
|
||||
|
||||
@@ -181,7 +181,7 @@ interface SampleEntityRepository extends Repository<SampleEntity, String> {
|
||||
[[elasticsearch.misc.sorts]]
|
||||
== Sort options
|
||||
|
||||
In addition to the default sort options described in xref:repositories/query-methods-details.adoc#repositories.paging-and-sorting[Paging and Sorting], Spring Data Elasticsearch provides the class `org.springframework.data.elasticsearch.core.query.Order` which derives from `org.springframework.data.domain.Sort.Order`.
|
||||
In addition to the default sort options described in <<repositories.paging-and-sorting>>, Spring Data Elasticsearch provides the class `org.springframework.data.elasticsearch.core.query.Order` which derives from `org.springframework.data.domain.Sort.Order`.
|
||||
It offers additional parameters that can be sent to Elasticsearch when specifying the sorting of the result (see https://www.elastic.co/guide/en/elasticsearch/reference/7.15/sort-search-results.html).
|
||||
|
||||
There also is the `org.springframework.data.elasticsearch.core.query.GeoDistanceOrder` class which can be used to have the result of a search operation ordered by geographical distance.
|
||||
@@ -365,8 +365,9 @@ operations.putScript( <.>
|
||||
|
||||
To use a search template in a search query, Spring Data Elasticsearch provides the `SearchTemplateQuery`, an implementation of the `org.springframework.data.elasticsearch.core.query.Query` interface.
|
||||
|
||||
In the following code, we will add a call using a search template query to a custom repository implementation (see
|
||||
xref:repositories/custom-implementations.adoc[]) as an example how this can be integrated into a repository call.
|
||||
In the following code, we will add a call using a search template query to a custom repository implementation (see
|
||||
<<repositories.custom-implementations>>) as
|
||||
an example how this can be integrated into a repository call.
|
||||
|
||||
We first define the custom repository fragment interface:
|
||||
|
||||
@@ -398,7 +399,7 @@ public class PersonCustomRepositoryImpl implements PersonCustomRepository {
|
||||
var query = SearchTemplateQuery.builder() <.>
|
||||
.withId("person-firstname") <.>
|
||||
.withParams(
|
||||
Map.of( <.>
|
||||
Map.of( <.>
|
||||
"firstName", firstName,
|
||||
"from", pageable.getOffset(),
|
||||
"size", pageable.getPageSize()
|
||||
@@ -418,35 +419,3 @@ public class PersonCustomRepositoryImpl implements PersonCustomRepository {
|
||||
<.> The parameters are passed in a `Map<String,Object>`
|
||||
<.> Do the search in the same way as with the other query types.
|
||||
====
|
||||
|
||||
[[elasticsearch.misc.nested-sort]]
|
||||
== Nested sort
|
||||
Spring Data Elasticsearch supports sorting within nested objects (https://www.elastic.co/guide/en/elasticsearch/reference/8.9/sort-search-results.html#nested-sorting)
|
||||
|
||||
The following example, taken from the `org.springframework.data.elasticsearch.core.query.sort.NestedSortIntegrationTests` class, shows how to define the nested sort.
|
||||
|
||||
====
|
||||
[source,java]
|
||||
----
|
||||
var filter = StringQuery.builder("""
|
||||
{ "term": {"movies.actors.sex": "m"} }
|
||||
""").build();
|
||||
var order = new org.springframework.data.elasticsearch.core.query.Order(Sort.Direction.DESC,
|
||||
"movies.actors.yearOfBirth")
|
||||
.withNested(
|
||||
Nested.builder("movies")
|
||||
.withNested(
|
||||
Nested.builder("movies.actors")
|
||||
.withFilter(filter)
|
||||
.build())
|
||||
.build());
|
||||
|
||||
var query = Query.findAll().addSort(Sort.by(order));
|
||||
|
||||
----
|
||||
====
|
||||
|
||||
About the filter query: It is not possible to use a `CriteriaQuery` here, as this query would be converted into a Elasticsearch nested query which does not work in the filter context. So only `StringQuery` or `NativeQuery` can be used here. When using one of these, like the term query above, the Elasticsearch field names must be used, so take care, when these are redefined with the `@Field(name="...")` definition.
|
||||
|
||||
For the definition of the order path and the nested paths, the Java entity property names should be used.
|
||||
|
||||
+5
-22
@@ -1,28 +1,11 @@
|
||||
[[new-features]]
|
||||
= What's new
|
||||
|
||||
[[new-features.5-2-2]]
|
||||
== New in Spring Data Elasticsearch 5.2.2
|
||||
* Upgrade to Elasticsearch 8.11.3
|
||||
|
||||
[[new-features.5-2-0]]
|
||||
== New in Spring Data Elasticsearch 5.2
|
||||
|
||||
* Upgrade to Elasticsearch 8.11.1
|
||||
* The `JsonpMapper` for Elasticsearch is now configurable and provided as bean.
|
||||
* Improved AOT runtime hints for Elasticsearch client library classes.
|
||||
* Add Kotlin extensions and repository coroutine support.
|
||||
* Introducing `VersionConflictException` class thrown in case thatElasticsearch reports an 409 error with a version conflict.
|
||||
* Enable MultiField annotation on property getter
|
||||
* Support nested sort option
|
||||
* Improved scripted und runtime field support
|
||||
* Improved refresh policy support
|
||||
|
||||
[[new-features.5-1-0]]
|
||||
== New in Spring Data Elasticsearch 5.1
|
||||
|
||||
* Upgrade to Elasticsearch 8.7.1
|
||||
* Allow specification of the TLS certificate when connecting to an Elasticsearch 8 cluster
|
||||
* Upgrade to Elasticsearch 8.7.0
|
||||
* Allow specification of the TLS certificate when connecting to an Elasticsearch 8 cluster
|
||||
|
||||
[[new-features.5-0-0]]
|
||||
== New in Spring Data Elasticsearch 5.0
|
||||
@@ -71,7 +54,7 @@
|
||||
* Upgrade to Elasticsearch 7.6.2.
|
||||
* Deprecation of `TransportClient` usage.
|
||||
* Implements most of the mapping-types available for the index mappings.
|
||||
* Removal of the Jackson `ObjectMapper`, now using the xref:elasticsearch/object-mapping.adoc#elasticsearch.mapping.meta-model[MappingElasticsearchConverter]
|
||||
* Removal of the Jackson `ObjectMapper`, now using the <<elasticsearch.mapping.meta-model,MappingElasticsearchConverter>>
|
||||
* Cleanup of the API in the `*Operations` interfaces, grouping and renaming methods so that they match the Elasticsearch API, deprecating the old methods, aligning with other Spring Data modules.
|
||||
* Introduction of `SearchHit<T>` class to represent a found document together with the relevant result metadata for this document (i.e. _sortValues_).
|
||||
* Introduction of the `SearchHits<T>` class to represent a whole search result together with the metadata for the complete search result (i.e. _max_score_).
|
||||
@@ -85,7 +68,7 @@
|
||||
|
||||
* Secured Elasticsearch cluster support with Basic Authentication and SSL transport.
|
||||
* Upgrade to Elasticsearch 6.8.1.
|
||||
* Reactive programming support with xref:elasticsearch/repositories/reactive-elasticsearch-repositories.adoc[Reactive Elasticsearch Repositories] and xref:.
|
||||
* Introduction of the xref:elasticsearch/object-mapping.adoc#elasticsearch.mapping.meta-model[ElasticsearchEntityMapper] as an alternative to the Jackson `ObjectMapper`.
|
||||
* Reactive programming support with <<elasticsearch.reactive.operations>> and <<elasticsearch.reactive.repositories>>.
|
||||
* Introduction of the <<elasticsearch.mapping.meta-model,ElasticsearchEntityMapper>> as an alternative to the Jackson `ObjectMapper`.
|
||||
* Field name customization in `@Field`.
|
||||
* Support for Delete by Query.
|
||||
+14
-16
@@ -25,11 +25,11 @@ The most important attributes are (check the API documentation for the complete
|
||||
This can contain a SpEL template expression like `"log-#{T(java.time.LocalDate).now().toString()}"`
|
||||
** `createIndex`: flag whether to create an index on repository bootstrapping.
|
||||
Default value is _true_.
|
||||
See xref:elasticsearch/repositories/elasticsearch-repositories.adoc#elasticsearch.repositories.autocreation[Automatic creation of indices with the corresponding mapping]
|
||||
See <<elasticsearch.repositories.autocreation>>
|
||||
|
||||
|
||||
* `@Id`: Applied at the field level to mark the field used for identity purpose.
|
||||
* `@Transient`, `@ReadOnlyProperty`, `@WriteOnlyProperty`: see the following section xref:elasticsearch/object-mapping.adoc#elasticsearch.mapping.meta-model.annotations.read-write[Controlling which properties are written to and read from Elasticsearch] for detailed information.
|
||||
* `@Transient`, `@ReadOnlyProperty`, `@WriteOnlyProperty`: see the following section <<elasticsearch.mapping.meta-model.annotations.read-write>> for detailed information.
|
||||
* `@PersistenceConstructor`: Marks a given constructor - even a package protected one - to use when instantiating the object from the database.
|
||||
Constructor arguments are mapped by name to the key values in the retrieved Document.
|
||||
* `@Field`: Applied at the field level and defines properties of the field, most of the attributes map to the respective https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping.html[Elasticsearch Mapping] definitions (the following list is not complete, check the annotation Javadoc for a complete reference):
|
||||
@@ -38,8 +38,8 @@ Constructor arguments are mapped by name to the key values in the retrieved Docu
|
||||
See https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-types.html[Elasticsearch Mapping Types].
|
||||
If the field type is not specified, it defaults to `FieldType.Auto`.
|
||||
This means, that no mapping entry is written for the property and that Elasticsearch will add a mapping entry dynamically when the first data for this property is stored (check the Elasticsearch documentation for dynamic mapping rules).
|
||||
** `format`: One or more built-in date formats, see the next section xref:elasticsearch/object-mapping.adoc#elasticsearch.mapping.meta-model.annotations.date-formats[Date format mapping].
|
||||
** `pattern`: One or more custom date formats, see the next section xref:elasticsearch/object-mapping.adoc#elasticsearch.mapping.meta-model.annotations.date-formats[Date format mapping].
|
||||
** `format`: One or more built-in date formats, see the next section <<elasticsearch.mapping.meta-model.annotations.date-formats>>.
|
||||
** `pattern`: One or more custom date formats, see the next section <<elasticsearch.mapping.meta-model.annotations.date-formats>>.
|
||||
** `store`: Flag whether the original field value should be store in Elasticsearch, default value is _false_.
|
||||
** `analyzer`, `searchAnalyzer`, `normalizer` for specifying custom analyzers and normalizer.
|
||||
* `@GeoPoint`: Marks a field as _geo_point_ datatype.
|
||||
@@ -56,10 +56,10 @@ This section details the annotations that define if the value of a property is w
|
||||
|
||||
`@Transient`: A property annotated with this annotation will not be written to the mapping, it's value will not be sent to Elasticsearch and when documents are returned from Elasticsearch, this property will not be set in the resulting entity.
|
||||
|
||||
`@ReadOnlyProperty`: A property with this annotation will not have its value written to Elasticsearch, but when returning data, the property will be filled with the value returned in the document from Elasticsearch.
|
||||
`@ReadOnlyProperty`: A property with this annotaiton will not have its value written to Elasticsearch, but when returning data, the proeprty will be filled with the value returned in the document from Elasticsearch.
|
||||
One use case for this are runtime fields defined in the index mapping.
|
||||
|
||||
`@WriteOnlyProperty`: A property with this annotation will have its value stored in Elasticsearch but will not be set with any value when reading document.
|
||||
`@WriteOnlyProperty`: A property with this annotaiton will have its value stored in Elasticsearch but will not be set with any value when reading document.
|
||||
This can be used for example for synthesized fields which should go into the Elasticsearch index but are not used elsewhere.
|
||||
|
||||
[[elasticsearch.mapping.meta-model.annotations.date-formats]]
|
||||
@@ -71,7 +71,7 @@ This paragraph describes the use of
|
||||
|
||||
There are two attributes of the `@Field` annotation that define which date format information is written to the mapping (also see https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-date-format.html#built-in-date-formats[Elasticsearch Built In Formats] and https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-date-format.html#custom-date-formats[Elasticsearch Custom Date Formats])
|
||||
|
||||
The `format` attribute is used to define at least one of the predefined formats.
|
||||
The `format` attributes is used to define at least one of the predefined formats.
|
||||
If it is not defined, then a default value of __date_optional_time_ and _epoch_millis_ is used.
|
||||
|
||||
The `pattern` attribute can be used to add additional custom format strings.
|
||||
@@ -158,7 +158,7 @@ Supported classes for the type `<T>` are `Integer`, `Long`, `Float`, `Double`, `
|
||||
Without further configuration, Spring Data Elasticsearch will use the property name of an object as field name in Elasticsearch.
|
||||
This can be changed for individual field by using the `@Field` annotation on that property.
|
||||
|
||||
It is also possible to define a `FieldNamingStrategy` in the configuration of the client (xref:elasticsearch/clients.adoc[Elasticsearch Clients]).
|
||||
It is also possible to define a `FieldNamingStrategy` in the configuration of the client (<<elasticsearch.clients>>).
|
||||
If for example a `SnakeCaseFieldNamingStrategy` is configured, the property _sampleProperty_ of the object would be mapped to _sample_property_ in Elasticsearch.
|
||||
A `FieldNamingStrategy` applies to all entities; it can be overwritten by setting a specific name with `@Field` on a property.
|
||||
|
||||
@@ -187,12 +187,11 @@ public String getProperty() {
|
||||
[[elasticsearch.mapping.meta-model.annotations.misc]]
|
||||
==== Other property annotations
|
||||
|
||||
[[indexedindexname]]
|
||||
===== @IndexedIndexName
|
||||
|
||||
This annotation can be set on a String property of an entity.
|
||||
This property will not be written to the mapping, it will not be stored in Elasticsearch and its value will not be read from an Elasticsearch document.
|
||||
After an entity is persisted, for example with a call to `ElasticsearchOperations.save(T entity)`, the entity
|
||||
After an entity is persisted, for example with a call to `ElasticsearchOperations.save(T entity)`, the entity
|
||||
returned from that call will contain the name of the index that an entity was saved to in that property.
|
||||
This is useful when the index name is dynamically set by a bean, or when writing to a write alias.
|
||||
|
||||
@@ -261,13 +260,12 @@ public class Person {
|
||||
|
||||
NOTE: Type hints will not be written for nested Objects unless the properties type is `Object`, an interface or the actual value type does not match the properties declaration.
|
||||
|
||||
[[disabling-type-hints]]
|
||||
===== Disabling Type Hints
|
||||
|
||||
It may be necessary to disable writing of type hints when the index that should be used already exists without having the type hints defined in its mapping and with the mapping mode set to strict.
|
||||
In this case, writing the type hint will produce an error, as the field cannot be added automatically.
|
||||
|
||||
Type hints can be disabled for the whole application by overriding the method `writeTypeHints()` in a configuration class derived from `AbstractElasticsearchConfiguration` (see xref:elasticsearch/clients.adoc[Elasticsearch Clients]).
|
||||
Type hints can be disabled for the whole application by overriding the method `writeTypeHints()` in a configuration class derived from `AbstractElasticsearchConfiguration` (see <<elasticsearch.clients>>).
|
||||
|
||||
As an alternative they can be disabled for a single index with the `@Document` annotation:
|
||||
|
||||
@@ -351,7 +349,7 @@ The following GeoJson types are implemented:
|
||||
[[elasticsearch.mapping.meta-model.rules.collections]]
|
||||
==== Collections
|
||||
|
||||
For values inside Collections apply the same mapping rules as for aggregate roots when it comes to _type hints_ and xref:elasticsearch/object-mapping.adoc#elasticsearch.mapping.meta-model.conversions[Custom Conversions].
|
||||
For values inside Collections apply the same mapping rules as for aggregate roots when it comes to _type hints_ and <<elasticsearch.mapping.meta-model.conversions>>.
|
||||
|
||||
.Collections
|
||||
====
|
||||
@@ -379,7 +377,7 @@ public class Person {
|
||||
[[elasticsearch.mapping.meta-model.rules.maps]]
|
||||
==== Maps
|
||||
|
||||
For values inside Maps apply the same mapping rules as for aggregate roots when it comes to _type hints_ and xref:elasticsearch/object-mapping.adoc#elasticsearch.mapping.meta-model.conversions[Custom Conversions].
|
||||
For values inside Maps apply the same mapping rules as for aggregate roots when it comes to _type hints_ and <<elasticsearch.mapping.meta-model.conversions>>.
|
||||
However the Map key needs to a String to be processed by Elasticsearch.
|
||||
|
||||
.Collections
|
||||
@@ -414,7 +412,7 @@ public class Person {
|
||||
[[elasticsearch.mapping.meta-model.conversions]]
|
||||
=== Custom Conversions
|
||||
|
||||
Looking at the `Configuration` from the xref:elasticsearch/object-mapping.adoc#elasticsearch.mapping.meta-model[previous section] `ElasticsearchCustomConversions` allows registering specific rules for mapping domain and simple types.
|
||||
Looking at the `Configuration` from the <<elasticsearch.mapping.meta-model, previous section>> `ElasticsearchCustomConversions` allows registering specific rules for mapping domain and simple types.
|
||||
|
||||
.Meta Model Object Mapping Configuration
|
||||
====
|
||||
@@ -440,7 +438,7 @@ public class Config extends ElasticsearchConfiguration {
|
||||
|
||||
@WritingConverter <2>
|
||||
static class AddressToMap implements Converter<Address, Map<String, Object>> {
|
||||
|
||||
|
||||
@Override
|
||||
public Map<String, Object> convert(Address source) {
|
||||
|
||||
+11
-10
@@ -1,7 +1,7 @@
|
||||
[[elasticsearch.operations]]
|
||||
= Elasticsearch Operations
|
||||
|
||||
Spring Data Elasticsearch uses several interfaces to define the operations that can be called against an Elasticsearch index (for a description of the reactive interfaces see xref:elasticsearch/reactive-template.adoc[]).
|
||||
Spring Data Elasticsearch uses several interfaces to define the operations that can be called against an Elasticsearch index (for a description of the reactive interfaces see <<elasticsearch.reactive.operations>>).
|
||||
|
||||
* `IndexOperations` defines actions on index level like creating or deleting an index.
|
||||
* `DocumentOperations` defines actions to store, update and retrieve entities based on their id.
|
||||
@@ -21,12 +21,12 @@ The default implementations of the interfaces offer:
|
||||
====
|
||||
.Index management and automatic creation of indices and mappings.
|
||||
The `IndexOperations` interface and the provided implementation which can be obtained from an `ElasticsearchOperations` instance - for example with a call to `operations.indexOps(clazz)`- give the user the ability to create indices, put mappings or store template and alias information in the Elasticsearch cluster.
|
||||
Details of the index that will be created can be set by using the `@Setting` annotation, refer to xref:elasticsearch/misc.adoc#elasticsearc.misc.index.settings[Index settings] for further information.
|
||||
Details of the index that will be created can be set by using the `@Setting` annotation, refer to <<elasticsearc.misc.index.settings>> for further information.
|
||||
|
||||
**None of these operations are done automatically** by the implementations of `IndexOperations` or `ElasticsearchOperations`.
|
||||
It is the user's responsibility to call the methods.
|
||||
|
||||
There is support for automatic creation of indices and writing the mappings when using Spring Data Elasticsearch repositories, see xref:elasticsearch/repositories/elasticsearch-repositories.adoc#elasticsearch.repositories.autocreation[Automatic creation of indices with the corresponding mapping]
|
||||
There is support for automatic creation of indices and writing the mappings when using Spring Data Elasticsearch repositories, see <<elasticsearch.repositories.autocreation>>
|
||||
|
||||
====
|
||||
|
||||
@@ -34,7 +34,7 @@ There is support for automatic creation of indices and writing the mappings when
|
||||
== Usage examples
|
||||
|
||||
The example shows how to use an injected `ElasticsearchOperations` instance in a Spring REST controller.
|
||||
The example assumes that `Person` is a class that is annotated with `@Document`, `@Id` etc (see xref:elasticsearch/object-mapping.adoc#elasticsearch.mapping.meta-model.annotations[Mapping Annotation Overview]).
|
||||
The example assumes that `Person` is a class that is annotated with `@Document`, `@Id` etc (see <<elasticsearch.mapping.meta-model.annotations>>).
|
||||
|
||||
.ElasticsearchOperations usage
|
||||
====
|
||||
@@ -73,6 +73,7 @@ The id is read from the returned entity, as it might have been null in the `pers
|
||||
|
||||
To see the full possibilities of `ElasticsearchOperations` please refer to the API documentation.
|
||||
|
||||
include::reactive-elasticsearch-operations.adoc[leveloffset=+1]
|
||||
|
||||
[[elasticsearch.operations.searchresulttypes]]
|
||||
== Search Result Types
|
||||
@@ -125,7 +126,7 @@ Almost all of the methods defined in the `SearchOperations` and `ReactiveSearchO
|
||||
=== CriteriaQuery
|
||||
|
||||
`CriteriaQuery` based queries allow the creation of queries to search for data without knowing the syntax or basics of Elasticsearch queries.
|
||||
They allow the user to build queries by simply chaining and combining `Criteria` objects that specify the criteria the searched documents must fulfill.
|
||||
They allow the user to build queries by simply chaining and combining `Criteria` objects that specifiy the criteria the searched documents must fulfill.
|
||||
|
||||
NOTE: when talking about AND or OR when combining criteria keep in mind, that in Elasticsearch AND are converted to a **must** condition and OR to a **should**
|
||||
|
||||
@@ -213,14 +214,14 @@ Using `StringQuery` may be appropriate if you already have an Elasticsearch quer
|
||||
`NativeQuery` is the class to use when you have a complex query, or a query that cannot be expressed by using the `Criteria` API, for example when building queries and using aggregates.
|
||||
It allows to use all the different `co.elastic.clients.elasticsearch._types.query_dsl.Query` implementations from the Elasticsearch library therefore named "native".
|
||||
|
||||
The following code shows how to search for persons with a given `firstName` and for the found documents have a terms aggregation that counts the number of occurrences of the `lastName` for these persons:
|
||||
The following code shows how to search for persons with a given firstname and for the found documents have a terms aggregation that counts the number of occurences of the lastnames for these persons:
|
||||
|
||||
====
|
||||
[source,java]
|
||||
----
|
||||
Query query = NativeQuery.builder()
|
||||
.withAggregation("lastNames", Aggregation.of(a -> a
|
||||
.terms(ta -> ta.field("lastName").size(10))))
|
||||
.terms(ta -> ta.field("last-name").size(10))))
|
||||
.withQuery(q -> q
|
||||
.match(m -> m
|
||||
.field("firstName")
|
||||
@@ -234,8 +235,8 @@ SearchHits<Person> searchHits = operations.search(query, Person.class);
|
||||
----
|
||||
====
|
||||
|
||||
[[elasticsearch.operations.searchtemplatequery]]
|
||||
[[elasticsearch.operations.searchtemplateScOp§query]]
|
||||
=== SearchTemplateQuery
|
||||
|
||||
This is a special implementation of the `Query` interface to be used in combination with a stored search template.
|
||||
See xref:elasticsearch/misc.adoc#elasticsearch.misc.searchtemplates[Search Template support] for further information.
|
||||
This is a special implementation of the `Query` interface to be used in combination with a stored search template.
|
||||
See <<elasticsearch.misc.searchtemplates>> for further information.
|
||||
+41
-7
@@ -29,13 +29,13 @@ class Book {
|
||||
[[elasticsearch.repositories.autocreation]]
|
||||
== Automatic creation of indices with the corresponding mapping
|
||||
|
||||
The `@Document` annotation has an argument `createIndex`.
|
||||
If this argument is set to true - which is the default value - Spring Data Elasticsearch will during bootstrapping the repository support on application startup check if the index defined by the `@Document` annotation exists.
|
||||
The `@Document` annotation has an argument `createIndex`. If this argument is set to true - which is the default value - Spring Data Elasticsearch will during bootstrapping the repository support on application startup check if the index defined by the `@Document` annotation exists.
|
||||
|
||||
If it does not exist, the index will be created and the mappings derived from the entity's annotations (see xref:elasticsearch/object-mapping.adoc[Elasticsearch Object Mapping]) will be written to the newly created index.
|
||||
Details of the index that will be created can be set by using the `@Setting` annotation, refer to xref:elasticsearch/misc.adoc#elasticsearc.misc.index.settings[Index settings] for further information.
|
||||
If it does not exist, the index will be created and the mappings derived from the entity's annotations (see <<elasticsearch.mapping>>) will be written to the newly created index. Details of the index that will be created can be set by using the `@Setting` annotation, refer to <<elasticsearc.misc.index.settings>> for further information.
|
||||
|
||||
include::elasticsearch-repository-queries.adoc[leveloffset=+1]
|
||||
|
||||
include::reactive-elasticsearch-repositories.adoc[leveloffset=+1]
|
||||
|
||||
[[elasticsearch.repositories.annotations]]
|
||||
== Annotations for repository methods
|
||||
@@ -43,7 +43,7 @@ Details of the index that will be created can be set by using the `@Setting` ann
|
||||
[[elasticsearch.repositories.annotations.highlight]]
|
||||
=== @Highlight
|
||||
|
||||
The `@Highlight` annotation on a repository method defines for which fields of the returned entity highlighting should be included.To search for some text in a `Book` 's name or summary and have the found data highlighted, the following repository method can be used:
|
||||
The `@Highlight` annotation on a repository method defines for which fields of the returned entity highlighting should be included. To search for some text in a `Book` 's name or summary and have the found data highlighted, the following repository method can be used:
|
||||
|
||||
====
|
||||
[source,java]
|
||||
@@ -125,17 +125,51 @@ class ProductService {
|
||||
|
||||
<1> The `EnableElasticsearchRepositories` annotation activates the Repository support.
|
||||
If no base package is configured, it will use the one of the configuration class it is put on.
|
||||
<2> Provide a Bean named `elasticsearchTemplate` of type `ElasticsearchOperations` by using one of the configurations shown in the xref:elasticsearch/template.adoc[Elasticsearch Operations] chapter.
|
||||
<2> Provide a Bean named `elasticsearchTemplate` of type `ElasticsearchOperations` by using one of the configurations shown in the <<elasticsearch.operations>> chapter.
|
||||
<3> Let Spring inject the Repository bean into your class.
|
||||
====
|
||||
|
||||
[[elasticsearch.cdi]]
|
||||
== Elasticsearch Repositories using CDI
|
||||
|
||||
The Spring Data Elasticsearch repositories can also be set up using CDI functionality.
|
||||
|
||||
.Spring Data Elasticsearch repositories using CDI
|
||||
====
|
||||
[source,java]
|
||||
----
|
||||
class ElasticsearchTemplateProducer {
|
||||
|
||||
@Produces
|
||||
@ApplicationScoped
|
||||
public ElasticsearchOperations createElasticsearchTemplate() {
|
||||
// ... <1>
|
||||
}
|
||||
}
|
||||
|
||||
class ProductService {
|
||||
|
||||
private ProductRepository repository; <2>
|
||||
public Page<Product> findAvailableBookByName(String name, Pageable pageable) {
|
||||
return repository.findByAvailableTrueAndNameStartingWith(name, pageable);
|
||||
}
|
||||
@Inject
|
||||
public void setRepository(ProductRepository repository) {
|
||||
this.repository = repository;
|
||||
}
|
||||
}
|
||||
----
|
||||
<1> Create a component by using the same calls as are used in the <<elasticsearch.operations>> chapter.
|
||||
<2> Let the CDI framework inject the Repository into your class.
|
||||
|
||||
====
|
||||
|
||||
[[elasticsearch.namespace]]
|
||||
== Spring Namespace
|
||||
|
||||
The Spring Data Elasticsearch module contains a custom namespace allowing definition of repository beans as well as elements for instantiating a `ElasticsearchServer` .
|
||||
|
||||
Using the `repositories` element looks up Spring Data repositories as described in xref:repositories/create-instances.adoc[].
|
||||
Using the `repositories` element looks up Spring Data repositories as described in <<repositories.create-instances>> .
|
||||
|
||||
.Setting up Elasticsearch repositories using Namespace
|
||||
====
|
||||
+3
-3
@@ -10,12 +10,12 @@ The Elasticsearch module supports all basic query building feature as string que
|
||||
=== Declared queries
|
||||
|
||||
Deriving the query from the method name is not always sufficient and/or may result in unreadable method names.
|
||||
In this case one might make use of the `@Query` annotation (see xref:elasticsearch/repositories/elasticsearch-repository-queries.adoc#elasticsearch.query-methods.at-query[Using @Query Annotation] ).
|
||||
In this case one might make use of the `@Query` annotation (see <<elasticsearch.query-methods.at-query>> ).
|
||||
|
||||
[[elasticsearch.query-methods.criterions]]
|
||||
== Query creation
|
||||
|
||||
Generally the query creation mechanism for Elasticsearch works as described in xref:repositories/query-methods-details.adoc[].
|
||||
Generally the query creation mechanism for Elasticsearch works as described in <<repositories.query-methods>>.
|
||||
Here's a short example of what a Elasticsearch query method translates into:
|
||||
|
||||
.Query creation from method names
|
||||
@@ -137,7 +137,7 @@ A list of supported keywords for Elasticsearch is shown below.
|
||||
|
||||
|
||||
| `GreaterThanEqual`
|
||||
| `findByPriceGreaterThanEqual`
|
||||
| `findByPriceGreaterThan`
|
||||
| `{ "query" : {
|
||||
"bool" : {
|
||||
"must" : [
|
||||
+1
-1
@@ -10,7 +10,7 @@ Spring Data Elasticsearch supports routing definitions on storing and retrieving
|
||||
[[elasticsearch.routing.join-types]]
|
||||
== Routing on join-types
|
||||
|
||||
When using join-types (see xref:elasticsearch/join-types.adoc[Join-Type implementation]), Spring Data Elasticsearch will automatically use the `parent` property of the entity's `JoinField` property as the value for the routing.
|
||||
When using join-types (see <<elasticsearch.jointype>>), Spring Data Elasticsearch will automatically use the `parent` property of the entity's `JoinField` property as the value for the routing.
|
||||
|
||||
This is correct for all the use-cases where the parent-child relationship has just one level.
|
||||
If it is deeper, like a child-parent-grandparent relationship - like in the above example from _vote_ -> _answer_ -> _question_ - then the routing needs to explicitly specified by using the techniques described in the next section (the _vote_ needs the _question.id_ as routing value).
|
||||
@@ -0,0 +1,20 @@
|
||||
[[elasticsearch.migration]]
|
||||
= Appendix E: Migration Guides
|
||||
|
||||
// line breaks required otherwise the TOC breaks due to joining of first/last lines.
|
||||
:leveloffset: +1
|
||||
include::elasticsearch-migration-guide-3.2-4.0.adoc[]
|
||||
|
||||
include::elasticsearch-migration-guide-4.0-4.1.adoc[]
|
||||
|
||||
include::elasticsearch-migration-guide-4.1-4.2.adoc[]
|
||||
|
||||
include::elasticsearch-migration-guide-4.2-4.3.adoc[]
|
||||
|
||||
include::elasticsearch-migration-guide-4.3-4.4.adoc[]
|
||||
|
||||
include::elasticsearch-migration-guide-4.4-5.0.adoc[]
|
||||
|
||||
include::elasticsearch-migration-guide-5.0-5.1.adoc[]
|
||||
|
||||
:leveloffset: -1
|
||||
+10
-5
@@ -5,13 +5,18 @@
|
||||
|
||||
The `ReactiveElasticsearchTemplate` is the default implementation of `ReactiveElasticsearchOperations`.
|
||||
|
||||
[[elasticsearch.reactive.operations]]
|
||||
== Reactive Elasticsearch Operations
|
||||
|
||||
To get started the `ReactiveElasticsearchOperations` needs to know about the actual client to work with.
|
||||
Please see xref:elasticsearch/clients.adoc#elasticsearch.clients.reactiverestclient[Reactive Rest Client] for details on the client and how to configure it.
|
||||
Please see <<elasticsearch.clients.reactiverestclient>> for details on the client and how to configure it.
|
||||
|
||||
[[elasticsearch.reactive.operations.usage]]
|
||||
== Reactive Operations Usage
|
||||
=== Reactive Operations Usage
|
||||
|
||||
`ReactiveElasticsearchOperations` lets you save, find and delete your domain objects and map those objects to documents stored in Elasticsearch.
|
||||
`ReactiveElasticsearchOperations` lets you save, find and delete your domain objects and map those objects to documents
|
||||
stored
|
||||
in Elasticsearch.
|
||||
|
||||
Consider the following:
|
||||
|
||||
@@ -56,8 +61,8 @@ The above outputs the following sequence on the console.
|
||||
> QjWCWWcBXiLAnp77ksfR
|
||||
> 0
|
||||
----
|
||||
|
||||
<.> Insert a new `Person` document into the _marvel_ index . The `id` is generated on server side and set into the instance returned.
|
||||
<.> Insert a new `Person` document into the _marvel_ index . The `id` is generated on server
|
||||
side and set into the instance returned.
|
||||
<.> Lookup the `Person` with matching `id` in the _marvel_ index.
|
||||
<.> Delete the `Person` with matching `id`, extracted from the given instance, in the _marvel_ index.
|
||||
<.> Count the total number of documents in the _marvel_ index.
|
||||
+4
-2
@@ -1,9 +1,11 @@
|
||||
[[elasticsearch.reactive.repositories]]
|
||||
= Reactive Elasticsearch Repositories
|
||||
|
||||
Reactive Elasticsearch repository support builds on the core repository support explained in xref:repositories.adoc[] utilizing operations provided via xref:elasticsearch/reactive-template.adoc[] executed by a xref:elasticsearch/clients.adoc#elasticsearch.clients.reactiverestclient[Reactive REST Client].
|
||||
Reactive Elasticsearch repository support builds on the core repository support explained in <<repositories>> utilizing
|
||||
operations provided via <<elasticsearch.reactive.operations>> executed by a <<elasticsearch.clients.reactive>>.
|
||||
|
||||
Spring Data Elasticsearch reactive repository support uses https://projectreactor.io/[Project Reactor] as its reactive composition library of choice.
|
||||
Spring Data Elasticsearch reactive repository support uses https://projectreactor.io/[Project Reactor] as its reactive
|
||||
composition library of choice.
|
||||
|
||||
There are 3 main interfaces to be used:
|
||||
|
||||
@@ -21,27 +21,17 @@ import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author Peter-Josef Meisch
|
||||
* @author Illia Ulianov
|
||||
* @since 4.1
|
||||
*/
|
||||
public class BulkFailureException extends DataRetrievalFailureException {
|
||||
private final Map<String, FailureDetails> failedDocuments;
|
||||
private final Map<String, String> failedDocuments;
|
||||
|
||||
public BulkFailureException(String msg, Map<String, FailureDetails> failedDocuments) {
|
||||
public BulkFailureException(String msg, Map<String, String> failedDocuments) {
|
||||
super(msg);
|
||||
this.failedDocuments = failedDocuments;
|
||||
}
|
||||
|
||||
public Map<String, FailureDetails> getFailedDocuments() {
|
||||
public Map<String, String> getFailedDocuments() {
|
||||
return failedDocuments;
|
||||
}
|
||||
|
||||
/**
|
||||
* Details about a document saving failure.
|
||||
*
|
||||
* @author Illia Ulianov
|
||||
* @since 5.2
|
||||
*/
|
||||
public record FailureDetails(Integer status, String errorMessage) {
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,7 +48,6 @@ public enum DateFormat {
|
||||
date_hour_minute_second_fraction("uuuu-MM-dd'T'HH:mm:ss.SSS"), //
|
||||
date_hour_minute_second_millis("uuuu-MM-dd'T'HH:mm:ss.SSS"), //
|
||||
date_optional_time("uuuu-MM-dd['T'HH:mm:ss.SSSXXX]"), //
|
||||
strict_date_optional_time_nanos("uuuu-MM-dd['T'HH:mm:ss.SSSSSSXXX]"), //
|
||||
date_time("uuuu-MM-dd'T'HH:mm:ss.SSSXXX"), //
|
||||
date_time_no_millis("uuuu-MM-dd'T'HH:mm:ssVV"), // here Elasticsearch uses the zone-id in its implementation
|
||||
epoch_millis("epoch_millis"), //
|
||||
|
||||
@@ -58,13 +58,6 @@ public @interface Document {
|
||||
*/
|
||||
boolean createIndex() default true;
|
||||
|
||||
/**
|
||||
* If true, the index mapping will be written on repository bootstrapping even when the index already exists. This
|
||||
* allows for automatically updating the mapping with new properties. Changes on existing properties will lead to an
|
||||
* error from the Elasticsearch server.
|
||||
*/
|
||||
boolean alwaysWriteMapping() default false;
|
||||
|
||||
/**
|
||||
* Configuration of version management.
|
||||
*/
|
||||
|
||||
@@ -61,31 +61,7 @@ public enum FieldType {
|
||||
/** since 4.2 */
|
||||
Wildcard("wildcard"), //
|
||||
/** @since 4.2 */
|
||||
Dense_Vector("dense_vector"), //
|
||||
/**
|
||||
* @since 5.2
|
||||
*/
|
||||
Constant_Keyword("constant_keyword"), //
|
||||
/**
|
||||
* @since 5.2
|
||||
*/
|
||||
Alias("alias"), //
|
||||
/**
|
||||
* @since 5.2
|
||||
*/
|
||||
Version("version"), //
|
||||
/**
|
||||
* @since 5.2
|
||||
*/
|
||||
Murmur3("murmur3"), //
|
||||
/**
|
||||
* @since 5.2
|
||||
*/
|
||||
Match_Only_Text("match_only_text"), //
|
||||
/**
|
||||
* @since 5.2
|
||||
*/
|
||||
Annotated_Text("annotated_text") //
|
||||
Dense_Vector("dense_vector") //
|
||||
;
|
||||
|
||||
private final String mappedName;
|
||||
|
||||
@@ -30,7 +30,7 @@ import java.lang.annotation.Target;
|
||||
* @author Peter-Josef Meisch
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ ElementType.FIELD, ElementType.ANNOTATION_TYPE, ElementType.METHOD })
|
||||
@Target({ ElementType.FIELD, ElementType.ANNOTATION_TYPE })
|
||||
@Documented
|
||||
public @interface MultiField {
|
||||
|
||||
|
||||
@@ -3,7 +3,6 @@ package org.springframework.data.elasticsearch.annotations;
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* Marks a property to be populated with the result of a scripted field retrieved from an Elasticsearch response.
|
||||
* @author Ryan Murfitt
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
|
||||
+5
-1
@@ -27,18 +27,21 @@ import org.springframework.data.elasticsearch.client.elc.EntityAsMap;
|
||||
import org.springframework.data.elasticsearch.core.event.AfterConvertCallback;
|
||||
import org.springframework.data.elasticsearch.core.event.AfterLoadCallback;
|
||||
import org.springframework.data.elasticsearch.core.event.AfterSaveCallback;
|
||||
import org.springframework.data.elasticsearch.core.event.AuditingEntityCallback;
|
||||
import org.springframework.data.elasticsearch.core.event.BeforeConvertCallback;
|
||||
import org.springframework.data.elasticsearch.core.event.ReactiveAfterConvertCallback;
|
||||
import org.springframework.data.elasticsearch.core.event.ReactiveAfterLoadCallback;
|
||||
import org.springframework.data.elasticsearch.core.event.ReactiveAfterSaveCallback;
|
||||
import org.springframework.data.elasticsearch.core.event.ReactiveAuditingEntityCallback;
|
||||
import org.springframework.data.elasticsearch.core.event.ReactiveBeforeConvertCallback;
|
||||
import org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
/**
|
||||
* @author Peter-Josef Meisch
|
||||
* @since 5.1
|
||||
*/
|
||||
public class SpringDataElasticsearchRuntimeHints implements RuntimeHintsRegistrar {
|
||||
public class ElasticsearchRuntimeHints implements RuntimeHintsRegistrar {
|
||||
|
||||
@Override
|
||||
public void registerHints(RuntimeHints hints, @Nullable ClassLoader classLoader) {
|
||||
@@ -67,5 +70,6 @@ public class SpringDataElasticsearchRuntimeHints implements RuntimeHintsRegistra
|
||||
|
||||
// properties needed to log the different versions
|
||||
hints.resources().registerPattern("versions.properties");
|
||||
hints.resources().registerPattern("co/elastic/clients/version.properties");
|
||||
}
|
||||
}
|
||||
@@ -137,6 +137,7 @@ public interface ClientConfiguration {
|
||||
* Returns the {@link java.time.Duration connect timeout}.
|
||||
*
|
||||
* @see java.net.Socket#connect(SocketAddress, int)
|
||||
* @see io.netty.channel.ChannelOption#CONNECT_TIMEOUT_MILLIS
|
||||
*/
|
||||
Duration getConnectTimeout();
|
||||
|
||||
@@ -144,6 +145,8 @@ public interface ClientConfiguration {
|
||||
* Returns the {@link java.time.Duration socket timeout} which is typically applied as SO-timeout/read timeout.
|
||||
*
|
||||
* @see java.net.Socket#setSoTimeout(int)
|
||||
* @see io.netty.handler.timeout.ReadTimeoutHandler
|
||||
* @see io.netty.handler.timeout.WriteTimeoutHandler
|
||||
*/
|
||||
Duration getSocketTimeout();
|
||||
|
||||
@@ -291,6 +294,7 @@ public interface ClientConfiguration {
|
||||
* @param timeout the timeout to use. Must not be {@literal null}.
|
||||
* @return the {@link TerminalClientConfigurationBuilder}
|
||||
* @see java.net.Socket#connect(SocketAddress, int)
|
||||
* @see io.netty.channel.ChannelOption#CONNECT_TIMEOUT_MILLIS
|
||||
*/
|
||||
TerminalClientConfigurationBuilder withConnectTimeout(Duration timeout);
|
||||
|
||||
@@ -311,6 +315,8 @@ public interface ClientConfiguration {
|
||||
* @param timeout the timeout to use. Must not be {@literal null}.
|
||||
* @return the {@link TerminalClientConfigurationBuilder}
|
||||
* @see java.net.Socket#setSoTimeout(int)
|
||||
* @see io.netty.handler.timeout.ReadTimeoutHandler
|
||||
* @see io.netty.handler.timeout.WriteTimeoutHandler
|
||||
*/
|
||||
TerminalClientConfigurationBuilder withSocketTimeout(Duration timeout);
|
||||
|
||||
|
||||
+2
-1
@@ -59,6 +59,7 @@ class ClientConfigurationBuilder
|
||||
@Nullable private String pathPrefix;
|
||||
@Nullable private String proxy;
|
||||
private Supplier<HttpHeaders> headersSupplier = HttpHeaders::new;
|
||||
@Deprecated private final HttpClientConfigCallback httpClientConfigurer = httpClientBuilder -> httpClientBuilder;
|
||||
private final List<ClientConfiguration.ClientConfigurationCallback<?>> clientConfigurers = new ArrayList<>();
|
||||
|
||||
/*
|
||||
@@ -241,7 +242,7 @@ class ClientConfigurationBuilder
|
||||
}
|
||||
|
||||
return new DefaultClientConfiguration(hosts, headers, useSsl, sslContext, caFingerprint, soTimeout, connectTimeout,
|
||||
pathPrefix, hostnameVerifier, proxy, clientConfigurers, headersSupplier);
|
||||
pathPrefix, hostnameVerifier, proxy, httpClientConfigurer, clientConfigurers, headersSupplier);
|
||||
}
|
||||
|
||||
private static InetSocketAddress parse(String hostAndPort) {
|
||||
|
||||
@@ -0,0 +1,195 @@
|
||||
/*
|
||||
* Copyright 2018-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.elasticsearch.client;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
/**
|
||||
* Logging Utility to log client requests and responses. Logs client requests and responses to Elasticsearch to a
|
||||
* dedicated logger: {@code org.springframework.data.elasticsearch.client.WIRE} on trace level.
|
||||
*
|
||||
* @author Mark Paluch
|
||||
* @author Christoph Strobl
|
||||
* @author Peter-Josef Meisch
|
||||
* @author Oliver Drotbohm
|
||||
* @since 3.2
|
||||
* @deprecated since 5.0, Elasticsearch's RestClient has a trace level logging available.
|
||||
*/
|
||||
@Deprecated
|
||||
public abstract class ClientLogger {
|
||||
|
||||
private static final Log WIRE_LOGGER = LogFactory.getLog("org.springframework.data.elasticsearch.client.WIRE");
|
||||
|
||||
private ClientLogger() {}
|
||||
|
||||
/**
|
||||
* Returns {@literal true} if the logger is enabled.
|
||||
*
|
||||
* @return {@literal true} if the logger is enabled.
|
||||
*/
|
||||
public static boolean isEnabled() {
|
||||
return WIRE_LOGGER.isTraceEnabled();
|
||||
}
|
||||
|
||||
/**
|
||||
* Log an outgoing HTTP request.
|
||||
*
|
||||
* @param logId the correlation id, see {@link #newLogId()}.
|
||||
* @param method HTTP method
|
||||
* @param endpoint URI
|
||||
* @param parameters optional parameters.
|
||||
*/
|
||||
public static void logRequest(String logId, String method, String endpoint, Object parameters) {
|
||||
|
||||
if (isEnabled()) {
|
||||
WIRE_LOGGER.trace(String.format("[%s] Sending request %s %s with parameters: %s", logId, method.toUpperCase(),
|
||||
endpoint, parameters));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Log an outgoing HTTP request.
|
||||
*
|
||||
* @param logId the correlation id, see {@link #newLogId()}.
|
||||
* @param method HTTP method
|
||||
* @param endpoint URI
|
||||
* @param parameters optional parameters.
|
||||
* @param headers a String containing the headers
|
||||
* @since 4.4
|
||||
*/
|
||||
public static void logRequest(String logId, String method, String endpoint, Object parameters, String headers) {
|
||||
|
||||
if (isEnabled()) {
|
||||
WIRE_LOGGER.trace(String.format("[%s] Sending request%n%s %s%nParameters: %s%nHeaders: %s", logId,
|
||||
method.toUpperCase(), endpoint, parameters, headers));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Log an outgoing HTTP request with a request body.
|
||||
*
|
||||
* @param logId the correlation id, see {@link #newLogId()}.
|
||||
* @param method HTTP method
|
||||
* @param endpoint URI
|
||||
* @param parameters optional parameters.
|
||||
* @param body body content supplier.
|
||||
*/
|
||||
public static void logRequest(String logId, String method, String endpoint, Object parameters,
|
||||
Supplier<Object> body) {
|
||||
|
||||
if (isEnabled()) {
|
||||
WIRE_LOGGER.trace(String.format("[%s] Sending request %s %s with parameters: %s%nRequest body: %s", logId,
|
||||
method.toUpperCase(), endpoint, parameters, body.get()));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Log an outgoing HTTP request with a request body.
|
||||
*
|
||||
* @param logId the correlation id, see {@link #newLogId()}.
|
||||
* @param method HTTP method
|
||||
* @param endpoint URI
|
||||
* @param parameters optional parameters.
|
||||
* @param headers a String containing the headers
|
||||
* @param body body content supplier.
|
||||
* @since 4.4
|
||||
*/
|
||||
public static void logRequest(String logId, String method, String endpoint, Object parameters, String headers,
|
||||
Supplier<Object> body) {
|
||||
|
||||
if (isEnabled()) {
|
||||
WIRE_LOGGER.trace(String.format("[%s] Sending request%n%s %s%nParameters: %s%nHeaders: %s%nRequest body: %s",
|
||||
logId, method.toUpperCase(), endpoint, parameters, headers, body.get()));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Log a raw HTTP response without logging the body.
|
||||
*
|
||||
* @param logId the correlation id, see {@link #newLogId()}.
|
||||
* @param statusCode the HTTP status code.
|
||||
*/
|
||||
public static void logRawResponse(String logId, @Nullable Integer statusCode) {
|
||||
|
||||
if (isEnabled()) {
|
||||
WIRE_LOGGER.trace(String.format("[%s] Received raw response: %d", logId, statusCode));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Log a raw HTTP response without logging the body.
|
||||
*
|
||||
* @param logId the correlation id, see {@link #newLogId()}.
|
||||
* @param statusCode the HTTP status code.
|
||||
* @param headers a String containing the headers
|
||||
*/
|
||||
public static void logRawResponse(String logId, @Nullable Integer statusCode, String headers) {
|
||||
|
||||
if (isEnabled()) {
|
||||
WIRE_LOGGER.trace(String.format("[%s] Received response: %d%n%s", logId, statusCode, headers));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Log a raw HTTP response along with the body.
|
||||
*
|
||||
* @param logId the correlation id, see {@link #newLogId()}.
|
||||
* @param statusCode the HTTP status code.
|
||||
* @param body body content.
|
||||
*/
|
||||
public static void logResponse(String logId, Integer statusCode, String body) {
|
||||
|
||||
if (isEnabled()) {
|
||||
WIRE_LOGGER.trace(String.format("[%s] Received response: %d%nResponse body: %s", logId, statusCode, body));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Log a raw HTTP response along with the body.
|
||||
*
|
||||
* @param logId the correlation id, see {@link #newLogId()}.
|
||||
* @param statusCode the HTTP status code.
|
||||
* @param headers a String containing the headers
|
||||
* @param body body content.
|
||||
* @since 4.4
|
||||
*/
|
||||
public static void logResponse(String logId, @Nullable Integer statusCode, String headers, String body) {
|
||||
|
||||
if (isEnabled()) {
|
||||
WIRE_LOGGER.trace(String.format("[%s] Received response: %d%nHeaders: %s%nResponse body: %s", logId, statusCode,
|
||||
headers, body));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new, unique correlation id to improve tracing across log events.
|
||||
*
|
||||
* @return a new, unique correlation id.
|
||||
*/
|
||||
public static String newLogId() {
|
||||
|
||||
if (!isEnabled()) {
|
||||
return "-";
|
||||
}
|
||||
|
||||
return ObjectUtils.getIdentityHexString(new Object());
|
||||
}
|
||||
}
|
||||
+4
-1
@@ -49,13 +49,15 @@ class DefaultClientConfiguration implements ClientConfiguration {
|
||||
@Nullable private final String pathPrefix;
|
||||
@Nullable private final HostnameVerifier hostnameVerifier;
|
||||
@Nullable private final String proxy;
|
||||
private final HttpClientConfigCallback httpClientConfigurer;
|
||||
private final Supplier<HttpHeaders> headersSupplier;
|
||||
private final List<ClientConfigurationCallback<?>> clientConfigurers;
|
||||
|
||||
DefaultClientConfiguration(List<InetSocketAddress> hosts, HttpHeaders headers, boolean useSsl,
|
||||
@Nullable SSLContext sslContext, @Nullable String caFingerprint, Duration soTimeout, Duration connectTimeout,
|
||||
@Nullable String pathPrefix, @Nullable HostnameVerifier hostnameVerifier, @Nullable String proxy,
|
||||
List<ClientConfigurationCallback<?>> clientConfigurers, Supplier<HttpHeaders> headersSupplier) {
|
||||
HttpClientConfigCallback httpClientConfigurer, List<ClientConfigurationCallback<?>> clientConfigurers,
|
||||
Supplier<HttpHeaders> headersSupplier) {
|
||||
|
||||
this.hosts = List.copyOf(hosts);
|
||||
this.headers = headers;
|
||||
@@ -67,6 +69,7 @@ class DefaultClientConfiguration implements ClientConfiguration {
|
||||
this.pathPrefix = pathPrefix;
|
||||
this.hostnameVerifier = hostnameVerifier;
|
||||
this.proxy = proxy;
|
||||
this.httpClientConfigurer = httpClientConfigurer;
|
||||
this.clientConfigurers = clientConfigurers;
|
||||
this.headersSupplier = headersSupplier;
|
||||
}
|
||||
|
||||
+2
-2
@@ -115,8 +115,8 @@ final class DocumentAdapters {
|
||||
if (source == null) {
|
||||
document = Document.from(hitFieldsAsMap);
|
||||
} else {
|
||||
if (source instanceof EntityAsMap entityAsMap) {
|
||||
document = Document.from(entityAsMap);
|
||||
if (source instanceof EntityAsMap) {
|
||||
document = Document.from((EntityAsMap) source);
|
||||
} else if (source instanceof JsonData jsonData) {
|
||||
document = Document.from(jsonData.to(EntityAsMap.class));
|
||||
} else {
|
||||
|
||||
+101
-118
@@ -16,7 +16,6 @@
|
||||
package org.springframework.data.elasticsearch.client.elc;
|
||||
|
||||
import co.elastic.clients.elasticsearch.ElasticsearchClient;
|
||||
import co.elastic.clients.json.JsonpMapper;
|
||||
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
|
||||
import co.elastic.clients.transport.ElasticsearchTransport;
|
||||
import co.elastic.clients.transport.TransportOptions;
|
||||
@@ -25,6 +24,8 @@ import co.elastic.clients.transport.Version;
|
||||
import co.elastic.clients.transport.rest_client.RestClientOptions;
|
||||
import co.elastic.clients.transport.rest_client.RestClientTransport;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.time.Duration;
|
||||
import java.util.Arrays;
|
||||
@@ -34,10 +35,9 @@ import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.HttpRequest;
|
||||
import org.apache.http.HttpRequestInterceptor;
|
||||
import org.apache.http.*;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.entity.ByteArrayEntity;
|
||||
import org.apache.http.entity.ContentType;
|
||||
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
|
||||
import org.apache.http.message.BasicHeader;
|
||||
@@ -47,6 +47,7 @@ import org.elasticsearch.client.RequestOptions;
|
||||
import org.elasticsearch.client.RestClient;
|
||||
import org.elasticsearch.client.RestClientBuilder;
|
||||
import org.springframework.data.elasticsearch.client.ClientConfiguration;
|
||||
import org.springframework.data.elasticsearch.client.ClientLogger;
|
||||
import org.springframework.data.elasticsearch.support.HttpHeaders;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
@@ -57,18 +58,15 @@ import org.springframework.util.Assert;
|
||||
* @author Peter-Josef Meisch
|
||||
* @since 4.4
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public final class ElasticsearchClients {
|
||||
/**
|
||||
* Name of whose value can be used to correlate log messages for this request.
|
||||
*/
|
||||
private static final String LOG_ID_ATTRIBUTE = ElasticsearchClients.class.getName() + ".LOG_ID";
|
||||
private static final String X_SPRING_DATA_ELASTICSEARCH_CLIENT = "X-SpringDataElasticsearch-Client";
|
||||
public static final String IMPERATIVE_CLIENT = "imperative";
|
||||
public static final String REACTIVE_CLIENT = "reactive";
|
||||
private static final String IMPERATIVE_CLIENT = "imperative";
|
||||
private static final String REACTIVE_CLIENT = "reactive";
|
||||
|
||||
private static final JsonpMapper DEFAULT_JSONP_MAPPER = new JacksonJsonpMapper();
|
||||
|
||||
// region reactive client
|
||||
/**
|
||||
* Creates a new {@link ReactiveElasticsearchClient}
|
||||
*
|
||||
@@ -79,7 +77,7 @@ public final class ElasticsearchClients {
|
||||
|
||||
Assert.notNull(clientConfiguration, "clientConfiguration must not be null");
|
||||
|
||||
return createReactive(getRestClient(clientConfiguration), null, DEFAULT_JSONP_MAPPER);
|
||||
return createReactive(getRestClient(clientConfiguration), null);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -94,24 +92,7 @@ public final class ElasticsearchClients {
|
||||
|
||||
Assert.notNull(clientConfiguration, "ClientConfiguration must not be null!");
|
||||
|
||||
return createReactive(getRestClient(clientConfiguration), transportOptions, DEFAULT_JSONP_MAPPER);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link ReactiveElasticsearchClient}
|
||||
*
|
||||
* @param clientConfiguration configuration options, must not be {@literal null}.
|
||||
* @param transportOptions options to be added to each request.
|
||||
* @param jsonpMapper the JsonpMapper to use
|
||||
* @return the {@link ReactiveElasticsearchClient}
|
||||
*/
|
||||
public static ReactiveElasticsearchClient createReactive(ClientConfiguration clientConfiguration,
|
||||
@Nullable TransportOptions transportOptions, JsonpMapper jsonpMapper) {
|
||||
|
||||
Assert.notNull(clientConfiguration, "ClientConfiguration must not be null!");
|
||||
Assert.notNull(jsonpMapper, "jsonpMapper must not be null");
|
||||
|
||||
return createReactive(getRestClient(clientConfiguration), transportOptions, jsonpMapper);
|
||||
return createReactive(getRestClient(clientConfiguration), transportOptions);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -121,7 +102,7 @@ public final class ElasticsearchClients {
|
||||
* @return the {@link ReactiveElasticsearchClient}
|
||||
*/
|
||||
public static ReactiveElasticsearchClient createReactive(RestClient restClient) {
|
||||
return createReactive(restClient, null, DEFAULT_JSONP_MAPPER);
|
||||
return createReactive(restClient, null);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -132,29 +113,10 @@ public final class ElasticsearchClients {
|
||||
* @return the {@link ReactiveElasticsearchClient}
|
||||
*/
|
||||
public static ReactiveElasticsearchClient createReactive(RestClient restClient,
|
||||
@Nullable TransportOptions transportOptions, JsonpMapper jsonpMapper) {
|
||||
|
||||
Assert.notNull(restClient, "restClient must not be null");
|
||||
|
||||
var transport = getElasticsearchTransport(restClient, REACTIVE_CLIENT, transportOptions, jsonpMapper);
|
||||
return createReactive(transport);
|
||||
@Nullable TransportOptions transportOptions) {
|
||||
return new ReactiveElasticsearchClient(getElasticsearchTransport(restClient, REACTIVE_CLIENT, transportOptions));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link ReactiveElasticsearchClient} that uses the given {@link ElasticsearchTransport}.
|
||||
*
|
||||
* @param transport the transport to use
|
||||
* @return the {@link ElasticsearchClient
|
||||
*/
|
||||
public static ReactiveElasticsearchClient createReactive(ElasticsearchTransport transport) {
|
||||
|
||||
Assert.notNull(transport, "transport must not be null");
|
||||
|
||||
return new ReactiveElasticsearchClient(transport);
|
||||
}
|
||||
// endregion
|
||||
|
||||
// region imperative client
|
||||
/**
|
||||
* Creates a new imperative {@link ElasticsearchClient}
|
||||
*
|
||||
@@ -162,7 +124,7 @@ public final class ElasticsearchClients {
|
||||
* @return the {@link ElasticsearchClient}
|
||||
*/
|
||||
public static ElasticsearchClient createImperative(ClientConfiguration clientConfiguration) {
|
||||
return createImperative(getRestClient(clientConfiguration), null, DEFAULT_JSONP_MAPPER);
|
||||
return createImperative(getRestClient(clientConfiguration), null);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -174,7 +136,7 @@ public final class ElasticsearchClients {
|
||||
*/
|
||||
public static ElasticsearchClient createImperative(ClientConfiguration clientConfiguration,
|
||||
TransportOptions transportOptions) {
|
||||
return createImperative(getRestClient(clientConfiguration), transportOptions, DEFAULT_JSONP_MAPPER);
|
||||
return createImperative(getRestClient(clientConfiguration), transportOptions);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -184,7 +146,7 @@ public final class ElasticsearchClients {
|
||||
* @return the {@link ElasticsearchClient}
|
||||
*/
|
||||
public static ElasticsearchClient createImperative(RestClient restClient) {
|
||||
return createImperative(restClient, null, DEFAULT_JSONP_MAPPER);
|
||||
return createImperative(restClient, null);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -192,51 +154,17 @@ public final class ElasticsearchClients {
|
||||
*
|
||||
* @param restClient the RestClient to use
|
||||
* @param transportOptions options to be added to each request.
|
||||
* @param jsonpMapper the mapper for the transport to use
|
||||
* @return the {@link ElasticsearchClient}
|
||||
*/
|
||||
public static ElasticsearchClient createImperative(RestClient restClient, @Nullable TransportOptions transportOptions,
|
||||
JsonpMapper jsonpMapper) {
|
||||
public static ElasticsearchClient createImperative(RestClient restClient,
|
||||
@Nullable TransportOptions transportOptions) {
|
||||
|
||||
Assert.notNull(restClient, "restClient must not be null");
|
||||
|
||||
ElasticsearchTransport transport = getElasticsearchTransport(restClient, IMPERATIVE_CLIENT, transportOptions,
|
||||
jsonpMapper);
|
||||
|
||||
return createImperative(transport);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link ElasticsearchClient} that uses the given {@link ElasticsearchTransport}.
|
||||
*
|
||||
* @param transport the transport to use
|
||||
* @return the {@link ElasticsearchClient
|
||||
*/
|
||||
public static AutoCloseableElasticsearchClient createImperative(ElasticsearchTransport transport) {
|
||||
|
||||
Assert.notNull(transport, "transport must not be null");
|
||||
ElasticsearchTransport transport = getElasticsearchTransport(restClient, IMPERATIVE_CLIENT, transportOptions);
|
||||
|
||||
return new AutoCloseableElasticsearchClient(transport);
|
||||
}
|
||||
// endregion
|
||||
|
||||
// region low level RestClient
|
||||
private static RestClientOptions.Builder getRestClientOptionsBuilder(@Nullable TransportOptions transportOptions) {
|
||||
|
||||
if (transportOptions instanceof RestClientOptions restClientOptions) {
|
||||
return restClientOptions.toBuilder();
|
||||
}
|
||||
|
||||
var builder = new RestClientOptions.Builder(RequestOptions.DEFAULT.toBuilder());
|
||||
|
||||
if (transportOptions != null) {
|
||||
transportOptions.headers().forEach(header -> builder.addHeader(header.getKey(), header.getValue()));
|
||||
transportOptions.queryParameters().forEach(builder::setParameter);
|
||||
builder.onWarnings(transportOptions.onWarnings());
|
||||
}
|
||||
|
||||
return builder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a low level {@link RestClient} for the given configuration.
|
||||
@@ -272,6 +200,13 @@ public final class ElasticsearchClients {
|
||||
clientConfiguration.getHostNameVerifier().ifPresent(clientBuilder::setSSLHostnameVerifier);
|
||||
clientBuilder.addInterceptorLast(new CustomHeaderInjector(clientConfiguration.getHeadersSupplier()));
|
||||
|
||||
if (ClientLogger.isEnabled()) {
|
||||
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
|
||||
|
||||
clientBuilder.addInterceptorLast((HttpRequestInterceptor) interceptor);
|
||||
clientBuilder.addInterceptorLast((HttpResponseInterceptor) interceptor);
|
||||
}
|
||||
|
||||
RequestConfig.Builder requestConfigBuilder = RequestConfig.custom();
|
||||
Duration connectTimeout = clientConfiguration.getConnectTimeout();
|
||||
|
||||
@@ -308,55 +243,37 @@ public final class ElasticsearchClients {
|
||||
}
|
||||
return builder;
|
||||
}
|
||||
// endregion
|
||||
|
||||
// region Elasticsearch transport
|
||||
/**
|
||||
* Creates an {@link ElasticsearchTransport} that will use the given client that additionally is customized with a
|
||||
* header to contain the clientType
|
||||
*
|
||||
* @param restClient the client to use
|
||||
* @param clientType the client type to pass in each request as header
|
||||
* @param transportOptions options for the transport
|
||||
* @param jsonpMapper mapper for the transport
|
||||
* @return ElasticsearchTransport
|
||||
*/
|
||||
public static ElasticsearchTransport getElasticsearchTransport(RestClient restClient, String clientType,
|
||||
@Nullable TransportOptions transportOptions, JsonpMapper jsonpMapper) {
|
||||
|
||||
Assert.notNull(restClient, "restClient must not be null");
|
||||
Assert.notNull(clientType, "clientType must not be null");
|
||||
Assert.notNull(jsonpMapper, "jsonpMapper must not be null");
|
||||
private static ElasticsearchTransport getElasticsearchTransport(RestClient restClient, String clientType,
|
||||
@Nullable TransportOptions transportOptions) {
|
||||
|
||||
TransportOptions.Builder transportOptionsBuilder = transportOptions != null ? transportOptions.toBuilder()
|
||||
: new RestClientOptions(RequestOptions.DEFAULT).toBuilder();
|
||||
|
||||
RestClientOptions.Builder restClientOptionsBuilder = getRestClientOptionsBuilder(transportOptions);
|
||||
|
||||
ContentType jsonContentType = Version.VERSION == null ? ContentType.APPLICATION_JSON
|
||||
: ContentType.create("application/vnd.elasticsearch+json",
|
||||
new BasicNameValuePair("compatible-with", String.valueOf(Version.VERSION.major())));
|
||||
|
||||
Consumer<String> setHeaderIfNotPresent = header -> {
|
||||
if (restClientOptionsBuilder.build().headers().stream() //
|
||||
if (transportOptionsBuilder.build().headers().stream() //
|
||||
.noneMatch((h) -> h.getKey().equalsIgnoreCase(header))) {
|
||||
// need to add the compatibility header, this is only done automatically when not passing in custom options.
|
||||
// code copied from RestClientTransport as it is not available outside the package
|
||||
restClientOptionsBuilder.addHeader(header, jsonContentType.toString());
|
||||
transportOptionsBuilder.addHeader(header, jsonContentType.toString());
|
||||
}
|
||||
};
|
||||
|
||||
setHeaderIfNotPresent.accept("Content-Type");
|
||||
setHeaderIfNotPresent.accept("Accept");
|
||||
|
||||
restClientOptionsBuilder.addHeader(X_SPRING_DATA_ELASTICSEARCH_CLIENT, clientType);
|
||||
TransportOptions transportOptionsWithHeader = transportOptionsBuilder
|
||||
.addHeader(X_SPRING_DATA_ELASTICSEARCH_CLIENT, clientType).build();
|
||||
|
||||
return new RestClientTransport(restClient, jsonpMapper, restClientOptionsBuilder.build());
|
||||
return new RestClientTransport(restClient, new JacksonJsonpMapper(), transportOptionsWithHeader);
|
||||
}
|
||||
// endregion
|
||||
|
||||
private static List<String> formattedHosts(List<InetSocketAddress> hosts, boolean useSsl) {
|
||||
return hosts.stream().map(it -> (useSsl ? "https" : "http") + "://" + it.getHostString() + ':' + it.getPort())
|
||||
return hosts.stream().map(it -> (useSsl ? "https" : "http") + "://" + it.getHostString() + ":" + it.getPort())
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@@ -367,12 +284,78 @@ public final class ElasticsearchClients {
|
||||
.toArray(org.apache.http.Header[]::new);
|
||||
}
|
||||
|
||||
/**
|
||||
* Logging interceptors for Elasticsearch client logging.
|
||||
*
|
||||
* @see ClientLogger
|
||||
* @since 4.4
|
||||
* @deprecated since 5.0
|
||||
*/
|
||||
@Deprecated
|
||||
private static class HttpLoggingInterceptor implements HttpResponseInterceptor, HttpRequestInterceptor {
|
||||
|
||||
@Override
|
||||
public void process(HttpRequest request, HttpContext context) throws IOException {
|
||||
|
||||
String logId = (String) context.getAttribute(LOG_ID_ATTRIBUTE);
|
||||
|
||||
if (logId == null) {
|
||||
logId = ClientLogger.newLogId();
|
||||
context.setAttribute(LOG_ID_ATTRIBUTE, logId);
|
||||
}
|
||||
|
||||
String headers = Arrays.stream(request.getAllHeaders())
|
||||
.map(header -> header.getName()
|
||||
+ ((header.getName().equals("Authorization")) ? ": *****" : ": " + header.getValue()))
|
||||
.collect(Collectors.joining(", ", "[", "]"));
|
||||
|
||||
if (request instanceof HttpEntityEnclosingRequest entityRequest
|
||||
&& ((HttpEntityEnclosingRequest) request).getEntity() != null) {
|
||||
|
||||
HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
|
||||
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
|
||||
entity.writeTo(buffer);
|
||||
|
||||
if (!entity.isRepeatable()) {
|
||||
entityRequest.setEntity(new ByteArrayEntity(buffer.toByteArray()));
|
||||
}
|
||||
|
||||
ClientLogger.logRequest(logId, request.getRequestLine().getMethod(), request.getRequestLine().getUri(), "",
|
||||
headers, buffer::toString);
|
||||
} else {
|
||||
ClientLogger.logRequest(logId, request.getRequestLine().getMethod(), request.getRequestLine().getUri(), "",
|
||||
headers);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void process(HttpResponse response, HttpContext context) throws IOException {
|
||||
|
||||
String logId = (String) context.getAttribute(LOG_ID_ATTRIBUTE);
|
||||
|
||||
String headers = Arrays.stream(response.getAllHeaders())
|
||||
.map(header -> header.getName()
|
||||
+ ((header.getName().equals("Authorization")) ? ": *****" : ": " + header.getValue()))
|
||||
.collect(Collectors.joining(", ", "[", "]"));
|
||||
|
||||
// no way of logging the body, in this callback, it is not read yet, later there is no callback possibility in
|
||||
// RestClient or RestClientTransport
|
||||
ClientLogger.logRawResponse(logId, response.getStatusLine().getStatusCode(), headers);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Interceptor to inject custom supplied headers.
|
||||
*
|
||||
* @since 4.4
|
||||
*/
|
||||
private record CustomHeaderInjector(Supplier<HttpHeaders> headersSupplier) implements HttpRequestInterceptor {
|
||||
private static class CustomHeaderInjector implements HttpRequestInterceptor {
|
||||
|
||||
public CustomHeaderInjector(Supplier<HttpHeaders> headersSupplier) {
|
||||
this.headersSupplier = headersSupplier;
|
||||
}
|
||||
|
||||
private final Supplier<HttpHeaders> headersSupplier;
|
||||
|
||||
@Override
|
||||
public void process(HttpRequest request, HttpContext context) {
|
||||
|
||||
+7
-39
@@ -16,9 +16,6 @@
|
||||
package org.springframework.data.elasticsearch.client.elc;
|
||||
|
||||
import co.elastic.clients.elasticsearch.ElasticsearchClient;
|
||||
import co.elastic.clients.json.JsonpMapper;
|
||||
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
|
||||
import co.elastic.clients.transport.ElasticsearchTransport;
|
||||
import co.elastic.clients.transport.TransportOptions;
|
||||
import co.elastic.clients.transport.rest_client.RestClientOptions;
|
||||
|
||||
@@ -33,8 +30,7 @@ import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Base class for a @{@link org.springframework.context.annotation.Configuration} class to set up the Elasticsearch
|
||||
* connection using the Elasticsearch Client. This class exposes different parts of the setup as Spring beans. Deriving
|
||||
* classes must provide the {@link ClientConfiguration} to use.
|
||||
* connection using the Elasticsearch Client.
|
||||
*
|
||||
* @author Peter-Josef Meisch
|
||||
* @since 4.4
|
||||
@@ -46,11 +42,11 @@ public abstract class ElasticsearchConfiguration extends ElasticsearchConfigurat
|
||||
*
|
||||
* @return configuration, must not be {@literal null}
|
||||
*/
|
||||
@Bean(name = "elasticsearchClientConfiguration")
|
||||
@Bean(name="elasticsearchClientConfiguration")
|
||||
public abstract ClientConfiguration clientConfiguration();
|
||||
|
||||
/**
|
||||
* Provides the underlying low level Elasticsearch RestClient.
|
||||
* Provides the underlying low level RestClient.
|
||||
*
|
||||
* @param clientConfiguration configuration for the client, must not be {@literal null}
|
||||
* @return RestClient
|
||||
@@ -63,35 +59,18 @@ public abstract class ElasticsearchConfiguration extends ElasticsearchConfigurat
|
||||
return ElasticsearchClients.getRestClient(clientConfiguration);
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides the Elasticsearch transport to be used. The default implementation uses the {@link RestClient} bean and
|
||||
* the {@link JsonpMapper} bean provided in this class.
|
||||
*
|
||||
* @return the {@link ElasticsearchTransport}
|
||||
* @since 5.2
|
||||
*/
|
||||
@Bean
|
||||
public ElasticsearchTransport elasticsearchTransport(RestClient restClient, JsonpMapper jsonpMapper) {
|
||||
|
||||
Assert.notNull(restClient, "restClient must not be null");
|
||||
Assert.notNull(jsonpMapper, "jsonpMapper must not be null");
|
||||
|
||||
return ElasticsearchClients.getElasticsearchTransport(restClient, ElasticsearchClients.IMPERATIVE_CLIENT,
|
||||
transportOptions(), jsonpMapper);
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides the {@link ElasticsearchClient} to be used.
|
||||
*
|
||||
* @param transport the {@link ElasticsearchTransport} to use
|
||||
* @param restClient the low level RestClient to use
|
||||
* @return ElasticsearchClient instance
|
||||
*/
|
||||
@Bean
|
||||
public ElasticsearchClient elasticsearchClient(ElasticsearchTransport transport) {
|
||||
public ElasticsearchClient elasticsearchClient(RestClient restClient) {
|
||||
|
||||
Assert.notNull(transport, "transport must not be null");
|
||||
Assert.notNull(restClient, "restClient must not be null");
|
||||
|
||||
return ElasticsearchClients.createImperative(transport);
|
||||
return ElasticsearchClients.createImperative(restClient, transportOptions());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -110,17 +89,6 @@ public abstract class ElasticsearchConfiguration extends ElasticsearchConfigurat
|
||||
return template;
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides the JsonpMapper bean that is used in the {@link #elasticsearchTransport(RestClient, JsonpMapper)} method.
|
||||
*
|
||||
* @return the {@link JsonpMapper} to use
|
||||
* @since 5.2
|
||||
*/
|
||||
@Bean
|
||||
public JsonpMapper jsonpMapper() {
|
||||
return new JacksonJsonpMapper();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the options that should be added to every request. Must not be {@literal null}
|
||||
*/
|
||||
|
||||
+9
-15
@@ -32,7 +32,6 @@ import org.springframework.dao.support.PersistenceExceptionTranslator;
|
||||
import org.springframework.data.elasticsearch.NoSuchIndexException;
|
||||
import org.springframework.data.elasticsearch.ResourceNotFoundException;
|
||||
import org.springframework.data.elasticsearch.UncategorizedElasticsearchException;
|
||||
import org.springframework.data.elasticsearch.VersionConflictException;
|
||||
|
||||
/**
|
||||
* Simple {@link PersistenceExceptionTranslator} for Elasticsearch. Convert the given runtime exception to an
|
||||
@@ -69,7 +68,9 @@ public class ElasticsearchExceptionTranslator implements PersistenceExceptionTra
|
||||
@Override
|
||||
public DataAccessException translateExceptionIfPossible(RuntimeException ex) {
|
||||
|
||||
checkForConflictException(ex);
|
||||
if (isSeqNoConflict(ex)) {
|
||||
return new OptimisticLockingFailureException("Cannot index a document due to seq_no+primary_term conflict", ex);
|
||||
}
|
||||
|
||||
if (ex instanceof ElasticsearchException elasticsearchException) {
|
||||
|
||||
@@ -92,10 +93,6 @@ public class ElasticsearchExceptionTranslator implements PersistenceExceptionTra
|
||||
|
||||
return new ResourceNotFoundException(errorReason);
|
||||
}
|
||||
|
||||
if (response.status() == 409) {
|
||||
|
||||
}
|
||||
String body = JsonUtils.toJson(response, jsonpMapper);
|
||||
|
||||
if (errorType != null && errorType.contains("validation_exception")) {
|
||||
@@ -113,7 +110,7 @@ public class ElasticsearchExceptionTranslator implements PersistenceExceptionTra
|
||||
return null;
|
||||
}
|
||||
|
||||
private void checkForConflictException(Throwable exception) {
|
||||
private boolean isSeqNoConflict(Throwable exception) {
|
||||
Integer status = null;
|
||||
String message = null;
|
||||
|
||||
@@ -121,17 +118,14 @@ public class ElasticsearchExceptionTranslator implements PersistenceExceptionTra
|
||||
status = responseException.getResponse().getStatusLine().getStatusCode();
|
||||
message = responseException.getMessage();
|
||||
} else if (exception.getCause() != null) {
|
||||
checkForConflictException(exception.getCause());
|
||||
return isSeqNoConflict(exception.getCause());
|
||||
}
|
||||
|
||||
if (status != null && message != null) {
|
||||
if (status == 409 && message.contains("type\":\"version_conflict_engine_exception"))
|
||||
if (message.contains("version conflict, required seqNo")) {
|
||||
throw new OptimisticLockingFailureException("Cannot index a document due to seq_no+primary_term conflict",
|
||||
exception);
|
||||
} else if (message.contains("version conflict, current version [")) {
|
||||
throw new VersionConflictException("Version conflict", exception);
|
||||
}
|
||||
return status == 409 && message.contains("type\":\"version_conflict_engine_exception")
|
||||
&& message.contains("version conflict, required seqNo");
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
+7
-22
@@ -50,7 +50,6 @@ import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverte
|
||||
import org.springframework.data.elasticsearch.core.document.Document;
|
||||
import org.springframework.data.elasticsearch.core.document.SearchDocumentResponse;
|
||||
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
|
||||
import org.springframework.data.elasticsearch.core.query.BaseQueryBuilder;
|
||||
import org.springframework.data.elasticsearch.core.query.BulkOptions;
|
||||
import org.springframework.data.elasticsearch.core.query.ByQueryResponse;
|
||||
import org.springframework.data.elasticsearch.core.query.IndexQuery;
|
||||
@@ -71,7 +70,6 @@ import org.springframework.util.Assert;
|
||||
*
|
||||
* @author Peter-Josef Meisch
|
||||
* @author Hamid Rahimi
|
||||
* @author Illia Ulianov
|
||||
* @since 4.4
|
||||
*/
|
||||
public class ElasticsearchTemplate extends AbstractElasticsearchTemplate {
|
||||
@@ -141,7 +139,7 @@ public class ElasticsearchTemplate extends AbstractElasticsearchTemplate {
|
||||
public <T> T get(String id, Class<T> clazz, IndexCoordinates index) {
|
||||
|
||||
GetRequest getRequest = requestConverter.documentGetRequest(elasticsearchConverter.convertId(id),
|
||||
routingResolver.getRouting(), index);
|
||||
routingResolver.getRouting(), index, false);
|
||||
GetResponse<EntityAsMap> getResponse = execute(client -> client.get(getRequest, EntityAsMap.class));
|
||||
|
||||
ReadDocumentCallback<T> callback = new ReadDocumentCallback<>(elasticsearchConverter, clazz, index);
|
||||
@@ -224,16 +222,8 @@ public class ElasticsearchTemplate extends AbstractElasticsearchTemplate {
|
||||
Object queryObject = query.getObject();
|
||||
|
||||
if (queryObject != null) {
|
||||
query.setObject(entityOperations.updateIndexedObject(
|
||||
queryObject,
|
||||
new IndexedObjectInformation(
|
||||
indexResponse.id(),
|
||||
indexResponse.index(),
|
||||
indexResponse.seqNo(),
|
||||
indexResponse.primaryTerm(),
|
||||
indexResponse.version()),
|
||||
elasticsearchConverter,
|
||||
routingResolver));
|
||||
query.setObject(updateIndexedObject(queryObject, new IndexedObjectInformation(indexResponse.id(),
|
||||
indexResponse.index(), indexResponse.seqNo(), indexResponse.primaryTerm(), indexResponse.version())));
|
||||
}
|
||||
|
||||
return indexResponse.id();
|
||||
@@ -245,9 +235,9 @@ public class ElasticsearchTemplate extends AbstractElasticsearchTemplate {
|
||||
Assert.notNull(id, "id must not be null");
|
||||
Assert.notNull(index, "index must not be null");
|
||||
|
||||
ExistsRequest request = requestConverter.documentExistsRequest(id, routingResolver.getRouting(), index);
|
||||
GetRequest request = requestConverter.documentGetRequest(id, routingResolver.getRouting(), index, true);
|
||||
|
||||
return execute(client -> client.exists(request)).value();
|
||||
return execute(client -> client.get(request, EntityAsMap.class)).found();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -648,11 +638,6 @@ public class ElasticsearchTemplate extends AbstractElasticsearchTemplate {
|
||||
return NativeQuery.builder().withQuery(qb -> qb.ids(iq -> iq.values(ids))).build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public BaseQueryBuilder queryBuilderWithIds(List<String> ids) {
|
||||
return NativeQuery.builder().withIds(ids);
|
||||
}
|
||||
|
||||
/**
|
||||
* extract the list of {@link IndexedObjectInformation} from a {@link BulkResponse}.
|
||||
*
|
||||
@@ -662,11 +647,11 @@ public class ElasticsearchTemplate extends AbstractElasticsearchTemplate {
|
||||
protected List<IndexedObjectInformation> checkForBulkOperationFailure(BulkResponse bulkResponse) {
|
||||
|
||||
if (bulkResponse.errors()) {
|
||||
Map<String, BulkFailureException.FailureDetails> failedDocuments = new HashMap<>();
|
||||
Map<String, String> failedDocuments = new HashMap<>();
|
||||
for (BulkResponseItem item : bulkResponse.items()) {
|
||||
|
||||
if (item.error() != null) {
|
||||
failedDocuments.put(item.id(), new BulkFailureException.FailureDetails(item.status(), item.error().reason()));
|
||||
failedDocuments.put(item.id(), item.error().reason());
|
||||
}
|
||||
}
|
||||
throw new BulkFailureException(
|
||||
|
||||
+3
-3
@@ -26,8 +26,8 @@ import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.core.annotation.AnnotatedElementUtils;
|
||||
import org.springframework.dao.InvalidDataAccessApiUsageException;
|
||||
import org.springframework.data.elasticsearch.UncategorizedElasticsearchException;
|
||||
@@ -60,7 +60,7 @@ import org.springframework.util.Assert;
|
||||
public class IndicesTemplate extends ChildTemplate<ElasticsearchTransport, ElasticsearchIndicesClient>
|
||||
implements IndexOperations {
|
||||
|
||||
private static final Log LOGGER = LogFactory.getLog(IndicesTemplate.class);
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(IndicesTemplate.class);
|
||||
|
||||
// we need a cluster client as well because ES has put some methods from the indices API into the cluster client
|
||||
// (component templates)
|
||||
|
||||
-7
@@ -128,13 +128,6 @@ public class ReactiveElasticsearchClient extends ApiClient<ElasticsearchTranspor
|
||||
return Mono.fromFuture(transport.performRequestAsync(request, endpoint, transportOptions));
|
||||
}
|
||||
|
||||
public Mono<BooleanResponse> exists(ExistsRequest request) {
|
||||
|
||||
Assert.notNull(request, "request must not be null");
|
||||
|
||||
return Mono.fromFuture(transport.performRequestAsync(request, ExistsRequest._ENDPOINT, transportOptions));
|
||||
}
|
||||
|
||||
public <T, P> Mono<UpdateResponse<T>> update(UpdateRequest<T, P> request, Class<T> clazz) {
|
||||
|
||||
Assert.notNull(request, "request must not be null");
|
||||
|
||||
+6
-39
@@ -15,9 +15,6 @@
|
||||
*/
|
||||
package org.springframework.data.elasticsearch.client.elc;
|
||||
|
||||
import co.elastic.clients.json.JsonpMapper;
|
||||
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
|
||||
import co.elastic.clients.transport.ElasticsearchTransport;
|
||||
import co.elastic.clients.transport.TransportOptions;
|
||||
import co.elastic.clients.transport.rest_client.RestClientOptions;
|
||||
|
||||
@@ -32,8 +29,7 @@ import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Base class for a @{@link org.springframework.context.annotation.Configuration} class to set up the Elasticsearch
|
||||
* connection using the {@link ReactiveElasticsearchClient}. This class exposes different parts of the setup as Spring
|
||||
* beans. Deriving * classes must provide the {@link ClientConfiguration} to use.
|
||||
* connection using the {@link ReactiveElasticsearchClient}.
|
||||
*
|
||||
* @author Peter-Josef Meisch
|
||||
* @since 4.4
|
||||
@@ -45,7 +41,7 @@ public abstract class ReactiveElasticsearchConfiguration extends ElasticsearchCo
|
||||
*
|
||||
* @return configuration, must not be {@literal null}
|
||||
*/
|
||||
@Bean(name = "elasticsearchClientConfiguration")
|
||||
@Bean(name="elasticsearchClientConfiguration")
|
||||
public abstract ClientConfiguration clientConfiguration();
|
||||
|
||||
/**
|
||||
@@ -62,35 +58,18 @@ public abstract class ReactiveElasticsearchConfiguration extends ElasticsearchCo
|
||||
return ElasticsearchClients.getRestClient(clientConfiguration);
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides the Elasticsearch transport to be used. The default implementation uses the {@link RestClient} bean and
|
||||
* the {@link JsonpMapper} bean provided in this class.
|
||||
*
|
||||
* @return the {@link ElasticsearchTransport}
|
||||
* @since 5.2
|
||||
*/
|
||||
@Bean
|
||||
public ElasticsearchTransport elasticsearchTransport(RestClient restClient, JsonpMapper jsonpMapper) {
|
||||
|
||||
Assert.notNull(restClient, "restClient must not be null");
|
||||
Assert.notNull(jsonpMapper, "jsonpMapper must not be null");
|
||||
|
||||
return ElasticsearchClients.getElasticsearchTransport(restClient, ElasticsearchClients.REACTIVE_CLIENT,
|
||||
transportOptions(), jsonpMapper);
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides the {@link ReactiveElasticsearchClient} instance used.
|
||||
*
|
||||
* @param transport the ElasticsearchTransport to use
|
||||
* @param restClient the low level RestClient to use
|
||||
* @return ReactiveElasticsearchClient instance.
|
||||
*/
|
||||
@Bean
|
||||
public ReactiveElasticsearchClient reactiveElasticsearchClient(ElasticsearchTransport transport) {
|
||||
public ReactiveElasticsearchClient reactiveElasticsearchClient(RestClient restClient) {
|
||||
|
||||
Assert.notNull(transport, "transport must not be null");
|
||||
Assert.notNull(restClient, "restClient must not be null");
|
||||
|
||||
return ElasticsearchClients.createReactive(transport);
|
||||
return ElasticsearchClients.createReactive(restClient, transportOptions());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -109,18 +88,6 @@ public abstract class ReactiveElasticsearchConfiguration extends ElasticsearchCo
|
||||
return template;
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides the JsonpMapper that is used in the {@link #elasticsearchTransport(RestClient, JsonpMapper)} method and
|
||||
* exposes it as a bean.
|
||||
*
|
||||
* @return the {@link JsonpMapper} to use
|
||||
* @since 5.2
|
||||
*/
|
||||
@Bean
|
||||
public JsonpMapper jsonpMapper() {
|
||||
return new JacksonJsonpMapper();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the options that should be added to every request. Must not be {@literal null}
|
||||
*/
|
||||
|
||||
+93
-71
@@ -15,16 +15,16 @@
|
||||
*/
|
||||
package org.springframework.data.elasticsearch.client.elc;
|
||||
|
||||
import static co.elastic.clients.util.ApiTypeHelper.*;
|
||||
import static org.springframework.data.elasticsearch.client.elc.TypeUtils.*;
|
||||
import static co.elastic.clients.util.ApiTypeHelper.DANGEROUS_disableRequiredPropertiesCheck;
|
||||
import static org.springframework.data.elasticsearch.client.elc.TypeUtils.result;
|
||||
|
||||
import co.elastic.clients.elasticsearch._types.Result;
|
||||
import co.elastic.clients.elasticsearch.core.*;
|
||||
import co.elastic.clients.elasticsearch.core.bulk.BulkResponseItem;
|
||||
import co.elastic.clients.elasticsearch.core.get.GetResult;
|
||||
import co.elastic.clients.elasticsearch.core.search.ResponseBody;
|
||||
import co.elastic.clients.json.JsonpMapper;
|
||||
import co.elastic.clients.transport.Version;
|
||||
import co.elastic.clients.transport.endpoints.BooleanResponse;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.util.function.Tuple2;
|
||||
@@ -38,32 +38,22 @@ import java.util.function.BiFunction;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.reactivestreams.Publisher;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.elasticsearch.BulkFailureException;
|
||||
import org.springframework.data.elasticsearch.NoSuchIndexException;
|
||||
import org.springframework.data.elasticsearch.UncategorizedElasticsearchException;
|
||||
import org.springframework.data.elasticsearch.client.UnsupportedBackendOperation;
|
||||
import org.springframework.data.elasticsearch.core.AbstractReactiveElasticsearchTemplate;
|
||||
import org.springframework.data.elasticsearch.core.AggregationContainer;
|
||||
import org.springframework.data.elasticsearch.core.IndexedObjectInformation;
|
||||
import org.springframework.data.elasticsearch.core.MultiGetItem;
|
||||
import org.springframework.data.elasticsearch.core.ReactiveIndexOperations;
|
||||
import org.springframework.data.elasticsearch.core.*;
|
||||
import org.springframework.data.elasticsearch.core.cluster.ReactiveClusterOperations;
|
||||
import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter;
|
||||
import org.springframework.data.elasticsearch.core.document.Document;
|
||||
import org.springframework.data.elasticsearch.core.document.SearchDocument;
|
||||
import org.springframework.data.elasticsearch.core.document.SearchDocumentResponse;
|
||||
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
|
||||
import org.springframework.data.elasticsearch.core.query.BaseQuery;
|
||||
import org.springframework.data.elasticsearch.core.query.BaseQueryBuilder;
|
||||
import org.springframework.data.elasticsearch.core.query.BulkOptions;
|
||||
import org.springframework.data.elasticsearch.core.query.ByQueryResponse;
|
||||
import org.springframework.data.elasticsearch.core.query.Query;
|
||||
import org.springframework.data.elasticsearch.core.query.SearchTemplateQuery;
|
||||
import org.springframework.data.elasticsearch.core.query.UpdateQuery;
|
||||
import org.springframework.data.elasticsearch.core.query.*;
|
||||
import org.springframework.data.elasticsearch.core.query.UpdateResponse;
|
||||
import org.springframework.data.elasticsearch.core.reindex.ReindexRequest;
|
||||
import org.springframework.data.elasticsearch.core.reindex.ReindexResponse;
|
||||
@@ -78,12 +68,11 @@ import org.springframework.util.StringUtils;
|
||||
* Elasticsearch client.
|
||||
*
|
||||
* @author Peter-Josef Meisch
|
||||
* @author Illia Ulianov
|
||||
* @since 4.4
|
||||
*/
|
||||
public class ReactiveElasticsearchTemplate extends AbstractReactiveElasticsearchTemplate {
|
||||
|
||||
private static final Log LOGGER = LogFactory.getLog(ReactiveElasticsearchTemplate.class);
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(ReactiveElasticsearchTemplate.class);
|
||||
|
||||
private final ReactiveElasticsearchClient client;
|
||||
private final RequestConverter requestConverter;
|
||||
@@ -111,7 +100,7 @@ public class ReactiveElasticsearchTemplate extends AbstractReactiveElasticsearch
|
||||
getRefreshPolicy());
|
||||
return Mono.just(entity) //
|
||||
.zipWith(//
|
||||
Mono.from(execute(client -> client.index(indexRequest))) //
|
||||
Mono.from(execute((ClientCallback<Publisher<IndexResponse>>) client -> client.index(indexRequest))) //
|
||||
.map(indexResponse -> new IndexResponseMetaData(indexResponse.id(), //
|
||||
indexResponse.index(), //
|
||||
indexResponse.seqNo(), //
|
||||
@@ -141,16 +130,13 @@ public class ReactiveElasticsearchTemplate extends AbstractReactiveElasticsearch
|
||||
.flatMap(indexAndResponse -> {
|
||||
T savedEntity = entities.entityAt(indexAndResponse.getT1());
|
||||
BulkResponseItem response = indexAndResponse.getT2();
|
||||
var updatedEntity = entityOperations.updateIndexedObject(
|
||||
savedEntity, new IndexedObjectInformation( //
|
||||
response.id(), //
|
||||
response.index(), //
|
||||
response.seqNo(), //
|
||||
response.primaryTerm(), //
|
||||
response.version()),
|
||||
converter,
|
||||
routingResolver);
|
||||
return maybeCallbackAfterSave(updatedEntity, index);
|
||||
updateIndexedObject(savedEntity, new IndexedObjectInformation( //
|
||||
response.id(), //
|
||||
response.index(), //
|
||||
response.seqNo(), //
|
||||
response.primaryTerm(), //
|
||||
response.version()));
|
||||
return maybeCallbackAfterSave(savedEntity, index);
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -161,11 +147,11 @@ public class ReactiveElasticsearchTemplate extends AbstractReactiveElasticsearch
|
||||
Assert.notNull(id, "id must not be null");
|
||||
Assert.notNull(index, "index must not be null");
|
||||
|
||||
ExistsRequest existsRequest = requestConverter.documentExistsRequest(id, routingResolver.getRouting(), index);
|
||||
GetRequest getRequest = requestConverter.documentGetRequest(id, routingResolver.getRouting(), index, true);
|
||||
|
||||
return Mono.from(execute(
|
||||
((ClientCallback<Publisher<BooleanResponse>>) client -> client.exists(existsRequest))))
|
||||
.map(BooleanResponse::value) //
|
||||
((ClientCallback<Publisher<GetResponse<EntityAsMap>>>) client -> client.get(getRequest, EntityAsMap.class))))
|
||||
.map(GetResult::found) //
|
||||
.onErrorReturn(NoSuchIndexException.class, false);
|
||||
}
|
||||
|
||||
@@ -176,7 +162,9 @@ public class ReactiveElasticsearchTemplate extends AbstractReactiveElasticsearch
|
||||
|
||||
DeleteByQueryRequest request = requestConverter.documentDeleteByQueryRequest(query, routingResolver.getRouting(),
|
||||
entityType, index, getRefreshPolicy());
|
||||
return Mono.from(execute(client -> client.deleteByQuery(request))).map(responseConverter::byQueryResponse);
|
||||
return Mono
|
||||
.from(execute((ClientCallback<Publisher<DeleteByQueryResponse>>) client -> client.deleteByQuery(request)))
|
||||
.map(responseConverter::byQueryResponse);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -186,10 +174,10 @@ public class ReactiveElasticsearchTemplate extends AbstractReactiveElasticsearch
|
||||
Assert.notNull(entityType, "entityType must not be null");
|
||||
Assert.notNull(index, "index must not be null");
|
||||
|
||||
GetRequest getRequest = requestConverter.documentGetRequest(id, routingResolver.getRouting(), index);
|
||||
GetRequest getRequest = requestConverter.documentGetRequest(id, routingResolver.getRouting(), index, false);
|
||||
|
||||
Mono<GetResponse<EntityAsMap>> getResponse = Mono
|
||||
.from(execute(client -> client.get(getRequest, EntityAsMap.class)));
|
||||
Mono<GetResponse<EntityAsMap>> getResponse = Mono.from(execute(
|
||||
(ClientCallback<Publisher<GetResponse<EntityAsMap>>>) client -> client.get(getRequest, EntityAsMap.class)));
|
||||
|
||||
ReadDocumentCallback<T> callback = new ReadDocumentCallback<>(converter, entityType, index);
|
||||
return getResponse.flatMap(response -> callback.toEntity(DocumentAdapters.from(response)));
|
||||
@@ -204,7 +192,9 @@ public class ReactiveElasticsearchTemplate extends AbstractReactiveElasticsearch
|
||||
true);
|
||||
|
||||
return Mono.from(execute( //
|
||||
client -> client.reindex(reindexRequestES))).map(responseConverter::reindexResponse);
|
||||
(ClientCallback<Publisher<co.elastic.clients.elasticsearch.core.ReindexResponse>>) client -> client
|
||||
.reindex(reindexRequestES)))
|
||||
.map(responseConverter::reindexResponse);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -216,7 +206,8 @@ public class ReactiveElasticsearchTemplate extends AbstractReactiveElasticsearch
|
||||
false);
|
||||
|
||||
return Mono.from(execute( //
|
||||
client -> client.reindex(reindexRequestES)))
|
||||
(ClientCallback<Publisher<co.elastic.clients.elasticsearch.core.ReindexResponse>>) client -> client
|
||||
.reindex(reindexRequestES)))
|
||||
.flatMap(response -> (response.task() == null)
|
||||
? Mono.error(
|
||||
new UnsupportedBackendOperation("ElasticsearchClient did not return a task id on submit request"))
|
||||
@@ -232,10 +223,13 @@ public class ReactiveElasticsearchTemplate extends AbstractReactiveElasticsearch
|
||||
UpdateRequest<Document, ?> request = requestConverter.documentUpdateRequest(updateQuery, index, getRefreshPolicy(),
|
||||
routingResolver.getRouting());
|
||||
|
||||
return Mono.from(execute(client -> client.update(request, Document.class))).flatMap(response -> {
|
||||
UpdateResponse.Result result = result(response.result());
|
||||
return result == null ? Mono.empty() : Mono.just(UpdateResponse.of(result));
|
||||
});
|
||||
return Mono.from(execute(
|
||||
(ClientCallback<Publisher<co.elastic.clients.elasticsearch.core.UpdateResponse<Document>>>) client -> client
|
||||
.update(request, Document.class)))
|
||||
.flatMap(response -> {
|
||||
UpdateResponse.Result result = result(response.result());
|
||||
return result == null ? Mono.empty() : Mono.just(UpdateResponse.of(result));
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -266,12 +260,12 @@ public class ReactiveElasticsearchTemplate extends AbstractReactiveElasticsearch
|
||||
private Mono<BulkResponse> checkForBulkOperationFailure(BulkResponse bulkResponse) {
|
||||
|
||||
if (bulkResponse.errors()) {
|
||||
Map<String, BulkFailureException.FailureDetails> failedDocuments = new HashMap<>();
|
||||
Map<String, String> failedDocuments = new HashMap<>();
|
||||
|
||||
for (BulkResponseItem item : bulkResponse.items()) {
|
||||
|
||||
if (item.error() != null) {
|
||||
failedDocuments.put(item.id(), new BulkFailureException.FailureDetails(item.status(), item.error().reason()));
|
||||
failedDocuments.put(item.id(), item.error().reason());
|
||||
}
|
||||
}
|
||||
BulkFailureException exception = new BulkFailureException(
|
||||
@@ -298,7 +292,7 @@ public class ReactiveElasticsearchTemplate extends AbstractReactiveElasticsearch
|
||||
|
||||
private Mono<String> doDelete(DeleteRequest request) {
|
||||
|
||||
return Mono.from(execute(client -> client.delete(request))) //
|
||||
return Mono.from(execute((ClientCallback<Publisher<DeleteResponse>>) client -> client.delete(request))) //
|
||||
.flatMap(deleteResponse -> {
|
||||
if (deleteResponse.result() == Result.NotFound) {
|
||||
return Mono.empty();
|
||||
@@ -317,7 +311,8 @@ public class ReactiveElasticsearchTemplate extends AbstractReactiveElasticsearch
|
||||
|
||||
ReadDocumentCallback<T> callback = new ReadDocumentCallback<>(converter, clazz, index);
|
||||
|
||||
Publisher<MgetResponse<EntityAsMap>> response = execute(client -> client.mget(request, EntityAsMap.class));
|
||||
Publisher<MgetResponse<EntityAsMap>> response = execute(
|
||||
(ClientCallback<Publisher<MgetResponse<EntityAsMap>>>) client -> client.mget(request, EntityAsMap.class));
|
||||
|
||||
return Mono.from(response)//
|
||||
.flatMapMany(it -> Flux.fromIterable(DocumentAdapters.from(it))) //
|
||||
@@ -369,14 +364,14 @@ public class ReactiveElasticsearchTemplate extends AbstractReactiveElasticsearch
|
||||
|
||||
BiFunction<PitSearchAfter, Throwable, Publisher<?>> asyncError = (psa, ex) -> {
|
||||
if (LOGGER.isErrorEnabled()) {
|
||||
LOGGER.error("Error during pit/search_after", ex);
|
||||
LOGGER.error(String.format("Error during pit/search_after"), ex);
|
||||
}
|
||||
return cleanupPit(psa);
|
||||
};
|
||||
|
||||
Function<PitSearchAfter, Publisher<?>> asyncCancel = psa -> {
|
||||
if (LOGGER.isWarnEnabled()) {
|
||||
LOGGER.warn("pit/search_after was cancelled");
|
||||
LOGGER.warn(String.format("pit/search_after was cancelled"));
|
||||
}
|
||||
return cleanupPit(psa);
|
||||
};
|
||||
@@ -388,8 +383,8 @@ public class ReactiveElasticsearchTemplate extends AbstractReactiveElasticsearch
|
||||
SearchRequest firstSearchRequest = requestConverter.searchRequest(baseQuery, routingResolver.getRouting(),
|
||||
clazz, index, false, true);
|
||||
|
||||
return Mono.from(execute(client -> client.search(firstSearchRequest, EntityAsMap.class)))
|
||||
.expand(entityAsMapSearchResponse -> {
|
||||
return Mono.from(execute((ClientCallback<Publisher<ResponseBody<EntityAsMap>>>) client -> client
|
||||
.search(firstSearchRequest, EntityAsMap.class))).expand(entityAsMapSearchResponse -> {
|
||||
|
||||
var hits = entityAsMapSearchResponse.hits().hits();
|
||||
if (CollectionUtils.isEmpty(hits)) {
|
||||
@@ -401,7 +396,8 @@ public class ReactiveElasticsearchTemplate extends AbstractReactiveElasticsearch
|
||||
baseQuery.setSearchAfter(sortOptions);
|
||||
SearchRequest followSearchRequest = requestConverter.searchRequest(baseQuery,
|
||||
routingResolver.getRouting(), clazz, index, false, true);
|
||||
return Mono.from(execute(client -> client.search(followSearchRequest, EntityAsMap.class)));
|
||||
return Mono.from(execute((ClientCallback<Publisher<ResponseBody<EntityAsMap>>>) client -> client
|
||||
.search(followSearchRequest, EntityAsMap.class)));
|
||||
});
|
||||
|
||||
};
|
||||
@@ -458,7 +454,9 @@ public class ReactiveElasticsearchTemplate extends AbstractReactiveElasticsearch
|
||||
SearchRequest searchRequest = requestConverter.searchRequest(query, routingResolver.getRouting(), entityType, index,
|
||||
true);
|
||||
|
||||
return Mono.from(execute(client -> client.search(searchRequest, EntityAsMap.class)))
|
||||
return Mono
|
||||
.from(execute((ClientCallback<Publisher<ResponseBody<EntityAsMap>>>) client -> client.search(searchRequest,
|
||||
EntityAsMap.class)))
|
||||
.map(searchResponse -> searchResponse.hits().total() != null ? searchResponse.hits().total().value() : 0L);
|
||||
}
|
||||
|
||||
@@ -467,7 +465,9 @@ public class ReactiveElasticsearchTemplate extends AbstractReactiveElasticsearch
|
||||
SearchRequest searchRequest = requestConverter.searchRequest(query, routingResolver.getRouting(), clazz, index,
|
||||
false, false);
|
||||
|
||||
return Mono.from(execute(client -> client.search(searchRequest, EntityAsMap.class))) //
|
||||
return Mono
|
||||
.from(execute((ClientCallback<Publisher<ResponseBody<EntityAsMap>>>) client -> client.search(searchRequest,
|
||||
EntityAsMap.class))) //
|
||||
.flatMapIterable(entityAsMapSearchResponse -> entityAsMapSearchResponse.hits().hits()) //
|
||||
.map(entityAsMapHit -> DocumentAdapters.from(entityAsMapHit, jsonpMapper));
|
||||
}
|
||||
@@ -476,7 +476,9 @@ public class ReactiveElasticsearchTemplate extends AbstractReactiveElasticsearch
|
||||
|
||||
var request = requestConverter.searchTemplate(query, routingResolver.getRouting(), index);
|
||||
|
||||
return Mono.from(execute(client -> client.searchTemplate(request, EntityAsMap.class))) //
|
||||
return Mono
|
||||
.from(execute((ClientCallback<Publisher<SearchTemplateResponse<EntityAsMap>>>) client -> client
|
||||
.searchTemplate(request, EntityAsMap.class))) //
|
||||
.flatMapIterable(entityAsMapSearchResponse -> entityAsMapSearchResponse.hits().hits()) //
|
||||
.map(entityAsMapHit -> DocumentAdapters.from(entityAsMapHit, jsonpMapper));
|
||||
}
|
||||
@@ -495,7 +497,9 @@ public class ReactiveElasticsearchTemplate extends AbstractReactiveElasticsearch
|
||||
SearchDocumentResponse.EntityCreator<T> entityCreator = searchDocument -> callback.toEntity(searchDocument)
|
||||
.toFuture();
|
||||
|
||||
return Mono.from(execute(client -> client.search(searchRequest, EntityAsMap.class)))
|
||||
return Mono
|
||||
.from(execute((ClientCallback<Publisher<ResponseBody<EntityAsMap>>>) client -> client.search(searchRequest,
|
||||
EntityAsMap.class)))
|
||||
.map(searchResponse -> SearchDocumentResponseBuilder.from(searchResponse, entityCreator, jsonpMapper));
|
||||
}
|
||||
|
||||
@@ -516,7 +520,9 @@ public class ReactiveElasticsearchTemplate extends AbstractReactiveElasticsearch
|
||||
Assert.notNull(ignoreUnavailable, "ignoreUnavailable must not be null");
|
||||
|
||||
var request = requestConverter.searchOpenPointInTimeRequest(index, keepAlive, ignoreUnavailable);
|
||||
return Mono.from(execute(client -> client.openPointInTime(request))).map(OpenPointInTimeResponse::id);
|
||||
return Mono
|
||||
.from(execute((ClientCallback<Publisher<OpenPointInTimeResponse>>) client -> client.openPointInTime(request)))
|
||||
.map(OpenPointInTimeResponse::id);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -525,7 +531,9 @@ public class ReactiveElasticsearchTemplate extends AbstractReactiveElasticsearch
|
||||
Assert.notNull(pit, "pit must not be null");
|
||||
|
||||
ClosePointInTimeRequest request = requestConverter.searchClosePointInTime(pit);
|
||||
return Mono.from(execute(client -> client.closePointInTime(request))).map(ClosePointInTimeResponse::succeeded);
|
||||
return Mono
|
||||
.from(execute((ClientCallback<Publisher<ClosePointInTimeResponse>>) client -> client.closePointInTime(request)))
|
||||
.map(ClosePointInTimeResponse::succeeded);
|
||||
}
|
||||
|
||||
// endregion
|
||||
@@ -537,7 +545,8 @@ public class ReactiveElasticsearchTemplate extends AbstractReactiveElasticsearch
|
||||
Assert.notNull(script, "script must not be null");
|
||||
|
||||
var request = requestConverter.scriptPut(script);
|
||||
return Mono.from(execute(client -> client.putScript(request))).map(PutScriptResponse::acknowledged);
|
||||
return Mono.from(execute((ClientCallback<Publisher<PutScriptResponse>>) client -> client.putScript(request)))
|
||||
.map(PutScriptResponse::acknowledged);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -546,7 +555,8 @@ public class ReactiveElasticsearchTemplate extends AbstractReactiveElasticsearch
|
||||
Assert.notNull(name, "name must not be null");
|
||||
|
||||
var request = requestConverter.scriptGet(name);
|
||||
return Mono.from(execute(client -> client.getScript(request))).mapNotNull(responseConverter::scriptResponse);
|
||||
return Mono.from(execute((ClientCallback<Publisher<GetScriptResponse>>) client -> client.getScript(request)))
|
||||
.mapNotNull(responseConverter::scriptResponse);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -554,7 +564,8 @@ public class ReactiveElasticsearchTemplate extends AbstractReactiveElasticsearch
|
||||
Assert.notNull(name, "name must not be null");
|
||||
|
||||
var request = requestConverter.scriptDelete(name);
|
||||
return Mono.from(execute(client -> client.deleteScript(request))).map(DeleteScriptResponse::acknowledged);
|
||||
return Mono.from(execute((ClientCallback<Publisher<DeleteScriptResponse>>) client -> client.deleteScript(request)))
|
||||
.map(DeleteScriptResponse::acknowledged);
|
||||
}
|
||||
// endregion
|
||||
|
||||
@@ -577,6 +588,22 @@ public class ReactiveElasticsearchTemplate extends AbstractReactiveElasticsearch
|
||||
})).map(infoResponse -> infoResponse.version().number());
|
||||
}
|
||||
|
||||
@Override
|
||||
@Deprecated
|
||||
public <T> Publisher<T> execute(ReactiveElasticsearchOperations.ClientCallback<Publisher<T>> callback) {
|
||||
throw new UnsupportedBackendOperation("direct execution on the WebClient is not supported for this class");
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> Publisher<T> executeWithIndicesClient(IndicesClientCallback<Publisher<T>> callback) {
|
||||
throw new UnsupportedOperationException("not implemented");
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> Publisher<T> executeWithClusterClient(ClusterClientCallback<Publisher<T>> callback) {
|
||||
throw new UnsupportedOperationException("not implemented");
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReactiveIndexOperations indexOps(IndexCoordinates index) {
|
||||
return new ReactiveIndicesTemplate(client.indices(), getReactiveClusterTemplate(), converter, index);
|
||||
@@ -592,9 +619,9 @@ public class ReactiveElasticsearchTemplate extends AbstractReactiveElasticsearch
|
||||
return getReactiveClusterTemplate();
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 5.1
|
||||
*/
|
||||
/**
|
||||
* @since 5.1
|
||||
*/
|
||||
private ReactiveClusterTemplate getReactiveClusterTemplate() {
|
||||
return new ReactiveClusterTemplate(client.cluster(), converter);
|
||||
}
|
||||
@@ -606,16 +633,11 @@ public class ReactiveElasticsearchTemplate extends AbstractReactiveElasticsearch
|
||||
|
||||
@Override
|
||||
public Query idsQuery(List<String> ids) {
|
||||
return NativeQuery.builder().withQuery(qb -> qb.ids(iq -> iq.values(ids))).build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public BaseQueryBuilder queryBuilderWithIds(List<String> ids) {
|
||||
return NativeQuery.builder().withIds(ids);
|
||||
return NativeQuery.builder().withQuery(Queries.idsQueryAsQuery(ids)).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback interface to be used with {@link #execute(ReactiveElasticsearchTemplate.ClientCallback<>)} for operating
|
||||
* Callback interface to be used with {@link #execute(ReactiveElasticsearchOperations.ClientCallback)} for operating
|
||||
* directly on {@link ReactiveElasticsearchClient}.
|
||||
*
|
||||
* @param <T>
|
||||
|
||||
+130
-217
@@ -19,9 +19,7 @@ import static org.springframework.data.elasticsearch.client.elc.TypeUtils.*;
|
||||
import static org.springframework.util.CollectionUtils.*;
|
||||
|
||||
import co.elastic.clients.elasticsearch._types.Conflicts;
|
||||
import co.elastic.clients.elasticsearch._types.ExpandWildcard;
|
||||
import co.elastic.clients.elasticsearch._types.InlineScript;
|
||||
import co.elastic.clients.elasticsearch._types.NestedSortValue;
|
||||
import co.elastic.clients.elasticsearch._types.OpType;
|
||||
import co.elastic.clients.elasticsearch._types.SortOptions;
|
||||
import co.elastic.clients.elasticsearch._types.SortOrder;
|
||||
@@ -69,12 +67,10 @@ import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.springframework.dao.InvalidDataAccessApiUsageException;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.elasticsearch.core.RefreshPolicy;
|
||||
import org.springframework.data.elasticsearch.core.ScriptType;
|
||||
import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter;
|
||||
import org.springframework.data.elasticsearch.core.document.Document;
|
||||
import org.springframework.data.elasticsearch.core.index.*;
|
||||
@@ -110,8 +106,6 @@ import org.springframework.util.StringUtils;
|
||||
@SuppressWarnings("ClassCanBeRecord")
|
||||
class RequestConverter {
|
||||
|
||||
private static final Log LOGGER = LogFactory.getLog(RequestConverter.class);
|
||||
|
||||
// the default max result window size of Elasticsearch
|
||||
public static final Integer INDEX_MAX_RESULT_WINDOW = 10_000;
|
||||
|
||||
@@ -269,7 +263,36 @@ class RequestConverter {
|
||||
List<Action> actions = new ArrayList<>();
|
||||
aliasActions.getActions().forEach(aliasAction -> {
|
||||
|
||||
var actionBuilder = getBuilder(aliasAction);
|
||||
Action.Builder actionBuilder = new Action.Builder();
|
||||
|
||||
if (aliasAction instanceof AliasAction.Add add) {
|
||||
AliasActionParameters parameters = add.getParameters();
|
||||
actionBuilder.add(addActionBuilder -> {
|
||||
addActionBuilder //
|
||||
.indices(Arrays.asList(parameters.getIndices())) //
|
||||
.isHidden(parameters.getHidden()) //
|
||||
.isWriteIndex(parameters.getWriteIndex()) //
|
||||
.routing(parameters.getRouting()) //
|
||||
.indexRouting(parameters.getIndexRouting()) //
|
||||
.searchRouting(parameters.getSearchRouting()); //
|
||||
|
||||
if (parameters.getAliases() != null) {
|
||||
addActionBuilder.aliases(Arrays.asList(parameters.getAliases()));
|
||||
}
|
||||
|
||||
Query filterQuery = parameters.getFilterQuery();
|
||||
|
||||
if (filterQuery != null) {
|
||||
elasticsearchConverter.updateQuery(filterQuery, parameters.getFilterQueryClass());
|
||||
co.elastic.clients.elasticsearch._types.query_dsl.Query esQuery = getQuery(filterQuery, null);
|
||||
if (esQuery != null) {
|
||||
addActionBuilder.filter(esQuery);
|
||||
|
||||
}
|
||||
}
|
||||
return addActionBuilder;
|
||||
});
|
||||
}
|
||||
|
||||
if (aliasAction instanceof AliasAction.Remove remove) {
|
||||
AliasActionParameters parameters = remove.getParameters();
|
||||
@@ -298,49 +321,17 @@ class RequestConverter {
|
||||
return updateAliasRequestBuilder.build();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private Action.Builder getBuilder(AliasAction aliasAction) {
|
||||
Action.Builder actionBuilder = new Action.Builder();
|
||||
|
||||
if (aliasAction instanceof AliasAction.Add add) {
|
||||
AliasActionParameters parameters = add.getParameters();
|
||||
actionBuilder.add(addActionBuilder -> {
|
||||
addActionBuilder //
|
||||
.indices(Arrays.asList(parameters.getIndices())) //
|
||||
.isHidden(parameters.getHidden()) //
|
||||
.isWriteIndex(parameters.getWriteIndex()) //
|
||||
.routing(parameters.getRouting()) //
|
||||
.indexRouting(parameters.getIndexRouting()) //
|
||||
.searchRouting(parameters.getSearchRouting()); //
|
||||
|
||||
if (parameters.getAliases() != null) {
|
||||
addActionBuilder.aliases(Arrays.asList(parameters.getAliases()));
|
||||
}
|
||||
|
||||
Query filterQuery = parameters.getFilterQuery();
|
||||
|
||||
if (filterQuery != null) {
|
||||
elasticsearchConverter.updateQuery(filterQuery, parameters.getFilterQueryClass());
|
||||
co.elastic.clients.elasticsearch._types.query_dsl.Query esQuery = getQuery(filterQuery, null);
|
||||
if (esQuery != null) {
|
||||
addActionBuilder.filter(esQuery);
|
||||
}
|
||||
}
|
||||
return addActionBuilder;
|
||||
});
|
||||
}
|
||||
return actionBuilder;
|
||||
}
|
||||
|
||||
public PutMappingRequest indicesPutMappingRequest(IndexCoordinates indexCoordinates, Document mapping) {
|
||||
|
||||
Assert.notNull(indexCoordinates, "indexCoordinates must not be null");
|
||||
Assert.notNull(mapping, "mapping must not be null");
|
||||
|
||||
return new PutMappingRequest.Builder()
|
||||
.withJson(new StringReader(mapping.toJson()))
|
||||
.index(Arrays.asList(indexCoordinates.getIndexNames()))
|
||||
PutMappingRequest request = new PutMappingRequest.Builder() //
|
||||
.withJson(new StringReader(mapping.toJson())) //
|
||||
.index(Arrays.asList(indexCoordinates.getIndexNames())) //
|
||||
.build();
|
||||
|
||||
return request;
|
||||
}
|
||||
|
||||
public GetMappingRequest indicesGetMappingRequest(IndexCoordinates indexCoordinates) {
|
||||
@@ -420,7 +411,10 @@ class RequestConverter {
|
||||
|
||||
if (parametersAliases != null) {
|
||||
for (String aliasName : parametersAliases) {
|
||||
builder.aliases(aliasName, aliasBuilder -> buildAlias(parameters, aliasBuilder));
|
||||
builder.aliases(aliasName, aliasBuilder -> {
|
||||
|
||||
return buildAlias(parameters, aliasBuilder);
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -780,27 +774,25 @@ class RequestConverter {
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
public GetRequest documentGetRequest(String id, @Nullable String routing, IndexCoordinates indexCoordinates) {
|
||||
public GetRequest documentGetRequest(String id, @Nullable String routing, IndexCoordinates indexCoordinates,
|
||||
boolean forExistsRequest) {
|
||||
|
||||
Assert.notNull(id, "id must not be null");
|
||||
Assert.notNull(indexCoordinates, "indexCoordinates must not be null");
|
||||
|
||||
return GetRequest.of(grb -> grb //
|
||||
.index(indexCoordinates.getIndexName()) //
|
||||
.id(id) //
|
||||
.routing(routing));
|
||||
}
|
||||
return GetRequest.of(grb -> {
|
||||
grb //
|
||||
.index(indexCoordinates.getIndexName()) //
|
||||
.id(id) //
|
||||
.routing(routing);
|
||||
|
||||
public co.elastic.clients.elasticsearch.core.ExistsRequest documentExistsRequest(String id, @Nullable String routing,
|
||||
IndexCoordinates indexCoordinates) {
|
||||
if (forExistsRequest) {
|
||||
grb.source(scp -> scp.fetch(false));
|
||||
}
|
||||
|
||||
Assert.notNull(id, "id must not be null");
|
||||
Assert.notNull(indexCoordinates, "indexCoordinates must not be null");
|
||||
return grb;
|
||||
});
|
||||
|
||||
return co.elastic.clients.elasticsearch.core.ExistsRequest.of(erb -> erb
|
||||
.index(indexCoordinates.getIndexName())
|
||||
.id(id)
|
||||
.routing(routing));
|
||||
}
|
||||
|
||||
public <T> MgetRequest documentMgetRequest(Query query, Class<T> clazz, IndexCoordinates index) {
|
||||
@@ -857,10 +849,11 @@ class RequestConverter {
|
||||
StringBuilder sb = new StringBuilder(remote.getScheme());
|
||||
sb.append("://");
|
||||
sb.append(remote.getHost());
|
||||
sb.append(':');
|
||||
sb.append(":");
|
||||
sb.append(remote.getPort());
|
||||
|
||||
if (remote.getPathPrefix() != null) {
|
||||
sb.append("");
|
||||
sb.append(remote.getPathPrefix());
|
||||
}
|
||||
|
||||
@@ -880,7 +873,7 @@ class RequestConverter {
|
||||
}
|
||||
|
||||
SourceFilter sourceFilter = source.getSourceFilter();
|
||||
if (sourceFilter != null && sourceFilter.getIncludes() != null) {
|
||||
if (sourceFilter != null) {
|
||||
s.sourceFields(Arrays.asList(sourceFilter.getIncludes()));
|
||||
}
|
||||
return s;
|
||||
@@ -1034,8 +1027,8 @@ class RequestConverter {
|
||||
int val;
|
||||
try {
|
||||
val = Integer.parseInt(waitForActiveShards);
|
||||
} catch (NumberFormatException e) {
|
||||
throw new IllegalArgumentException("cannot parse ActiveShardCount[" + waitForActiveShards + ']', e);
|
||||
} catch (NumberFormatException var3) {
|
||||
throw new IllegalArgumentException("cannot parse ActiveShardCount[" + waitForActiveShards + "]", var3);
|
||||
}
|
||||
uqb.waitForActiveShards(wfa -> wfa.count(val));
|
||||
}
|
||||
@@ -1120,6 +1113,7 @@ class RequestConverter {
|
||||
IndexCoordinates indexCoordinates, boolean forCount, boolean forBatchedSearch,
|
||||
@Nullable Long scrollTimeInMillis) {
|
||||
|
||||
String[] indexNames = indexCoordinates.getIndexNames();
|
||||
Assert.notNull(query, "query must not be null");
|
||||
Assert.notNull(indexCoordinates, "indexCoordinates must not be null");
|
||||
|
||||
@@ -1157,12 +1151,9 @@ class RequestConverter {
|
||||
var query = param.query();
|
||||
mrb.searches(sb -> sb //
|
||||
.header(h -> {
|
||||
var searchType = (query instanceof NativeQuery nativeQuery && nativeQuery.getKnnQuery() != null) ? null
|
||||
: searchType(query.getSearchType());
|
||||
|
||||
h //
|
||||
.index(Arrays.asList(param.index().getIndexNames())) //
|
||||
.searchType(searchType) //
|
||||
.searchType(searchType(query.getSearchType())) //
|
||||
.requestCache(query.getRequestCache()) //
|
||||
;
|
||||
|
||||
@@ -1195,10 +1186,9 @@ class RequestConverter {
|
||||
}
|
||||
|
||||
if (!isEmpty(query.getFields())) {
|
||||
bb.fields(fb -> {
|
||||
query.getFields().forEach(fb::field);
|
||||
return fb;
|
||||
});
|
||||
var fieldAndFormats = query.getFields().stream()
|
||||
.map(field -> FieldAndFormat.of(b -> b.field(field))).toList();
|
||||
bb.fields(fieldAndFormats);
|
||||
}
|
||||
|
||||
if (!isEmpty(query.getStoredFields())) {
|
||||
@@ -1237,23 +1227,14 @@ class RequestConverter {
|
||||
Map<String, RuntimeField> runtimeMappings = new HashMap<>();
|
||||
query.getRuntimeFields().forEach(runtimeField -> {
|
||||
RuntimeField esRuntimeField = RuntimeField.of(rt -> {
|
||||
RuntimeField.Builder rfb = rt
|
||||
RuntimeField.Builder builder = rt
|
||||
.type(RuntimeFieldType._DESERIALIZER.parse(runtimeField.getType()));
|
||||
String script = runtimeField.getScript();
|
||||
|
||||
if (script != null) {
|
||||
rfb
|
||||
.script(s -> s
|
||||
.inline(is -> {
|
||||
is.source(script);
|
||||
|
||||
if (runtimeField.getParams() != null) {
|
||||
is.params(TypeUtils.paramsMap(runtimeField.getParams()));
|
||||
}
|
||||
return is;
|
||||
}));
|
||||
builder = builder.script(s -> s.inline(is -> is.source(script)));
|
||||
}
|
||||
return rfb;
|
||||
return builder;
|
||||
});
|
||||
runtimeMappings.put(runtimeField.getName(), esRuntimeField);
|
||||
});
|
||||
@@ -1262,15 +1243,15 @@ class RequestConverter {
|
||||
|
||||
if (!isEmpty(query.getIndicesBoost())) {
|
||||
bb.indicesBoost(query.getIndicesBoost().stream()
|
||||
.map(indexBoost -> Map.of(indexBoost.getIndexName(), (double) indexBoost.getBoost()))
|
||||
.map(indexBoost -> Map.of(indexBoost.getIndexName(), Double.valueOf(indexBoost.getBoost())))
|
||||
.collect(Collectors.toList()));
|
||||
}
|
||||
|
||||
query.getScriptedFields().forEach(scriptedField -> bb.scriptFields(scriptedField.getFieldName(),
|
||||
sf -> sf.script(getScript(scriptedField.getScriptData()))));
|
||||
|
||||
if (query instanceof NativeQuery nativeQuery) {
|
||||
prepareNativeSearch(nativeQuery, bb);
|
||||
if (query instanceof NativeQuery) {
|
||||
prepareNativeSearch((NativeQuery) query, bb);
|
||||
}
|
||||
return bb;
|
||||
} //
|
||||
@@ -1292,15 +1273,12 @@ class RequestConverter {
|
||||
|
||||
ElasticsearchPersistentEntity<?> persistentEntity = getPersistentEntity(clazz);
|
||||
|
||||
var searchType = (query instanceof NativeQuery nativeQuery && nativeQuery.getKnnQuery() != null) ? null
|
||||
: searchType(query.getSearchType());
|
||||
|
||||
builder //
|
||||
.version(true) //
|
||||
.trackScores(query.getTrackScores()) //
|
||||
.allowNoIndices(query.getAllowNoIndices()) //
|
||||
.source(getSourceConfig(query)) //
|
||||
.searchType(searchType) //
|
||||
.searchType(searchType(query.getSearchType())) //
|
||||
.timeout(timeStringMs(query.getTimeout())) //
|
||||
.requestCache(query.getRequestCache()) //
|
||||
;
|
||||
@@ -1342,7 +1320,8 @@ class RequestConverter {
|
||||
}
|
||||
|
||||
if (!isEmpty(query.getFields())) {
|
||||
var fieldAndFormats = query.getFields().stream().map(field -> FieldAndFormat.of(b -> b.field(field))).toList();
|
||||
var fieldAndFormats = query.getFields().stream()
|
||||
.map(field -> FieldAndFormat.of(b -> b.field(field))).toList();
|
||||
builder.fields(fieldAndFormats);
|
||||
}
|
||||
|
||||
@@ -1351,7 +1330,7 @@ class RequestConverter {
|
||||
}
|
||||
|
||||
if (query.getIndicesOptions() != null) {
|
||||
addIndicesOptions(builder, query.getIndicesOptions());
|
||||
// new Elasticsearch client does not support the old Indices options, need to be adapted
|
||||
}
|
||||
|
||||
if (query.isLimiting()) {
|
||||
@@ -1367,8 +1346,8 @@ class RequestConverter {
|
||||
query.getScriptedFields().forEach(scriptedField -> builder.scriptFields(scriptedField.getFieldName(),
|
||||
sf -> sf.script(getScript(scriptedField.getScriptData()))));
|
||||
|
||||
if (query instanceof NativeQuery nativeQuery) {
|
||||
prepareNativeSearch(nativeQuery, builder);
|
||||
if (query instanceof NativeQuery) {
|
||||
prepareNativeSearch((NativeQuery) query, builder);
|
||||
}
|
||||
// query.getSort() must be checked after prepareNativeSearch as this already might hav a sort set that must have
|
||||
// higher priority
|
||||
@@ -1402,23 +1381,14 @@ class RequestConverter {
|
||||
|
||||
Map<String, RuntimeField> runtimeMappings = new HashMap<>();
|
||||
query.getRuntimeFields()
|
||||
.forEach(runtimeField -> runtimeMappings.put(runtimeField.getName(), RuntimeField.of(rfb -> {
|
||||
rfb.type(RuntimeFieldType._DESERIALIZER.parse(runtimeField.getType()));
|
||||
.forEach(runtimeField -> runtimeMappings.put(runtimeField.getName(), RuntimeField.of(runtimeFieldBuilder -> {
|
||||
runtimeFieldBuilder.type(RuntimeFieldType._DESERIALIZER.parse(runtimeField.getType()));
|
||||
String script = runtimeField.getScript();
|
||||
|
||||
if (script != null) {
|
||||
rfb
|
||||
.script(s -> s
|
||||
.inline(is -> {
|
||||
is.source(script);
|
||||
|
||||
if (runtimeField.getParams() != null) {
|
||||
is.params(TypeUtils.paramsMap(runtimeField.getParams()));
|
||||
}
|
||||
return is;
|
||||
}));
|
||||
runtimeFieldBuilder.script(s -> s.inline(is -> is.source(script)));
|
||||
}
|
||||
|
||||
return rfb;
|
||||
return runtimeFieldBuilder;
|
||||
})));
|
||||
builder.runtimeMappings(runtimeMappings);
|
||||
}
|
||||
@@ -1430,15 +1400,13 @@ class RequestConverter {
|
||||
} else if (forBatchedSearch) {
|
||||
// request_cache is not allowed on scroll requests.
|
||||
builder.requestCache(null);
|
||||
// limit the number of documents in a batch if not already set in a pageable
|
||||
if (query.getPageable().isUnpaged()) {
|
||||
builder.size(query.getReactiveBatchSize());
|
||||
}
|
||||
// limit the number of documents in a batch
|
||||
builder.size(query.getReactiveBatchSize());
|
||||
}
|
||||
|
||||
if (!isEmpty(query.getIndicesBoost())) {
|
||||
builder.indicesBoost(query.getIndicesBoost().stream()
|
||||
.map(indexBoost -> Map.of(indexBoost.getIndexName(), (double) indexBoost.getBoost()))
|
||||
.map(indexBoost -> Map.of(indexBoost.getIndexName(), Double.valueOf(indexBoost.getBoost())))
|
||||
.collect(Collectors.toList()));
|
||||
}
|
||||
|
||||
@@ -1449,33 +1417,6 @@ class RequestConverter {
|
||||
}
|
||||
}
|
||||
|
||||
private void addIndicesOptions(SearchRequest.Builder builder, IndicesOptions indicesOptions) {
|
||||
|
||||
indicesOptions.getOptions().forEach(option -> {
|
||||
switch (option) {
|
||||
case ALLOW_NO_INDICES -> builder.allowNoIndices(true);
|
||||
case IGNORE_UNAVAILABLE -> builder.ignoreUnavailable(true);
|
||||
case IGNORE_THROTTLED -> builder.ignoreThrottled(true);
|
||||
// the following ones aren't supported by the builder
|
||||
case FORBID_ALIASES_TO_MULTIPLE_INDICES, FORBID_CLOSED_INDICES, IGNORE_ALIASES -> {
|
||||
if (LOGGER.isWarnEnabled()) {
|
||||
LOGGER
|
||||
.warn(String.format("indices option %s is not supported by the Elasticsearch client.", option.name()));
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
builder.expandWildcards(indicesOptions.getExpandWildcards().stream()
|
||||
.map(wildcardStates -> switch (wildcardStates) {
|
||||
case OPEN -> ExpandWildcard.Open;
|
||||
case CLOSED -> ExpandWildcard.Closed;
|
||||
case HIDDEN -> ExpandWildcard.Hidden;
|
||||
case ALL -> ExpandWildcard.All;
|
||||
case NONE -> ExpandWildcard.None;
|
||||
}).collect(Collectors.toList()));
|
||||
}
|
||||
|
||||
private Rescore getRescore(RescorerQuery rescorerQuery) {
|
||||
|
||||
return Rescore.of(r -> r //
|
||||
@@ -1518,88 +1459,59 @@ class RequestConverter {
|
||||
private SortOptions getSortOptions(Sort.Order order, @Nullable ElasticsearchPersistentEntity<?> persistentEntity) {
|
||||
SortOrder sortOrder = order.getDirection().isDescending() ? SortOrder.Desc : SortOrder.Asc;
|
||||
|
||||
Order.Mode mode = order.getDirection().isAscending() ? Order.Mode.min : Order.Mode.max;
|
||||
Order.Mode mode = Order.DEFAULT_MODE;
|
||||
String unmappedType = null;
|
||||
String missing = null;
|
||||
NestedSortValue nestedSortValue = null;
|
||||
|
||||
if (order instanceof Order o) {
|
||||
mode = o.getMode();
|
||||
unmappedType = o.getUnmappedType();
|
||||
}
|
||||
|
||||
if (SortOptions.Kind.Score.jsonValue().equals(order.getProperty())) {
|
||||
return SortOptions.of(so -> so.score(s -> s.order(sortOrder)));
|
||||
}
|
||||
} else {
|
||||
ElasticsearchPersistentProperty property = (persistentEntity != null) //
|
||||
? persistentEntity.getPersistentProperty(order.getProperty()) //
|
||||
: null;
|
||||
String fieldName = property != null ? property.getFieldName() : order.getProperty();
|
||||
|
||||
if (order instanceof Order o) {
|
||||
Order.Mode finalMode = mode;
|
||||
if (order instanceof GeoDistanceOrder geoDistanceOrder) {
|
||||
|
||||
if (o.getMode() != null) {
|
||||
mode = o.getMode();
|
||||
}
|
||||
unmappedType = o.getUnmappedType();
|
||||
missing = o.getMissing();
|
||||
nestedSortValue = getNestedSort(o.getNested(), persistentEntity);
|
||||
}
|
||||
Order.Mode finalMode = mode;
|
||||
String finalUnmappedType = unmappedType;
|
||||
var finalNestedSortValue = nestedSortValue;
|
||||
|
||||
ElasticsearchPersistentProperty property = (persistentEntity != null) //
|
||||
? persistentEntity.getPersistentProperty(order.getProperty()) //
|
||||
: null;
|
||||
String fieldName = property != null ? property.getFieldName() : order.getProperty();
|
||||
|
||||
if (order instanceof GeoDistanceOrder geoDistanceOrder) {
|
||||
return getSortOptions(geoDistanceOrder, fieldName, finalMode);
|
||||
}
|
||||
|
||||
var finalMissing = missing != null ? missing
|
||||
: (order.getNullHandling() == Sort.NullHandling.NULLS_FIRST) ? "_first"
|
||||
return SortOptions.of(so -> so //
|
||||
.geoDistance(gd -> gd //
|
||||
.field(fieldName) //
|
||||
.location(loc -> loc.latlon(Queries.latLon(geoDistanceOrder.getGeoPoint()))) //
|
||||
.distanceType(geoDistanceType(geoDistanceOrder.getDistanceType())).mode(sortMode(finalMode)) //
|
||||
.order(sortOrder(geoDistanceOrder.getDirection())) //
|
||||
.unit(distanceUnit(geoDistanceOrder.getUnit())) //
|
||||
.ignoreUnmapped(geoDistanceOrder.getIgnoreUnmapped())));
|
||||
} else {
|
||||
String missing = (order.getNullHandling() == Sort.NullHandling.NULLS_FIRST) ? "_first"
|
||||
: ((order.getNullHandling() == Sort.NullHandling.NULLS_LAST) ? "_last" : null);
|
||||
String finalUnmappedType = unmappedType;
|
||||
return SortOptions.of(so -> so //
|
||||
.field(f -> {
|
||||
f.field(fieldName) //
|
||||
.order(sortOrder) //
|
||||
.mode(sortMode(finalMode));
|
||||
|
||||
return SortOptions.of(so -> so //
|
||||
.field(f -> {
|
||||
f.field(fieldName) //
|
||||
.order(sortOrder) //
|
||||
.mode(sortMode(finalMode));
|
||||
if (finalUnmappedType != null) {
|
||||
FieldType fieldType = fieldType(finalUnmappedType);
|
||||
|
||||
if (finalUnmappedType != null) {
|
||||
FieldType fieldType = fieldType(finalUnmappedType);
|
||||
if (fieldType != null) {
|
||||
f.unmappedType(fieldType);
|
||||
}
|
||||
}
|
||||
|
||||
if (fieldType != null) {
|
||||
f.unmappedType(fieldType);
|
||||
}
|
||||
}
|
||||
|
||||
if (finalMissing != null) {
|
||||
f.missing(fv -> fv //
|
||||
.stringValue(finalMissing));
|
||||
}
|
||||
|
||||
if (finalNestedSortValue != null) {
|
||||
f.nested(finalNestedSortValue);
|
||||
}
|
||||
|
||||
return f;
|
||||
}));
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private NestedSortValue getNestedSort(@Nullable Order.Nested nested,
|
||||
@Nullable ElasticsearchPersistentEntity<?> persistentEntity) {
|
||||
return (nested == null || persistentEntity == null) ? null
|
||||
: NestedSortValue.of(b -> b //
|
||||
.path(elasticsearchConverter.updateFieldNames(nested.getPath(), persistentEntity)) //
|
||||
.maxChildren(nested.getMaxChildren()) //
|
||||
.nested(getNestedSort(nested.getNested(), persistentEntity)) //
|
||||
.filter(getQuery(nested.getFilter(), persistentEntity.getType())));
|
||||
}
|
||||
|
||||
private static SortOptions getSortOptions(GeoDistanceOrder geoDistanceOrder, String fieldName, Order.Mode finalMode) {
|
||||
return SortOptions.of(so -> so //
|
||||
.geoDistance(gd -> gd //
|
||||
.field(fieldName) //
|
||||
.location(loc -> loc.latlon(Queries.latLon(geoDistanceOrder.getGeoPoint()))) //
|
||||
.distanceType(geoDistanceType(geoDistanceOrder.getDistanceType())).mode(sortMode(finalMode)) //
|
||||
.order(sortOrder(geoDistanceOrder.getDirection())) //
|
||||
.unit(distanceUnit(geoDistanceOrder.getUnit())) //
|
||||
.ignoreUnmapped(geoDistanceOrder.getIgnoreUnmapped())));
|
||||
if (missing != null) {
|
||||
f.missing(fv -> fv //
|
||||
.stringValue(missing));
|
||||
}
|
||||
return f;
|
||||
}));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("DuplicatedCode")
|
||||
@@ -1679,8 +1591,7 @@ class RequestConverter {
|
||||
|
||||
if (query instanceof CriteriaQuery) {
|
||||
CriteriaFilterProcessor.createQuery(((CriteriaQuery) query).getCriteria()).ifPresent(builder::postFilter);
|
||||
} else // noinspection StatementWithEmptyBody
|
||||
if (query instanceof StringQuery) {
|
||||
} else if (query instanceof StringQuery) {
|
||||
// no filter for StringQuery
|
||||
} else if (query instanceof NativeQuery) {
|
||||
builder.postFilter(((NativeQuery) query).getFilter());
|
||||
@@ -1695,7 +1606,7 @@ class RequestConverter {
|
||||
Assert.notNull(query, "query must not be null");
|
||||
Assert.notNull(index, "index must not be null");
|
||||
|
||||
return co.elastic.clients.elasticsearch._types.query_dsl.MoreLikeThisQuery
|
||||
co.elastic.clients.elasticsearch._types.query_dsl.MoreLikeThisQuery moreLikeThisQuery = co.elastic.clients.elasticsearch._types.query_dsl.MoreLikeThisQuery
|
||||
.of(q -> {
|
||||
q.like(Like.of(l -> l.document(ld -> ld.index(index.getIndexName()).id(query.getId()))))
|
||||
.fields(query.getFields());
|
||||
@@ -1734,6 +1645,8 @@ class RequestConverter {
|
||||
|
||||
return q;
|
||||
});
|
||||
|
||||
return moreLikeThisQuery;
|
||||
}
|
||||
|
||||
public OpenPointInTimeRequest searchOpenPointInTimeRequest(IndexCoordinates index, Duration keepAlive,
|
||||
@@ -1779,7 +1692,7 @@ class RequestConverter {
|
||||
}
|
||||
|
||||
var expandWildcards = query.getExpandWildcards();
|
||||
if (expandWildcards != null && !expandWildcards.isEmpty()) {
|
||||
if (!expandWildcards.isEmpty()) {
|
||||
builder.expandWildcards(expandWildcards(expandWildcards));
|
||||
}
|
||||
|
||||
|
||||
+4
-4
@@ -40,8 +40,8 @@ import java.util.*;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.data.elasticsearch.ElasticsearchErrorCause;
|
||||
import org.springframework.data.elasticsearch.core.IndexInformation;
|
||||
import org.springframework.data.elasticsearch.core.MultiGetItem;
|
||||
@@ -65,7 +65,7 @@ import org.springframework.util.Assert;
|
||||
*/
|
||||
class ResponseConverter {
|
||||
|
||||
private static final Log LOGGER = LogFactory.getLog(ResponseConverter.class);
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(ResponseConverter.class);
|
||||
|
||||
private final JsonpMapper jsonpMapper;
|
||||
|
||||
@@ -193,7 +193,7 @@ class ResponseConverter {
|
||||
if (indexMappingRecord == null) {
|
||||
|
||||
if (mappings.size() != 1) {
|
||||
LOGGER.warn(String.format("no mapping returned for index %s", indexCoordinates.getIndexName()));
|
||||
LOGGER.warn("no mapping returned for index {}", indexCoordinates.getIndexName());
|
||||
return Document.create();
|
||||
}
|
||||
String index = mappings.keySet().iterator().next();
|
||||
|
||||
@@ -31,7 +31,6 @@ import co.elastic.clients.json.JsonData;
|
||||
import java.io.StringReader;
|
||||
import java.time.Duration;
|
||||
import java.util.EnumSet;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
@@ -48,7 +47,6 @@ import org.springframework.data.elasticsearch.core.query.RescorerQuery;
|
||||
import org.springframework.data.elasticsearch.core.query.UpdateResponse;
|
||||
import org.springframework.data.elasticsearch.core.reindex.ReindexRequest;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Utility to handle new Elasticsearch client type values.
|
||||
@@ -486,18 +484,4 @@ final class TypeUtils {
|
||||
return settings != null ? IndexSettings.of(b -> b.withJson(new StringReader(Document.from(settings).toJson())))
|
||||
: null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 5.2
|
||||
*/
|
||||
static Map<String, JsonData> paramsMap(Map<String, Object> params) {
|
||||
|
||||
Assert.notNull(params, "params must not be null");
|
||||
|
||||
Map<String, JsonData> mappedParams = new LinkedHashMap<>();
|
||||
params.forEach((key, value) -> {
|
||||
mappedParams.put(key, JsonData.of(value));
|
||||
});
|
||||
return mappedParams;
|
||||
}
|
||||
}
|
||||
|
||||
+6
-23
@@ -15,10 +15,8 @@
|
||||
*/
|
||||
package org.springframework.data.elasticsearch.client.elc.aot;
|
||||
|
||||
import co.elastic.clients.elasticsearch._types.mapping.RuntimeFieldType;
|
||||
import co.elastic.clients.elasticsearch._types.mapping.TypeMapping;
|
||||
import co.elastic.clients.elasticsearch.indices.IndexSettings;
|
||||
import co.elastic.clients.elasticsearch.indices.PutMappingRequest;
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.springframework.aot.hint.MemberCategory;
|
||||
import org.springframework.aot.hint.RuntimeHints;
|
||||
import org.springframework.aot.hint.RuntimeHintsRegistrar;
|
||||
@@ -26,8 +24,6 @@ import org.springframework.aot.hint.TypeReference;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
/**
|
||||
* runtime hints for the Elasticsearch client libraries, as these do not provide any of their own.
|
||||
*
|
||||
* @author Peter-Josef Meisch
|
||||
* @since 5.1
|
||||
*/
|
||||
@@ -35,22 +31,9 @@ public class ElasticsearchClientRuntimeHints implements RuntimeHintsRegistrar {
|
||||
|
||||
@Override
|
||||
public void registerHints(RuntimeHints hints, @Nullable ClassLoader classLoader) {
|
||||
|
||||
hints.reflection()
|
||||
.registerType(TypeReference.of(IndexSettings.class), builder -> builder.withField("_DESERIALIZER")) //
|
||||
.registerType(TypeReference.of(PutMappingRequest.class), builder -> builder.withField("_DESERIALIZER")) //
|
||||
.registerType(TypeReference.of(RuntimeFieldType.class), builder -> builder.withField("_DESERIALIZER"))//
|
||||
.registerType(TypeReference.of(TypeMapping.class), builder -> builder.withField("_DESERIALIZER")) //
|
||||
;
|
||||
|
||||
hints.serialization() //
|
||||
.registerType(org.apache.http.impl.auth.BasicScheme.class) //
|
||||
.registerType(org.apache.http.impl.auth.RFC2617Scheme.class) //
|
||||
.registerType(java.util.HashMap.class) //
|
||||
;
|
||||
|
||||
hints.resources() //
|
||||
.registerPattern("co/elastic/clients/version.properties") //
|
||||
;
|
||||
// needed for the http client used by the Elasticsearch client
|
||||
hints.serialization().registerType(org.apache.http.impl.auth.BasicScheme.class);
|
||||
hints.serialization().registerType(org.apache.http.impl.auth.RFC2617Scheme.class);
|
||||
hints.serialization().registerType(java.util.HashMap.class);
|
||||
}
|
||||
}
|
||||
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright 2018-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.elasticsearch.client.erhlc;
|
||||
|
||||
import org.elasticsearch.client.RestHighLevelClient;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.data.elasticsearch.config.ElasticsearchConfigurationSupport;
|
||||
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
|
||||
import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter;
|
||||
|
||||
/**
|
||||
* @author Christoph Strobl
|
||||
* @author Peter-Josef Meisch
|
||||
* @since 3.2
|
||||
* @see ElasticsearchConfigurationSupport
|
||||
* @deprecated since 5.0
|
||||
*/
|
||||
@Deprecated
|
||||
public abstract class AbstractElasticsearchConfiguration extends ElasticsearchConfigurationSupport {
|
||||
|
||||
/**
|
||||
* Return the {@link RestHighLevelClient} instance used to connect to the cluster. <br />
|
||||
*
|
||||
* @return never {@literal null}.
|
||||
*/
|
||||
@Bean
|
||||
public abstract RestHighLevelClient elasticsearchClient();
|
||||
|
||||
/**
|
||||
* Creates {@link ElasticsearchOperations}.
|
||||
*
|
||||
* @return never {@literal null}.
|
||||
*/
|
||||
@Bean(name = { "elasticsearchOperations", "elasticsearchTemplate" })
|
||||
public ElasticsearchOperations elasticsearchOperations(ElasticsearchConverter elasticsearchConverter,
|
||||
RestHighLevelClient elasticsearchClient) {
|
||||
|
||||
ElasticsearchRestTemplate template = new ElasticsearchRestTemplate(elasticsearchClient, elasticsearchConverter);
|
||||
template.setRefreshPolicy(refreshPolicy());
|
||||
|
||||
return template;
|
||||
}
|
||||
}
|
||||
+81
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* Copyright 2018-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.elasticsearch.client.erhlc;
|
||||
|
||||
import org.elasticsearch.action.support.IndicesOptions;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.data.elasticsearch.config.ElasticsearchConfigurationSupport;
|
||||
import org.springframework.data.elasticsearch.core.ReactiveElasticsearchOperations;
|
||||
import org.springframework.data.elasticsearch.core.RefreshPolicy;
|
||||
import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
/**
|
||||
* @author Christoph Strobl
|
||||
* @author Peter-Josef Meisch
|
||||
* @since 3.2
|
||||
* @see ElasticsearchConfigurationSupport
|
||||
* @deprecated since 5.0
|
||||
*/
|
||||
@Deprecated
|
||||
public abstract class AbstractReactiveElasticsearchConfiguration extends ElasticsearchConfigurationSupport {
|
||||
|
||||
/**
|
||||
* Return the {@link ReactiveElasticsearchClient} instance used to connect to the cluster. <br />
|
||||
*
|
||||
* @return never {@literal null}.
|
||||
*/
|
||||
@Bean
|
||||
public abstract ReactiveElasticsearchClient reactiveElasticsearchClient();
|
||||
|
||||
/**
|
||||
* Creates {@link ReactiveElasticsearchOperations}.
|
||||
*
|
||||
* @return never {@literal null}.
|
||||
*/
|
||||
@Bean
|
||||
public ReactiveElasticsearchOperations reactiveElasticsearchTemplate(ElasticsearchConverter elasticsearchConverter,
|
||||
ReactiveElasticsearchClient reactiveElasticsearchClient) {
|
||||
|
||||
ReactiveElasticsearchTemplate template = new ReactiveElasticsearchTemplate(reactiveElasticsearchClient,
|
||||
elasticsearchConverter);
|
||||
template.setIndicesOptions(indicesOptions());
|
||||
template.setRefreshPolicy(refreshPolicy());
|
||||
|
||||
return template;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set up the write {@link RefreshPolicy}. Default is set to null to use the cluster defaults..
|
||||
*
|
||||
* @return {@literal null} to use the server defaults.
|
||||
*/
|
||||
@Nullable
|
||||
protected RefreshPolicy refreshPolicy() {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set up the read {@link IndicesOptions}. Default is set to {@link IndicesOptions#strictExpandOpenAndForbidClosed()}.
|
||||
*
|
||||
* @return {@literal null} to use the server defaults.
|
||||
*/
|
||||
@Nullable
|
||||
protected IndicesOptions indicesOptions() {
|
||||
return IndicesOptions.strictExpandOpenAndForbidClosed();
|
||||
}
|
||||
|
||||
}
|
||||
+273
@@ -0,0 +1,273 @@
|
||||
/*
|
||||
* Copyright 2013-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.elasticsearch.client.erhlc;
|
||||
|
||||
import static org.springframework.data.elasticsearch.core.query.Criteria.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.elasticsearch.common.geo.GeoDistance;
|
||||
import org.elasticsearch.index.query.BoolQueryBuilder;
|
||||
import org.elasticsearch.index.query.GeoBoundingBoxQueryBuilder;
|
||||
import org.elasticsearch.index.query.GeoDistanceQueryBuilder;
|
||||
import org.elasticsearch.index.query.QueryBuilder;
|
||||
import org.elasticsearch.index.query.QueryBuilders;
|
||||
import org.springframework.data.elasticsearch.core.geo.GeoBox;
|
||||
import org.springframework.data.elasticsearch.core.geo.GeoJson;
|
||||
import org.springframework.data.elasticsearch.core.geo.GeoPoint;
|
||||
import org.springframework.data.elasticsearch.core.query.Criteria;
|
||||
import org.springframework.data.geo.Box;
|
||||
import org.springframework.data.geo.Distance;
|
||||
import org.springframework.data.geo.Metrics;
|
||||
import org.springframework.data.geo.Point;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* CriteriaFilterProcessor
|
||||
*
|
||||
* @author Franck Marchand
|
||||
* @author Mohsin Husen
|
||||
* @author Artur Konczak
|
||||
* @author Peter-Josef Meisch
|
||||
* @deprecated since 5.0
|
||||
*/
|
||||
@Deprecated
|
||||
class CriteriaFilterProcessor {
|
||||
|
||||
@Nullable
|
||||
QueryBuilder createFilter(Criteria criteria) {
|
||||
|
||||
List<QueryBuilder> filterBuilders = new ArrayList<>();
|
||||
|
||||
for (Criteria chainedCriteria : criteria.getCriteriaChain()) {
|
||||
|
||||
if (chainedCriteria.isOr()) {
|
||||
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
|
||||
queriesForEntries(chainedCriteria).forEach(boolQuery::should);
|
||||
filterBuilders.add(boolQuery);
|
||||
} else if (chainedCriteria.isNegating()) {
|
||||
List<QueryBuilder> negationFilters = buildNegationFilter(criteria.getField().getName(),
|
||||
criteria.getFilterCriteriaEntries().iterator());
|
||||
|
||||
filterBuilders.addAll(negationFilters);
|
||||
} else {
|
||||
filterBuilders.addAll(queriesForEntries(chainedCriteria));
|
||||
}
|
||||
}
|
||||
|
||||
QueryBuilder filter = null;
|
||||
|
||||
if (!filterBuilders.isEmpty()) {
|
||||
|
||||
if (filterBuilders.size() == 1) {
|
||||
filter = filterBuilders.get(0);
|
||||
} else {
|
||||
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
|
||||
filterBuilders.forEach(boolQuery::must);
|
||||
filter = boolQuery;
|
||||
}
|
||||
}
|
||||
|
||||
return filter;
|
||||
}
|
||||
|
||||
private List<QueryBuilder> queriesForEntries(Criteria criteria) {
|
||||
|
||||
Assert.notNull(criteria.getField(), "criteria must have a field");
|
||||
String fieldName = criteria.getField().getName();
|
||||
Assert.notNull(fieldName, "Unknown field");
|
||||
|
||||
return criteria.getFilterCriteriaEntries().stream()
|
||||
.map(entry -> queryFor(entry.getKey(), entry.getValue(), fieldName)).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private QueryBuilder queryFor(OperationKey key, Object value, String fieldName) {
|
||||
|
||||
QueryBuilder filter = null;
|
||||
|
||||
switch (key) {
|
||||
case WITHIN -> {
|
||||
Assert.isTrue(value instanceof Object[], "Value of a geo distance filter should be an array of two values.");
|
||||
filter = withinQuery(fieldName, (Object[]) value);
|
||||
}
|
||||
case BBOX -> {
|
||||
Assert.isTrue(value instanceof Object[],
|
||||
"Value of a boundedBy filter should be an array of one or two values.");
|
||||
filter = boundingBoxQuery(fieldName, (Object[]) value);
|
||||
}
|
||||
case GEO_INTERSECTS -> {
|
||||
Assert.isTrue(value instanceof GeoJson<?>, "value of a GEO_INTERSECTS filter must be a GeoJson object");
|
||||
filter = geoJsonQuery(fieldName, (GeoJson<?>) value, "intersects");
|
||||
}
|
||||
case GEO_IS_DISJOINT -> {
|
||||
Assert.isTrue(value instanceof GeoJson<?>, "value of a GEO_IS_DISJOINT filter must be a GeoJson object");
|
||||
filter = geoJsonQuery(fieldName, (GeoJson<?>) value, "disjoint");
|
||||
}
|
||||
case GEO_WITHIN -> {
|
||||
Assert.isTrue(value instanceof GeoJson<?>, "value of a GEO_WITHIN filter must be a GeoJson object");
|
||||
filter = geoJsonQuery(fieldName, (GeoJson<?>) value, "within");
|
||||
}
|
||||
case GEO_CONTAINS -> {
|
||||
Assert.isTrue(value instanceof GeoJson<?>, "value of a GEO_CONTAINS filter must be a GeoJson object");
|
||||
filter = geoJsonQuery(fieldName, (GeoJson<?>) value, "contains");
|
||||
}
|
||||
}
|
||||
|
||||
return filter;
|
||||
}
|
||||
|
||||
private QueryBuilder withinQuery(String fieldName, Object... valArray) {
|
||||
|
||||
GeoDistanceQueryBuilder filter = QueryBuilders.geoDistanceQuery(fieldName);
|
||||
|
||||
Assert.noNullElements(valArray, "Geo distance filter takes 2 not null elements array as parameter.");
|
||||
Assert.isTrue(valArray.length == 2, "Geo distance filter takes a 2-elements array as parameter.");
|
||||
Assert.isTrue(valArray[0] instanceof GeoPoint || valArray[0] instanceof String || valArray[0] instanceof Point,
|
||||
"First element of a geo distance filter must be a GeoPoint, a Point or a text");
|
||||
Assert.isTrue(valArray[1] instanceof String || valArray[1] instanceof Distance,
|
||||
"Second element of a geo distance filter must be a text or a Distance");
|
||||
|
||||
StringBuilder dist = new StringBuilder();
|
||||
|
||||
if (valArray[1] instanceof Distance) {
|
||||
extractDistanceString((Distance) valArray[1], dist);
|
||||
} else {
|
||||
dist.append((String) valArray[1]);
|
||||
}
|
||||
|
||||
if (valArray[0]instanceof GeoPoint loc) {
|
||||
filter.point(loc.getLat(), loc.getLon()).distance(dist.toString()).geoDistance(GeoDistance.PLANE);
|
||||
} else if (valArray[0] instanceof Point) {
|
||||
GeoPoint loc = GeoPoint.fromPoint((Point) valArray[0]);
|
||||
filter.point(loc.getLat(), loc.getLon()).distance(dist.toString()).geoDistance(GeoDistance.PLANE);
|
||||
} else {
|
||||
String loc = (String) valArray[0];
|
||||
if (loc.contains(",")) {
|
||||
String[] c = loc.split(",");
|
||||
filter.point(Double.parseDouble(c[0]), Double.parseDouble(c[1])).distance(dist.toString())
|
||||
.geoDistance(GeoDistance.PLANE);
|
||||
} else {
|
||||
filter.geohash(loc).distance(dist.toString()).geoDistance(GeoDistance.PLANE);
|
||||
}
|
||||
}
|
||||
|
||||
return filter;
|
||||
}
|
||||
|
||||
private QueryBuilder boundingBoxQuery(String fieldName, Object... valArray) {
|
||||
|
||||
Assert.noNullElements(valArray, "Geo boundedBy filter takes a not null element array as parameter.");
|
||||
|
||||
GeoBoundingBoxQueryBuilder filter = QueryBuilders.geoBoundingBoxQuery(fieldName);
|
||||
|
||||
if (valArray.length == 1) {
|
||||
// GeoEnvelop
|
||||
oneParameterBBox(filter, valArray[0]);
|
||||
} else if (valArray.length == 2) {
|
||||
// 2x GeoPoint
|
||||
// 2x text
|
||||
twoParameterBBox(filter, valArray);
|
||||
} else {
|
||||
throw new IllegalArgumentException(
|
||||
"Geo distance filter takes a 1-elements array(GeoBox) or 2-elements array(GeoPoints or Strings(format lat,lon or geohash)).");
|
||||
}
|
||||
|
||||
return filter;
|
||||
}
|
||||
|
||||
private QueryBuilder geoJsonQuery(String fieldName, GeoJson<?> geoJson, String relation) {
|
||||
return QueryBuilders.wrapperQuery(buildJsonQuery(fieldName, geoJson, relation));
|
||||
}
|
||||
|
||||
private String buildJsonQuery(String fieldName, GeoJson<?> geoJson, String relation) {
|
||||
return "{\"geo_shape\": {\"" + fieldName + "\": {\"shape\": " + geoJson.toJson() + ", \"relation\": \"" + relation
|
||||
+ "\"}}}";
|
||||
}
|
||||
|
||||
/**
|
||||
* extract the distance string from a {@link org.springframework.data.geo.Distance} object.
|
||||
*
|
||||
* @param distance distance object to extract string from
|
||||
* @param sb StringBuilder to build the distance string
|
||||
*/
|
||||
private void extractDistanceString(Distance distance, StringBuilder sb) {
|
||||
// handle Distance object
|
||||
sb.append((int) distance.getValue());
|
||||
|
||||
Metrics metric = (Metrics) distance.getMetric();
|
||||
|
||||
switch (metric) {
|
||||
case KILOMETERS -> sb.append("km");
|
||||
case MILES -> sb.append("mi");
|
||||
}
|
||||
}
|
||||
|
||||
private void oneParameterBBox(GeoBoundingBoxQueryBuilder filter, Object value) {
|
||||
Assert.isTrue(value instanceof GeoBox || value instanceof Box,
|
||||
"single-element of boundedBy filter must be type of GeoBox or Box");
|
||||
|
||||
GeoBox geoBBox;
|
||||
if (value instanceof Box) {
|
||||
geoBBox = GeoBox.fromBox((Box) value);
|
||||
} else {
|
||||
geoBBox = (GeoBox) value;
|
||||
}
|
||||
|
||||
filter.setCorners(geoBBox.getTopLeft().getLat(), geoBBox.getTopLeft().getLon(), geoBBox.getBottomRight().getLat(),
|
||||
geoBBox.getBottomRight().getLon());
|
||||
}
|
||||
|
||||
private static boolean isType(Object[] array, Class<?> clazz) {
|
||||
for (Object o : array) {
|
||||
if (!clazz.isInstance(o)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private void twoParameterBBox(GeoBoundingBoxQueryBuilder filter, Object... values) {
|
||||
Assert.isTrue(isType(values, GeoPoint.class) || isType(values, String.class),
|
||||
" both elements of boundedBy filter must be type of GeoPoint or text(format lat,lon or geohash)");
|
||||
if (values[0]instanceof GeoPoint topLeft) {
|
||||
GeoPoint bottomRight = (GeoPoint) values[1];
|
||||
filter.setCorners(topLeft.getLat(), topLeft.getLon(), bottomRight.getLat(), bottomRight.getLon());
|
||||
} else {
|
||||
String topLeft = (String) values[0];
|
||||
String bottomRight = (String) values[1];
|
||||
filter.setCorners(topLeft, bottomRight);
|
||||
}
|
||||
}
|
||||
|
||||
private List<QueryBuilder> buildNegationFilter(String fieldName, Iterator<Criteria.CriteriaEntry> it) {
|
||||
List<QueryBuilder> notFilterList = new LinkedList<>();
|
||||
|
||||
while (it.hasNext()) {
|
||||
Criteria.CriteriaEntry criteriaEntry = it.next();
|
||||
QueryBuilder notFilter = QueryBuilders.boolQuery()
|
||||
.mustNot(queryFor(criteriaEntry.getKey(), criteriaEntry.getValue(), fieldName));
|
||||
notFilterList.add(notFilter);
|
||||
}
|
||||
|
||||
return notFilterList;
|
||||
}
|
||||
}
|
||||
+294
@@ -0,0 +1,294 @@
|
||||
/*
|
||||
* Copyright 2013-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.elasticsearch.client.erhlc;
|
||||
|
||||
import static org.elasticsearch.index.query.Operator.*;
|
||||
import static org.elasticsearch.index.query.QueryBuilders.*;
|
||||
import static org.springframework.data.elasticsearch.core.query.Criteria.*;
|
||||
import static org.springframework.util.StringUtils.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.lucene.queryparser.flexible.standard.QueryParserUtil;
|
||||
import org.apache.lucene.search.join.ScoreMode;
|
||||
import org.elasticsearch.index.query.BoolQueryBuilder;
|
||||
import org.elasticsearch.index.query.QueryBuilder;
|
||||
import org.springframework.data.elasticsearch.annotations.FieldType;
|
||||
import org.springframework.data.elasticsearch.core.query.Criteria;
|
||||
import org.springframework.data.elasticsearch.core.query.Field;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* CriteriaQueryProcessor
|
||||
*
|
||||
* @author Rizwan Idrees
|
||||
* @author Mohsin Husen
|
||||
* @author Franck Marchand
|
||||
* @author Artur Konczak
|
||||
* @author Rasmus Faber-Espensen
|
||||
* @author James Bodkin
|
||||
* @author Peter-Josef Meisch
|
||||
* @author Ezequiel Antúnez Camacho
|
||||
* @deprecated since 5.0
|
||||
*/
|
||||
@Deprecated
|
||||
class CriteriaQueryProcessor {
|
||||
|
||||
@Nullable
|
||||
QueryBuilder createQuery(Criteria criteria) {
|
||||
|
||||
Assert.notNull(criteria, "criteria must not be null");
|
||||
|
||||
List<QueryBuilder> shouldQueryBuilders = new ArrayList<>();
|
||||
List<QueryBuilder> mustNotQueryBuilders = new ArrayList<>();
|
||||
List<QueryBuilder> mustQueryBuilders = new ArrayList<>();
|
||||
|
||||
QueryBuilder firstQuery = null;
|
||||
boolean negateFirstQuery = false;
|
||||
|
||||
for (Criteria chainedCriteria : criteria.getCriteriaChain()) {
|
||||
QueryBuilder queryFragment = queryForEntries(chainedCriteria);
|
||||
|
||||
if (queryFragment != null) {
|
||||
|
||||
if (firstQuery == null) {
|
||||
firstQuery = queryFragment;
|
||||
negateFirstQuery = chainedCriteria.isNegating();
|
||||
continue;
|
||||
}
|
||||
|
||||
if (chainedCriteria.isOr()) {
|
||||
shouldQueryBuilders.add(queryFragment);
|
||||
} else if (chainedCriteria.isNegating()) {
|
||||
mustNotQueryBuilders.add(queryFragment);
|
||||
} else {
|
||||
mustQueryBuilders.add(queryFragment);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (Criteria subCriteria : criteria.getSubCriteria()) {
|
||||
|
||||
QueryBuilder subQuery = createQuery(subCriteria);
|
||||
|
||||
if (subQuery != null) {
|
||||
if (criteria.isOr()) {
|
||||
shouldQueryBuilders.add(subQuery);
|
||||
} else if (criteria.isNegating()) {
|
||||
mustNotQueryBuilders.add(subQuery);
|
||||
} else {
|
||||
mustQueryBuilders.add(subQuery);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (firstQuery != null) {
|
||||
|
||||
if (!shouldQueryBuilders.isEmpty() && mustNotQueryBuilders.isEmpty() && mustQueryBuilders.isEmpty()) {
|
||||
shouldQueryBuilders.add(0, firstQuery);
|
||||
} else {
|
||||
|
||||
if (negateFirstQuery) {
|
||||
mustNotQueryBuilders.add(0, firstQuery);
|
||||
} else {
|
||||
mustQueryBuilders.add(0, firstQuery);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
BoolQueryBuilder query = null;
|
||||
|
||||
if (!shouldQueryBuilders.isEmpty() || !mustNotQueryBuilders.isEmpty() || !mustQueryBuilders.isEmpty()) {
|
||||
|
||||
query = boolQuery();
|
||||
|
||||
for (QueryBuilder qb : shouldQueryBuilders) {
|
||||
query.should(qb);
|
||||
}
|
||||
for (QueryBuilder qb : mustNotQueryBuilders) {
|
||||
query.mustNot(qb);
|
||||
}
|
||||
for (QueryBuilder qb : mustQueryBuilders) {
|
||||
query.must(qb);
|
||||
}
|
||||
}
|
||||
|
||||
return query;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private QueryBuilder queryForEntries(Criteria criteria) {
|
||||
|
||||
Field field = criteria.getField();
|
||||
|
||||
if (field == null || criteria.getQueryCriteriaEntries().isEmpty())
|
||||
return null;
|
||||
|
||||
String fieldName = field.getName();
|
||||
Assert.notNull(fieldName, "Unknown field " + fieldName);
|
||||
|
||||
Iterator<Criteria.CriteriaEntry> it = criteria.getQueryCriteriaEntries().iterator();
|
||||
QueryBuilder query;
|
||||
|
||||
if (criteria.getQueryCriteriaEntries().size() == 1) {
|
||||
query = queryFor(it.next(), field);
|
||||
} else {
|
||||
query = boolQuery();
|
||||
while (it.hasNext()) {
|
||||
Criteria.CriteriaEntry entry = it.next();
|
||||
((BoolQueryBuilder) query).must(queryFor(entry, field));
|
||||
}
|
||||
}
|
||||
|
||||
addBoost(query, criteria.getBoost());
|
||||
|
||||
if (hasText(field.getPath())) {
|
||||
query = nestedQuery(field.getPath(), query, ScoreMode.Avg);
|
||||
}
|
||||
|
||||
return query;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private QueryBuilder queryFor(Criteria.CriteriaEntry entry, Field field) {
|
||||
|
||||
QueryBuilder query = null;
|
||||
String fieldName = field.getName();
|
||||
boolean isKeywordField = FieldType.Keyword == field.getFieldType();
|
||||
|
||||
OperationKey key = entry.getKey();
|
||||
|
||||
// operations without a value
|
||||
switch (key) {
|
||||
case EXISTS -> query = existsQuery(fieldName);
|
||||
case EMPTY -> query = boolQuery().must(existsQuery(fieldName)).mustNot(wildcardQuery(fieldName, "*"));
|
||||
case NOT_EMPTY -> query = wildcardQuery(fieldName, "*");
|
||||
default -> {}
|
||||
}
|
||||
|
||||
if (query != null) {
|
||||
return query;
|
||||
}
|
||||
|
||||
// now operation keys with a value
|
||||
Object value = entry.getValue();
|
||||
String searchText = QueryParserUtil.escape(value.toString());
|
||||
|
||||
switch (key) {
|
||||
case EQUALS:
|
||||
query = queryStringQuery(searchText).field(fieldName).defaultOperator(AND);
|
||||
break;
|
||||
case CONTAINS:
|
||||
query = queryStringQuery('*' + searchText + '*').field(fieldName).analyzeWildcard(true);
|
||||
break;
|
||||
case STARTS_WITH:
|
||||
query = queryStringQuery(searchText + '*').field(fieldName).analyzeWildcard(true);
|
||||
break;
|
||||
case ENDS_WITH:
|
||||
query = queryStringQuery('*' + searchText).field(fieldName).analyzeWildcard(true);
|
||||
break;
|
||||
case EXPRESSION:
|
||||
query = queryStringQuery(value.toString()).field(fieldName);
|
||||
break;
|
||||
case LESS_EQUAL:
|
||||
query = rangeQuery(fieldName).lte(value);
|
||||
break;
|
||||
case GREATER_EQUAL:
|
||||
query = rangeQuery(fieldName).gte(value);
|
||||
break;
|
||||
case BETWEEN:
|
||||
Object[] ranges = (Object[]) value;
|
||||
query = rangeQuery(fieldName).from(ranges[0]).to(ranges[1]);
|
||||
break;
|
||||
case LESS:
|
||||
query = rangeQuery(fieldName).lt(value);
|
||||
break;
|
||||
case GREATER:
|
||||
query = rangeQuery(fieldName).gt(value);
|
||||
break;
|
||||
case FUZZY:
|
||||
query = fuzzyQuery(fieldName, searchText);
|
||||
break;
|
||||
case MATCHES:
|
||||
query = matchQuery(fieldName, value).operator(org.elasticsearch.index.query.Operator.OR);
|
||||
break;
|
||||
case MATCHES_ALL:
|
||||
query = matchQuery(fieldName, value).operator(org.elasticsearch.index.query.Operator.AND);
|
||||
break;
|
||||
case IN:
|
||||
if (value instanceof Iterable<?> iterable) {
|
||||
if (isKeywordField) {
|
||||
query = boolQuery().must(termsQuery(fieldName, toStringList(iterable)));
|
||||
} else {
|
||||
query = queryStringQuery(orQueryString(iterable)).field(fieldName);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case NOT_IN:
|
||||
if (value instanceof Iterable<?> iterable) {
|
||||
if (isKeywordField) {
|
||||
query = boolQuery().mustNot(termsQuery(fieldName, toStringList(iterable)));
|
||||
} else {
|
||||
query = queryStringQuery("NOT(" + orQueryString(iterable) + ')').field(fieldName);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case REGEXP:
|
||||
query = regexpQuery(fieldName, value.toString());
|
||||
break;
|
||||
}
|
||||
return query;
|
||||
}
|
||||
|
||||
private static List<String> toStringList(Iterable<?> iterable) {
|
||||
List<String> list = new ArrayList<>();
|
||||
for (Object item : iterable) {
|
||||
list.add(item != null ? item.toString() : null);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
private static String orQueryString(Iterable<?> iterable) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
for (Object item : iterable) {
|
||||
|
||||
if (item != null) {
|
||||
|
||||
if (sb.length() > 0) {
|
||||
sb.append(' ');
|
||||
}
|
||||
sb.append('"');
|
||||
sb.append(QueryParserUtil.escape(item.toString()));
|
||||
sb.append('"');
|
||||
}
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private void addBoost(@Nullable QueryBuilder query, float boost) {
|
||||
|
||||
if (query == null || Float.isNaN(boost)) {
|
||||
return;
|
||||
}
|
||||
|
||||
query.boost(boost);
|
||||
}
|
||||
}
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Copyright 2021-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.elasticsearch.client.erhlc;
|
||||
|
||||
import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest;
|
||||
import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
|
||||
import org.elasticsearch.client.RequestOptions;
|
||||
import org.springframework.data.elasticsearch.core.cluster.ClusterHealth;
|
||||
import org.springframework.data.elasticsearch.core.cluster.ClusterOperations;
|
||||
|
||||
/**
|
||||
* Default implementation of {@link ClusterOperations} using the {@link ElasticsearchRestTemplate}.
|
||||
*
|
||||
* @author Peter-Josef Meisch
|
||||
* @since 4.2
|
||||
* @deprecated since 5.0
|
||||
*/
|
||||
@Deprecated
|
||||
class DefaultClusterOperations implements ClusterOperations {
|
||||
|
||||
private final ElasticsearchRestTemplate template;
|
||||
|
||||
DefaultClusterOperations(ElasticsearchRestTemplate template) {
|
||||
this.template = template;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ClusterHealth health() {
|
||||
|
||||
ClusterHealthResponse clusterHealthResponse = template
|
||||
.execute(client -> client.cluster().health(new ClusterHealthRequest(), RequestOptions.DEFAULT));
|
||||
return ResponseConverter.clusterHealth(clusterHealthResponse);
|
||||
}
|
||||
}
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Copyright 2021-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.elasticsearch.client.erhlc;
|
||||
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest;
|
||||
import org.springframework.data.elasticsearch.core.ReactiveElasticsearchOperations;
|
||||
import org.springframework.data.elasticsearch.core.cluster.ClusterHealth;
|
||||
import org.springframework.data.elasticsearch.core.cluster.ReactiveClusterOperations;
|
||||
|
||||
/**
|
||||
* Default implementation of {@link ReactiveClusterOperations} using the {@link ReactiveElasticsearchOperations}.
|
||||
*
|
||||
* @author Peter-Josef Meisch
|
||||
* @since 4.2
|
||||
* @deprecated since 5.0
|
||||
*/
|
||||
@Deprecated
|
||||
public class DefaultReactiveClusterOperations implements ReactiveClusterOperations {
|
||||
private final ReactiveElasticsearchOperations operations;
|
||||
|
||||
public DefaultReactiveClusterOperations(ReactiveElasticsearchOperations operations) {
|
||||
this.operations = operations;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<ClusterHealth> health() {
|
||||
return Mono.from(operations.executeWithClusterClient(
|
||||
client -> client.health(new ClusterHealthRequest()).map(ResponseConverter::clusterHealth)));
|
||||
}
|
||||
}
|
||||
+907
@@ -0,0 +1,907 @@
|
||||
/*
|
||||
* Copyright 2018-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.elasticsearch.client.erhlc;
|
||||
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.ConnectException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Collection;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import org.elasticsearch.ElasticsearchException;
|
||||
import org.elasticsearch.ElasticsearchStatusException;
|
||||
import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest;
|
||||
import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
|
||||
import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest;
|
||||
import org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest;
|
||||
import org.elasticsearch.action.admin.indices.close.CloseIndexRequest;
|
||||
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
|
||||
import org.elasticsearch.action.admin.indices.flush.FlushRequest;
|
||||
import org.elasticsearch.action.admin.indices.flush.FlushResponse;
|
||||
import org.elasticsearch.action.admin.indices.open.OpenIndexRequest;
|
||||
import org.elasticsearch.action.admin.indices.refresh.RefreshRequest;
|
||||
import org.elasticsearch.action.admin.indices.refresh.RefreshResponse;
|
||||
import org.elasticsearch.action.admin.indices.settings.get.GetSettingsRequest;
|
||||
import org.elasticsearch.action.admin.indices.settings.get.GetSettingsResponse;
|
||||
import org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest;
|
||||
import org.elasticsearch.action.bulk.BulkRequest;
|
||||
import org.elasticsearch.action.bulk.BulkResponse;
|
||||
import org.elasticsearch.action.delete.DeleteRequest;
|
||||
import org.elasticsearch.action.delete.DeleteResponse;
|
||||
import org.elasticsearch.action.get.GetRequest;
|
||||
import org.elasticsearch.action.get.GetResponse;
|
||||
import org.elasticsearch.action.get.MultiGetItemResponse;
|
||||
import org.elasticsearch.action.get.MultiGetRequest;
|
||||
import org.elasticsearch.action.get.MultiGetResponse;
|
||||
import org.elasticsearch.action.index.IndexRequest;
|
||||
import org.elasticsearch.action.index.IndexResponse;
|
||||
import org.elasticsearch.action.main.MainRequest;
|
||||
import org.elasticsearch.action.main.MainResponse;
|
||||
import org.elasticsearch.action.search.ClearScrollRequest;
|
||||
import org.elasticsearch.action.search.ClearScrollResponse;
|
||||
import org.elasticsearch.action.search.SearchRequest;
|
||||
import org.elasticsearch.action.search.SearchResponse;
|
||||
import org.elasticsearch.action.search.SearchScrollRequest;
|
||||
import org.elasticsearch.action.support.master.AcknowledgedResponse;
|
||||
import org.elasticsearch.action.update.UpdateRequest;
|
||||
import org.elasticsearch.action.update.UpdateResponse;
|
||||
import org.elasticsearch.client.GetAliasesResponse;
|
||||
import org.elasticsearch.client.Request;
|
||||
import org.elasticsearch.client.indices.*;
|
||||
import org.elasticsearch.client.tasks.TaskSubmissionResponse;
|
||||
import org.elasticsearch.core.TimeValue;
|
||||
import org.elasticsearch.index.get.GetResult;
|
||||
import org.elasticsearch.index.reindex.BulkByScrollResponse;
|
||||
import org.elasticsearch.index.reindex.DeleteByQueryRequest;
|
||||
import org.elasticsearch.index.reindex.ReindexRequest;
|
||||
import org.elasticsearch.index.reindex.UpdateByQueryRequest;
|
||||
import org.elasticsearch.rest.BytesRestResponse;
|
||||
import org.elasticsearch.rest.RestStatus;
|
||||
import org.elasticsearch.script.mustache.SearchTemplateRequest;
|
||||
import org.elasticsearch.script.mustache.SearchTemplateResponse;
|
||||
import org.elasticsearch.search.SearchHit;
|
||||
import org.elasticsearch.search.SearchHits;
|
||||
import org.elasticsearch.search.aggregations.Aggregation;
|
||||
import org.elasticsearch.search.suggest.Suggest;
|
||||
import org.elasticsearch.xcontent.DeprecationHandler;
|
||||
import org.elasticsearch.xcontent.NamedXContentRegistry;
|
||||
import org.elasticsearch.xcontent.XContentParser;
|
||||
import org.elasticsearch.xcontent.XContentType;
|
||||
import org.reactivestreams.Publisher;
|
||||
import org.springframework.data.elasticsearch.RestStatusException;
|
||||
import org.springframework.data.elasticsearch.UncategorizedElasticsearchException;
|
||||
import org.springframework.data.elasticsearch.client.ClientConfiguration;
|
||||
import org.springframework.data.elasticsearch.client.ClientLogger;
|
||||
import org.springframework.data.elasticsearch.client.ElasticsearchHost;
|
||||
import org.springframework.data.elasticsearch.client.NoReachableHostException;
|
||||
import org.springframework.data.elasticsearch.client.erhlc.HostProvider.Verification;
|
||||
import org.springframework.data.elasticsearch.client.erhlc.ReactiveElasticsearchClient.Cluster;
|
||||
import org.springframework.data.elasticsearch.client.erhlc.ReactiveElasticsearchClient.Indices;
|
||||
import org.springframework.data.elasticsearch.client.util.ScrollState;
|
||||
import org.springframework.data.elasticsearch.core.query.ByQueryResponse;
|
||||
import org.springframework.data.util.Lazy;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
import org.springframework.web.reactive.function.BodyExtractors;
|
||||
import org.springframework.web.reactive.function.client.ClientRequest;
|
||||
import org.springframework.web.reactive.function.client.ClientResponse;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
import org.springframework.web.reactive.function.client.WebClient.RequestBodySpec;
|
||||
|
||||
/**
|
||||
* A {@link WebClient} based {@link ReactiveElasticsearchClient} that connects to an Elasticsearch cluster using HTTP.
|
||||
*
|
||||
* @author Christoph Strobl
|
||||
* @author Mark Paluch
|
||||
* @author Peter-Josef Meisch
|
||||
* @author Huw Ayling-Miller
|
||||
* @author Henrique Amaral
|
||||
* @author Roman Puchkovskiy
|
||||
* @author Russell Parry
|
||||
* @author Thomas Geese
|
||||
* @author Brian Clozel
|
||||
* @author Farid Faoudi
|
||||
* @author George Popides
|
||||
* @author Sijia Liu
|
||||
* @since 3.2
|
||||
* @see ClientConfiguration
|
||||
* @see ReactiveRestClients
|
||||
* @deprecated since 5.0
|
||||
*/
|
||||
@Deprecated
|
||||
public class DefaultReactiveElasticsearchClient implements ReactiveElasticsearchClient, Indices, Cluster {
|
||||
|
||||
private final HostProvider<?> hostProvider;
|
||||
private final RequestCreator requestCreator;
|
||||
private Supplier<org.springframework.data.elasticsearch.support.HttpHeaders> headersSupplier = org.springframework.data.elasticsearch.support.HttpHeaders::new;
|
||||
|
||||
/**
|
||||
* Create a new {@link DefaultReactiveElasticsearchClient} using the given {@link HostProvider} to obtain server
|
||||
* connections.
|
||||
*
|
||||
* @param hostProvider must not be {@literal null}.
|
||||
*/
|
||||
public DefaultReactiveElasticsearchClient(HostProvider<?> hostProvider) {
|
||||
this(hostProvider, new DefaultRequestCreator());
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@link DefaultReactiveElasticsearchClient} using the given {@link HostProvider} to obtain server
|
||||
* connections and the given {@link RequestCreator}.
|
||||
*
|
||||
* @param hostProvider must not be {@literal null}.
|
||||
* @param requestCreator must not be {@literal null}.
|
||||
*/
|
||||
public DefaultReactiveElasticsearchClient(HostProvider<?> hostProvider, RequestCreator requestCreator) {
|
||||
|
||||
Assert.notNull(hostProvider, "HostProvider must not be null");
|
||||
Assert.notNull(requestCreator, "RequestCreator must not be null");
|
||||
|
||||
this.hostProvider = hostProvider;
|
||||
this.requestCreator = requestCreator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@link DefaultReactiveElasticsearchClient} aware of the given nodes in the cluster. <br />
|
||||
* <strong>NOTE</strong> If the cluster requires authentication be sure to provide the according {@link HttpHeaders}
|
||||
* correctly.
|
||||
*
|
||||
* @param headers Use {@link HttpHeaders} to provide eg. authentication data. Must not be {@literal null}.
|
||||
* @param hosts must not be {@literal null} nor empty!
|
||||
* @return new instance of {@link DefaultReactiveElasticsearchClient}.
|
||||
*/
|
||||
public static ReactiveElasticsearchClient create(HttpHeaders headers, String... hosts) {
|
||||
|
||||
Assert.notNull(headers, "HttpHeaders must not be null");
|
||||
Assert.notEmpty(hosts, "Elasticsearch Cluster needs to consist of at least one host");
|
||||
|
||||
var httpHeaders = new org.springframework.data.elasticsearch.support.HttpHeaders();
|
||||
httpHeaders.addAll(headers);
|
||||
ClientConfiguration clientConfiguration = ClientConfiguration.builder().connectedTo(hosts)
|
||||
.withDefaultHeaders(httpHeaders).build();
|
||||
return create(clientConfiguration);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@link DefaultReactiveElasticsearchClient} given {@link ClientConfiguration}. <br />
|
||||
* <strong>NOTE</strong> If the cluster requires authentication be sure to provide the according {@link HttpHeaders}
|
||||
* correctly.
|
||||
*
|
||||
* @param clientConfiguration Client configuration. Must not be {@literal null}.
|
||||
* @return new instance of {@link DefaultReactiveElasticsearchClient}.
|
||||
*/
|
||||
public static ReactiveElasticsearchClient create(ClientConfiguration clientConfiguration) {
|
||||
return create(clientConfiguration, new DefaultRequestCreator());
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@link DefaultReactiveElasticsearchClient} given {@link ClientConfiguration} and
|
||||
* {@link RequestCreator}. <br />
|
||||
* <strong>NOTE</strong> If the cluster requires authentication be sure to provide the according {@link HttpHeaders}
|
||||
* correctly.
|
||||
*
|
||||
* @param clientConfiguration Client configuration. Must not be {@literal null}.
|
||||
* @param requestCreator Request creator. Must not be {@literal null}.
|
||||
* @return new instance of {@link DefaultReactiveElasticsearchClient}.
|
||||
*/
|
||||
public static ReactiveElasticsearchClient create(ClientConfiguration clientConfiguration,
|
||||
RequestCreator requestCreator) {
|
||||
|
||||
Assert.notNull(clientConfiguration, "ClientConfiguration must not be null");
|
||||
Assert.notNull(requestCreator, "RequestCreator must not be null");
|
||||
|
||||
WebClientProvider provider = WebClientProvider.getWebClientProvider(clientConfiguration);
|
||||
|
||||
HostProvider<?> hostProvider = HostProvider.provider(provider, clientConfiguration.getHeadersSupplier(),
|
||||
clientConfiguration.getEndpoints().toArray(new InetSocketAddress[0]));
|
||||
|
||||
DefaultReactiveElasticsearchClient client = new DefaultReactiveElasticsearchClient(hostProvider, requestCreator);
|
||||
|
||||
client.setHeadersSupplier(clientConfiguration.getHeadersSupplier());
|
||||
|
||||
return client;
|
||||
}
|
||||
|
||||
public void setHeadersSupplier(Supplier<org.springframework.data.elasticsearch.support.HttpHeaders> headersSupplier) {
|
||||
|
||||
Assert.notNull(headersSupplier, "headersSupplier must not be null");
|
||||
|
||||
this.headersSupplier = headersSupplier;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Boolean> ping(HttpHeaders headers) {
|
||||
|
||||
return sendRequest(new MainRequest(), requestCreator.ping(), RawActionResponse.class, headers) //
|
||||
.flatMap(response -> response.releaseBody().thenReturn(response.statusCode().is2xxSuccessful())) //
|
||||
.onErrorResume(NoReachableHostException.class, error -> Mono.just(false)).next();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<MainResponse> info(HttpHeaders headers) {
|
||||
|
||||
return sendRequest(new MainRequest(), requestCreator.info(), MainResponse.class, headers) //
|
||||
.next();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<GetResult> get(HttpHeaders headers, GetRequest getRequest) {
|
||||
|
||||
return sendRequest(getRequest, requestCreator.get(), GetResponse.class, headers) //
|
||||
.filter(GetResponse::isExists) //
|
||||
.map(DefaultReactiveElasticsearchClient::getResponseToGetResult) //
|
||||
.next();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Flux<MultiGetItemResponse> multiGet(HttpHeaders headers, MultiGetRequest multiGetRequest) {
|
||||
|
||||
return sendRequest(multiGetRequest, requestCreator.multiGet(), MultiGetResponse.class, headers)
|
||||
.map(MultiGetResponse::getResponses) //
|
||||
.flatMap(Flux::fromArray); //
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Boolean> exists(HttpHeaders headers, GetRequest getRequest) {
|
||||
|
||||
return sendRequest(getRequest, requestCreator.exists(), RawActionResponse.class, headers) //
|
||||
.flatMap(response -> response.releaseBody().thenReturn(response.statusCode().is2xxSuccessful())) //
|
||||
.next();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<IndexResponse> index(HttpHeaders headers, IndexRequest indexRequest) {
|
||||
return sendRequest(indexRequest, requestCreator.index(), IndexResponse.class, headers).next();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Indices indices() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Cluster cluster() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<UpdateResponse> update(HttpHeaders headers, UpdateRequest updateRequest) {
|
||||
return sendRequest(updateRequest, requestCreator.update(), UpdateResponse.class, headers).next();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<DeleteResponse> delete(HttpHeaders headers, DeleteRequest deleteRequest) {
|
||||
|
||||
return sendRequest(deleteRequest, requestCreator.delete(), DeleteResponse.class, headers) //
|
||||
.next();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Long> count(HttpHeaders headers, SearchRequest searchRequest) {
|
||||
searchRequest.source().trackTotalHits(true);
|
||||
searchRequest.source().size(0);
|
||||
searchRequest.source().fetchSource(false);
|
||||
return sendRequest(searchRequest, requestCreator.search(), SearchResponse.class, headers) //
|
||||
.map(SearchResponse::getHits) //
|
||||
.map(searchHits -> searchHits.getTotalHits().value) //
|
||||
.next();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Flux<SearchHit> searchTemplate(HttpHeaders headers, SearchTemplateRequest searchTemplateRequest) {
|
||||
return sendRequest(searchTemplateRequest, requestCreator.searchTemplate(), SearchTemplateResponse.class, headers)
|
||||
.map(response -> response.getResponse().getHits()).flatMap(Flux::fromIterable);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Flux<SearchHit> search(HttpHeaders headers, SearchRequest searchRequest) {
|
||||
|
||||
return sendRequest(searchRequest, requestCreator.search(), SearchResponse.class, headers) //
|
||||
.map(SearchResponse::getHits) //
|
||||
.flatMap(Flux::fromIterable);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<SearchResponse> searchForResponse(HttpHeaders headers, SearchRequest searchRequest) {
|
||||
return sendRequest(searchRequest, requestCreator.search(), SearchResponse.class, headers).next();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Flux<Suggest> suggest(HttpHeaders headers, SearchRequest searchRequest) {
|
||||
return sendRequest(searchRequest, requestCreator.search(), SearchResponse.class, headers) //
|
||||
.map(SearchResponse::getSuggest);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Flux<Aggregation> aggregate(HttpHeaders headers, SearchRequest searchRequest) {
|
||||
|
||||
Assert.notNull(headers, "headers must not be null");
|
||||
Assert.notNull(searchRequest, "searchRequest must not be null");
|
||||
|
||||
searchRequest.source().size(0);
|
||||
searchRequest.source().trackTotalHits(false);
|
||||
|
||||
return sendRequest(searchRequest, requestCreator.search(), SearchResponse.class, headers) //
|
||||
.map(SearchResponse::getAggregations) //
|
||||
.flatMap(Flux::fromIterable);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Flux<SearchHit> scroll(HttpHeaders headers, SearchRequest searchRequest) {
|
||||
|
||||
TimeValue scrollTimeout = searchRequest.scroll() != null ? searchRequest.scroll().keepAlive()
|
||||
: TimeValue.timeValueMinutes(1);
|
||||
|
||||
if (searchRequest.scroll() == null) {
|
||||
searchRequest.scroll(scrollTimeout);
|
||||
}
|
||||
|
||||
return Flux.usingWhen(Mono.fromSupplier(ScrollState::new),
|
||||
|
||||
state -> sendRequest(searchRequest, requestCreator.search(), SearchResponse.class, headers)
|
||||
.expand(searchResponse -> {
|
||||
|
||||
state.updateScrollId(searchResponse.getScrollId());
|
||||
if (isEmpty(searchResponse.getHits())) {
|
||||
return Mono.empty();
|
||||
}
|
||||
|
||||
return sendRequest(new SearchScrollRequest(searchResponse.getScrollId()).scroll(scrollTimeout),
|
||||
requestCreator.scroll(), SearchResponse.class, headers);
|
||||
|
||||
}),
|
||||
state -> cleanupScroll(headers, state), //
|
||||
(state, ex) -> cleanupScroll(headers, state), //
|
||||
state -> cleanupScroll(headers, state)) //
|
||||
.filter(it -> !isEmpty(it.getHits())) //
|
||||
.map(SearchResponse::getHits) //
|
||||
.flatMapIterable(Function.identity()); //
|
||||
}
|
||||
|
||||
private static boolean isEmpty(@Nullable SearchHits hits) {
|
||||
return hits != null && hits.getHits() != null && hits.getHits().length == 0;
|
||||
}
|
||||
|
||||
private Publisher<?> cleanupScroll(HttpHeaders headers, ScrollState state) {
|
||||
|
||||
if (state.getScrollIds().isEmpty()) {
|
||||
return Mono.empty();
|
||||
}
|
||||
|
||||
ClearScrollRequest clearScrollRequest = new ClearScrollRequest();
|
||||
clearScrollRequest.scrollIds(state.getScrollIds());
|
||||
|
||||
// just send the request, resources get cleaned up anyways after scrollTimeout has been reached.
|
||||
return sendRequest(clearScrollRequest, requestCreator.clearScroll(), ClearScrollResponse.class, headers);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<BulkByScrollResponse> deleteBy(HttpHeaders headers, DeleteByQueryRequest deleteRequest) {
|
||||
|
||||
return sendRequest(deleteRequest, requestCreator.deleteByQuery(), BulkByScrollResponse.class, headers) //
|
||||
.next();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<ByQueryResponse> updateBy(HttpHeaders headers, UpdateByQueryRequest updateRequest) {
|
||||
return sendRequest(updateRequest, requestCreator.updateByQuery(), BulkByScrollResponse.class, headers) //
|
||||
.next() //
|
||||
.map(ResponseConverter::byQueryResponseOf);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<BulkResponse> bulk(HttpHeaders headers, BulkRequest bulkRequest) {
|
||||
return sendRequest(bulkRequest, requestCreator.bulk(), BulkResponse.class, headers) //
|
||||
.next();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<BulkByScrollResponse> reindex(HttpHeaders headers, ReindexRequest reindexRequest) {
|
||||
return sendRequest(reindexRequest, requestCreator.reindex(), BulkByScrollResponse.class, headers).next();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<String> submitReindex(HttpHeaders headers, ReindexRequest reindexRequest) {
|
||||
return sendRequest(reindexRequest, requestCreator.submitReindex(), TaskSubmissionResponse.class, headers).next()
|
||||
.map(TaskSubmissionResponse::getTask);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> Mono<T> execute(ReactiveElasticsearchClientCallback<T> callback) {
|
||||
|
||||
return this.hostProvider.getActive(Verification.LAZY) //
|
||||
.flatMap(callback::doWithClient) //
|
||||
.onErrorResume(throwable -> {
|
||||
|
||||
if (isCausedByConnectionException(throwable)) {
|
||||
return hostProvider.getActive(Verification.ACTIVE) //
|
||||
.flatMap(callback::doWithClient);
|
||||
}
|
||||
|
||||
return Mono.error(throwable);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* checks if the given throwable is a {@link ConnectException} or has one in it's cause chain
|
||||
*
|
||||
* @param throwable the throwable to check
|
||||
* @return true if throwable is caused by a {@link ConnectException}
|
||||
*/
|
||||
private boolean isCausedByConnectionException(Throwable throwable) {
|
||||
|
||||
Throwable t = throwable;
|
||||
do {
|
||||
|
||||
if (t instanceof ConnectException) {
|
||||
return true;
|
||||
}
|
||||
|
||||
t = t.getCause();
|
||||
} while (t != null);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Status> status() {
|
||||
|
||||
return hostProvider.clusterInfo() //
|
||||
.map(it -> new ClientStatus(it.getNodes()));
|
||||
}
|
||||
|
||||
// --> Private Response helpers
|
||||
|
||||
private static GetResult getResponseToGetResult(GetResponse response) {
|
||||
|
||||
return new GetResult(response.getIndex(), response.getType(), response.getId(), response.getSeqNo(),
|
||||
response.getPrimaryTerm(), response.getVersion(), response.isExists(), response.getSourceAsBytesRef(),
|
||||
response.getFields(), null);
|
||||
}
|
||||
|
||||
// -->
|
||||
|
||||
private <REQ, RESP> Flux<RESP> sendRequest(REQ request, Function<REQ, Request> converter, Class<RESP> responseType,
|
||||
HttpHeaders headers) {
|
||||
return sendRequest(converter.apply(request), responseType, headers);
|
||||
}
|
||||
|
||||
private <Resp> Flux<Resp> sendRequest(Request request, Class<Resp> responseType, HttpHeaders headers) {
|
||||
|
||||
String logId = ClientLogger.newLogId();
|
||||
|
||||
return Flux
|
||||
.from(execute(webClient -> sendRequest(webClient, logId, request, headers).exchangeToMono(clientResponse -> {
|
||||
Publisher<? extends Resp> publisher = readResponseBody(logId, request, clientResponse, responseType);
|
||||
return Mono.from(publisher);
|
||||
})));
|
||||
}
|
||||
|
||||
private RequestBodySpec sendRequest(WebClient webClient, String logId, Request request, HttpHeaders headers) {
|
||||
|
||||
RequestBodySpec requestBodySpec = webClient.method(HttpMethod.valueOf(request.getMethod().toUpperCase())) //
|
||||
.uri(builder -> {
|
||||
|
||||
builder = builder.path(request.getEndpoint());
|
||||
|
||||
if (!ObjectUtils.isEmpty(request.getParameters())) {
|
||||
for (Entry<String, String> entry : request.getParameters().entrySet()) {
|
||||
builder = builder.queryParam(entry.getKey(), entry.getValue());
|
||||
}
|
||||
}
|
||||
return builder.build();
|
||||
}) //
|
||||
.attribute(ClientRequest.LOG_ID_ATTRIBUTE, logId) //
|
||||
.headers(theHeaders -> {
|
||||
|
||||
// add all the headers explicitly set
|
||||
theHeaders.addAll(headers);
|
||||
|
||||
// and now those that might be set on the request.
|
||||
if (request.getOptions() != null) {
|
||||
|
||||
if (!ObjectUtils.isEmpty(request.getOptions().getHeaders())) {
|
||||
request.getOptions().getHeaders().forEach(it -> theHeaders.add(it.getName(), it.getValue()));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (request.getEntity() != null) {
|
||||
|
||||
Lazy<String> body = bodyExtractor(request);
|
||||
|
||||
ClientLogger.logRequest(logId, request.getMethod().toUpperCase(), request.getEndpoint(), request.getParameters(),
|
||||
body::get);
|
||||
|
||||
requestBodySpec.contentType(MediaType.valueOf(request.getEntity().getContentType().getValue()))
|
||||
.body(Mono.fromSupplier(body), String.class);
|
||||
} else {
|
||||
ClientLogger.logRequest(logId, request.getMethod().toUpperCase(), request.getEndpoint(), request.getParameters());
|
||||
}
|
||||
|
||||
return requestBodySpec;
|
||||
}
|
||||
|
||||
// region indices operations
|
||||
@Override
|
||||
public Mono<Boolean> createIndex(HttpHeaders headers,
|
||||
org.elasticsearch.action.admin.indices.create.CreateIndexRequest createIndexRequest) {
|
||||
|
||||
return sendRequest(createIndexRequest, requestCreator.indexCreate(), AcknowledgedResponse.class, headers) //
|
||||
.map(AcknowledgedResponse::isAcknowledged) //
|
||||
.next();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Boolean> createIndex(HttpHeaders headers, CreateIndexRequest createIndexRequest) {
|
||||
|
||||
return sendRequest(createIndexRequest, requestCreator.createIndexRequest(), AcknowledgedResponse.class, headers) //
|
||||
.map(AcknowledgedResponse::isAcknowledged) //
|
||||
.next();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Void> closeIndex(HttpHeaders headers, CloseIndexRequest closeIndexRequest) {
|
||||
|
||||
return sendRequest(closeIndexRequest, requestCreator.indexClose(), AcknowledgedResponse.class, headers) //
|
||||
.then();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Boolean> existsIndex(HttpHeaders headers,
|
||||
org.elasticsearch.action.admin.indices.get.GetIndexRequest request) {
|
||||
|
||||
return sendRequest(request, requestCreator.indexExists(), RawActionResponse.class, headers) //
|
||||
.flatMap(response -> response.releaseBody().thenReturn(response.statusCode().is2xxSuccessful())) //
|
||||
.next();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Boolean> existsIndex(HttpHeaders headers, GetIndexRequest request) {
|
||||
return sendRequest(request, requestCreator.indexExistsRequest(), RawActionResponse.class, headers) //
|
||||
.flatMap(response -> response.releaseBody().thenReturn(response.statusCode().is2xxSuccessful())) //
|
||||
.next();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Boolean> deleteIndex(HttpHeaders headers, DeleteIndexRequest request) {
|
||||
|
||||
return sendRequest(request, requestCreator.indexDelete(), AcknowledgedResponse.class, headers) //
|
||||
.map(AcknowledgedResponse::isAcknowledged) //
|
||||
.next();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Void> flushIndex(HttpHeaders headers, FlushRequest flushRequest) {
|
||||
|
||||
return sendRequest(flushRequest, requestCreator.flushIndex(), FlushResponse.class, headers) //
|
||||
.then();
|
||||
}
|
||||
|
||||
@Override
|
||||
@Deprecated
|
||||
public Mono<org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse> getMapping(HttpHeaders headers,
|
||||
org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest getMappingsRequest) {
|
||||
return sendRequest(getMappingsRequest, requestCreator.getMapping(),
|
||||
org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse.class, headers).next();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<GetMappingsResponse> getMapping(HttpHeaders headers, GetMappingsRequest getMappingsRequest) {
|
||||
return sendRequest(getMappingsRequest, requestCreator.getMappingRequest(), GetMappingsResponse.class, headers) //
|
||||
.next();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<GetFieldMappingsResponse> getFieldMapping(HttpHeaders headers,
|
||||
GetFieldMappingsRequest getFieldMappingsRequest) {
|
||||
return sendRequest(getFieldMappingsRequest, requestCreator.getFieldMapping(), GetFieldMappingsResponse.class,
|
||||
headers).next();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<GetSettingsResponse> getSettings(HttpHeaders headers, GetSettingsRequest getSettingsRequest) {
|
||||
return sendRequest(getSettingsRequest, requestCreator.getSettings(), GetSettingsResponse.class, headers).next();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Boolean> putMapping(HttpHeaders headers,
|
||||
org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest putMappingRequest) {
|
||||
|
||||
return sendRequest(putMappingRequest, requestCreator.putMapping(), AcknowledgedResponse.class, headers) //
|
||||
.map(AcknowledgedResponse::isAcknowledged) //
|
||||
.next();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Boolean> putMapping(HttpHeaders headers, PutMappingRequest putMappingRequest) {
|
||||
return sendRequest(putMappingRequest, requestCreator.putMappingRequest(), AcknowledgedResponse.class, headers) //
|
||||
.map(AcknowledgedResponse::isAcknowledged) //
|
||||
.next();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Void> openIndex(HttpHeaders headers, OpenIndexRequest request) {
|
||||
|
||||
return sendRequest(request, requestCreator.indexOpen(), AcknowledgedResponse.class, headers) //
|
||||
.then();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Void> refreshIndex(HttpHeaders headers, RefreshRequest refreshRequest) {
|
||||
|
||||
return sendRequest(refreshRequest, requestCreator.indexRefresh(), RefreshResponse.class, headers) //
|
||||
.then();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Boolean> updateAliases(HttpHeaders headers, IndicesAliasesRequest indicesAliasesRequest) {
|
||||
return sendRequest(indicesAliasesRequest, requestCreator.updateAlias(), AcknowledgedResponse.class, headers)
|
||||
.map(AcknowledgedResponse::isAcknowledged).next();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<GetAliasesResponse> getAliases(HttpHeaders headers, GetAliasesRequest getAliasesRequest) {
|
||||
return sendRequest(getAliasesRequest, requestCreator.getAlias(), GetAliasesResponse.class, headers).next();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Boolean> putTemplate(HttpHeaders headers, PutIndexTemplateRequest putIndexTemplateRequest) {
|
||||
return sendRequest(putIndexTemplateRequest, requestCreator.putTemplate(), AcknowledgedResponse.class, headers)
|
||||
.map(AcknowledgedResponse::isAcknowledged).next();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<GetIndexTemplatesResponse> getTemplate(HttpHeaders headers,
|
||||
GetIndexTemplatesRequest getIndexTemplatesRequest) {
|
||||
return (sendRequest(getIndexTemplatesRequest, requestCreator.getTemplates(), GetIndexTemplatesResponse.class,
|
||||
headers)).next();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Boolean> existsTemplate(HttpHeaders headers, IndexTemplatesExistRequest indexTemplatesExistRequest) {
|
||||
return sendRequest(indexTemplatesExistRequest, requestCreator.templatesExist(), RawActionResponse.class, headers) //
|
||||
.flatMap(response -> response.releaseBody().thenReturn(response.statusCode().is2xxSuccessful())) //
|
||||
.next();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Boolean> deleteTemplate(HttpHeaders headers, DeleteIndexTemplateRequest deleteIndexTemplateRequest) {
|
||||
return sendRequest(deleteIndexTemplateRequest, requestCreator.deleteTemplate(), AcknowledgedResponse.class, headers)
|
||||
.map(AcknowledgedResponse::isAcknowledged).next();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<GetIndexResponse> getIndex(HttpHeaders headers, GetIndexRequest getIndexRequest) {
|
||||
return sendRequest(getIndexRequest, requestCreator.getIndex(), GetIndexResponse.class, headers).next();
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
// region cluster operations
|
||||
@Override
|
||||
public Mono<ClusterHealthResponse> health(HttpHeaders headers, ClusterHealthRequest clusterHealthRequest) {
|
||||
return sendRequest(clusterHealthRequest, requestCreator.clusterHealth(), ClusterHealthResponse.class, headers)
|
||||
.next();
|
||||
}
|
||||
// endregion
|
||||
|
||||
// region helper functions
|
||||
private <T> Publisher<? extends T> readResponseBody(String logId, Request request, ClientResponse response,
|
||||
Class<T> responseType) {
|
||||
|
||||
if (RawActionResponse.class.equals(responseType)) {
|
||||
|
||||
ClientLogger.logRawResponse(logId, response.statusCode().value());
|
||||
return Mono.just(responseType.cast(RawActionResponse.create(response)));
|
||||
}
|
||||
|
||||
if (response.statusCode().is5xxServerError()) {
|
||||
|
||||
ClientLogger.logRawResponse(logId, response.statusCode().value());
|
||||
return handleServerError(request, response);
|
||||
}
|
||||
|
||||
if (response.statusCode().is4xxClientError()) {
|
||||
|
||||
ClientLogger.logRawResponse(logId, response.statusCode().value());
|
||||
return handleClientError(logId, response, responseType);
|
||||
}
|
||||
|
||||
return response.body(BodyExtractors.toMono(byte[].class)) //
|
||||
.map(it -> new String(it, StandardCharsets.UTF_8)) //
|
||||
.doOnNext(it -> ClientLogger.logResponse(logId, response.statusCode().value(), it)) //
|
||||
.flatMap(content -> doDecode(response, responseType, content));
|
||||
}
|
||||
|
||||
private static <T> Mono<T> doDecode(ClientResponse response, Class<T> responseType, String content) {
|
||||
|
||||
String mediaType = response.headers().contentType().map(MediaType::toString).orElse(XContentType.JSON.mediaType());
|
||||
|
||||
try {
|
||||
|
||||
Method fromXContent = ReflectionUtils.findMethod(responseType, "fromXContent", XContentParser.class);
|
||||
|
||||
if (fromXContent == null) {
|
||||
return Mono.error(new UncategorizedElasticsearchException(
|
||||
"No method named fromXContent found in " + responseType.getCanonicalName()));
|
||||
}
|
||||
return Mono.justOrEmpty(responseType
|
||||
.cast(ReflectionUtils.invokeMethod(fromXContent, responseType, createParser(mediaType, content))));
|
||||
|
||||
} catch (Throwable errorParseFailure) { // cause elasticsearch also uses AssertionError
|
||||
|
||||
try {
|
||||
return Mono.error(BytesRestResponse.errorFromXContent(createParser(mediaType, content)));
|
||||
} catch (Exception e) {
|
||||
|
||||
return Mono.error(new RestStatusException(response.statusCode().value(), content));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static XContentParser createParser(String mediaType, String content) throws IOException {
|
||||
return XContentType.fromMediaTypeOrFormat(mediaType) //
|
||||
.xContent() //
|
||||
.createParser(new NamedXContentRegistry(NamedXContents.getDefaultNamedXContents()),
|
||||
DeprecationHandler.THROW_UNSUPPORTED_OPERATION, content);
|
||||
}
|
||||
|
||||
private Lazy<String> bodyExtractor(Request request) {
|
||||
|
||||
return Lazy.of(() -> {
|
||||
|
||||
try {
|
||||
return EntityUtils.toString(request.getEntity());
|
||||
} catch (IOException e) {
|
||||
throw new RequestBodyEncodingException("Error encoding request", e);
|
||||
}
|
||||
});
|
||||
}
|
||||
// endregion
|
||||
|
||||
// region error and exception handling
|
||||
private <T> Publisher<? extends T> handleServerError(Request request, ClientResponse response) {
|
||||
|
||||
int statusCode = response.statusCode().value();
|
||||
RestStatus status = RestStatus.fromCode(statusCode);
|
||||
String mediaType = response.headers().contentType().map(MediaType::toString).orElse(XContentType.JSON.mediaType());
|
||||
|
||||
return response.body(BodyExtractors.toMono(byte[].class)) //
|
||||
.switchIfEmpty(Mono.error(new RestStatusException(status.getStatus(),
|
||||
String.format("%s request to %s returned error code %s and no body.", request.getMethod(),
|
||||
request.getEndpoint(), statusCode))))
|
||||
.map(bytes -> new String(bytes, StandardCharsets.UTF_8)) //
|
||||
.flatMap(content -> contentOrError(content, mediaType, status))
|
||||
.flatMap(unused -> Mono
|
||||
.error(new RestStatusException(status.getStatus(), String.format("%s request to %s returned error code %s.",
|
||||
request.getMethod(), request.getEndpoint(), statusCode))));
|
||||
}
|
||||
|
||||
private <T> Publisher<? extends T> handleClientError(String logId, ClientResponse response, Class<T> responseType) {
|
||||
|
||||
int statusCode = response.statusCode().value();
|
||||
RestStatus status = RestStatus.fromCode(statusCode);
|
||||
String mediaType = response.headers().contentType().map(MediaType::toString).orElse(XContentType.JSON.mediaType());
|
||||
|
||||
return response.body(BodyExtractors.toMono(byte[].class)) //
|
||||
.map(bytes -> new String(bytes, StandardCharsets.UTF_8)) //
|
||||
.flatMap(content -> contentOrError(content, mediaType, status)) //
|
||||
.doOnNext(content -> ClientLogger.logResponse(logId, response.statusCode().value(), content)) //
|
||||
.flatMap(content -> doDecode(response, responseType, content));
|
||||
}
|
||||
|
||||
/**
|
||||
* checks if the given content body contains an {@link ElasticsearchException}, if yes it is returned in a Mono.error.
|
||||
* Otherwise the content is returned in the Mono
|
||||
*
|
||||
* @param content the content to analyze
|
||||
* @param mediaType the returned media type
|
||||
* @param status the response status
|
||||
* @return a Mono with the content or an Mono.error
|
||||
*/
|
||||
private static Mono<String> contentOrError(String content, String mediaType, RestStatus status) {
|
||||
|
||||
ElasticsearchException exception = getElasticsearchException(content, mediaType, status);
|
||||
|
||||
if (exception != null) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
buildExceptionMessages(sb, exception);
|
||||
return Mono.error(new RestStatusException(status.getStatus(), sb.toString(), exception));
|
||||
}
|
||||
|
||||
return Mono.just(content);
|
||||
}
|
||||
|
||||
/**
|
||||
* tries to parse an {@link ElasticsearchException} from the given body content
|
||||
*
|
||||
* @param content the content to analyse
|
||||
* @param mediaType the type of the body content
|
||||
* @return an {@link ElasticsearchException} or {@literal null}.
|
||||
*/
|
||||
@Nullable
|
||||
private static ElasticsearchException getElasticsearchException(String content, String mediaType, RestStatus status) {
|
||||
|
||||
try {
|
||||
XContentParser parser = createParser(mediaType, content);
|
||||
// we have a JSON object with an error and a status field
|
||||
parser.nextToken(); // Skip START_OBJECT
|
||||
|
||||
XContentParser.Token token;
|
||||
do {
|
||||
token = parser.nextToken();
|
||||
|
||||
if ("error".equals(parser.currentName())) {
|
||||
return ElasticsearchException.failureFromXContent(parser);
|
||||
}
|
||||
} while (token == XContentParser.Token.FIELD_NAME);
|
||||
|
||||
return null;
|
||||
} catch (Exception e) {
|
||||
return new ElasticsearchStatusException(content, status);
|
||||
}
|
||||
}
|
||||
|
||||
private static void buildExceptionMessages(StringBuilder sb, Throwable t) {
|
||||
|
||||
sb.append(t.getMessage());
|
||||
for (Throwable throwable : t.getSuppressed()) {
|
||||
sb.append(", ");
|
||||
buildExceptionMessages(sb, throwable);
|
||||
}
|
||||
}
|
||||
// endregion
|
||||
|
||||
// region internal classes
|
||||
/**
|
||||
* Reactive client {@link ReactiveElasticsearchClient.Status} implementation.
|
||||
*
|
||||
* @author Christoph Strobl
|
||||
*/
|
||||
static class ClientStatus implements Status {
|
||||
|
||||
private final Collection<ElasticsearchHost> connectedHosts;
|
||||
|
||||
ClientStatus(Collection<ElasticsearchHost> connectedHosts) {
|
||||
this.connectedHosts = connectedHosts;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.elasticsearch.client.erhlc.ReactiveElasticsearchClient.Status#hosts()
|
||||
*/
|
||||
@Override
|
||||
public Collection<ElasticsearchHost> hosts() {
|
||||
return connectedHosts;
|
||||
}
|
||||
}
|
||||
|
||||
// endregion
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* Copyright 2021-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.elasticsearch.client.erhlc;
|
||||
|
||||
/**
|
||||
* @author Roman Puchkovskiy
|
||||
* @since 4.0
|
||||
* @deprecated since 5.0
|
||||
*/
|
||||
@Deprecated
|
||||
class DefaultRequestCreator implements RequestCreator {}
|
||||
+193
@@ -0,0 +1,193 @@
|
||||
/*
|
||||
* Copyright 2018-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.elasticsearch.client.erhlc;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.client.reactive.ClientHttpConnector;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
import org.springframework.web.reactive.function.client.WebClient.Builder;
|
||||
import org.springframework.web.util.DefaultUriBuilderFactory;
|
||||
|
||||
/**
|
||||
* Default {@link WebClientProvider} that uses cached {@link WebClient} instances per {@code hostAndPort}.
|
||||
*
|
||||
* @author Mark Paluch
|
||||
* @author Christoph Strobl
|
||||
* @author Huw Ayling-Miller
|
||||
* @author Peter-Josef Meisch
|
||||
* @since 3.2
|
||||
* @deprecated since 5.0
|
||||
*/
|
||||
@Deprecated
|
||||
class DefaultWebClientProvider implements WebClientProvider {
|
||||
|
||||
private final Map<InetSocketAddress, WebClient> cachedClients;
|
||||
|
||||
private final String scheme;
|
||||
private final @Nullable ClientHttpConnector connector;
|
||||
private final Consumer<Throwable> errorListener;
|
||||
private final HttpHeaders headers;
|
||||
private final @Nullable String pathPrefix;
|
||||
private final Function<WebClient, WebClient> webClientConfigurer;
|
||||
private final Consumer<WebClient.RequestHeadersSpec<?>> requestConfigurer;
|
||||
|
||||
/**
|
||||
* Create new {@link DefaultWebClientProvider} with empty {@link HttpHeaders} and no-op {@literal error listener}.
|
||||
*
|
||||
* @param scheme must not be {@literal null}.
|
||||
* @param connector can be {@literal null}.
|
||||
*/
|
||||
DefaultWebClientProvider(String scheme, @Nullable ClientHttpConnector connector) {
|
||||
this(scheme, connector, e -> {}, HttpHeaders.EMPTY, null, Function.identity(), requestHeadersSpec -> {});
|
||||
}
|
||||
|
||||
/**
|
||||
* Create new {@link DefaultWebClientProvider} with empty {@link HttpHeaders} and no-op {@literal error listener}.
|
||||
*
|
||||
* @param scheme must not be {@literal null}.
|
||||
* @param connector can be {@literal null}.
|
||||
* @param errorListener must not be {@literal null}.
|
||||
* @param headers must not be {@literal null}.
|
||||
* @param pathPrefix can be {@literal null}.
|
||||
* @param webClientConfigurer must not be {@literal null}.
|
||||
* @param requestConfigurer must not be {@literal null}.
|
||||
*/
|
||||
private DefaultWebClientProvider(String scheme, @Nullable ClientHttpConnector connector,
|
||||
Consumer<Throwable> errorListener, HttpHeaders headers, @Nullable String pathPrefix,
|
||||
Function<WebClient, WebClient> webClientConfigurer, Consumer<WebClient.RequestHeadersSpec<?>> requestConfigurer) {
|
||||
|
||||
Assert.notNull(scheme, "Scheme must not be null! A common scheme would be 'http'.");
|
||||
Assert.notNull(errorListener, "errorListener must not be null! You may want use a no-op one 'e -> {}' instead.");
|
||||
Assert.notNull(headers, "headers must not be null! Think about using 'HttpHeaders.EMPTY' as an alternative.");
|
||||
Assert.notNull(webClientConfigurer,
|
||||
"webClientConfigurer must not be null! You may want use a no-op one 'Function.identity()' instead.");
|
||||
Assert.notNull(requestConfigurer,
|
||||
"requestConfigurer must not be null! You may want use a no-op one 'r -> {}' instead.\"");
|
||||
|
||||
this.cachedClients = new ConcurrentHashMap<>();
|
||||
this.scheme = scheme;
|
||||
this.connector = connector;
|
||||
this.errorListener = errorListener;
|
||||
this.headers = headers;
|
||||
this.pathPrefix = pathPrefix;
|
||||
this.webClientConfigurer = webClientConfigurer;
|
||||
this.requestConfigurer = requestConfigurer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public WebClient get(InetSocketAddress endpoint) {
|
||||
|
||||
Assert.notNull(endpoint, "Endpoint must not be empty!");
|
||||
|
||||
return this.cachedClients.computeIfAbsent(endpoint, this::createWebClientForSocketAddress);
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpHeaders getDefaultHeaders() {
|
||||
return headers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Consumer<Throwable> getErrorListener() {
|
||||
return this.errorListener;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public String getPathPrefix() {
|
||||
return pathPrefix;
|
||||
}
|
||||
|
||||
@Override
|
||||
public WebClientProvider withDefaultHeaders(HttpHeaders headers) {
|
||||
|
||||
Assert.notNull(headers, "HttpHeaders must not be null.");
|
||||
|
||||
HttpHeaders merged = new HttpHeaders();
|
||||
merged.addAll(this.headers);
|
||||
merged.addAll(headers);
|
||||
|
||||
return new DefaultWebClientProvider(scheme, connector, errorListener, merged, pathPrefix, webClientConfigurer,
|
||||
requestConfigurer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public WebClientProvider withRequestConfigurer(Consumer<WebClient.RequestHeadersSpec<?>> requestConfigurer) {
|
||||
|
||||
Assert.notNull(requestConfigurer, "requestConfigurer must not be null.");
|
||||
|
||||
return new DefaultWebClientProvider(scheme, connector, errorListener, headers, pathPrefix, webClientConfigurer,
|
||||
requestConfigurer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public WebClientProvider withErrorListener(Consumer<Throwable> errorListener) {
|
||||
|
||||
Assert.notNull(errorListener, "Error listener must not be null.");
|
||||
|
||||
Consumer<Throwable> listener = this.errorListener.andThen(errorListener);
|
||||
return new DefaultWebClientProvider(scheme, this.connector, listener, headers, pathPrefix, webClientConfigurer,
|
||||
requestConfigurer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public WebClientProvider withPathPrefix(String pathPrefix) {
|
||||
Assert.notNull(pathPrefix, "pathPrefix must not be null.");
|
||||
|
||||
return new DefaultWebClientProvider(this.scheme, this.connector, this.errorListener, this.headers, pathPrefix,
|
||||
webClientConfigurer, requestConfigurer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public WebClientProvider withWebClientConfigurer(Function<WebClient, WebClient> webClientConfigurer) {
|
||||
return new DefaultWebClientProvider(scheme, connector, errorListener, headers, pathPrefix, webClientConfigurer,
|
||||
requestConfigurer);
|
||||
|
||||
}
|
||||
|
||||
protected WebClient createWebClientForSocketAddress(InetSocketAddress socketAddress) {
|
||||
|
||||
Builder builder = WebClient.builder() //
|
||||
.defaultHeaders(it -> it.addAll(getDefaultHeaders())) //
|
||||
.defaultRequest(requestConfigurer);
|
||||
|
||||
if (connector != null) {
|
||||
builder = builder.clientConnector(connector);
|
||||
}
|
||||
|
||||
String baseUrl = String.format("%s://%s:%d%s", this.scheme, socketAddress.getHostString(), socketAddress.getPort(),
|
||||
pathPrefix == null ? "" : '/' + pathPrefix);
|
||||
|
||||
DefaultUriBuilderFactory uriBuilderFactory = new DefaultUriBuilderFactory(baseUrl);
|
||||
// the template will already be encoded by the RequestConverters methods
|
||||
uriBuilderFactory.setEncodingMode(DefaultUriBuilderFactory.EncodingMode.VALUES_ONLY);
|
||||
builder.uriBuilderFactory(uriBuilderFactory); //
|
||||
|
||||
WebClient webClient = builder //
|
||||
.filter((request, next) -> next.exchange(request) //
|
||||
.doOnError(errorListener)) //
|
||||
.build(); //
|
||||
return webClientConfigurer.apply(webClient);
|
||||
}
|
||||
}
|
||||
+750
@@ -0,0 +1,750 @@
|
||||
/*
|
||||
* Copyright 2019-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.elasticsearch.client.erhlc;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.elasticsearch.action.get.GetResponse;
|
||||
import org.elasticsearch.action.get.MultiGetItemResponse;
|
||||
import org.elasticsearch.action.get.MultiGetResponse;
|
||||
import org.elasticsearch.common.bytes.BytesReference;
|
||||
import org.elasticsearch.common.document.DocumentField;
|
||||
import org.elasticsearch.common.text.Text;
|
||||
import org.elasticsearch.index.get.GetResult;
|
||||
import org.elasticsearch.search.SearchHit;
|
||||
import org.elasticsearch.search.SearchHits;
|
||||
import org.springframework.data.elasticsearch.core.MultiGetItem;
|
||||
import org.springframework.data.elasticsearch.core.document.Document;
|
||||
import org.springframework.data.elasticsearch.core.document.Explanation;
|
||||
import org.springframework.data.elasticsearch.core.document.NestedMetaData;
|
||||
import org.springframework.data.elasticsearch.core.document.SearchDocument;
|
||||
import org.springframework.data.elasticsearch.core.document.SearchDocumentResponse;
|
||||
import org.springframework.data.mapping.MappingException;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonEncoding;
|
||||
import com.fasterxml.jackson.core.JsonFactory;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
|
||||
/**
|
||||
* Utility class to adapt {@link org.elasticsearch.action.get.GetResponse},
|
||||
* {@link org.elasticsearch.index.get.GetResult}, {@link org.elasticsearch.action.get.MultiGetResponse}
|
||||
* {@link org.elasticsearch.search.SearchHit}, {@link org.elasticsearch.common.document.DocumentField} to
|
||||
* {@link Document}.
|
||||
*
|
||||
* @author Mark Paluch
|
||||
* @author Peter-Josef Meisch
|
||||
* @author Roman Puchkovskiy
|
||||
* @author Matt Gilene
|
||||
* @since 4.0
|
||||
* @deprecated since 5.0
|
||||
*/
|
||||
@Deprecated
|
||||
public final class DocumentAdapters {
|
||||
|
||||
private DocumentAdapters() {}
|
||||
|
||||
/**
|
||||
* Create a {@link Document} from {@link GetResponse}.
|
||||
* <p>
|
||||
* Returns a {@link Document} using the getResponse if available.
|
||||
*
|
||||
* @param getResponse the getResponse {@link GetResponse}.
|
||||
* @return the adapted {@link Document}, null if getResponse.isExists() returns false.
|
||||
*/
|
||||
@Nullable
|
||||
public static Document from(GetResponse getResponse) {
|
||||
|
||||
Assert.notNull(getResponse, "GetResponse must not be null");
|
||||
|
||||
if (!getResponse.isExists()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (getResponse.isSourceEmpty()) {
|
||||
return fromDocumentFields(getResponse, getResponse.getIndex(), getResponse.getId(), getResponse.getVersion(),
|
||||
getResponse.getSeqNo(), getResponse.getPrimaryTerm());
|
||||
}
|
||||
|
||||
Document document = Document.from(getResponse.getSourceAsMap());
|
||||
document.setIndex(getResponse.getIndex());
|
||||
document.setId(getResponse.getId());
|
||||
document.setVersion(getResponse.getVersion());
|
||||
document.setSeqNo(getResponse.getSeqNo());
|
||||
document.setPrimaryTerm(getResponse.getPrimaryTerm());
|
||||
|
||||
return document;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a {@link Document} from {@link GetResult}.
|
||||
* <p>
|
||||
* Returns a {@link Document} using the source if available.
|
||||
*
|
||||
* @param source the source {@link GetResult}.
|
||||
* @return the adapted {@link Document}, null if source.isExists() returns false.
|
||||
*/
|
||||
@Nullable
|
||||
public static Document from(GetResult source) {
|
||||
|
||||
Assert.notNull(source, "GetResult must not be null");
|
||||
|
||||
if (!source.isExists()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (source.isSourceEmpty()) {
|
||||
return fromDocumentFields(source, source.getIndex(), source.getId(), source.getVersion(), source.getSeqNo(),
|
||||
source.getPrimaryTerm());
|
||||
}
|
||||
|
||||
Document document = Document.from(source.getSource());
|
||||
document.setIndex(source.getIndex());
|
||||
document.setId(source.getId());
|
||||
document.setVersion(source.getVersion());
|
||||
document.setSeqNo(source.getSeqNo());
|
||||
document.setPrimaryTerm(source.getPrimaryTerm());
|
||||
|
||||
return document;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a List of {@link MultiGetItem<Document>}s from {@link MultiGetResponse}.
|
||||
*
|
||||
* @param source the source {@link MultiGetResponse}, not {@literal null}.
|
||||
* @return a list of Documents, contains null values for not found Documents.
|
||||
*/
|
||||
public static List<MultiGetItem<Document>> from(MultiGetResponse source) {
|
||||
|
||||
Assert.notNull(source, "MultiGetResponse must not be null");
|
||||
|
||||
return Arrays.stream(source.getResponses()) //
|
||||
.map(DocumentAdapters::from) //
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a {@link MultiGetItem<Document>} from a {@link MultiGetItemResponse}.
|
||||
*
|
||||
* @param itemResponse the response, must not be {@literal null}
|
||||
* @return the MultiGetItem
|
||||
*/
|
||||
public static MultiGetItem<Document> from(MultiGetItemResponse itemResponse) {
|
||||
|
||||
MultiGetItem.Failure failure = ResponseConverter.getFailure(itemResponse);
|
||||
return MultiGetItem.of(itemResponse.isFailed() ? null : DocumentAdapters.from(itemResponse.getResponse()), failure);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a {@link SearchDocument} from {@link SearchHit}.
|
||||
* <p>
|
||||
* Returns a {@link SearchDocument} using the source if available.
|
||||
*
|
||||
* @param source the source {@link SearchHit}.
|
||||
* @return the adapted {@link SearchDocument}.
|
||||
*/
|
||||
public static SearchDocument from(SearchHit source) {
|
||||
|
||||
Assert.notNull(source, "SearchHit must not be null");
|
||||
|
||||
Map<String, List<String>> highlightFields = new HashMap<>(source.getHighlightFields().entrySet().stream() //
|
||||
.collect(Collectors.toMap(Map.Entry::getKey,
|
||||
entry -> Arrays.stream(entry.getValue().getFragments()).map(Text::string).collect(Collectors.toList()))));
|
||||
|
||||
Map<String, SearchDocumentResponse> innerHits = new LinkedHashMap<>();
|
||||
Map<String, SearchHits> sourceInnerHits = source.getInnerHits();
|
||||
|
||||
if (sourceInnerHits != null) {
|
||||
sourceInnerHits.forEach((name, searchHits) -> innerHits.put(name, SearchDocumentResponseBuilder.from(searchHits,
|
||||
null, null, null, searchDocument -> CompletableFuture.completedFuture(null))));
|
||||
}
|
||||
|
||||
NestedMetaData nestedMetaData = from(source.getNestedIdentity());
|
||||
Explanation explanation = from(source.getExplanation());
|
||||
List<String> matchedQueries = from(source.getMatchedQueries());
|
||||
|
||||
BytesReference sourceRef = source.getSourceRef();
|
||||
Map<String, DocumentField> sourceFields = source.getFields();
|
||||
|
||||
if (sourceRef == null || sourceRef.length() == 0) {
|
||||
return new SearchDocumentAdapter(
|
||||
fromDocumentFields(source, source.getIndex(), source.getId(), source.getVersion(), source.getSeqNo(),
|
||||
source.getPrimaryTerm()),
|
||||
source.getScore(), source.getSortValues(), sourceFields, highlightFields, innerHits, nestedMetaData,
|
||||
explanation, matchedQueries);
|
||||
}
|
||||
|
||||
Document document = Document.from(source.getSourceAsMap());
|
||||
document.setIndex(source.getIndex());
|
||||
document.setId(source.getId());
|
||||
|
||||
if (source.getVersion() >= 0) {
|
||||
document.setVersion(source.getVersion());
|
||||
}
|
||||
document.setSeqNo(source.getSeqNo());
|
||||
document.setPrimaryTerm(source.getPrimaryTerm());
|
||||
|
||||
return new SearchDocumentAdapter(document, source.getScore(), source.getSortValues(), sourceFields, highlightFields,
|
||||
innerHits, nestedMetaData, explanation, matchedQueries);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static Explanation from(@Nullable org.apache.lucene.search.Explanation explanation) {
|
||||
|
||||
if (explanation == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
List<Explanation> details = new ArrayList<>();
|
||||
for (org.apache.lucene.search.Explanation detail : explanation.getDetails()) {
|
||||
details.add(from(detail));
|
||||
}
|
||||
|
||||
return new Explanation(explanation.isMatch(), explanation.getValue().doubleValue(), explanation.getDescription(),
|
||||
details);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static NestedMetaData from(@Nullable SearchHit.NestedIdentity nestedIdentity) {
|
||||
|
||||
if (nestedIdentity == null) {
|
||||
return null;
|
||||
}
|
||||
NestedMetaData child = from(nestedIdentity.getChild());
|
||||
return NestedMetaData.of(nestedIdentity.getField().string(), nestedIdentity.getOffset(), child);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static List<String> from(@Nullable String[] matchedQueries) {
|
||||
return matchedQueries == null ? null : Arrays.asList(matchedQueries);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an unmodifiable {@link Document} from {@link Iterable} of {@link DocumentField}s.
|
||||
*
|
||||
* @param documentFields the {@link DocumentField}s backing the {@link Document}.
|
||||
* @param index the index where the Document was found
|
||||
* @param id the document id
|
||||
* @param version the document version
|
||||
* @param seqNo the seqNo if the document
|
||||
* @param primaryTerm the primaryTerm of the document
|
||||
* @return the adapted {@link Document}.
|
||||
*/
|
||||
public static Document fromDocumentFields(Iterable<DocumentField> documentFields, String index, String id,
|
||||
long version, long seqNo, long primaryTerm) {
|
||||
|
||||
if (documentFields instanceof Collection) {
|
||||
return new DocumentFieldAdapter((Collection<DocumentField>) documentFields, index, id, version, seqNo,
|
||||
primaryTerm);
|
||||
}
|
||||
|
||||
List<DocumentField> fields = new ArrayList<>();
|
||||
for (DocumentField documentField : documentFields) {
|
||||
fields.add(documentField);
|
||||
}
|
||||
|
||||
return new DocumentFieldAdapter(fields, index, id, version, seqNo, primaryTerm);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adapter for a collection of {@link DocumentField}s.
|
||||
*/
|
||||
static class DocumentFieldAdapter implements Document {
|
||||
|
||||
private final Collection<DocumentField> documentFields;
|
||||
private final Map<String, DocumentField> documentFieldMap;
|
||||
private final String index;
|
||||
private final String id;
|
||||
private final long version;
|
||||
private final long seqNo;
|
||||
private final long primaryTerm;
|
||||
|
||||
DocumentFieldAdapter(Collection<DocumentField> documentFields, String index, String id, long version, long seqNo,
|
||||
long primaryTerm) {
|
||||
this.documentFields = documentFields;
|
||||
this.documentFieldMap = new LinkedHashMap<>(documentFields.size());
|
||||
documentFields.forEach(documentField -> documentFieldMap.put(documentField.getName(), documentField));
|
||||
this.index = index;
|
||||
this.id = id;
|
||||
this.version = version;
|
||||
this.seqNo = seqNo;
|
||||
this.primaryTerm = primaryTerm;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getIndex() {
|
||||
return index;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasId() {
|
||||
return StringUtils.hasLength(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasVersion() {
|
||||
return this.version >= 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getVersion() {
|
||||
|
||||
if (!hasVersion()) {
|
||||
throw new IllegalStateException("No version associated with this Document");
|
||||
}
|
||||
|
||||
return this.version;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasSeqNo() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getSeqNo() {
|
||||
|
||||
if (!hasSeqNo()) {
|
||||
throw new IllegalStateException("No seq_no associated with this Document");
|
||||
}
|
||||
|
||||
return this.seqNo;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPrimaryTerm() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getPrimaryTerm() {
|
||||
|
||||
if (!hasPrimaryTerm()) {
|
||||
throw new IllegalStateException("No primary_term associated with this Document");
|
||||
}
|
||||
|
||||
return this.primaryTerm;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return documentFields.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return documentFields.isEmpty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsKey(Object key) {
|
||||
return documentFieldMap.containsKey(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsValue(Object value) {
|
||||
|
||||
for (DocumentField documentField : documentFields) {
|
||||
|
||||
Object fieldValue = getValue(documentField);
|
||||
if (fieldValue != null && fieldValue.equals(value) || value == fieldValue) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public Object get(Object key) {
|
||||
|
||||
DocumentField documentField = documentFieldMap.get(key);
|
||||
return documentField != null ? documentField.getValue() : null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object put(String key, Object value) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object remove(Object key) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putAll(Map<? extends String, ?> m) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> keySet() {
|
||||
return documentFieldMap.keySet();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<Object> values() {
|
||||
return documentFieldMap.values().stream().map(DocumentFieldAdapter::getValue).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<Entry<String, Object>> entrySet() {
|
||||
return documentFieldMap.entrySet().stream()
|
||||
.collect(Collectors.toMap(Entry::getKey, entry -> DocumentFieldAdapter.getValue(entry.getValue())))
|
||||
.entrySet();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void forEach(BiConsumer<? super String, ? super Object> action) {
|
||||
|
||||
Objects.requireNonNull(action);
|
||||
|
||||
documentFields.forEach(field -> action.accept(field.getName(), getValue(field)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toJson() {
|
||||
|
||||
JsonFactory nodeFactory = new JsonFactory();
|
||||
try {
|
||||
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
||||
JsonGenerator generator = nodeFactory.createGenerator(stream, JsonEncoding.UTF8);
|
||||
generator.writeStartObject();
|
||||
for (DocumentField value : documentFields) {
|
||||
if (value.getValues().size() > 1) {
|
||||
generator.writeArrayFieldStart(value.getName());
|
||||
for (Object val : value.getValues()) {
|
||||
generator.writeObject(val);
|
||||
}
|
||||
generator.writeEndArray();
|
||||
} else {
|
||||
generator.writeObjectField(value.getName(), value.getValue());
|
||||
}
|
||||
}
|
||||
generator.writeEndObject();
|
||||
generator.flush();
|
||||
return new String(stream.toByteArray(), StandardCharsets.UTF_8);
|
||||
} catch (IOException e) {
|
||||
throw new MappingException("Cannot render JSON", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return getClass().getSimpleName() + '@' + this.id + '#' + this.version + ' ' + toJson();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static Object getValue(DocumentField documentField) {
|
||||
|
||||
if (documentField.getValues().isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (documentField.getValues().size() == 1) {
|
||||
return documentField.getValue();
|
||||
}
|
||||
|
||||
return documentField.getValues();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adapter for a {@link SearchDocument}.
|
||||
*/
|
||||
static class SearchDocumentAdapter implements SearchDocument {
|
||||
|
||||
private final float score;
|
||||
private final Object[] sortValues;
|
||||
private final Map<String, List<Object>> fields = new HashMap<>();
|
||||
private final Document delegate;
|
||||
private final Map<String, List<String>> highlightFields = new HashMap<>();
|
||||
private final Map<String, SearchDocumentResponse> innerHits = new HashMap<>();
|
||||
@Nullable private final NestedMetaData nestedMetaData;
|
||||
@Nullable private final Explanation explanation;
|
||||
@Nullable private final List<String> matchedQueries;
|
||||
|
||||
SearchDocumentAdapter(Document delegate, float score, Object[] sortValues, Map<String, DocumentField> fields,
|
||||
Map<String, List<String>> highlightFields, Map<String, SearchDocumentResponse> innerHits,
|
||||
@Nullable NestedMetaData nestedMetaData, @Nullable Explanation explanation,
|
||||
@Nullable List<String> matchedQueries) {
|
||||
|
||||
this.delegate = delegate;
|
||||
this.score = score;
|
||||
this.sortValues = sortValues;
|
||||
fields.forEach((name, documentField) -> this.fields.put(name, documentField.getValues()));
|
||||
this.highlightFields.putAll(highlightFields);
|
||||
this.innerHits.putAll(innerHits);
|
||||
this.nestedMetaData = nestedMetaData;
|
||||
this.explanation = explanation;
|
||||
this.matchedQueries = matchedQueries;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SearchDocument append(String key, Object value) {
|
||||
delegate.append(key, value);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getScore() {
|
||||
return score;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, List<Object>> getFields() {
|
||||
return fields;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object[] getSortValues() {
|
||||
return sortValues;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, List<String>> getHighlightFields() {
|
||||
return highlightFields;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getIndex() {
|
||||
return delegate.getIndex();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasId() {
|
||||
return delegate.hasId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getId() {
|
||||
return delegate.getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setId(String id) {
|
||||
delegate.setId(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasVersion() {
|
||||
return delegate.hasVersion();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getVersion() {
|
||||
return delegate.getVersion();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setVersion(long version) {
|
||||
delegate.setVersion(version);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasSeqNo() {
|
||||
return delegate.hasSeqNo();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getSeqNo() {
|
||||
return delegate.getSeqNo();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSeqNo(long seqNo) {
|
||||
delegate.setSeqNo(seqNo);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPrimaryTerm() {
|
||||
return delegate.hasPrimaryTerm();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getPrimaryTerm() {
|
||||
return delegate.getPrimaryTerm();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPrimaryTerm(long primaryTerm) {
|
||||
delegate.setPrimaryTerm(primaryTerm);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, SearchDocumentResponse> getInnerHits() {
|
||||
return innerHits;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public NestedMetaData getNestedMetaData() {
|
||||
return nestedMetaData;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public <T> T get(Object key, Class<T> type) {
|
||||
return delegate.get(key, type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toJson() {
|
||||
return delegate.toJson();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return delegate.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return delegate.isEmpty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsKey(Object key) {
|
||||
return delegate.containsKey(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsValue(Object value) {
|
||||
return delegate.containsValue(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object get(Object key) {
|
||||
|
||||
if (delegate.containsKey(key)) {
|
||||
return delegate.get(key);
|
||||
}
|
||||
|
||||
// fallback to fields
|
||||
return fields.get(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object put(String key, Object value) {
|
||||
return delegate.put(key, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object remove(Object key) {
|
||||
return delegate.remove(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putAll(Map<? extends String, ?> m) {
|
||||
delegate.putAll(m);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
delegate.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> keySet() {
|
||||
return delegate.keySet();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<Object> values() {
|
||||
return delegate.values();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<Entry<String, Object>> entrySet() {
|
||||
return delegate.entrySet();
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public Explanation getExplanation() {
|
||||
return explanation;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public List<String> getMatchedQueries() {
|
||||
return matchedQueries;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if (!(o instanceof SearchDocumentAdapter that)) {
|
||||
return false;
|
||||
}
|
||||
return Float.compare(that.score, score) == 0 && delegate.equals(that.delegate);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return delegate.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void forEach(BiConsumer<? super String, ? super Object> action) {
|
||||
delegate.forEach(action);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean remove(Object key, Object value) {
|
||||
return delegate.remove(key, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
|
||||
String id = hasId() ? getId() : "?";
|
||||
String version = hasVersion() ? Long.toString(getVersion()) : "?";
|
||||
|
||||
return getClass().getSimpleName() + '@' + id + '#' + version + ' ' + toJson();
|
||||
}
|
||||
}
|
||||
}
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Copyright 2021-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.elasticsearch.client.erhlc;
|
||||
|
||||
import org.elasticsearch.search.aggregations.Aggregation;
|
||||
import org.springframework.data.elasticsearch.core.AggregationContainer;
|
||||
import org.springframework.lang.NonNull;
|
||||
|
||||
/**
|
||||
* AggregationContainer implementation for an Elasticsearch7 aggregation.
|
||||
*
|
||||
* @author Peter-Josef Meisch
|
||||
* @since 4.3
|
||||
* @deprecated since 5.0
|
||||
*/
|
||||
@Deprecated
|
||||
public class ElasticsearchAggregation implements AggregationContainer<Aggregation> {
|
||||
|
||||
private final Aggregation aggregation;
|
||||
|
||||
public ElasticsearchAggregation(Aggregation aggregation) {
|
||||
this.aggregation = aggregation;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public Aggregation aggregation() {
|
||||
return aggregation;
|
||||
}
|
||||
}
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Copyright 2021-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.elasticsearch.client.erhlc;
|
||||
|
||||
import org.elasticsearch.search.aggregations.Aggregations;
|
||||
import org.springframework.data.elasticsearch.core.AggregationsContainer;
|
||||
import org.springframework.lang.NonNull;
|
||||
|
||||
/**
|
||||
* AggregationsContainer implementation for the Elasticsearch7 aggregations.
|
||||
*
|
||||
* @author Peter-Josef Meisch
|
||||
* @since 4.3
|
||||
* @deprecated since 5.0
|
||||
*/
|
||||
@Deprecated
|
||||
public class ElasticsearchAggregations implements AggregationsContainer<Aggregations> {
|
||||
|
||||
private final Aggregations aggregations;
|
||||
|
||||
public ElasticsearchAggregations(Aggregations aggregations) {
|
||||
this.aggregations = aggregations;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public Aggregations aggregations() {
|
||||
return aggregations;
|
||||
}
|
||||
}
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Copyright 2021-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.elasticsearch.client.erhlc;
|
||||
|
||||
import org.springframework.data.elasticsearch.core.cluster.ClusterOperations;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* @author Peter-Josef Meisch
|
||||
* @since 4.4
|
||||
* @deprecated since 5.0
|
||||
*/
|
||||
@Deprecated
|
||||
public class ElasticsearchClusterOperations {
|
||||
/**
|
||||
* Creates a ClusterOperations for a {@link ElasticsearchRestTemplate}.
|
||||
*
|
||||
* @param template the template, must not be {@literal null}
|
||||
* @return ClusterOperations
|
||||
*/
|
||||
public static ClusterOperations forTemplate(ElasticsearchRestTemplate template) {
|
||||
|
||||
Assert.notNull(template, "template must not be null");
|
||||
|
||||
return new DefaultClusterOperations(template);
|
||||
}
|
||||
|
||||
}
|
||||
+144
@@ -0,0 +1,144 @@
|
||||
/*
|
||||
* Copyright 2018-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.elasticsearch.client.erhlc;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import org.elasticsearch.ElasticsearchException;
|
||||
import org.elasticsearch.ElasticsearchStatusException;
|
||||
import org.elasticsearch.common.ValidationException;
|
||||
import org.elasticsearch.index.engine.VersionConflictEngineException;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.dao.DataAccessResourceFailureException;
|
||||
import org.springframework.dao.DataIntegrityViolationException;
|
||||
import org.springframework.dao.OptimisticLockingFailureException;
|
||||
import org.springframework.dao.support.PersistenceExceptionTranslator;
|
||||
import org.springframework.data.elasticsearch.NoSuchIndexException;
|
||||
import org.springframework.data.elasticsearch.RestStatusException;
|
||||
import org.springframework.data.elasticsearch.UncategorizedElasticsearchException;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Simple {@link PersistenceExceptionTranslator} for Elasticsearch. Convert the given runtime exception to an
|
||||
* appropriate exception from the {@code org.springframework.dao} hierarchy. Return {@literal null} if no translation is
|
||||
* appropriate: any other exception may have resulted from user code, and should not be translated.
|
||||
*
|
||||
* @author Christoph Strobl
|
||||
* @author Peter-Josef Meisch
|
||||
* @author Roman Puchkovskiy
|
||||
* @author Mark Paluch
|
||||
* @since 3.2
|
||||
* @deprecated since 5.0
|
||||
*/
|
||||
@Deprecated
|
||||
public class ElasticsearchExceptionTranslator implements PersistenceExceptionTranslator {
|
||||
|
||||
@Override
|
||||
public DataAccessException translateExceptionIfPossible(RuntimeException ex) {
|
||||
|
||||
if (isSeqNoConflict(ex)) {
|
||||
return new OptimisticLockingFailureException("Cannot index a document due to seq_no+primary_term conflict", ex);
|
||||
}
|
||||
|
||||
if (ex instanceof ElasticsearchException elasticsearchException) {
|
||||
|
||||
if (!indexAvailable(elasticsearchException)) {
|
||||
return new NoSuchIndexException(ObjectUtils.nullSafeToString(elasticsearchException.getMetadata("es.index")),
|
||||
ex);
|
||||
}
|
||||
|
||||
if (elasticsearchException instanceof ElasticsearchStatusException elasticsearchStatusException) {
|
||||
return new RestStatusException(elasticsearchStatusException.status().getStatus(),
|
||||
elasticsearchStatusException.getMessage(), elasticsearchStatusException);
|
||||
}
|
||||
|
||||
return new UncategorizedElasticsearchException(ex.getMessage(), ex);
|
||||
}
|
||||
|
||||
if (ex instanceof RestStatusException restStatusException) {
|
||||
Throwable cause = restStatusException.getCause();
|
||||
if (cause instanceof ElasticsearchException elasticsearchException) {
|
||||
|
||||
if (!indexAvailable(elasticsearchException)) {
|
||||
return new NoSuchIndexException(ObjectUtils.nullSafeToString(elasticsearchException.getMetadata("es.index")),
|
||||
ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (ex instanceof ValidationException) {
|
||||
return new DataIntegrityViolationException(ex.getMessage(), ex);
|
||||
}
|
||||
|
||||
Throwable cause = ex.getCause();
|
||||
if (cause instanceof IOException) {
|
||||
return new DataAccessResourceFailureException(ex.getMessage(), ex);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private boolean isSeqNoConflict(Exception exception) {
|
||||
|
||||
Integer status = null;
|
||||
String message = null;
|
||||
|
||||
if (exception instanceof ElasticsearchStatusException statusException) {
|
||||
|
||||
status = statusException.status().getStatus();
|
||||
message = statusException.getMessage();
|
||||
}
|
||||
|
||||
if (exception instanceof RestStatusException statusException) {
|
||||
|
||||
status = statusException.getStatus();
|
||||
message = statusException.getMessage();
|
||||
}
|
||||
|
||||
if (status != null && message != null) {
|
||||
return status == 409 && message.contains("type=version_conflict_engine_exception")
|
||||
&& message.contains("version conflict, required seqNo");
|
||||
}
|
||||
|
||||
if (exception instanceof VersionConflictEngineException versionConflictEngineException) {
|
||||
|
||||
return versionConflictEngineException.getMessage() != null
|
||||
&& versionConflictEngineException.getMessage().contains("version conflict, required seqNo");
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean indexAvailable(ElasticsearchException ex) {
|
||||
|
||||
List<String> metadata = ex.getMetadata("es.index_uuid");
|
||||
if (metadata == null) {
|
||||
|
||||
if (ex.getCause() instanceof ElasticsearchException) {
|
||||
return indexAvailable((ElasticsearchException) ex.getCause());
|
||||
}
|
||||
|
||||
if (ex instanceof ElasticsearchStatusException) {
|
||||
return StringUtils.hasText(ObjectUtils.nullSafeToString(ex.getIndex()));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return !CollectionUtils.contains(metadata.iterator(), "_na_");
|
||||
}
|
||||
}
|
||||
+698
@@ -0,0 +1,698 @@
|
||||
/*
|
||||
* Copyright 2013-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.elasticsearch.client.erhlc;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.elasticsearch.Version;
|
||||
import org.elasticsearch.action.DocWriteResponse;
|
||||
import org.elasticsearch.action.bulk.BulkItemResponse;
|
||||
import org.elasticsearch.action.bulk.BulkRequest;
|
||||
import org.elasticsearch.action.bulk.BulkResponse;
|
||||
import org.elasticsearch.action.delete.DeleteRequest;
|
||||
import org.elasticsearch.action.get.GetRequest;
|
||||
import org.elasticsearch.action.get.GetResponse;
|
||||
import org.elasticsearch.action.get.MultiGetRequest;
|
||||
import org.elasticsearch.action.get.MultiGetResponse;
|
||||
import org.elasticsearch.action.index.IndexRequest;
|
||||
import org.elasticsearch.action.index.IndexResponse;
|
||||
import org.elasticsearch.action.search.ClearScrollRequest;
|
||||
import org.elasticsearch.action.search.MultiSearchRequest;
|
||||
import org.elasticsearch.action.search.MultiSearchResponse;
|
||||
import org.elasticsearch.action.search.SearchRequest;
|
||||
import org.elasticsearch.action.search.SearchResponse;
|
||||
import org.elasticsearch.action.search.SearchScrollRequest;
|
||||
import org.elasticsearch.action.support.WriteRequest;
|
||||
import org.elasticsearch.action.update.UpdateRequest;
|
||||
import org.elasticsearch.client.RequestOptions;
|
||||
import org.elasticsearch.client.RestHighLevelClient;
|
||||
import org.elasticsearch.core.TimeValue;
|
||||
import org.elasticsearch.index.query.MoreLikeThisQueryBuilder;
|
||||
import org.elasticsearch.index.query.QueryBuilders;
|
||||
import org.elasticsearch.index.reindex.BulkByScrollResponse;
|
||||
import org.elasticsearch.index.reindex.DeleteByQueryRequest;
|
||||
import org.elasticsearch.index.reindex.UpdateByQueryRequest;
|
||||
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
|
||||
import org.elasticsearch.search.suggest.SuggestBuilder;
|
||||
import org.springframework.data.elasticsearch.BulkFailureException;
|
||||
import org.springframework.data.elasticsearch.core.AbstractElasticsearchTemplate;
|
||||
import org.springframework.data.elasticsearch.core.IndexOperations;
|
||||
import org.springframework.data.elasticsearch.core.IndexedObjectInformation;
|
||||
import org.springframework.data.elasticsearch.core.MultiGetItem;
|
||||
import org.springframework.data.elasticsearch.core.RefreshPolicy;
|
||||
import org.springframework.data.elasticsearch.core.SearchHits;
|
||||
import org.springframework.data.elasticsearch.core.SearchScrollHits;
|
||||
import org.springframework.data.elasticsearch.core.cluster.ClusterOperations;
|
||||
import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter;
|
||||
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
|
||||
import org.springframework.data.elasticsearch.core.query.BulkOptions;
|
||||
import org.springframework.data.elasticsearch.core.query.ByQueryResponse;
|
||||
import org.springframework.data.elasticsearch.core.query.IndexQuery;
|
||||
import org.springframework.data.elasticsearch.core.query.MoreLikeThisQuery;
|
||||
import org.springframework.data.elasticsearch.core.query.Query;
|
||||
import org.springframework.data.elasticsearch.core.query.UpdateQuery;
|
||||
import org.springframework.data.elasticsearch.core.query.UpdateResponse;
|
||||
import org.springframework.data.elasticsearch.core.reindex.ReindexRequest;
|
||||
import org.springframework.data.elasticsearch.core.reindex.ReindexResponse;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* ElasticsearchRestTemplate
|
||||
*
|
||||
* @author Rizwan Idrees
|
||||
* @author Mohsin Husen
|
||||
* @author Artur Konczak
|
||||
* @author Kevin Leturc
|
||||
* @author Mason Chan
|
||||
* @author Young Gu
|
||||
* @author Oliver Gierke
|
||||
* @author Mark Janssen
|
||||
* @author Chris White
|
||||
* @author Mark Paluch
|
||||
* @author Ilkang Na
|
||||
* @author Alen Turkovic
|
||||
* @author Sascha Woo
|
||||
* @author Ted Liang
|
||||
* @author Don Wellington
|
||||
* @author Zetang Zeng
|
||||
* @author Peter Nowak
|
||||
* @author Ivan Greene
|
||||
* @author Christoph Strobl
|
||||
* @author Lorenzo Spinelli
|
||||
* @author Dmitriy Yakovlev
|
||||
* @author Roman Puchkovskiy
|
||||
* @author Martin Choraine
|
||||
* @author Farid Azaza
|
||||
* @author Peter-Josef Meisch
|
||||
* @author Mathias Teier
|
||||
* @author Gyula Attila Csorogi
|
||||
* @author Massimiliano Poggi
|
||||
* @author Farid Faoudi
|
||||
* @author Sijia Liu
|
||||
* @author Hamid Rahimi
|
||||
* @since 4.4
|
||||
* @deprecated since 5.0
|
||||
*/
|
||||
@Deprecated
|
||||
public class ElasticsearchRestTemplate extends AbstractElasticsearchTemplate {
|
||||
|
||||
private static final Log LOGGER = LogFactory.getLog(ElasticsearchRestTemplate.class);
|
||||
|
||||
private final RestHighLevelClient client;
|
||||
private final ElasticsearchExceptionTranslator exceptionTranslator = new ElasticsearchExceptionTranslator();
|
||||
protected RequestFactory requestFactory;
|
||||
|
||||
// region _initialization
|
||||
public ElasticsearchRestTemplate(RestHighLevelClient client) {
|
||||
|
||||
Assert.notNull(client, "Client must not be null!");
|
||||
|
||||
this.client = client;
|
||||
requestFactory = new RequestFactory(this.elasticsearchConverter);
|
||||
|
||||
}
|
||||
|
||||
public ElasticsearchRestTemplate(RestHighLevelClient client, ElasticsearchConverter elasticsearchConverter) {
|
||||
|
||||
super(elasticsearchConverter);
|
||||
|
||||
Assert.notNull(client, "Client must not be null!");
|
||||
|
||||
this.client = client;
|
||||
requestFactory = new RequestFactory(this.elasticsearchConverter);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected AbstractElasticsearchTemplate doCopy() {
|
||||
ElasticsearchRestTemplate copy = new ElasticsearchRestTemplate(client, elasticsearchConverter);
|
||||
copy.requestFactory = this.requestFactory;
|
||||
return copy;
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 4.0
|
||||
*/
|
||||
public RequestFactory getRequestFactory() {
|
||||
return requestFactory;
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
// region IndexOperations
|
||||
@Override
|
||||
public IndexOperations indexOps(Class<?> clazz) {
|
||||
|
||||
Assert.notNull(clazz, "clazz must not be null");
|
||||
|
||||
return new RestIndexTemplate(this, clazz);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IndexOperations indexOps(IndexCoordinates index) {
|
||||
|
||||
Assert.notNull(index, "index must not be null");
|
||||
|
||||
return new RestIndexTemplate(this, index);
|
||||
}
|
||||
// endregion
|
||||
|
||||
// region ClusterOperations
|
||||
@Override
|
||||
public ClusterOperations cluster() {
|
||||
return ElasticsearchClusterOperations.forTemplate(this);
|
||||
}
|
||||
// endregion
|
||||
|
||||
// region DocumentOperations
|
||||
public String doIndex(IndexQuery query, IndexCoordinates index) {
|
||||
|
||||
IndexRequest request = prepareWriteRequest(requestFactory.indexRequest(query, index));
|
||||
IndexResponse indexResponse = execute(client -> client.index(request, RequestOptions.DEFAULT));
|
||||
|
||||
Object queryObject = query.getObject();
|
||||
|
||||
if (queryObject != null) {
|
||||
query.setObject(updateIndexedObject(queryObject, new IndexedObjectInformation( //
|
||||
indexResponse.getId(), //
|
||||
indexResponse.getIndex(), //
|
||||
indexResponse.getSeqNo(), //
|
||||
indexResponse.getPrimaryTerm(), //
|
||||
indexResponse.getVersion())));
|
||||
}
|
||||
|
||||
return indexResponse.getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public <T> T get(String id, Class<T> clazz, IndexCoordinates index) {
|
||||
|
||||
GetRequest request = requestFactory.getRequest(id, routingResolver.getRouting(), index);
|
||||
GetResponse response = execute(client -> client.get(request, RequestOptions.DEFAULT));
|
||||
|
||||
DocumentCallback<T> callback = new ReadDocumentCallback<>(elasticsearchConverter, clazz, index);
|
||||
return callback.doWith(DocumentAdapters.from(response));
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> List<MultiGetItem<T>> multiGet(Query query, Class<T> clazz, IndexCoordinates index) {
|
||||
|
||||
Assert.notNull(index, "index must not be null");
|
||||
|
||||
MultiGetRequest request = requestFactory.multiGetRequest(query, clazz, index);
|
||||
MultiGetResponse result = execute(client -> client.mget(request, RequestOptions.DEFAULT));
|
||||
|
||||
DocumentCallback<T> callback = new ReadDocumentCallback<>(elasticsearchConverter, clazz, index);
|
||||
return DocumentAdapters.from(result).stream() //
|
||||
.map(multiGetItem -> MultiGetItem.of( //
|
||||
multiGetItem.isFailed() ? null : callback.doWith(multiGetItem.getItem()), multiGetItem.getFailure())) //
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean doExists(String id, IndexCoordinates index) {
|
||||
GetRequest request = requestFactory.getRequest(id, routingResolver.getRouting(), index);
|
||||
request.fetchSourceContext(FetchSourceContext.DO_NOT_FETCH_SOURCE);
|
||||
return execute(client -> client.get(request, RequestOptions.DEFAULT).isExists());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void bulkUpdate(List<UpdateQuery> queries, BulkOptions bulkOptions, IndexCoordinates index) {
|
||||
|
||||
Assert.notNull(queries, "List of UpdateQuery must not be null");
|
||||
Assert.notNull(bulkOptions, "BulkOptions must not be null");
|
||||
|
||||
doBulkOperation(queries, bulkOptions, index);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String doDelete(String id, @Nullable String routing, IndexCoordinates index) {
|
||||
|
||||
Assert.notNull(id, "id must not be null");
|
||||
Assert.notNull(index, "index must not be null");
|
||||
|
||||
DeleteRequest request = prepareWriteRequest(
|
||||
requestFactory.deleteRequest(elasticsearchConverter.convertId(id), routing, index));
|
||||
return execute(client -> client.delete(request, RequestOptions.DEFAULT).getId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByQueryResponse delete(Query query, Class<?> clazz, IndexCoordinates index) {
|
||||
DeleteByQueryRequest deleteByQueryRequest = requestFactory.deleteByQueryRequest(query, routingResolver.getRouting(),
|
||||
clazz, index);
|
||||
return ResponseConverter
|
||||
.byQueryResponseOf(execute(client -> client.deleteByQuery(deleteByQueryRequest, RequestOptions.DEFAULT)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public UpdateResponse update(UpdateQuery query, IndexCoordinates index) {
|
||||
UpdateRequest request = requestFactory.updateRequest(query, index);
|
||||
|
||||
if (query.getRefreshPolicy() == null && getRefreshPolicy() != null) {
|
||||
request.setRefreshPolicy(RequestFactory.toElasticsearchRefreshPolicy(getRefreshPolicy()));
|
||||
}
|
||||
|
||||
if (query.getRouting() == null && routingResolver.getRouting() != null) {
|
||||
request.routing(routingResolver.getRouting());
|
||||
}
|
||||
|
||||
UpdateResponse.Result result = UpdateResponse.Result
|
||||
.valueOf(execute(client -> client.update(request, RequestOptions.DEFAULT)).getResult().name());
|
||||
return new UpdateResponse(result);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByQueryResponse updateByQuery(UpdateQuery query, IndexCoordinates index) {
|
||||
|
||||
Assert.notNull(query, "query must not be null");
|
||||
Assert.notNull(index, "index must not be null");
|
||||
|
||||
UpdateByQueryRequest updateByQueryRequest = requestFactory.updateByQueryRequest(query, index);
|
||||
|
||||
if (query.getRefreshPolicy() == null && getRefreshPolicy() != null) {
|
||||
updateByQueryRequest.setRefresh(getRefreshPolicy() == RefreshPolicy.IMMEDIATE);
|
||||
}
|
||||
|
||||
if (query.getRouting() == null && routingResolver.getRouting() != null) {
|
||||
updateByQueryRequest.setRouting(routingResolver.getRouting());
|
||||
}
|
||||
|
||||
final BulkByScrollResponse bulkByScrollResponse = execute(
|
||||
client -> client.updateByQuery(updateByQueryRequest, RequestOptions.DEFAULT));
|
||||
return ResponseConverter.byQueryResponseOf(bulkByScrollResponse);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReindexResponse reindex(ReindexRequest reindexRequest) {
|
||||
|
||||
Assert.notNull(reindexRequest, "reindexRequest must not be null");
|
||||
|
||||
org.elasticsearch.index.reindex.ReindexRequest reindexRequestES = requestFactory.reindexRequest(reindexRequest);
|
||||
BulkByScrollResponse bulkByScrollResponse = execute(
|
||||
client -> client.reindex(reindexRequestES, RequestOptions.DEFAULT));
|
||||
return ResponseConverter.reindexResponseOf(bulkByScrollResponse);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String submitReindex(ReindexRequest reindexRequest) {
|
||||
|
||||
Assert.notNull(reindexRequest, "reindexRequest must not be null");
|
||||
|
||||
org.elasticsearch.index.reindex.ReindexRequest reindexRequestES = requestFactory.reindexRequest(reindexRequest);
|
||||
return execute(client -> client.submitReindexTask(reindexRequestES, RequestOptions.DEFAULT).getTask());
|
||||
}
|
||||
|
||||
public List<IndexedObjectInformation> doBulkOperation(List<?> queries, BulkOptions bulkOptions,
|
||||
IndexCoordinates index) {
|
||||
BulkRequest bulkRequest = prepareWriteRequest(requestFactory.bulkRequest(queries, bulkOptions, index));
|
||||
List<IndexedObjectInformation> indexedObjectInformationList = checkForBulkOperationFailure(
|
||||
execute(client -> client.bulk(bulkRequest, RequestOptions.DEFAULT)));
|
||||
updateIndexedObjectsWithQueries(queries, indexedObjectInformationList);
|
||||
return indexedObjectInformationList;
|
||||
}
|
||||
|
||||
/**
|
||||
* Preprocess the write request before it is sent to the server, e.g. by setting the
|
||||
* {@link WriteRequest#setRefreshPolicy(String) refresh policy} if applicable.
|
||||
*
|
||||
* @param request must not be {@literal null}.
|
||||
* @param <R> the request type
|
||||
* @return the processed {@link WriteRequest}.
|
||||
*/
|
||||
protected <R extends WriteRequest<R>> R prepareWriteRequest(R request) {
|
||||
|
||||
if (refreshPolicy == null) {
|
||||
return request;
|
||||
}
|
||||
|
||||
return request.setRefreshPolicy(RequestFactory.toElasticsearchRefreshPolicy(refreshPolicy));
|
||||
}
|
||||
|
||||
/**
|
||||
* extract the list of {@link IndexedObjectInformation} from a {@link BulkResponse}.
|
||||
*
|
||||
* @param bulkResponse the response to evaluate
|
||||
* @return the list of the {@link IndexedObjectInformation}s
|
||||
*/
|
||||
protected List<IndexedObjectInformation> checkForBulkOperationFailure(BulkResponse bulkResponse) {
|
||||
|
||||
if (bulkResponse.hasFailures()) {
|
||||
Map<String, String> failedDocuments = new HashMap<>();
|
||||
for (BulkItemResponse item : bulkResponse.getItems()) {
|
||||
|
||||
if (item.isFailed())
|
||||
failedDocuments.put(item.getId(), item.getFailureMessage());
|
||||
}
|
||||
throw new BulkFailureException(
|
||||
"Bulk operation has failures. Use BulkFailureException.getFailedDocuments() for detailed messages ["
|
||||
+ failedDocuments + ']',
|
||||
failedDocuments);
|
||||
}
|
||||
|
||||
return Stream.of(bulkResponse.getItems()).map(bulkItemResponse -> {
|
||||
DocWriteResponse response = bulkItemResponse.getResponse();
|
||||
if (response != null) {
|
||||
return new IndexedObjectInformation( //
|
||||
response.getId(), //
|
||||
response.getIndex(), //
|
||||
response.getSeqNo(), //
|
||||
response.getPrimaryTerm(), //
|
||||
response.getVersion());
|
||||
} else {
|
||||
return new IndexedObjectInformation(bulkItemResponse.getId(), bulkItemResponse.getIndex(), null, null, null);
|
||||
}
|
||||
|
||||
}).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
// region SearchOperations
|
||||
@Override
|
||||
public long count(Query query, @Nullable Class<?> clazz, IndexCoordinates index) {
|
||||
|
||||
Assert.notNull(query, "query must not be null");
|
||||
Assert.notNull(index, "index must not be null");
|
||||
|
||||
final Boolean trackTotalHits = query.getTrackTotalHits();
|
||||
query.setTrackTotalHits(true);
|
||||
SearchRequest searchRequest = requestFactory.searchRequest(query, routingResolver.getRouting(), clazz, index);
|
||||
query.setTrackTotalHits(trackTotalHits);
|
||||
|
||||
searchRequest.source().size(0);
|
||||
|
||||
return SearchHitsUtil
|
||||
.getTotalCount(execute(client -> client.search(searchRequest, RequestOptions.DEFAULT).getHits()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> SearchHits<T> search(Query query, Class<T> clazz, IndexCoordinates index) {
|
||||
SearchRequest searchRequest = requestFactory.searchRequest(query, routingResolver.getRouting(), clazz, index);
|
||||
SearchResponse response = execute(client -> client.search(searchRequest, RequestOptions.DEFAULT));
|
||||
|
||||
ReadDocumentCallback<T> documentCallback = new ReadDocumentCallback<>(elasticsearchConverter, clazz, index);
|
||||
SearchDocumentResponseCallback<SearchHits<T>> callback = new ReadSearchDocumentResponseCallback<>(clazz, index);
|
||||
|
||||
return callback.doWith(SearchDocumentResponseBuilder.from(response, getEntityCreator(documentCallback)));
|
||||
}
|
||||
|
||||
protected <T> SearchHits<T> doSearch(MoreLikeThisQuery query, Class<T> clazz, IndexCoordinates index) {
|
||||
MoreLikeThisQueryBuilder moreLikeThisQueryBuilder = requestFactory.moreLikeThisQueryBuilder(query, index);
|
||||
return search(
|
||||
new NativeSearchQueryBuilder().withQuery(moreLikeThisQueryBuilder).withPageable(query.getPageable()).build(),
|
||||
clazz, index);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> SearchScrollHits<T> searchScrollStart(long scrollTimeInMillis, Query query, Class<T> clazz,
|
||||
IndexCoordinates index) {
|
||||
|
||||
Assert.notNull(query.getPageable(), "pageable of query must not be null.");
|
||||
|
||||
SearchRequest searchRequest = requestFactory.searchRequest(query, routingResolver.getRouting(), clazz, index);
|
||||
searchRequest.scroll(TimeValue.timeValueMillis(scrollTimeInMillis));
|
||||
|
||||
SearchResponse response = execute(client -> client.search(searchRequest, RequestOptions.DEFAULT));
|
||||
|
||||
ReadDocumentCallback<T> documentCallback = new ReadDocumentCallback<>(elasticsearchConverter, clazz, index);
|
||||
SearchDocumentResponseCallback<SearchScrollHits<T>> callback = new ReadSearchScrollDocumentResponseCallback<>(clazz,
|
||||
index);
|
||||
return callback.doWith(SearchDocumentResponseBuilder.from(response, getEntityCreator(documentCallback)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> SearchScrollHits<T> searchScrollContinue(String scrollId, long scrollTimeInMillis, Class<T> clazz,
|
||||
IndexCoordinates index) {
|
||||
|
||||
SearchScrollRequest request = new SearchScrollRequest(scrollId);
|
||||
request.scroll(TimeValue.timeValueMillis(scrollTimeInMillis));
|
||||
|
||||
SearchResponse response = execute(client -> client.scroll(request, RequestOptions.DEFAULT));
|
||||
|
||||
ReadDocumentCallback<T> documentCallback = new ReadDocumentCallback<>(elasticsearchConverter, clazz, index);
|
||||
SearchDocumentResponseCallback<SearchScrollHits<T>> callback = new ReadSearchScrollDocumentResponseCallback<>(clazz,
|
||||
index);
|
||||
return callback.doWith(SearchDocumentResponseBuilder.from(response, getEntityCreator(documentCallback)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void searchScrollClear(List<String> scrollIds) {
|
||||
try {
|
||||
ClearScrollRequest request = new ClearScrollRequest();
|
||||
request.scrollIds(scrollIds);
|
||||
execute(client -> client.clearScroll(request, RequestOptions.DEFAULT));
|
||||
} catch (Exception e) {
|
||||
LOGGER.warn(String.format("Could not clear scroll: %s", e.getMessage()));
|
||||
}
|
||||
}
|
||||
|
||||
public SearchResponse suggest(SuggestBuilder suggestion, IndexCoordinates index) {
|
||||
SearchRequest searchRequest = requestFactory.searchRequest(suggestion, index);
|
||||
return execute(client -> client.search(searchRequest, RequestOptions.DEFAULT));
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> List<SearchHits<T>> multiSearch(List<? extends Query> queries, Class<T> clazz, IndexCoordinates index) {
|
||||
MultiSearchRequest request = new MultiSearchRequest();
|
||||
for (Query query : queries) {
|
||||
request.add(requestFactory.searchRequest(query, routingResolver.getRouting(), clazz, index));
|
||||
}
|
||||
|
||||
MultiSearchResponse.Item[] items = getMultiSearchResult(request);
|
||||
|
||||
ReadDocumentCallback<T> documentCallback = new ReadDocumentCallback<>(elasticsearchConverter, clazz, index);
|
||||
SearchDocumentResponseCallback<SearchHits<T>> callback = new ReadSearchDocumentResponseCallback<>(clazz, index);
|
||||
List<SearchHits<T>> res = new ArrayList<>(queries.size());
|
||||
for (int i = 0; i < queries.size(); i++) {
|
||||
res.add(callback
|
||||
.doWith(SearchDocumentResponseBuilder.from(items[i].getResponse(), getEntityCreator(documentCallback))));
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
@Override
|
||||
public List<SearchHits<?>> multiSearch(List<? extends Query> queries, List<Class<?>> classes) {
|
||||
|
||||
Assert.notNull(queries, "queries must not be null");
|
||||
Assert.notNull(classes, "classes must not be null");
|
||||
Assert.isTrue(queries.size() == classes.size(), "queries and classes must have the same size");
|
||||
|
||||
MultiSearchRequest request = new MultiSearchRequest();
|
||||
Iterator<Class<?>> it = classes.iterator();
|
||||
for (Query query : queries) {
|
||||
Class<?> clazz = it.next();
|
||||
request
|
||||
.add(requestFactory.searchRequest(query, routingResolver.getRouting(), clazz, getIndexCoordinatesFor(clazz)));
|
||||
}
|
||||
|
||||
MultiSearchResponse.Item[] items = getMultiSearchResult(request);
|
||||
|
||||
List<SearchHits<?>> res = new ArrayList<>(queries.size());
|
||||
Iterator<Class<?>> it1 = classes.iterator();
|
||||
for (int i = 0; i < queries.size(); i++) {
|
||||
Class entityClass = it1.next();
|
||||
|
||||
IndexCoordinates index = getIndexCoordinatesFor(entityClass);
|
||||
ReadDocumentCallback<?> documentCallback = new ReadDocumentCallback<>(elasticsearchConverter, entityClass, index);
|
||||
SearchDocumentResponseCallback<SearchHits<?>> callback = new ReadSearchDocumentResponseCallback<>(entityClass,
|
||||
index);
|
||||
|
||||
SearchResponse response = items[i].getResponse();
|
||||
res.add(callback.doWith(SearchDocumentResponseBuilder.from(response, getEntityCreator(documentCallback))));
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
@Override
|
||||
public List<SearchHits<?>> multiSearch(List<? extends Query> queries, List<Class<?>> classes,
|
||||
IndexCoordinates index) {
|
||||
|
||||
Assert.notNull(queries, "queries must not be null");
|
||||
Assert.notNull(classes, "classes must not be null");
|
||||
Assert.notNull(index, "index must not be null");
|
||||
Assert.isTrue(queries.size() == classes.size(), "queries and classes must have the same size");
|
||||
|
||||
MultiSearchRequest request = new MultiSearchRequest();
|
||||
Iterator<Class<?>> it = classes.iterator();
|
||||
for (Query query : queries) {
|
||||
request.add(requestFactory.searchRequest(query, routingResolver.getRouting(), it.next(), index));
|
||||
}
|
||||
|
||||
MultiSearchResponse.Item[] items = getMultiSearchResult(request);
|
||||
|
||||
List<SearchHits<?>> res = new ArrayList<>(queries.size());
|
||||
Iterator<Class<?>> it1 = classes.iterator();
|
||||
for (int i = 0; i < queries.size(); i++) {
|
||||
Class entityClass = it1.next();
|
||||
|
||||
ReadDocumentCallback<?> documentCallback = new ReadDocumentCallback<>(elasticsearchConverter, entityClass, index);
|
||||
SearchDocumentResponseCallback<SearchHits<?>> callback = new ReadSearchDocumentResponseCallback<>(entityClass,
|
||||
index);
|
||||
|
||||
SearchResponse response = items[i].getResponse();
|
||||
res.add(callback.doWith(SearchDocumentResponseBuilder.from(response, getEntityCreator(documentCallback))));
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<SearchHits<?>> multiSearch(List<? extends Query> queries, List<Class<?>> classes,
|
||||
List<IndexCoordinates> indexes) {
|
||||
|
||||
Assert.notNull(queries, "queries must not be null");
|
||||
Assert.notNull(classes, "classes must not be null");
|
||||
Assert.notNull(indexes, "indexes must not be null");
|
||||
Assert.isTrue(queries.size() == classes.size() && queries.size() == indexes.size(),
|
||||
"queries, classes and indexes must have the same size");
|
||||
|
||||
MultiSearchRequest request = new MultiSearchRequest();
|
||||
Iterator<Class<?>> it = classes.iterator();
|
||||
Iterator<IndexCoordinates> indexesIt = indexes.iterator();
|
||||
for (Query query : queries) {
|
||||
request.add(requestFactory.searchRequest(query, routingResolver.getRouting(), it.next(), indexesIt.next()));
|
||||
}
|
||||
|
||||
MultiSearchResponse.Item[] items = getMultiSearchResult(request);
|
||||
|
||||
List<SearchHits<?>> res = new ArrayList<>(queries.size());
|
||||
Iterator<Class<?>> it1 = classes.iterator();
|
||||
Iterator<IndexCoordinates> indexesIt1 = indexes.iterator();
|
||||
for (int i = 0; i < queries.size(); i++) {
|
||||
Class entityClass = it1.next();
|
||||
IndexCoordinates index = indexesIt1.next();
|
||||
|
||||
ReadDocumentCallback<?> documentCallback = new ReadDocumentCallback<>(elasticsearchConverter, entityClass, index);
|
||||
SearchDocumentResponseCallback<SearchHits<?>> callback = new ReadSearchDocumentResponseCallback<>(entityClass,
|
||||
index);
|
||||
|
||||
SearchResponse response = items[i].getResponse();
|
||||
res.add(callback.doWith(SearchDocumentResponseBuilder.from(response, getEntityCreator(documentCallback))));
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
protected MultiSearchResponse.Item[] getMultiSearchResult(MultiSearchRequest request) {
|
||||
MultiSearchResponse response = execute(client -> client.msearch(request, RequestOptions.DEFAULT));
|
||||
MultiSearchResponse.Item[] items = response.getResponses();
|
||||
Assert.isTrue(items.length == request.requests().size(), "Response should has same length with queries");
|
||||
return items;
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
// region ClientCallback
|
||||
/**
|
||||
* Callback interface to be used with {@link #execute(ClientCallback)} for operating directly on
|
||||
* {@link RestHighLevelClient}.
|
||||
*
|
||||
* @since 4.0
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface ClientCallback<T> {
|
||||
T doWithClient(RestHighLevelClient client) throws IOException;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a callback with the {@link RestHighLevelClient}
|
||||
*
|
||||
* @param callback the callback to execute, must not be {@literal null}
|
||||
* @param <T> the type returned from the callback
|
||||
* @return the callback result
|
||||
* @since 4.0
|
||||
*/
|
||||
public <T> T execute(ClientCallback<T> callback) {
|
||||
|
||||
Assert.notNull(callback, "callback must not be null");
|
||||
|
||||
try {
|
||||
return callback.doWithClient(client);
|
||||
} catch (IOException | RuntimeException e) {
|
||||
throw translateException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* translates an Exception if possible. Exceptions that are no {@link RuntimeException}s are wrapped in a
|
||||
* RuntimeException
|
||||
*
|
||||
* @param exception the Exception to map
|
||||
* @return the potentially translated RuntimeException.
|
||||
* @since 4.0
|
||||
*/
|
||||
private RuntimeException translateException(Exception exception) {
|
||||
|
||||
RuntimeException runtimeException = exception instanceof RuntimeException ? (RuntimeException) exception
|
||||
: new RuntimeException(exception.getMessage(), exception);
|
||||
RuntimeException potentiallyTranslatedException = exceptionTranslator
|
||||
.translateExceptionIfPossible(runtimeException);
|
||||
|
||||
return potentiallyTranslatedException != null ? potentiallyTranslatedException : runtimeException;
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
// region helper methods
|
||||
@Override
|
||||
public String getClusterVersion() {
|
||||
try {
|
||||
return execute(client -> client.info(RequestOptions.DEFAULT)).getVersion().getNumber();
|
||||
} catch (Exception ignored) {}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Query matchAllQuery() {
|
||||
return new NativeSearchQueryBuilder().withQuery(QueryBuilders.matchAllQuery()).build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Query idsQuery(List<String> ids) {
|
||||
|
||||
Assert.notNull(ids, "ids must not be null");
|
||||
|
||||
return new NativeSearchQueryBuilder().withQuery(QueryBuilders.idsQuery().addIds(ids.toArray(new String[] {})))
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getVendor() {
|
||||
return "Elasticsearch";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getRuntimeLibraryVersion() {
|
||||
return Version.CURRENT.toString();
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public SearchResponse suggest(SuggestBuilder suggestion, Class<?> clazz) {
|
||||
return suggest(suggestion, getIndexCoordinatesFor(clazz));
|
||||
}
|
||||
|
||||
// endregion
|
||||
}
|
||||
+180
@@ -0,0 +1,180 @@
|
||||
/*
|
||||
* Copyright 2020-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.elasticsearch.client.erhlc;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.elasticsearch.search.fetch.subphase.highlight.AbstractHighlighterBuilder;
|
||||
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
|
||||
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity;
|
||||
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty;
|
||||
import org.springframework.data.elasticsearch.core.query.highlight.Highlight;
|
||||
import org.springframework.data.elasticsearch.core.query.highlight.HighlightCommonParameters;
|
||||
import org.springframework.data.elasticsearch.core.query.highlight.HighlightField;
|
||||
import org.springframework.data.elasticsearch.core.query.highlight.HighlightFieldParameters;
|
||||
import org.springframework.data.elasticsearch.core.query.highlight.HighlightParameters;
|
||||
import org.springframework.data.mapping.context.MappingContext;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Converts the {@link Highlight} annotation from a method to an Elasticsearch 7 {@link HighlightBuilder}.
|
||||
*
|
||||
* @author Peter-Josef Meisch
|
||||
* @deprecated since 5.0
|
||||
*/
|
||||
@Deprecated
|
||||
public class HighlightQueryBuilder {
|
||||
|
||||
private final MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext;
|
||||
|
||||
public HighlightQueryBuilder(
|
||||
MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext) {
|
||||
this.mappingContext = mappingContext;
|
||||
}
|
||||
|
||||
/**
|
||||
* creates an Elasticsearch HighlightBuilder from an annotation.
|
||||
*
|
||||
* @param highlight, must not be {@literal null}
|
||||
* @param type the entity type, used to map field names. If null, field names are not mapped.
|
||||
* @return the builder for the highlight
|
||||
*/
|
||||
public HighlightBuilder getHighlightBuilder(Highlight highlight, @Nullable Class<?> type) {
|
||||
HighlightBuilder highlightBuilder = new HighlightBuilder();
|
||||
|
||||
addParameters(highlight.getParameters(), highlightBuilder, type);
|
||||
|
||||
for (HighlightField highlightField : highlight.getFields()) {
|
||||
String mappedName = mapFieldName(highlightField.getName(), type);
|
||||
HighlightBuilder.Field field = new HighlightBuilder.Field(mappedName);
|
||||
|
||||
addParameters(highlightField.getParameters(), field, type);
|
||||
|
||||
highlightBuilder.field(field);
|
||||
}
|
||||
return highlightBuilder;
|
||||
}
|
||||
|
||||
private <P extends HighlightCommonParameters> void addParameters(P parameters, AbstractHighlighterBuilder<?> builder,
|
||||
@Nullable Class<?> type) {
|
||||
|
||||
if (StringUtils.hasLength(parameters.getBoundaryChars())) {
|
||||
builder.boundaryChars(parameters.getBoundaryChars().toCharArray());
|
||||
}
|
||||
|
||||
if (parameters.getBoundaryMaxScan() > -1) {
|
||||
builder.boundaryMaxScan(parameters.getBoundaryMaxScan());
|
||||
}
|
||||
|
||||
if (StringUtils.hasLength(parameters.getBoundaryScanner())) {
|
||||
builder.boundaryScannerType(parameters.getBoundaryScanner());
|
||||
}
|
||||
|
||||
if (StringUtils.hasLength(parameters.getBoundaryScannerLocale())) {
|
||||
builder.boundaryScannerLocale(parameters.getBoundaryScannerLocale());
|
||||
}
|
||||
|
||||
if (parameters.getForceSource()) { // default is false
|
||||
builder.forceSource(true);
|
||||
}
|
||||
|
||||
if (StringUtils.hasLength(parameters.getFragmenter())) {
|
||||
builder.fragmenter(parameters.getFragmenter());
|
||||
}
|
||||
|
||||
if (parameters.getFragmentSize() > -1) {
|
||||
builder.fragmentSize(parameters.getFragmentSize());
|
||||
}
|
||||
|
||||
if (parameters.getNoMatchSize() > -1) {
|
||||
builder.noMatchSize(parameters.getNoMatchSize());
|
||||
}
|
||||
|
||||
if (parameters.getNumberOfFragments() > -1) {
|
||||
builder.numOfFragments(parameters.getNumberOfFragments());
|
||||
}
|
||||
|
||||
if (StringUtils.hasLength(parameters.getOrder())) {
|
||||
builder.order(parameters.getOrder());
|
||||
}
|
||||
|
||||
if (parameters.getPhraseLimit() > -1) {
|
||||
builder.phraseLimit(parameters.getPhraseLimit());
|
||||
}
|
||||
|
||||
if (parameters.getPreTags().length > 0) {
|
||||
builder.preTags(parameters.getPreTags());
|
||||
}
|
||||
|
||||
if (parameters.getPostTags().length > 0) {
|
||||
builder.postTags(parameters.getPostTags());
|
||||
}
|
||||
|
||||
if (!parameters.getRequireFieldMatch()) { // default is true
|
||||
builder.requireFieldMatch(false);
|
||||
}
|
||||
|
||||
if (StringUtils.hasLength(parameters.getType())) {
|
||||
builder.highlighterType(parameters.getType());
|
||||
}
|
||||
|
||||
if (builder instanceof HighlightBuilder highlightBuilder
|
||||
&& parameters instanceof HighlightParameters highlightParameters) {
|
||||
|
||||
if (StringUtils.hasLength(highlightParameters.getEncoder())) {
|
||||
highlightBuilder.encoder(highlightParameters.getEncoder());
|
||||
}
|
||||
|
||||
if (StringUtils.hasLength(highlightParameters.getTagsSchema())) {
|
||||
highlightBuilder.tagsSchema(highlightParameters.getTagsSchema());
|
||||
}
|
||||
}
|
||||
|
||||
if (builder instanceof HighlightBuilder.Field field
|
||||
&& parameters instanceof HighlightFieldParameters fieldParameters) {
|
||||
|
||||
if ((fieldParameters).getFragmentOffset() > -1) {
|
||||
field.fragmentOffset(fieldParameters.getFragmentOffset());
|
||||
}
|
||||
|
||||
if (fieldParameters.getMatchedFields().length > 0) {
|
||||
field.matchedFields(Arrays.stream(fieldParameters.getMatchedFields()) //
|
||||
.map(fieldName -> mapFieldName(fieldName, type)) //
|
||||
.collect(Collectors.toList()) //
|
||||
.toArray(new String[] {})); //
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private String mapFieldName(String fieldName, @Nullable Class<?> type) {
|
||||
|
||||
if (type != null) {
|
||||
ElasticsearchPersistentEntity<?> persistentEntity = mappingContext.getPersistentEntity(type);
|
||||
|
||||
if (persistentEntity != null) {
|
||||
ElasticsearchPersistentProperty persistentProperty = persistentEntity.getPersistentProperty(fieldName);
|
||||
|
||||
if (persistentProperty != null) {
|
||||
return persistentProperty.getFieldName();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return fieldName;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,179 @@
|
||||
/*
|
||||
* Copyright 2018-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.elasticsearch.client.erhlc;
|
||||
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.Collections;
|
||||
import java.util.Set;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.springframework.data.elasticsearch.client.ElasticsearchHost;
|
||||
import org.springframework.data.elasticsearch.client.NoReachableHostException;
|
||||
import org.springframework.data.elasticsearch.support.HttpHeaders;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
|
||||
/**
|
||||
* Infrastructure helper class aware of hosts within the cluster and the health of those allowing easy selection of
|
||||
* active ones.
|
||||
*
|
||||
* @author Christoph Strobl
|
||||
* @author Mark Paluch
|
||||
* @author Peter-Josef Meisch
|
||||
* @since 3.2
|
||||
* @deprecated since 5.0
|
||||
*/
|
||||
@Deprecated
|
||||
public interface HostProvider<T extends HostProvider<T>> {
|
||||
|
||||
/**
|
||||
* Create a new {@link HostProvider} best suited for the given {@link WebClientProvider} and number of hosts.
|
||||
*
|
||||
* @param clientProvider must not be {@literal null} .
|
||||
* @param headersSupplier to supply custom headers, must not be {@literal null}
|
||||
* @param endpoints must not be {@literal null} nor empty.
|
||||
* @return new instance of {@link HostProvider}.
|
||||
*/
|
||||
static HostProvider<?> provider(WebClientProvider clientProvider, Supplier<HttpHeaders> headersSupplier,
|
||||
InetSocketAddress... endpoints) {
|
||||
|
||||
Assert.notNull(clientProvider, "WebClientProvider must not be null");
|
||||
Assert.notEmpty(endpoints, "Please provide at least one endpoint to connect to.");
|
||||
|
||||
if (endpoints.length == 1) {
|
||||
return new SingleNodeHostProvider(clientProvider, endpoints[0]);
|
||||
} else {
|
||||
return new MultiNodeHostProvider(clientProvider, endpoints);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Lookup an active host in {@link Verification#LAZY lazy} mode utilizing cached {@link ElasticsearchHost}.
|
||||
*
|
||||
* @return the {@link Mono} emitting the active host or {@link Mono#error(Throwable) an error} if none found.
|
||||
*/
|
||||
default Mono<InetSocketAddress> lookupActiveHost() {
|
||||
return lookupActiveHost(Verification.LAZY);
|
||||
}
|
||||
|
||||
/**
|
||||
* Lookup an active host in using the given {@link Verification}.
|
||||
*
|
||||
* @param verification
|
||||
* @return the {@link Mono} emitting the active host or {@link Mono#error(Throwable) an error}
|
||||
* ({@link NoReachableHostException}) if none found.
|
||||
*/
|
||||
Mono<InetSocketAddress> lookupActiveHost(Verification verification);
|
||||
|
||||
/**
|
||||
* Get the {@link WebClient} connecting to an active host utilizing cached {@link ElasticsearchHost}.
|
||||
*
|
||||
* @return the {@link Mono} emitting the client for an active host or {@link Mono#error(Throwable) an error} if none
|
||||
* found.
|
||||
*/
|
||||
default Mono<WebClient> getActive() {
|
||||
return getActive(Verification.LAZY);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the {@link WebClient} connecting to an active host.
|
||||
*
|
||||
* @param verification must not be {@literal null}.
|
||||
* @return the {@link Mono} emitting the client for an active host or {@link Mono#error(Throwable) an error} if none
|
||||
* found.
|
||||
*/
|
||||
default Mono<WebClient> getActive(Verification verification) {
|
||||
return lookupActiveHost(verification).map(this::createWebClient);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the {@link WebClient} connecting to an active host utilizing cached {@link ElasticsearchHost}.
|
||||
*
|
||||
* @return the {@link Mono} emitting the client for an active host or {@link Mono#error(Throwable) an error} if none
|
||||
* found.
|
||||
* @since 4.4
|
||||
*/
|
||||
default Mono<WebClient> getWebClient() {
|
||||
return getWebClient(Verification.LAZY);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the {@link WebClient} connecting to an active host.
|
||||
*
|
||||
* @param verification must not be {@literal null}.
|
||||
* @return the {@link Mono} emitting the client for an active host or {@link Mono#error(Throwable) an error} if none
|
||||
* found.
|
||||
* @since 4.4
|
||||
*/
|
||||
default Mono<WebClient> getWebClient(Verification verification) {
|
||||
return lookupActiveHost(verification).map(this::createWebClient);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a {@link WebClient} for {@link InetSocketAddress endpoint}.
|
||||
*
|
||||
* @param endpoint must not be {@literal null}.
|
||||
* @return a {@link WebClient} using the the given endpoint as {@literal transport url}.
|
||||
*/
|
||||
WebClient createWebClient(InetSocketAddress endpoint);
|
||||
|
||||
/**
|
||||
* Obtain information about known cluster nodes.
|
||||
*
|
||||
* @return the {@link Mono} emitting {@link ClusterInformation} when available.
|
||||
*/
|
||||
Mono<ClusterInformation> clusterInfo();
|
||||
|
||||
/**
|
||||
* {@link Verification} allows to influence the lookup strategy for active hosts.
|
||||
*
|
||||
* @author Christoph Strobl
|
||||
* @since 3.2
|
||||
*/
|
||||
enum Verification {
|
||||
|
||||
/**
|
||||
* Actively check for cluster node health.
|
||||
*/
|
||||
ACTIVE,
|
||||
|
||||
/**
|
||||
* Use cached data for cluster node health.
|
||||
*/
|
||||
LAZY
|
||||
}
|
||||
|
||||
/**
|
||||
* Value object accumulating information about an Elasticsearch cluster.
|
||||
*
|
||||
* @author Christoph Strobl
|
||||
* @since 3.2
|
||||
*/
|
||||
class ClusterInformation {
|
||||
|
||||
private final Set<ElasticsearchHost> nodes;
|
||||
|
||||
public ClusterInformation(Set<ElasticsearchHost> nodes) {
|
||||
this.nodes = nodes;
|
||||
}
|
||||
|
||||
public Set<ElasticsearchHost> getNodes() {
|
||||
return Collections.unmodifiableSet(nodes);
|
||||
}
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user