1
0
mirror of synced 2026-07-07 02:30:01 +00:00

Compare commits

...

114 Commits

Author SHA1 Message Date
Mark Paluch 9104740c3b DATAES-507 - Release version 3.1.4 (Lovelace SR4). 2019-01-10 12:35:56 +01:00
Mark Paluch 4d08095178 DATAES-507 - Prepare 3.1.4 (Lovelace SR4). 2019-01-10 12:34:54 +01:00
Mark Paluch 8c93961581 DATAES-507 - Updated changelog. 2019-01-10 12:34:53 +01:00
Mark Paluch ae75546cd8 DATAES-506 - Updated changelog. 2019-01-10 12:26:40 +01:00
Mark Paluch 16ba2e78cc DATAES-505 - Updated changelog. 2019-01-10 11:01:24 +01:00
Mark Paluch f9b7513ca2 DATAES-524 - Update copyright years to 2019. 2019-01-02 12:37:23 +01:00
Christoph Strobl f37bbdb75c DATAES-513 - Updated changelog. 2018-12-11 11:43:19 +01:00
Mark Paluch 6bcd850eb1 DATAES-496 - After release cleanups. 2018-11-27 14:23:35 +01:00
Mark Paluch 3f43f5efed DATAES-496 - Prepare next development iteration. 2018-11-27 14:23:34 +01:00
Mark Paluch 70f77bfd1f DATAES-496 - Release version 3.1.3 (Lovelace SR3). 2018-11-27 13:43:25 +01:00
Mark Paluch d887b1bc9b DATAES-496 - Prepare 3.1.3 (Lovelace SR3). 2018-11-27 13:42:18 +01:00
Mark Paluch 6636bbb364 DATAES-496 - Updated changelog. 2018-11-27 13:42:16 +01:00
Mark Paluch b116cce1e8 DATAES-490 - Updated changelog. 2018-11-27 12:36:51 +01:00
Mark Paluch ed7a761912 DATAES-491 - Updated changelog. 2018-11-27 11:27:26 +01:00
tsallase b0353ec4e5 DATAES-445 - Updated scroll API example.
Original pull request: #218
2018-11-20 11:52:36 +01:00
xhaggi 758e697aec DATAES-33 - Polishing
* Move @Parent property recognition to ElasticsearchPersistentProperty
* Move type contraints for @Version and @Parent fields to ElasticsearchPersistentProperty to fail faster
* remove unused constant SUPPORTED_ID_TYPES from SimpleElasticsearchPersistentProperty

Original pull request: #208
2018-11-20 11:48:01 +01:00
xhaggi 02761a48e0 DATAES-503 - Added missing copy_to property to @Field annotation.
Original pull request: #227
2018-11-20 11:25:31 +01:00
xhaggi e6fbc37550 DATAES-492 - Add missing normalizer property to @Field and @InnerField.
Original pull request: #222
2018-11-20 10:49:39 +01:00
Mark Paluch 0220f69f3f DATAES-489 - After release cleanups. 2018-10-29 13:59:18 +01:00
Mark Paluch c0dcda00e8 DATAES-489 - Prepare next development iteration. 2018-10-29 13:59:16 +01:00
Mark Paluch cef1fc7d77 DATAES-489 - Release version 3.1.2 (Lovelace SR2). 2018-10-29 12:53:52 +01:00
Mark Paluch 9ddf4868ef DATAES-489 - Prepare 3.1.2 (Lovelace SR2). 2018-10-29 12:52:55 +01:00
Mark Paluch dde561a805 DATAES-489 - Updated changelog. 2018-10-29 12:52:52 +01:00
Mark Paluch 950c48a069 DATAES-484 - Updated changelog. 2018-10-15 14:19:08 +02:00
Mark Paluch df9954b3ef DATAES-485 - Updated changelog. 2018-10-15 12:46:30 +02:00
Mark Paluch 32d5f5dcb9 DATAES-486 - After release cleanups. 2018-10-15 11:12:14 +02:00
Mark Paluch 0191d69d94 DATAES-486 - Prepare next development iteration. 2018-10-15 11:12:13 +02:00
Mark Paluch e7e0983ea5 DATAES-486 - Release version 3.1.1 (Lovelace SR1). 2018-10-15 10:42:05 +02:00
Mark Paluch a299260a03 DATAES-486 - Prepare 3.1.1 (Lovelace SR1). 2018-10-15 10:40:57 +02:00
Mark Paluch 627d96cbfc DATAES-486 - Updated changelog. 2018-10-15 10:40:56 +02:00
Mark Paluch efa3b4d17e DATAES-480 - After release cleanups. 2018-09-21 07:46:18 -04:00
Mark Paluch f5d44ad755 DATAES-480 - Prepare next development iteration. 2018-09-21 07:46:17 -04:00
Mark Paluch 5a50114b73 DATAES-480 - Release version 3.1 GA (Lovelace). 2018-09-21 07:08:39 -04:00
Mark Paluch 45a9384d4f DATAES-480 - Prepare 3.1 GA (Lovelace). 2018-09-21 07:07:51 -04:00
Mark Paluch 789acdc3dc DATAES-480 - Updated changelog. 2018-09-21 07:07:49 -04:00
Mark Paluch 946a60d24d DATAES-473 - Updated changelog. 2018-09-10 14:15:51 +02:00
Mark Paluch 838776dd63 DATAES-474 - Updated changelog. 2018-09-10 10:20:58 +02:00
jnizet b8324f9205 DATAES-479 - Allow specifying a HighlightBuilder when creating a query.
Original pull request: #217.
2018-08-24 15:59:12 +02:00
Oliver Gierke 94d18e8111 DATAES-472 - After release cleanups. 2018-08-20 10:56:53 +02:00
Oliver Gierke a1aa755253 DATAES-472 - Prepare next development iteration. 2018-08-20 10:56:52 +02:00
Oliver Gierke dfd9d962d3 DATAES-472 - Release version 3.1 RC2 (Lovelace). 2018-08-20 10:40:11 +02:00
Oliver Gierke e1d3f6f160 DATAES-472 - Prepare 3.1 RC2 (Lovelace). 2018-08-20 10:39:43 +02:00
Oliver Gierke 7bb108c998 DATAES-472 - Updated changelog. 2018-08-20 10:39:41 +02:00
Mark Paluch d1b71f2e73 DATAES-463 - Updated changelog. 2018-07-27 11:45:25 +02:00
Mark Paluch 88fe60c7d4 DATAES-465 - Updated changelog. 2018-07-26 16:23:57 +02:00
Mark Paluch 9a0a931ce2 DATAES-452 - After release cleanups. 2018-07-26 12:32:29 +02:00
Mark Paluch 02f2171cb6 DATAES-452 - Prepare next development iteration. 2018-07-26 12:32:25 +02:00
Mark Paluch 5d4cabae10 DATAES-452 - Release version 3.1 RC1 (Lovelace). 2018-07-26 12:06:35 +02:00
Mark Paluch 2632b69d12 DATAES-452 - Prepare 3.1 RC1 (Lovelace). 2018-07-26 12:04:30 +02:00
Mark Paluch d30328f2e1 DATAES-452 - Updated changelog. 2018-07-26 12:04:28 +02:00
Oliver Gierke 469455383f DATAES-470 - Fixed parsing of cluster nodes in TransportClientFactoryBean.
Extracted ClusterNodes value object to capture the parsing logic and actually properly test it. Added unit tests to verify the proper rejection and the two cases outlined in the ticket.

Related tickets: DATAES-283.
2018-07-12 21:48:31 +02:00
Oliver Gierke cdbc832068 DATAES-471 - Adapt object property access behavior after changes in Spring Data Commons.
Changes made for Spring Data Commons result in final fields being rejected for manipulation unless there's a wither method available on the object at hand. Unfortunately adapting Spring Data Elasticsearch to support that doesn't work easily as it requires breaking changes to ElasticsearchTemplate as most of the methods assume parameters being handed to be mutable, see the implementation of SimpleElasticsearchRepository.save(…) for instance.

We now mitigate the problem, by enforcing the BeanWrapperPropertyAccessor being used and treating all properties as mutable.

Related tickets: DATACMNS-1322.
2018-07-12 15:27:36 +02:00
xhaggi 143a359eca DATAES-317 - Introduce query logging in ElasticsearchTemplate.
Original pull request: #180.
2018-06-28 11:21:43 +02:00
xhaggi e58c43f391 DATAES-469 - Remove superfluous dependency elasticsearch. 2018-06-28 11:16:33 +02:00
petar.tahchiev d66b2a6f63 DATAES-283 - Get rid of Commons Lang dependency.
Replace all StringUtils and ArrayUtils usages with Springframework's StringUtils and ObjectUtils. Left the commons-lang as test-scope dependency as I believe it brings some values in the tests.

Original pull request: #211.
2018-06-20 20:10:21 +02:00
xhaggi ba3eba5734 DATAES-467 - Fix sorting by _score if Spring Data sort is used.
Original pull request: #209.
2018-06-18 11:18:31 +02:00
Mark Paluch 4a26af38c3 DATAES-448 - Updated changelog. 2018-06-13 21:39:54 +02:00
Oliver Gierke 5ddb46c435 DATAES-462 - Polishing.
SimpleElasticsearchPersistentProperty now already checks for the correct type of score properties. Added unit tests for that. Also added unit tests for SimpleElasticsearchPersistentEntity rejecting more than one score property being present.

Additional non-null assertions on components that are required so that we can remove superfluous null checks.

A bit o formatting, Javadoc, missing @since tags and license headers.

Original pull request: #207.
2018-06-13 19:00:36 +02:00
xhaggi d996406113 DATAES-462 - Add support for mapping max score and document scores.
Original pull request: #207.
2018-06-13 18:58:13 +02:00
xhaggi 112600261d DATAES-462 - Add query builder option to track scores.
Original pull request: #207.
2018-06-13 18:57:54 +02:00
Oliver Gierke 62a03a8fb7 DATAES-464 - DefaultEntityWriter now considers read-only and transient properties.
We now register a custom Jackson module that filters read-only and transient properties for serialization.
2018-06-13 18:21:37 +02:00
Mark Paluch ac62aaf856 DATAES-447 - Updated changelog. 2018-06-13 15:01:59 +02:00
Christoph Strobl 28629a6ede DATAES-440 - After release cleanups. 2018-05-17 10:09:36 +02:00
Christoph Strobl 12cc5ced16 DATAES-440 - Prepare next development iteration. 2018-05-17 10:09:34 +02:00
Christoph Strobl a807a3801c DATAES-440 - Release version 3.1 M3 (Lovelace). 2018-05-17 09:51:42 +02:00
Christoph Strobl ac2e5be02b DATAES-440 - Prepare 3.1 M3 (Lovelace). 2018-05-17 09:50:48 +02:00
Christoph Strobl b5b8066e6f DATAES-440 - Updated changelog. 2018-05-17 09:50:46 +02:00
Ted Liang e8e407f93b DATAES-312 - Fix NullHandling.NULLS_LAST in query.sort
Original pull request: #163
2018-05-17 09:31:59 +02:00
Nordine Bittich 1b0006f9f7 DATAES-420 - Analyzer of main field ignored when using @MultiField annotation 2018-05-15 17:16:29 +02:00
Oliver Gierke 36c52a5308 DATAES-451 - Adapt to SpEL extension API changes in Spring Data Commons.
Related tickets: DATACMNS-1260.
2018-05-14 17:23:29 +02:00
Mark Paluch 324ab13ca1 DATAES-437 - Updated changelog. 2018-05-08 15:27:21 +02:00
Mark Paluch c87499b189 DATAES-436 - Updated changelog. 2018-05-08 12:22:52 +02:00
xhaggi 64f259cb8e DATAES-427 - update version overview in README 2018-05-08 11:37:00 +02:00
xhaggi 8c4a669ce5 DATAES-285 - fix broken tests after 4970783ca4 2018-05-08 10:47:22 +02:00
xhaggi ace5a21b05 DATAES-285 - Polishing
* drop superfluous class FieldIndex
* change FieldType text and keyword starting with capital letters

Original commit: 089d7746be
2018-05-07 13:19:37 +02:00
xhaggi 9c235cd53c DATAES-412 - only the last highlight field is added to SearchRequest 2018-05-07 11:39:58 +02:00
Chris White ef1dca31e4 DATAES-198 - Fixed @Version annotation on fields. 2018-04-23 17:33:54 +02:00
Alen Turkovic de1afe8bb0 DATAES-422 - Add support for IndicesOptions in search queries.
See also: DATAES-191..
2018-04-20 14:32:01 +02:00
Oliver Gierke 92c3bbebed DATAES-363 - Polishing.
Original pull request: #183.
2018-04-18 12:36:27 +02:00
Michael Wirth 512e9eacad DATAES-363 - Fixed CrudRepository.existsById(…) implementation.
Properly use Optional.isPresent() over a null check.

Original pull request: #183.
2018-04-18 12:36:10 +02:00
Mark Paluch b8400d8d4d DATAES-427 - After release cleanups. 2018-04-13 15:08:34 +02:00
Mark Paluch c53ecdc41c DATAES-427 - Prepare next development iteration. 2018-04-13 15:08:32 +02:00
Mark Paluch c2aca20505 DATAES-427 - Release version 3.1 M2 (Lovelace). 2018-04-13 14:30:58 +02:00
Mark Paluch e703fbc8bf DATAES-427 - Prepare 3.1 M2 (Lovelace). 2018-04-13 14:30:01 +02:00
Mark Paluch 1b44f051c7 DATAES-427 - Updated changelog. 2018-04-13 14:29:59 +02:00
xhaggi b949daadea DATAES-438 - get rid of deprecation warning because of invalid mapping for @Id 2018-04-13 11:55:17 +01:00
Oliver Gierke 8c40bac9ab DATAES-439 - Adapt to API changes in Spring Data Commons.
Related tickets: DATACMNS-1275.
2018-04-09 14:40:47 +02:00
Mark Paluch 9fba9ea076 DATAES-430 - Updated changelog. 2018-04-04 17:12:55 +02:00
Mark Paluch b5d5fdc32e DATAES-423 - Updated changelog. 2018-04-04 15:16:22 +02:00
Oliver Gierke 6db756a233 DATAES-434 - Removed explicit declaration of Jackson library versions. 2018-03-27 19:38:13 +02:00
Artur Konczak e50e5f9146 DATAES-402 - fixed broken tests 2018-03-15 13:43:06 +00:00
Remco Zigterman 4856974c1a DATAES-402 - fixing paging information 2018-03-15 09:57:19 +00:00
Artur Konczak 51f9485700 DATAES-421 updated to latest version of ES 6.2.2 2018-03-15 09:14:43 +00:00
rivergod e7b93bee90 DATAES-421 - Update ES to 6.1.0 2018-03-15 09:14:43 +00:00
Mark Paluch 9bc9c47f42 DATAES-432 - Export composable repositories via CDI.
We now export composable repositories through our CDI extension. Repositories can now be customized either by a single custom implementation (as it was before) and by providing fragment interfaces along their fragment implementation.

This change aligns CDI support with the existing RepositoryFactory support we provide within a Spring application context.
2018-03-12 17:00:16 +01:00
Mark Paluch 4313d13ddd DATAES-429 - Updated changelog. 2018-02-28 11:17:39 +01:00
Mark Paluch 2075775478 DATAES-425 - Updated changelog. 2018-02-19 20:30:08 +01:00
Christoph Strobl e4009df6b9 DATAES-401 - After release cleanups. 2018-02-06 10:11:17 +01:00
Christoph Strobl 053e1726f5 DATAES-401 - Prepare next development iteration. 2018-02-06 10:11:15 +01:00
Christoph Strobl d2690abbdf DATAES-401 - Release version 3.1 M1 (Lovelace). 2018-02-06 09:47:10 +01:00
Christoph Strobl cf6d299941 DATAES-401 - Prepare 3.1 M1 (Lovelace). 2018-02-06 09:45:50 +01:00
Christoph Strobl 4d0159a560 DATAES-401 - Updated changelog. 2018-02-06 09:45:48 +01:00
Mark Paluch 028bc13eb7 DATAES-417 - Updated changelog. 2018-01-24 13:41:24 +01:00
Mark Paluch 9bfc6f23b9 DATAES-424 - Fix line endings to LF. 2018-01-24 13:06:40 +01:00
Mark Paluch 4bd263a25a DATAES-416 - Updated changelog. 2018-01-24 12:22:14 +01:00
Mark Paluch 00e30fd802 DATAES-411 - Updated changelog. 2017-11-27 16:43:43 +01:00
Mark Paluch 22e10801ca DATAES-404 - Updated changelog. 2017-11-27 15:58:47 +01:00
Oliver Gierke f7d77f05de DATAES-414 - Reduced scope of Log4j dependencies to test. 2017-11-02 18:40:56 +01:00
Oliver Gierke 71ebc3dd21 DATAES-400 - Updated changelog. 2017-10-27 16:36:52 +02:00
Oliver Gierke 51d0f95f65 DATAES-410 - Adapt API changes in Property in test cases. 2017-10-27 11:24:34 +02:00
Vladimir Tsanev 75f34e4d02 DATAES-361 - Move Log4j2 config file to test sources.
The configuration file for Log4j2 had been added to src/main/resources accidentally which caused it to be picked up by user applications, e.g. in a Spring Boot application effectively disabling Boot's auto-configuration. This file has now been moved to the test resources.

Original pull request: #191.
Related tickets: spring-projects/spring-boot#10634
2017-10-13 15:40:44 +02:00
Oliver Gierke 4a956270a8 DATAES-391 - Updated changelog. 2017-10-11 19:01:43 +02:00
Mark Paluch d227f21572 DATAES-392 - After release cleanups. 2017-10-02 11:37:23 +02:00
Mark Paluch fe6b3c65b3 DATAES-392 - Prepare next development iteration. 2017-10-02 11:37:22 +02:00
257 changed files with 13694 additions and 11962 deletions
+18 -18
View File
@@ -1,18 +1,18 @@
atlassian-ide-plugin.xml atlassian-ide-plugin.xml
## Ignore svn files ## Ignore svn files
.svn .svn
## ignore any target dir ## ignore any target dir
target target
## Ignore project files created by Eclipse ## Ignore project files created by Eclipse
.settings .settings
.project .project
.classpath .classpath
## Ignore project files created by IntelliJ IDEA ## Ignore project files created by IntelliJ IDEA
*.iml *.iml
*.ipr *.ipr
*.iws *.iws
.idea .idea
+7 -11
View File
@@ -52,17 +52,13 @@ the appropriate dependency version.
</repository> </repository>
``` ```
| spring data elasticsearch | elasticsearch | | spring data elasticsearch | elasticsearch |
|:-----------------------------------:|:-------------:| |:-------------------------:|:-------------:|
| 3.0.0.RC2 | 5.5.0 | | 3.1.x | 6.2.2 |
| 3.0.0.M4 | 5.4.0 | | 3.0.x | 5.5.0 |
| 2.0.4.RELEASE | 2.4.0 | | 2.1.x | 2.4.0 |
| 2.0.0.RELEASE | 2.2.0 | | 2.0.x | 2.2.0 |
| 1.4.0.M1 | 1.7.3 | | 1.3.x | 1.5.2 |
| 1.3.0.RELEASE | 1.5.2 |
| 1.2.0.RELEASE | 1.4.4 |
| 1.1.0.RELEASE | 1.3.2 |
| 1.0.0.RELEASE | 1.1.1 |
### ElasticsearchRepository ### ElasticsearchRepository
+11 -17
View File
@@ -4,12 +4,12 @@
<groupId>org.springframework.data</groupId> <groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId> <artifactId>spring-data-elasticsearch</artifactId>
<version>3.0.0.RELEASE</version> <version>3.1.4.RELEASE</version>
<parent> <parent>
<groupId>org.springframework.data.build</groupId> <groupId>org.springframework.data.build</groupId>
<artifactId>spring-data-parent</artifactId> <artifactId>spring-data-parent</artifactId>
<version>2.0.0.RELEASE</version> <version>2.1.4.RELEASE</version>
</parent> </parent>
<name>Spring Data Elasticsearch</name> <name>Spring Data Elasticsearch</name>
@@ -19,8 +19,9 @@
<properties> <properties>
<commonscollections>3.2.1</commonscollections> <commonscollections>3.2.1</commonscollections>
<commonslang>2.6</commonslang> <commonslang>2.6</commonslang>
<elasticsearch>5.5.0</elasticsearch> <elasticsearch>6.2.2</elasticsearch>
<springdata.commons>2.0.0.RELEASE</springdata.commons> <log4j>2.9.1</log4j>
<springdata.commons>2.1.4.RELEASE</springdata.commons>
<java-module-name>spring.data.elasticsearch</java-module-name> <java-module-name>spring.data.elasticsearch</java-module-name>
</properties> </properties>
@@ -55,6 +56,7 @@
<groupId>commons-lang</groupId> <groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId> <artifactId>commons-lang</artifactId>
<version>${commonslang}</version> <version>${commonslang}</version>
<scope>test</scope>
</dependency> </dependency>
<!-- JODA Time --> <!-- JODA Time -->
@@ -65,12 +67,6 @@
</dependency> </dependency>
<!-- Elasticsearch --> <!-- Elasticsearch -->
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>${elasticsearch}</version>
</dependency>
<dependency> <dependency>
<groupId>org.elasticsearch.client</groupId> <groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId> <artifactId>transport</artifactId>
@@ -86,25 +82,25 @@
<dependency> <dependency>
<groupId>org.slf4j</groupId> <groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId> <artifactId>log4j-over-slf4j</artifactId>
<version>1.7.22</version> <version>${slf4j}</version>
<scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.apache.logging.log4j</groupId> <groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId> <artifactId>log4j-core</artifactId>
<version>2.8.2</version> <version>${log4j}</version>
<scope>test</scope>
</dependency> </dependency>
<!-- Jackson JSON Mapper --> <!-- Jackson JSON Mapper -->
<dependency> <dependency>
<groupId>com.fasterxml.jackson.core</groupId> <groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId> <artifactId>jackson-core</artifactId>
<version>${jackson}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.fasterxml.jackson.core</groupId> <groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId> <artifactId>jackson-databind</artifactId>
<version>${jackson}</version>
</dependency> </dependency>
<!-- CDI --> <!-- CDI -->
@@ -145,7 +141,7 @@
</exclusion> </exclusion>
</exclusions> </exclusions>
</dependency> </dependency>
<!-- Upgrade xbean to 4.5 to prevent incompatibilities due to ASM versions --> <!-- Upgrade xbean to 4.5 to prevent incompatibilities due to ASM versions -->
<dependency> <dependency>
<groupId>org.apache.xbean</groupId> <groupId>org.apache.xbean</groupId>
@@ -176,8 +172,6 @@
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
</dependencies> </dependencies>
<build> <build>
+1 -1
View File
@@ -6,7 +6,7 @@ BioMed Central Development Team
:toc-placement!: :toc-placement!:
:spring-data-commons-docs: ../../../../spring-data-commons/src/main/asciidoc :spring-data-commons-docs: ../../../../spring-data-commons/src/main/asciidoc
(C) 2013-2015 The original author(s). (C) 2013-2019 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. 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.
@@ -23,50 +23,55 @@ Page<SampleEntity> sampleEntities =
---- ----
==== ====
[[elasticsearch.scan.and.scroll]] [[elasticsearch.scroll]]
== Using Scan And Scroll For Big Result Set == Using Scroll For Big Result Set
Elasticsearch has scan and scroll feature for getting big result set in chunks. `ElasticsearchTemplate` has scan and scroll methods that can be used as below. Elasticsearch has a scroll API for getting big result set in chunks. `ElasticsearchTemplate` has startScroll and continueScroll methods that can be used as below.
.Using Scan and Scroll .Using startScroll and continueScroll
==== ====
[source,java] [source,java]
---- ----
SearchQuery searchQuery = new NativeSearchQueryBuilder() SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(matchAllQuery()) .withQuery(matchAllQuery())
.withIndices("test-index") .withIndices(INDEX_NAME)
.withTypes("test-type") .withTypes(TYPE_NAME)
.withPageable(new PageRequest(0,1)) .withFields("message")
.withPageable(PageRequest.of(0, 10))
.build(); .build();
String scrollId = elasticsearchTemplate.scan(searchQuery,1000,false);
List<SampleEntity> sampleEntities = new ArrayList<SampleEntity>(); Page<SampleEntity> scroll = elasticsearchTemplate.startScroll(1000, searchQuery, SampleEntity.class);
boolean hasRecords = true;
while (hasRecords){ String scrollId = ((ScrolledPage) scroll).getScrollId();
Page<SampleEntity> page = elasticsearchTemplate.scroll(scrollId, 5000L , new ResultsMapper<SampleEntity>() List<SampleEntity> sampleEntities = new ArrayList<>();
{ while (scroll.hasContent()) {
@Override sampleEntities.addAll(scroll.getContent());
public Page<SampleEntity> mapResults(SearchResponse response) { scrollId = ((ScrolledPage) scroll).getScrollId();
List<SampleEntity> chunk = new ArrayList<SampleEntity>(); scroll = elasticsearchTemplate.continueScroll(scrollId, 1000, SampleEntity.class);
for(SearchHit searchHit : response.getHits()){ }
if(response.getHits().getHits().length <= 0) { elasticsearchTemplate.clearScroll(scrollId);
return null;
}
SampleEntity user = new SampleEntity();
user.setId(searchHit.getId());
user.setMessage((String)searchHit.getSource().get("message"));
chunk.add(user);
}
return new PageImpl<SampleEntity>(chunk);
}
});
if(page != null) {
sampleEntities.addAll(page.getContent());
hasRecords = page.hasNextPage();
}
else{
hasRecords = false;
}
}
}
---- ----
==== ====
`ElasticsearchTemplate` additionally has the stream method which wraps the scan and scroll operations into a CloseableIterator.
.Using stream
====
[source,java]
----
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(matchAllQuery())
.withIndices(INDEX_NAME)
.withTypes(TYPE_NAME)
.withFields("message")
.withPageable(PageRequest.of(0, 10))
.build();
CloseableIterator<SampleEntity> stream = elasticsearchTemplate.stream(searchQuery, SampleEntity.class);
List<SampleEntity> sampleEntities = new ArrayList<>();
while (stream.hasNext()) {
sampleEntities.add(stream.next());
}
----
====
@@ -1,51 +1,51 @@
/* /*
* Copyright 2013 the original author or authors. * Copyright 2013-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.data.elasticsearch; package org.springframework.data.elasticsearch;
import java.util.Map; import java.util.Map;
/** /**
* ElasticsearchException * ElasticsearchException
* *
* @author Rizwan Idrees * @author Rizwan Idrees
* @author Mohsin Husen * @author Mohsin Husen
*/ */
public class ElasticsearchException extends RuntimeException { public class ElasticsearchException extends RuntimeException {
private Map<String, String> failedDocuments; private Map<String, String> failedDocuments;
public ElasticsearchException(String message) { public ElasticsearchException(String message) {
super(message); super(message);
} }
public ElasticsearchException(String message, Throwable cause) { public ElasticsearchException(String message, Throwable cause) {
super(message, cause); super(message, cause);
} }
public ElasticsearchException(String message, Throwable cause, Map<String, String> failedDocuments) { public ElasticsearchException(String message, Throwable cause, Map<String, String> failedDocuments) {
super(message, cause); super(message, cause);
this.failedDocuments = failedDocuments; this.failedDocuments = failedDocuments;
} }
public ElasticsearchException(String message, Map<String, String> failedDocuments) { public ElasticsearchException(String message, Map<String, String> failedDocuments) {
super(message); super(message);
this.failedDocuments = failedDocuments; this.failedDocuments = failedDocuments;
} }
public Map<String, String> getFailedDocuments() { public Map<String, String> getFailedDocuments() {
return failedDocuments; return failedDocuments;
} }
} }
@@ -1,5 +1,5 @@
/* /*
* Copyright 2013 the original author or authors. * Copyright 2013-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
/* /*
* Copyright 2014 the original author or authors. * Copyright 2014-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -1,51 +1,51 @@
/* /*
* Copyright 2013 the original author or authors. * Copyright 2013-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.data.elasticsearch.annotations; package org.springframework.data.elasticsearch.annotations;
import java.lang.annotation.*; import java.lang.annotation.*;
import org.springframework.data.annotation.Persistent; import org.springframework.data.annotation.Persistent;
/** /**
* Document * Document
* *
* @author Rizwan Idrees * @author Rizwan Idrees
* @author Mohsin Husen * @author Mohsin Husen
* @author Mason Chan * @author Mason Chan
*/ */
@Persistent @Persistent
@Inherited @Inherited
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE}) @Target({ElementType.TYPE})
public @interface Document { public @interface Document {
String indexName(); String indexName();
String type() default ""; String type() default "";
boolean useServerConfiguration() default false; boolean useServerConfiguration() default false;
short shards() default 5; short shards() default 5;
short replicas() default 1; short replicas() default 1;
String refreshInterval() default "1s"; String refreshInterval() default "1s";
String indexStoreType() default "fs"; String indexStoreType() default "fs";
boolean createIndex() default true; boolean createIndex() default true;
} }
@@ -1,58 +1,62 @@
/* /*
* Copyright 2013 the original author or authors. * Copyright 2013-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.data.elasticsearch.annotations; package org.springframework.data.elasticsearch.annotations;
import java.lang.annotation.Documented; import java.lang.annotation.Documented;
import java.lang.annotation.ElementType; import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited; import java.lang.annotation.Inherited;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; import java.lang.annotation.Target;
/** /**
* @author Rizwan Idrees * @author Rizwan Idrees
* @author Mohsin Husen * @author Mohsin Husen
* @author Artur Konczak * @author Artur Konczak
* @author Jonathan Yan * @author Jonathan Yan
* @author Jakub Vavrik * @author Jakub Vavrik
* @author Kevin Leturc * @author Kevin Leturc
*/ */
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD) @Target(ElementType.FIELD)
@Documented @Documented
@Inherited @Inherited
public @interface Field { public @interface Field {
FieldType type() default FieldType.Auto; FieldType type() default FieldType.Auto;
boolean index() default true; boolean index() default true;
DateFormat format() default DateFormat.none; DateFormat format() default DateFormat.none;
String pattern() default ""; String pattern() default "";
boolean store() default false; boolean store() default false;
boolean fielddata() default false; boolean fielddata() default false;
String searchAnalyzer() default ""; String searchAnalyzer() default "";
String analyzer() default ""; String analyzer() default "";
String[] ignoreFields() default {}; String normalizer() default "";
boolean includeInParent() default false; String[] ignoreFields() default {};
}
boolean includeInParent() default false;
String[] copyTo() default {};
}
@@ -1,5 +1,5 @@
/* /*
* Copyright 2013 the original author or authors. * Copyright 2013-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -21,5 +21,17 @@ package org.springframework.data.elasticsearch.annotations;
* @author Artur Konczak * @author Artur Konczak
*/ */
public enum FieldType { public enum FieldType {
text, Integer, Long, Date, Float, Double, Boolean, Object, Auto, Nested, Ip, Attachment, keyword Text,
Integer,
Long,
Date,
Float,
Double,
Boolean,
Object,
Auto,
Nested,
Ip,
Attachment,
Keyword
} }
@@ -1,5 +1,5 @@
/* /*
* Copyright 2013 the original author or authors. * Copyright 2013-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
/* /*
* Copyright 2014 the original author or authors. * Copyright 2014-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -21,7 +21,9 @@ import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; import java.lang.annotation.Target;
/** /**
* * @author Artur Konczak
* @author Mohsin Husen
* @author Sascha Woo
*/ */
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD) @Target(ElementType.FIELD)
@@ -33,11 +35,17 @@ public @interface InnerField {
boolean index() default true; boolean index() default true;
DateFormat format() default DateFormat.none;
String pattern() default "";
boolean store() default false; boolean store() default false;
boolean fielddata() default false; boolean fielddata() default false;
String searchAnalyzer() default ""; String searchAnalyzer() default "";
String indexAnalyzer() default ""; String analyzer() default "";
String normalizer() default "";
} }
@@ -1,5 +1,5 @@
/* /*
* Copyright 2014 the original author or authors. * Copyright 2014-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
/* /*
* Copyright 2014 the original author or authors. * Copyright 2014-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
/* /*
* Copyright 2014 the original author or authors. * Copyright 2014-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -1,45 +1,45 @@
/* /*
* Copyright 2013 the original author or authors. * Copyright 2013-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.data.elasticsearch.annotations; package org.springframework.data.elasticsearch.annotations;
import java.lang.annotation.*; import java.lang.annotation.*;
/** /**
* Query * Query
* *
* @author Rizwan Idrees * @author Rizwan Idrees
* @author Mohsin Husen * @author Mohsin Husen
*/ */
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD) @Target(ElementType.METHOD)
@Documented @Documented
public @interface Query { public @interface Query {
/** /**
* Elasticsearch query to be used when executing query. May contain placeholders eg. ?0 * Elasticsearch query to be used when executing query. May contain placeholders eg. ?0
* *
* @return * @return
*/ */
String value() default ""; String value() default "";
/** /**
* Named Query Named looked up by repository. * Named Query Named looked up by repository.
* *
* @return * @return
*/ */
String name() default ""; String name() default "";
} }
@@ -0,0 +1,23 @@
package org.springframework.data.elasticsearch.annotations;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.data.annotation.ReadOnlyProperty;
/**
* Specifies that this field is used for storing the document score.
*
* @author Sascha Woo
* @since 3.1
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@Documented
@Inherited
@ReadOnlyProperty
public @interface Score {}
@@ -1,5 +1,5 @@
/* /*
* Copyright 2014 the original author or authors. * Copyright 2014-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -0,0 +1,102 @@
/*
* Copyright 2018-2019 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
*
* http://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.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
import org.elasticsearch.common.transport.TransportAddress;
import org.springframework.data.util.Streamable;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
* Value object to represent a list of cluster nodes.
*
* @author Oliver Gierke
* @since 3.1
*/
class ClusterNodes implements Streamable<TransportAddress> {
public static ClusterNodes DEFAULT = ClusterNodes.of("127.0.0.1:9300");
private static final String COLON = ":";
private static final String COMMA = ",";
private final List<TransportAddress> clusterNodes;
/**
* Creates a new {@link ClusterNodes} by parsing the given source.
*
* @param source must not be {@literal null} or empty.
*/
private ClusterNodes(String source) {
Assert.hasText(source, "Cluster nodes source must not be null or empty!");
String[] nodes = StringUtils.delimitedListToStringArray(source, COMMA);
this.clusterNodes = Arrays.stream(nodes).map(node -> {
String[] segments = StringUtils.delimitedListToStringArray(node, COLON);
Assert.isTrue(segments.length == 2,
() -> String.format("Invalid cluster node %s in %s! Must be in the format host:port!", node, source));
String host = segments[0].trim();
String port = segments[1].trim();
Assert.hasText(host, () -> String.format("No host name given cluster node %s!", node));
Assert.hasText(port, () -> String.format("No port given in cluster node %s!", node));
return new TransportAddress(toInetAddress(host), Integer.valueOf(port));
}).collect(Collectors.toList());
}
/**
* Creates a new {@link ClusterNodes} by parsing the given source. The expected format is a comma separated list of
* host-port-combinations separated by a colon: {@code host:port,host:port,…}.
*
* @param source must not be {@literal null} or empty.
* @return
*/
public static ClusterNodes of(String source) {
return new ClusterNodes(source);
}
/*
* (non-Javadoc)
* @see java.lang.Iterable#iterator()
*/
@Override
public Iterator<TransportAddress> iterator() {
return clusterNodes.iterator();
}
private static InetAddress toInetAddress(String host) {
try {
return InetAddress.getByName(host);
} catch (UnknownHostException o_O) {
throw new IllegalArgumentException(o_O);
}
}
}
@@ -1,5 +1,5 @@
/* /*
* Copyright 2015-2017 the original author or authors. * Copyright 2015-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -18,7 +18,6 @@ package org.springframework.data.elasticsearch.client;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.Collection; import java.util.Collection;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.client.Client; import org.elasticsearch.client.Client;
import org.elasticsearch.client.node.NodeClient; import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
@@ -31,6 +30,8 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.StringUtils;
import static java.util.Arrays.*; import static java.util.Arrays.*;
/** /**
@@ -38,6 +39,7 @@ import static java.util.Arrays.*;
* *
* @author Rizwan Idrees * @author Rizwan Idrees
* @author Mohsin Husen * @author Mohsin Husen
* @author Ilkang Na
*/ */
public class NodeClientFactoryBean implements FactoryBean<Client>, InitializingBean, DisposableBean { public class NodeClientFactoryBean implements FactoryBean<Client>, InitializingBean, DisposableBean {
@@ -85,21 +87,19 @@ public class NodeClientFactoryBean implements FactoryBean<Client>, InitializingB
nodeClient = (NodeClient) new TestNode( nodeClient = (NodeClient) new TestNode(
Settings.builder().put(loadConfig()) Settings.builder().put(loadConfig())
.put("transport.type", "netty4") .put("transport.type", "netty4")
.put("transport.type", "local")
.put("http.type", "netty4") .put("http.type", "netty4")
.put("path.home", this.pathHome) .put("path.home", this.pathHome)
.put("path.data", this.pathData) .put("path.data", this.pathData)
.put("cluster.name", this.clusterName) .put("cluster.name", this.clusterName)
.put("node.max_local_storage_nodes", 100) .put("node.max_local_storage_nodes", 100)
.put("script.inline", "true")
.build(), asList(Netty4Plugin.class)).start().client(); .build(), asList(Netty4Plugin.class)).start().client();
} }
private Settings loadConfig() throws IOException { private Settings loadConfig() throws IOException {
if (StringUtils.isNotBlank(pathConfiguration)) { if (!StringUtils.isEmpty(pathConfiguration)) {
InputStream stream = getClass().getClassLoader().getResourceAsStream(pathConfiguration); InputStream stream = getClass().getClassLoader().getResourceAsStream(pathConfiguration);
if (stream != null) { if (stream != null) {
return Settings.builder().loadFromStream(pathConfiguration, getClass().getClassLoader().getResourceAsStream(pathConfiguration)).build(); return Settings.builder().loadFromStream(pathConfiguration, getClass().getClassLoader().getResourceAsStream(pathConfiguration), false).build();
} }
logger.error(String.format("Unable to read node configuration from file [%s]", pathConfiguration)); logger.error(String.format("Unable to read node configuration from file [%s]", pathConfiguration));
} }
@@ -1,156 +1,152 @@
/* /*
* Copyright 2013 the original author or authors. * Copyright 2013-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.data.elasticsearch.client; package org.springframework.data.elasticsearch.client;
import static org.apache.commons.lang.StringUtils.*; import java.util.Properties;
import java.net.InetAddress; import org.elasticsearch.client.transport.TransportClient;
import java.util.Properties; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.elasticsearch.client.transport.TransportClient; import org.slf4j.Logger;
import org.elasticsearch.common.settings.Settings; import org.slf4j.LoggerFactory;
import org.elasticsearch.common.transport.InetSocketTransportAddress; import org.springframework.beans.factory.DisposableBean;
import org.elasticsearch.transport.client.PreBuiltTransportClient; import org.springframework.beans.factory.FactoryBean;
import org.slf4j.Logger; import org.springframework.beans.factory.InitializingBean;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean; /**
import org.springframework.beans.factory.FactoryBean; * TransportClientFactoryBean
import org.springframework.beans.factory.InitializingBean; *
import org.springframework.util.Assert; * @author Rizwan Idrees
* @author Mohsin Husen
/** * @author Jakub Vavrik
* TransportClientFactoryBean * @author Piotr Betkier
* * @author Ilkang Na
* @author Rizwan Idrees * @author Oliver Gierke
* @author Mohsin Husen */
* @author Jakub Vavrik public class TransportClientFactoryBean implements FactoryBean<TransportClient>, InitializingBean, DisposableBean {
* @author Piotr Betkier
*/ private static final Logger logger = LoggerFactory.getLogger(TransportClientFactoryBean.class);
private ClusterNodes clusterNodes = ClusterNodes.of("127.0.0.1:9300");
public class TransportClientFactoryBean implements FactoryBean<TransportClient>, InitializingBean, DisposableBean { private String clusterName = "elasticsearch";
private Boolean clientTransportSniff = true;
private static final Logger logger = LoggerFactory.getLogger(TransportClientFactoryBean.class); private Boolean clientIgnoreClusterName = Boolean.FALSE;
private String clusterNodes = "127.0.0.1:9300"; private String clientPingTimeout = "5s";
private String clusterName = "elasticsearch"; private String clientNodesSamplerInterval = "5s";
private Boolean clientTransportSniff = true; private TransportClient client;
private Boolean clientIgnoreClusterName = Boolean.FALSE; private Properties properties;
private String clientPingTimeout = "5s";
private String clientNodesSamplerInterval = "5s"; @Override
private TransportClient client; public void destroy() throws Exception {
private Properties properties; try {
static final String COLON = ":"; logger.info("Closing elasticSearch client");
static final String COMMA = ","; if (client != null) {
client.close();
@Override }
public void destroy() throws Exception { } catch (final Exception e) {
try { logger.error("Error closing ElasticSearch client: ", e);
logger.info("Closing elasticSearch client"); }
if (client != null) { }
client.close();
} @Override
} catch (final Exception e) { public TransportClient getObject() throws Exception {
logger.error("Error closing ElasticSearch client: ", e); return client;
} }
}
@Override
@Override public Class<TransportClient> getObjectType() {
public TransportClient getObject() throws Exception { return TransportClient.class;
return client; }
}
@Override
@Override public boolean isSingleton() {
public Class<TransportClient> getObjectType() { return false;
return TransportClient.class; }
}
@Override
@Override public void afterPropertiesSet() throws Exception {
public boolean isSingleton() { buildClient();
return false; }
}
protected void buildClient() throws Exception {
@Override
public void afterPropertiesSet() throws Exception { client = new PreBuiltTransportClient(settings());
buildClient();
} clusterNodes.stream() //
.peek(it -> logger.info("Adding transport node : " + it.toString())) //
protected void buildClient() throws Exception { .forEach(client::addTransportAddress);
client = new PreBuiltTransportClient(settings()); client.connectedNodes();
Assert.hasText(clusterNodes, "[Assertion failed] clusterNodes settings missing."); }
for (String clusterNode : split(clusterNodes, COMMA)) {
String hostName = substringBeforeLast(clusterNode, COLON); private Settings settings() {
String port = substringAfterLast(clusterNode, COLON); if (properties != null) {
Assert.hasText(hostName, "[Assertion failed] missing host name in 'clusterNodes'"); Settings.Builder builder = Settings.builder();
Assert.hasText(port, "[Assertion failed] missing port in 'clusterNodes'");
logger.info("adding transport node : " + clusterNode); properties.forEach((key, value) -> {
client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(hostName), Integer.valueOf(port))); builder.put(key.toString(), value.toString());
} });
client.connectedNodes();
} return builder.build();
}
private Settings settings() { return Settings.builder()
if (properties != null) { .put("cluster.name", clusterName)
return Settings.builder().put(properties).build(); .put("client.transport.sniff", clientTransportSniff)
} .put("client.transport.ignore_cluster_name", clientIgnoreClusterName)
return Settings.builder() .put("client.transport.ping_timeout", clientPingTimeout)
.put("cluster.name", clusterName) .put("client.transport.nodes_sampler_interval", clientNodesSamplerInterval)
.put("client.transport.sniff", clientTransportSniff) .build();
.put("client.transport.ignore_cluster_name", clientIgnoreClusterName) }
.put("client.transport.ping_timeout", clientPingTimeout)
.put("client.transport.nodes_sampler_interval", clientNodesSamplerInterval) public void setClusterNodes(String clusterNodes) {
.build(); this.clusterNodes = ClusterNodes.of(clusterNodes);
} }
public void setClusterNodes(String clusterNodes) { public void setClusterName(String clusterName) {
this.clusterNodes = clusterNodes; this.clusterName = clusterName;
} }
public void setClusterName(String clusterName) { public void setClientTransportSniff(Boolean clientTransportSniff) {
this.clusterName = clusterName; this.clientTransportSniff = clientTransportSniff;
} }
public void setClientTransportSniff(Boolean clientTransportSniff) { public String getClientNodesSamplerInterval() {
this.clientTransportSniff = clientTransportSniff; return clientNodesSamplerInterval;
} }
public String getClientNodesSamplerInterval() { public void setClientNodesSamplerInterval(String clientNodesSamplerInterval) {
return clientNodesSamplerInterval; this.clientNodesSamplerInterval = clientNodesSamplerInterval;
} }
public void setClientNodesSamplerInterval(String clientNodesSamplerInterval) { public String getClientPingTimeout() {
this.clientNodesSamplerInterval = clientNodesSamplerInterval; return clientPingTimeout;
} }
public String getClientPingTimeout() { public void setClientPingTimeout(String clientPingTimeout) {
return clientPingTimeout; this.clientPingTimeout = clientPingTimeout;
} }
public void setClientPingTimeout(String clientPingTimeout) { public Boolean getClientIgnoreClusterName() {
this.clientPingTimeout = clientPingTimeout; return clientIgnoreClusterName;
} }
public Boolean getClientIgnoreClusterName() { public void setClientIgnoreClusterName(Boolean clientIgnoreClusterName) {
return clientIgnoreClusterName; this.clientIgnoreClusterName = clientIgnoreClusterName;
} }
public void setClientIgnoreClusterName(Boolean clientIgnoreClusterName) { public void setProperties(Properties properties) {
this.clientIgnoreClusterName = clientIgnoreClusterName; this.properties = properties;
} }
}
public void setProperties(Properties properties) {
this.properties = properties;
}
}
@@ -1,41 +1,41 @@
/* /*
* Copyright 2013 the original author or authors. * Copyright 2013-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.data.elasticsearch.config; package org.springframework.data.elasticsearch.config;
import org.springframework.beans.factory.xml.NamespaceHandlerSupport; import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
import org.springframework.data.elasticsearch.repository.config.ElasticsearchRepositoryConfigExtension; import org.springframework.data.elasticsearch.repository.config.ElasticsearchRepositoryConfigExtension;
import org.springframework.data.repository.config.RepositoryBeanDefinitionParser; import org.springframework.data.repository.config.RepositoryBeanDefinitionParser;
import org.springframework.data.repository.config.RepositoryConfigurationExtension; import org.springframework.data.repository.config.RepositoryConfigurationExtension;
/** /**
* ElasticsearchNamespaceHandler * ElasticsearchNamespaceHandler
* *
* @author Rizwan Idrees * @author Rizwan Idrees
* @author Mohsin Husen * @author Mohsin Husen
*/ */
public class ElasticsearchNamespaceHandler extends NamespaceHandlerSupport { public class ElasticsearchNamespaceHandler extends NamespaceHandlerSupport {
@Override @Override
public void init() { public void init() {
RepositoryConfigurationExtension extension = new ElasticsearchRepositoryConfigExtension(); RepositoryConfigurationExtension extension = new ElasticsearchRepositoryConfigExtension();
RepositoryBeanDefinitionParser parser = new RepositoryBeanDefinitionParser(extension); RepositoryBeanDefinitionParser parser = new RepositoryBeanDefinitionParser(extension);
registerBeanDefinitionParser("repositories", parser); registerBeanDefinitionParser("repositories", parser);
registerBeanDefinitionParser("node-client", new NodeClientBeanDefinitionParser()); registerBeanDefinitionParser("node-client", new NodeClientBeanDefinitionParser());
registerBeanDefinitionParser("transport-client", new TransportClientBeanDefinitionParser()); registerBeanDefinitionParser("transport-client", new TransportClientBeanDefinitionParser());
} }
} }
@@ -1,5 +1,5 @@
/* /*
* Copyright 2015 the original author or authors. * Copyright 2015-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -1,56 +1,56 @@
/* /*
* Copyright 2013 the original author or authors. * Copyright 2013-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.data.elasticsearch.config; package org.springframework.data.elasticsearch.config;
import org.springframework.beans.factory.support.AbstractBeanDefinition; import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser; import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext; import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.data.elasticsearch.client.TransportClientFactoryBean; import org.springframework.data.elasticsearch.client.TransportClientFactoryBean;
import org.w3c.dom.Element; import org.w3c.dom.Element;
/** /**
* TransportClientBeanDefinitionParser * TransportClientBeanDefinitionParser
* *
* @author Rizwan Idrees * @author Rizwan Idrees
* @author Mohsin Husen * @author Mohsin Husen
*/ */
public class TransportClientBeanDefinitionParser extends AbstractBeanDefinitionParser { public class TransportClientBeanDefinitionParser extends AbstractBeanDefinitionParser {
@Override @Override
protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) { protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {
BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(TransportClientFactoryBean.class); BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(TransportClientFactoryBean.class);
setConfigurations(element, builder); setConfigurations(element, builder);
return getSourcedBeanDefinition(builder, element, parserContext); return getSourcedBeanDefinition(builder, element, parserContext);
} }
private void setConfigurations(Element element, BeanDefinitionBuilder builder) { private void setConfigurations(Element element, BeanDefinitionBuilder builder) {
builder.addPropertyValue("clusterNodes", element.getAttribute("cluster-nodes")); builder.addPropertyValue("clusterNodes", element.getAttribute("cluster-nodes"));
builder.addPropertyValue("clusterName", element.getAttribute("cluster-name")); builder.addPropertyValue("clusterName", element.getAttribute("cluster-name"));
builder.addPropertyValue("clientTransportSniff", Boolean.valueOf(element.getAttribute("client-transport-sniff"))); builder.addPropertyValue("clientTransportSniff", Boolean.valueOf(element.getAttribute("client-transport-sniff")));
builder.addPropertyValue("clientIgnoreClusterName", Boolean.valueOf(element.getAttribute("client-transport-ignore-cluster-name"))); builder.addPropertyValue("clientIgnoreClusterName", Boolean.valueOf(element.getAttribute("client-transport-ignore-cluster-name")));
builder.addPropertyValue("clientPingTimeout", element.getAttribute("client-transport-ping-timeout")); builder.addPropertyValue("clientPingTimeout", element.getAttribute("client-transport-ping-timeout"));
builder.addPropertyValue("clientNodesSamplerInterval", element.getAttribute("client-transport-nodes-sampler-interval")); builder.addPropertyValue("clientNodesSamplerInterval", element.getAttribute("client-transport-nodes-sampler-interval"));
} }
private AbstractBeanDefinition getSourcedBeanDefinition(BeanDefinitionBuilder builder, Element source, private AbstractBeanDefinition getSourcedBeanDefinition(BeanDefinitionBuilder builder, Element source,
ParserContext context) { ParserContext context) {
AbstractBeanDefinition definition = builder.getBeanDefinition(); AbstractBeanDefinition definition = builder.getBeanDefinition();
definition.setSource(context.extractSource(source)); definition.setSource(context.extractSource(source));
return definition; return definition;
} }
} }
@@ -1,5 +1,5 @@
/* /*
* Copyright 2014 the original author or authors. * Copyright 2014-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -15,11 +15,11 @@
*/ */
package org.springframework.data.elasticsearch.core; package org.springframework.data.elasticsearch.core;
import static org.apache.commons.lang.StringUtils.*;
import java.io.IOException; import java.io.IOException;
import org.springframework.data.elasticsearch.ElasticsearchException; import org.springframework.data.elasticsearch.ElasticsearchException;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/** /**
* @author Artur Konczak * @author Artur Konczak
@@ -29,11 +29,14 @@ public abstract class AbstractResultMapper implements ResultsMapper {
private EntityMapper entityMapper; private EntityMapper entityMapper;
public AbstractResultMapper(EntityMapper entityMapper) { public AbstractResultMapper(EntityMapper entityMapper) {
Assert.notNull(entityMapper, "EntityMapper must not be null!");
this.entityMapper = entityMapper; this.entityMapper = entityMapper;
} }
public <T> T mapEntity(String source, Class<T> clazz) { public <T> T mapEntity(String source, Class<T> clazz) {
if (isBlank(source)) { if (StringUtils.isEmpty(source)) {
return null; return null;
} }
try { try {
@@ -1,5 +1,5 @@
/* /*
* Copyright 2013-2017 the original author or authors. * Copyright 2013-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -1,206 +1,206 @@
/* /*
* Copyright 2013-2017 the original author or authors. * Copyright 2013-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.data.elasticsearch.core; package org.springframework.data.elasticsearch.core;
import static org.elasticsearch.index.query.Operator.AND; import static org.elasticsearch.index.query.Operator.AND;
import static org.elasticsearch.index.query.QueryBuilders.*; import static org.elasticsearch.index.query.QueryBuilders.*;
import static org.springframework.data.elasticsearch.core.query.Criteria.*; import static org.springframework.data.elasticsearch.core.query.Criteria.*;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.ListIterator; import java.util.ListIterator;
import org.apache.lucene.queryparser.flexible.core.util.StringUtils; import org.apache.lucene.queryparser.flexible.core.util.StringUtils;
import org.elasticsearch.index.query.*; import org.elasticsearch.index.query.*;
import org.springframework.data.elasticsearch.core.query.Criteria; import org.springframework.data.elasticsearch.core.query.Criteria;
import org.springframework.util.Assert; import org.springframework.util.Assert;
/** /**
* CriteriaQueryProcessor * CriteriaQueryProcessor
* *
* @author Rizwan Idrees * @author Rizwan Idrees
* @author Mohsin Husen * @author Mohsin Husen
* @author Franck Marchand * @author Franck Marchand
* @author Artur Konczak * @author Artur Konczak
*/ */
class CriteriaQueryProcessor { class CriteriaQueryProcessor {
QueryBuilder createQueryFromCriteria(Criteria criteria) { QueryBuilder createQueryFromCriteria(Criteria criteria) {
if (criteria == null) if (criteria == null)
return null; return null;
List<QueryBuilder> shouldQueryBuilderList = new LinkedList<>(); List<QueryBuilder> shouldQueryBuilderList = new LinkedList<>();
List<QueryBuilder> mustNotQueryBuilderList = new LinkedList<>(); List<QueryBuilder> mustNotQueryBuilderList = new LinkedList<>();
List<QueryBuilder> mustQueryBuilderList = new LinkedList<>(); List<QueryBuilder> mustQueryBuilderList = new LinkedList<>();
ListIterator<Criteria> chainIterator = criteria.getCriteriaChain().listIterator(); ListIterator<Criteria> chainIterator = criteria.getCriteriaChain().listIterator();
QueryBuilder firstQuery = null; QueryBuilder firstQuery = null;
boolean negateFirstQuery = false; boolean negateFirstQuery = false;
while (chainIterator.hasNext()) { while (chainIterator.hasNext()) {
Criteria chainedCriteria = chainIterator.next(); Criteria chainedCriteria = chainIterator.next();
QueryBuilder queryFragmentForCriteria = createQueryFragmentForCriteria(chainedCriteria); QueryBuilder queryFragmentForCriteria = createQueryFragmentForCriteria(chainedCriteria);
if (queryFragmentForCriteria != null) { if (queryFragmentForCriteria != null) {
if (firstQuery == null) { if (firstQuery == null) {
firstQuery = queryFragmentForCriteria; firstQuery = queryFragmentForCriteria;
negateFirstQuery = chainedCriteria.isNegating(); negateFirstQuery = chainedCriteria.isNegating();
continue; continue;
} }
if (chainedCriteria.isOr()) { if (chainedCriteria.isOr()) {
shouldQueryBuilderList.add(queryFragmentForCriteria); shouldQueryBuilderList.add(queryFragmentForCriteria);
} else if (chainedCriteria.isNegating()) { } else if (chainedCriteria.isNegating()) {
mustNotQueryBuilderList.add(queryFragmentForCriteria); mustNotQueryBuilderList.add(queryFragmentForCriteria);
} else { } else {
mustQueryBuilderList.add(queryFragmentForCriteria); mustQueryBuilderList.add(queryFragmentForCriteria);
} }
} }
} }
if (firstQuery != null) { if (firstQuery != null) {
if (!shouldQueryBuilderList.isEmpty() && mustNotQueryBuilderList.isEmpty() && mustQueryBuilderList.isEmpty()) { if (!shouldQueryBuilderList.isEmpty() && mustNotQueryBuilderList.isEmpty() && mustQueryBuilderList.isEmpty()) {
shouldQueryBuilderList.add(0, firstQuery); shouldQueryBuilderList.add(0, firstQuery);
} else { } else {
if (negateFirstQuery) { if (negateFirstQuery) {
mustNotQueryBuilderList.add(0, firstQuery); mustNotQueryBuilderList.add(0, firstQuery);
} else { } else {
mustQueryBuilderList.add(0, firstQuery); mustQueryBuilderList.add(0, firstQuery);
} }
} }
} }
BoolQueryBuilder query = null; BoolQueryBuilder query = null;
if (!shouldQueryBuilderList.isEmpty() || !mustNotQueryBuilderList.isEmpty() || !mustQueryBuilderList.isEmpty()) { if (!shouldQueryBuilderList.isEmpty() || !mustNotQueryBuilderList.isEmpty() || !mustQueryBuilderList.isEmpty()) {
query = boolQuery(); query = boolQuery();
for (QueryBuilder qb : shouldQueryBuilderList) { for (QueryBuilder qb : shouldQueryBuilderList) {
query.should(qb); query.should(qb);
} }
for (QueryBuilder qb : mustNotQueryBuilderList) { for (QueryBuilder qb : mustNotQueryBuilderList) {
query.mustNot(qb); query.mustNot(qb);
} }
for (QueryBuilder qb : mustQueryBuilderList) { for (QueryBuilder qb : mustQueryBuilderList) {
query.must(qb); query.must(qb);
} }
} }
return query; return query;
} }
private QueryBuilder createQueryFragmentForCriteria(Criteria chainedCriteria) { private QueryBuilder createQueryFragmentForCriteria(Criteria chainedCriteria) {
if (chainedCriteria.getQueryCriteriaEntries().isEmpty()) if (chainedCriteria.getQueryCriteriaEntries().isEmpty())
return null; return null;
Iterator<Criteria.CriteriaEntry> it = chainedCriteria.getQueryCriteriaEntries().iterator(); Iterator<Criteria.CriteriaEntry> it = chainedCriteria.getQueryCriteriaEntries().iterator();
boolean singeEntryCriteria = (chainedCriteria.getQueryCriteriaEntries().size() == 1); boolean singeEntryCriteria = (chainedCriteria.getQueryCriteriaEntries().size() == 1);
String fieldName = chainedCriteria.getField().getName(); String fieldName = chainedCriteria.getField().getName();
Assert.notNull(fieldName, "Unknown field"); Assert.notNull(fieldName, "Unknown field");
QueryBuilder query = null; QueryBuilder query = null;
if (singeEntryCriteria) { if (singeEntryCriteria) {
Criteria.CriteriaEntry entry = it.next(); Criteria.CriteriaEntry entry = it.next();
query = processCriteriaEntry(entry, fieldName); query = processCriteriaEntry(entry, fieldName);
} else { } else {
query = boolQuery(); query = boolQuery();
while (it.hasNext()) { while (it.hasNext()) {
Criteria.CriteriaEntry entry = it.next(); Criteria.CriteriaEntry entry = it.next();
((BoolQueryBuilder) query).must(processCriteriaEntry(entry, fieldName)); ((BoolQueryBuilder) query).must(processCriteriaEntry(entry, fieldName));
} }
} }
addBoost(query, chainedCriteria.getBoost()); addBoost(query, chainedCriteria.getBoost());
return query; return query;
} }
private QueryBuilder processCriteriaEntry(Criteria.CriteriaEntry entry,/* OperationKey key, Object value,*/ String fieldName) { private QueryBuilder processCriteriaEntry(Criteria.CriteriaEntry entry,/* OperationKey key, Object value,*/ String fieldName) {
Object value = entry.getValue(); Object value = entry.getValue();
if (value == null) { if (value == null) {
return null; return null;
} }
OperationKey key = entry.getKey(); OperationKey key = entry.getKey();
QueryBuilder query = null; QueryBuilder query = null;
String searchText = StringUtils.toString(value); String searchText = StringUtils.toString(value);
Iterable<Object> collection = null; Iterable<Object> collection = null;
switch (key) { switch (key) {
case EQUALS: case EQUALS:
query = queryStringQuery(searchText).field(fieldName).defaultOperator(AND); query = queryStringQuery(searchText).field(fieldName).defaultOperator(AND);
break; break;
case CONTAINS: case CONTAINS:
query = queryStringQuery("*" + searchText + "*").field(fieldName).analyzeWildcard(true); query = queryStringQuery("*" + searchText + "*").field(fieldName).analyzeWildcard(true);
break; break;
case STARTS_WITH: case STARTS_WITH:
query = queryStringQuery(searchText + "*").field(fieldName).analyzeWildcard(true); query = queryStringQuery(searchText + "*").field(fieldName).analyzeWildcard(true);
break; break;
case ENDS_WITH: case ENDS_WITH:
query = queryStringQuery("*" + searchText).field(fieldName).analyzeWildcard(true); query = queryStringQuery("*" + searchText).field(fieldName).analyzeWildcard(true);
break; break;
case EXPRESSION: case EXPRESSION:
query = queryStringQuery(searchText).field(fieldName); query = queryStringQuery(searchText).field(fieldName);
break; break;
case LESS_EQUAL: case LESS_EQUAL:
query = rangeQuery(fieldName).lte(value); query = rangeQuery(fieldName).lte(value);
break; break;
case GREATER_EQUAL: case GREATER_EQUAL:
query = rangeQuery(fieldName).gte(value); query = rangeQuery(fieldName).gte(value);
break; break;
case BETWEEN: case BETWEEN:
Object[] ranges = (Object[]) value; Object[] ranges = (Object[]) value;
query = rangeQuery(fieldName).from(ranges[0]).to(ranges[1]); query = rangeQuery(fieldName).from(ranges[0]).to(ranges[1]);
break; break;
case LESS: case LESS:
query = rangeQuery(fieldName).lt(value); query = rangeQuery(fieldName).lt(value);
break; break;
case GREATER: case GREATER:
query = rangeQuery(fieldName).gt(value); query = rangeQuery(fieldName).gt(value);
break; break;
case FUZZY: case FUZZY:
query = fuzzyQuery(fieldName, searchText); query = fuzzyQuery(fieldName, searchText);
break; break;
case IN: case IN:
query = boolQuery(); query = boolQuery();
collection = (Iterable<Object>) value; collection = (Iterable<Object>) value;
for (Object item : collection) { for (Object item : collection) {
((BoolQueryBuilder) query).should(queryStringQuery(item.toString()).field(fieldName)); ((BoolQueryBuilder) query).should(queryStringQuery(item.toString()).field(fieldName));
} }
break; break;
case NOT_IN: case NOT_IN:
query = boolQuery(); query = boolQuery();
collection = (Iterable<Object>) value; collection = (Iterable<Object>) value;
for (Object item : collection) { for (Object item : collection) {
((BoolQueryBuilder) query).mustNot(queryStringQuery(item.toString()).field(fieldName)); ((BoolQueryBuilder) query).mustNot(queryStringQuery(item.toString()).field(fieldName));
} }
break; break;
} }
return query; return query;
} }
private void addBoost(QueryBuilder query, float boost) { private void addBoost(QueryBuilder query, float boost) {
if (Float.isNaN(boost)) { if (Float.isNaN(boost)) {
return; return;
} }
query.boost(boost); query.boost(boost);
} }
} }
@@ -1,5 +1,5 @@
/* /*
* Copyright 2014 the original author or authors. * Copyright 2014-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -16,52 +16,142 @@
package org.springframework.data.elasticsearch.core; package org.springframework.data.elasticsearch.core;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import com.fasterxml.jackson.annotation.JsonIgnore; import org.springframework.data.annotation.ReadOnlyProperty;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.module.SimpleModule;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.core.geo.CustomGeoModule; import org.springframework.data.elasticsearch.core.geo.CustomGeoModule;
import org.springframework.data.geo.*; import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity;
import java.util.List; import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty;
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.util.Assert;
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.databind.BeanDescription;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.core.Version; import com.fasterxml.jackson.databind.SerializationConfig;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.module.SimpleModule; import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;
import com.fasterxml.jackson.databind.ser.BeanSerializerModifier;
/** /**
* DocumentMapper using jackson * EntityMapper based on a Jackson {@link ObjectMapper}.
* *
* @author Artur Konczak * @author Artur Konczak
* @author Petar Tahchiev * @author Petar Tahchiev
* @author Oliver Gierke
*/ */
public class DefaultEntityMapper implements EntityMapper { public class DefaultEntityMapper implements EntityMapper {
private ObjectMapper objectMapper; private ObjectMapper objectMapper;
public DefaultEntityMapper() { /**
* Creates a new {@link DefaultEntityMapper} using the given {@link MappingContext}.
*
* @param context must not be {@literal null}.
*/
public DefaultEntityMapper(
MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> context) {
Assert.notNull(context, "MappingContext must not be null!");
objectMapper = new ObjectMapper(); objectMapper = new ObjectMapper();
objectMapper.registerModule(new SpringDataElasticsearchModule(context));
objectMapper.registerModule(new CustomGeoModule());
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
objectMapper.registerModule(new CustomGeoModule());
} }
/*
* (non-Javadoc)
* @see org.springframework.data.elasticsearch.core.EntityMapper#mapToString(java.lang.Object)
*/
@Override @Override
public String mapToString(Object object) throws IOException { public String mapToString(Object object) throws IOException {
return objectMapper.writeValueAsString(object); return objectMapper.writeValueAsString(object);
} }
/*
* (non-Javadoc)
* @see org.springframework.data.elasticsearch.core.EntityMapper#mapToObject(java.lang.String, java.lang.Class)
*/
@Override @Override
public <T> T mapToObject(String source, Class<T> clazz) throws IOException { public <T> T mapToObject(String source, Class<T> clazz) throws IOException {
return objectMapper.readValue(source, clazz); return objectMapper.readValue(source, clazz);
} }
/**
* A simple Jackson module to register the {@link SpringDataSerializerModifier}.
*
* @author Oliver Gierke
* @since 3.1
*/
private static class SpringDataElasticsearchModule extends SimpleModule {
private static final long serialVersionUID = -9168968092458058966L;
/**
* Creates a new {@link SpringDataElasticsearchModule} using the given {@link MappingContext}.
*
* @param context must not be {@literal null}.
*/
public SpringDataElasticsearchModule(
MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> context) {
Assert.notNull(context, "MappingContext must not be null!");
setSerializerModifier(new SpringDataSerializerModifier(context));
}
/**
* A {@link BeanSerializerModifier} that will drop properties annotated with {@link ReadOnlyProperty} for
* serialization.
*
* @author Oliver Gierke
* @since 3.1
*/
private static class SpringDataSerializerModifier extends BeanSerializerModifier {
private final MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> context;
public SpringDataSerializerModifier(
MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> context) {
Assert.notNull(context, "MappingContext must not be null!");
this.context = context;
}
/*
* (non-Javadoc)
* @see com.fasterxml.jackson.databind.ser.BeanSerializerModifier#changeProperties(com.fasterxml.jackson.databind.SerializationConfig, com.fasterxml.jackson.databind.BeanDescription, java.util.List)
*/
@Override
public List<BeanPropertyWriter> changeProperties(SerializationConfig config, BeanDescription description,
List<BeanPropertyWriter> properties) {
Class<?> type = description.getBeanClass();
ElasticsearchPersistentEntity<?> entity = context.getPersistentEntity(type);
if (entity == null) {
return super.changeProperties(config, description, properties);
}
List<BeanPropertyWriter> result = new ArrayList<>(properties.size());
for (BeanPropertyWriter beanPropertyWriter : properties) {
ElasticsearchPersistentProperty property = entity.getPersistentProperty(beanPropertyWriter.getName());
if (property != null && property.isWritable()) {
result.add(beanPropertyWriter);
}
}
return result;
}
}
}
} }
@@ -1,5 +1,5 @@
/* /*
* Copyright 2014-2017 the original author or authors. * Copyright 2014-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -23,13 +23,12 @@ import java.util.Collection;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.get.MultiGetItemResponse; import org.elasticsearch.action.get.MultiGetItemResponse;
import org.elasticsearch.action.get.MultiGetResponse; import org.elasticsearch.action.get.MultiGetResponse;
import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.common.document.DocumentField;
import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHitField;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.ElasticsearchException; import org.springframework.data.elasticsearch.ElasticsearchException;
import org.springframework.data.elasticsearch.annotations.Document; import org.springframework.data.elasticsearch.annotations.Document;
@@ -38,62 +37,84 @@ import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
import org.springframework.data.elasticsearch.core.aggregation.impl.AggregatedPageImpl; import org.springframework.data.elasticsearch.core.aggregation.impl.AggregatedPageImpl;
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity; import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity;
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty; import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty;
import org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext;
import org.springframework.data.mapping.context.MappingContext; import org.springframework.data.mapping.context.MappingContext;
import org.springframework.util.Assert;
import com.fasterxml.jackson.core.JsonEncoding; import com.fasterxml.jackson.core.JsonEncoding;
import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonGenerator;
import org.springframework.util.StringUtils;
/** /**
* @author Artur Konczak * @author Artur Konczak
* @author Petar Tahchiev * @author Petar Tahchiev
* @author Young Gu * @author Young Gu
* @author Oliver Gierke * @author Oliver Gierke
* @author Chris White
* @author Mark Paluch * @author Mark Paluch
* @author Ilkang Na
* @author Sascha Woo
*/ */
public class DefaultResultMapper extends AbstractResultMapper { public class DefaultResultMapper extends AbstractResultMapper {
private MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext; private final MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext;
public DefaultResultMapper() { public DefaultResultMapper() {
super(new DefaultEntityMapper()); this(new SimpleElasticsearchMappingContext());
} }
public DefaultResultMapper(MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext) { public DefaultResultMapper(MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext) {
super(new DefaultEntityMapper());
super(new DefaultEntityMapper(mappingContext));
Assert.notNull(mappingContext, "MappingContext must not be null!");
this.mappingContext = mappingContext; this.mappingContext = mappingContext;
} }
public DefaultResultMapper(EntityMapper entityMapper) { public DefaultResultMapper(EntityMapper entityMapper) {
super(entityMapper); this(new SimpleElasticsearchMappingContext(), entityMapper);
} }
public DefaultResultMapper( public DefaultResultMapper(
MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext, MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext,
EntityMapper entityMapper) { EntityMapper entityMapper) {
super(entityMapper); super(entityMapper);
Assert.notNull(mappingContext, "MappingContext must not be null!");
this.mappingContext = mappingContext; this.mappingContext = mappingContext;
} }
@Override @Override
public <T> AggregatedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) { public <T> AggregatedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) {
long totalHits = response.getHits().totalHits();
long totalHits = response.getHits().getTotalHits();
float maxScore = response.getHits().getMaxScore();
List<T> results = new ArrayList<>(); List<T> results = new ArrayList<>();
for (SearchHit hit : response.getHits()) { for (SearchHit hit : response.getHits()) {
if (hit != null) { if (hit != null) {
T result = null; T result = null;
if (StringUtils.isNotBlank(hit.sourceAsString())) { if (!StringUtils.isEmpty(hit.getSourceAsString())) {
result = mapEntity(hit.sourceAsString(), clazz); result = mapEntity(hit.getSourceAsString(), clazz);
} else { } else {
result = mapEntity(hit.getFields().values(), clazz); result = mapEntity(hit.getFields().values(), clazz);
} }
setPersistentEntityId(result, hit.getId(), clazz); setPersistentEntityId(result, hit.getId(), clazz);
setPersistentEntityVersion(result, hit.getVersion(), clazz);
setPersistentEntityScore(result, hit.getScore(), clazz);
populateScriptFields(result, hit); populateScriptFields(result, hit);
results.add(result); results.add(result);
} }
} }
return new AggregatedPageImpl<T>(results, pageable, totalHits, response.getAggregations(), response.getScrollId()); return new AggregatedPageImpl<T>(results, pageable, totalHits, response.getAggregations(), response.getScrollId(),
maxScore);
} }
private <T> void populateScriptFields(T result, SearchHit hit) { private <T> void populateScriptFields(T result, SearchHit hit) {
@@ -102,14 +123,14 @@ public class DefaultResultMapper extends AbstractResultMapper {
ScriptedField scriptedField = field.getAnnotation(ScriptedField.class); ScriptedField scriptedField = field.getAnnotation(ScriptedField.class);
if (scriptedField != null) { if (scriptedField != null) {
String name = scriptedField.name().isEmpty() ? field.getName() : scriptedField.name(); String name = scriptedField.name().isEmpty() ? field.getName() : scriptedField.name();
SearchHitField searchHitField = hit.getFields().get(name); DocumentField searchHitField = hit.getFields().get(name);
if (searchHitField != null) { if (searchHitField != null) {
field.setAccessible(true); field.setAccessible(true);
try { try {
field.set(result, searchHitField.getValue()); field.set(result, searchHitField.getValue());
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
throw new ElasticsearchException("failed to set scripted field: " + name + " with value: " throw new ElasticsearchException(
+ searchHitField.getValue(), e); "failed to set scripted field: " + name + " with value: " + searchHitField.getValue(), e);
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
throw new ElasticsearchException("failed to access scripted field: " + name, e); throw new ElasticsearchException("failed to access scripted field: " + name, e);
} }
@@ -119,17 +140,17 @@ public class DefaultResultMapper extends AbstractResultMapper {
} }
} }
private <T> T mapEntity(Collection<SearchHitField> values, Class<T> clazz) { private <T> T mapEntity(Collection<DocumentField> values, Class<T> clazz) {
return mapEntity(buildJSONFromFields(values), clazz); return mapEntity(buildJSONFromFields(values), clazz);
} }
private String buildJSONFromFields(Collection<SearchHitField> values) { private String buildJSONFromFields(Collection<DocumentField> values) {
JsonFactory nodeFactory = new JsonFactory(); JsonFactory nodeFactory = new JsonFactory();
try { try {
ByteArrayOutputStream stream = new ByteArrayOutputStream(); ByteArrayOutputStream stream = new ByteArrayOutputStream();
JsonGenerator generator = nodeFactory.createGenerator(stream, JsonEncoding.UTF8); JsonGenerator generator = nodeFactory.createGenerator(stream, JsonEncoding.UTF8);
generator.writeStartObject(); generator.writeStartObject();
for (SearchHitField value : values) { for (DocumentField value : values) {
if (value.getValues().size() > 1) { if (value.getValues().size() > 1) {
generator.writeArrayFieldStart(value.getName()); generator.writeArrayFieldStart(value.getName());
for (Object val : value.getValues()) { for (Object val : value.getValues()) {
@@ -153,6 +174,7 @@ public class DefaultResultMapper extends AbstractResultMapper {
T result = mapEntity(response.getSourceAsString(), clazz); T result = mapEntity(response.getSourceAsString(), clazz);
if (result != null) { if (result != null) {
setPersistentEntityId(result, response.getId(), clazz); setPersistentEntityId(result, response.getId(), clazz);
setPersistentEntityVersion(result, response.getVersion(), clazz);
} }
return result; return result;
} }
@@ -164,6 +186,7 @@ public class DefaultResultMapper extends AbstractResultMapper {
if (!response.isFailed() && response.getResponse().isExists()) { if (!response.isFailed() && response.getResponse().isExists()) {
T result = mapEntity(response.getResponse().getSourceAsString(), clazz); T result = mapEntity(response.getResponse().getSourceAsString(), clazz);
setPersistentEntityId(result, response.getResponse().getId(), clazz); setPersistentEntityId(result, response.getResponse().getId(), clazz);
setPersistentEntityVersion(result, response.getResponse().getVersion(), clazz);
list.add(result); list.add(result);
} }
} }
@@ -171,9 +194,9 @@ public class DefaultResultMapper extends AbstractResultMapper {
} }
private <T> void setPersistentEntityId(T result, String id, Class<T> clazz) { private <T> void setPersistentEntityId(T result, String id, Class<T> clazz) {
if (mappingContext != null && clazz.isAnnotationPresent(Document.class)) { if (clazz.isAnnotationPresent(Document.class)) {
ElasticsearchPersistentEntity<?> persistentEntity = mappingContext.getRequiredPersistentEntity(clazz); ElasticsearchPersistentEntity<?> persistentEntity = mappingContext.getRequiredPersistentEntity(clazz);
ElasticsearchPersistentProperty idProperty = persistentEntity.getIdProperty(); ElasticsearchPersistentProperty idProperty = persistentEntity.getIdProperty();
@@ -181,7 +204,38 @@ public class DefaultResultMapper extends AbstractResultMapper {
if (idProperty != null && idProperty.getType().isAssignableFrom(String.class)) { if (idProperty != null && idProperty.getType().isAssignableFrom(String.class)) {
persistentEntity.getPropertyAccessor(result).setProperty(idProperty, id); persistentEntity.getPropertyAccessor(result).setProperty(idProperty, id);
} }
}
}
private <T> void setPersistentEntityVersion(T result, long version, Class<T> clazz) {
if (clazz.isAnnotationPresent(Document.class)) {
ElasticsearchPersistentEntity<?> persistentEntity = mappingContext.getPersistentEntity(clazz);
ElasticsearchPersistentProperty versionProperty = persistentEntity.getVersionProperty();
// Only deal with Long because ES versions are longs !
if (versionProperty != null && versionProperty.getType().isAssignableFrom(Long.class)) {
// check that a version was actually returned in the response, -1 would indicate that
// a search didn't request the version ids in the response, which would be an issue
Assert.isTrue(version != -1, "Version in response is -1");
persistentEntity.getPropertyAccessor(result).setProperty(versionProperty, version);
}
}
}
private <T> void setPersistentEntityScore(T result, float score, Class<T> clazz) {
if (clazz.isAnnotationPresent(Document.class)) {
ElasticsearchPersistentEntity<?> entity = mappingContext.getRequiredPersistentEntity(clazz);
if (!entity.hasScoreProperty()) {
return;
}
entity.getPropertyAccessor(result) //
.setProperty(entity.getScoreProperty(), score);
} }
} }
} }
File diff suppressed because it is too large Load Diff
@@ -1,5 +1,5 @@
/* /*
* Copyright 2013-2017 the original author or authors. * Copyright 2013-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -15,6 +15,12 @@
*/ */
package org.springframework.data.elasticsearch.core; package org.springframework.data.elasticsearch.core;
import static org.elasticsearch.client.Requests.*;
import static org.elasticsearch.index.VersionType.*;
import static org.elasticsearch.index.query.QueryBuilders.*;
import static org.springframework.data.elasticsearch.core.MappingBuilder.*;
import static org.springframework.util.CollectionUtils.*;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
@@ -26,7 +32,9 @@ import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import org.elasticsearch.action.ListenableActionFuture; import java.util.stream.Collectors;
import org.elasticsearch.action.ActionFuture;
import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest; import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest;
import org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest; import org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder; import org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder;
@@ -44,6 +52,7 @@ import org.elasticsearch.action.get.MultiGetResponse;
import org.elasticsearch.action.index.IndexRequestBuilder; import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.search.SearchRequestBuilder; import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchScrollRequestBuilder;
import org.elasticsearch.action.update.UpdateRequestBuilder; import org.elasticsearch.action.update.UpdateRequestBuilder;
import org.elasticsearch.action.update.UpdateResponse; import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.Client; import org.elasticsearch.client.Client;
@@ -51,15 +60,20 @@ import org.elasticsearch.client.Requests;
import org.elasticsearch.cluster.metadata.AliasMetaData; import org.elasticsearch.cluster.metadata.AliasMetaData;
import org.elasticsearch.common.Nullable; import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.collect.MapBuilder; import org.elasticsearch.common.collect.MapBuilder;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.MoreLikeThisQueryBuilder; import org.elasticsearch.index.query.MoreLikeThisQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.aggregations.AbstractAggregationBuilder; import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder; import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.ScoreSortBuilder;
import org.elasticsearch.search.sort.SortBuilder; import org.elasticsearch.search.sort.SortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder; import org.elasticsearch.search.sort.SortOrder;
import org.elasticsearch.search.suggest.SuggestBuilder; import org.elasticsearch.search.suggest.SuggestBuilder;
import org.slf4j.Logger; import org.slf4j.Logger;
@@ -84,15 +98,24 @@ import org.springframework.data.elasticsearch.core.facet.FacetRequest;
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity; import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity;
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty; import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty;
import org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext; import org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext;
import org.springframework.data.elasticsearch.core.query.*; import org.springframework.data.elasticsearch.core.query.AliasQuery;
import org.springframework.data.elasticsearch.core.query.CriteriaQuery;
import org.springframework.data.elasticsearch.core.query.DeleteQuery;
import org.springframework.data.elasticsearch.core.query.FetchSourceFilter;
import org.springframework.data.elasticsearch.core.query.GetQuery;
import org.springframework.data.elasticsearch.core.query.IndexBoost;
import org.springframework.data.elasticsearch.core.query.IndexQuery;
import org.springframework.data.elasticsearch.core.query.MoreLikeThisQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.Query;
import org.springframework.data.elasticsearch.core.query.ScriptField;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.data.elasticsearch.core.query.SourceFilter;
import org.springframework.data.elasticsearch.core.query.StringQuery;
import org.springframework.data.elasticsearch.core.query.UpdateQuery;
import org.springframework.data.util.CloseableIterator; import org.springframework.data.util.CloseableIterator;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import static org.apache.commons.lang.StringUtils.*; import org.springframework.util.StringUtils;
import static org.elasticsearch.client.Requests.*;
import static org.elasticsearch.index.VersionType.*;
import static org.elasticsearch.index.query.QueryBuilders.*;
import static org.springframework.data.elasticsearch.core.MappingBuilder.*;
import static org.springframework.util.CollectionUtils.isEmpty;
/** /**
* ElasticsearchTemplate * ElasticsearchTemplate
@@ -105,11 +128,20 @@ import static org.springframework.util.CollectionUtils.isEmpty;
* @author Young Gu * @author Young Gu
* @author Oliver Gierke * @author Oliver Gierke
* @author Mark Janssen * @author Mark Janssen
* @author Chris White
* @author Mark Paluch * @author Mark Paluch
* @author Ilkang Na
* @author Alen Turkovic
* @author Sascha Woo
* @author Ted Liang
* @author Jean-Baptiste Nizet
*/ */
public class ElasticsearchTemplate implements ElasticsearchOperations, ApplicationContextAware { public class ElasticsearchTemplate implements ElasticsearchOperations, ApplicationContextAware {
private static final Logger logger = LoggerFactory.getLogger(ElasticsearchTemplate.class); private static final Logger QUERY_LOGGER = LoggerFactory.getLogger("org.springframework.data.elasticsearch.core.QUERY");
private static final Logger LOGGER = LoggerFactory.getLogger(ElasticsearchTemplate.class);
private static final String FIELD_SCORE = "_score";
private Client client; private Client client;
private ElasticsearchConverter elasticsearchConverter; private ElasticsearchConverter elasticsearchConverter;
private ResultsMapper resultsMapper; private ResultsMapper resultsMapper;
@@ -173,13 +205,13 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
public <T> boolean putMapping(Class<T> clazz) { public <T> boolean putMapping(Class<T> clazz) {
if (clazz.isAnnotationPresent(Mapping.class)) { if (clazz.isAnnotationPresent(Mapping.class)) {
String mappingPath = clazz.getAnnotation(Mapping.class).mappingPath(); String mappingPath = clazz.getAnnotation(Mapping.class).mappingPath();
if (isNotBlank(mappingPath)) { if (!StringUtils.isEmpty(mappingPath)) {
String mappings = readFileFromClasspath(mappingPath); String mappings = readFileFromClasspath(mappingPath);
if (isNotBlank(mappings)) { if (!StringUtils.isEmpty(mappings)) {
return putMapping(clazz, mappings); return putMapping(clazz, mappings);
} }
} else { } else {
logger.info("mappingPath in @Mapping has to be defined. Building mappings using @Field"); LOGGER.info("mappingPath in @Mapping has to be defined. Building mappings using @Field");
} }
} }
ElasticsearchPersistentEntity<T> persistentEntity = getPersistentEntityFor(clazz); ElasticsearchPersistentEntity<T> persistentEntity = getPersistentEntityFor(clazz);
@@ -208,7 +240,7 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
Assert.notNull(type, "No type defined for putMapping()"); Assert.notNull(type, "No type defined for putMapping()");
PutMappingRequestBuilder requestBuilder = client.admin().indices().preparePutMapping(indexName).setType(type); PutMappingRequestBuilder requestBuilder = client.admin().indices().preparePutMapping(indexName).setType(type);
if (mapping instanceof String) { if (mapping instanceof String) {
requestBuilder.setSource(String.valueOf(mapping)); requestBuilder.setSource(String.valueOf(mapping), XContentType.JSON);
} else if (mapping instanceof Map) { } else if (mapping instanceof Map) {
requestBuilder.setSource((Map) mapping); requestBuilder.setSource((Map) mapping);
} else if (mapping instanceof XContentBuilder) { } else if (mapping instanceof XContentBuilder) {
@@ -310,7 +342,7 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
if (query.getFilter() != null) { if (query.getFilter() != null) {
request.setPostFilter(query.getFilter()); request.setPostFilter(query.getFilter());
} }
SearchResponse response = getSearchResponse(request.execute()); SearchResponse response = getSearchResponse(request);
return extractIds(response); return extractIds(response);
} }
@@ -333,11 +365,8 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
if (elasticsearchFilter != null) if (elasticsearchFilter != null)
searchRequestBuilder.setPostFilter(elasticsearchFilter); searchRequestBuilder.setPostFilter(elasticsearchFilter);
if (logger.isDebugEnabled()) {
logger.debug("doSearch query:\n" + searchRequestBuilder.toString());
}
SearchResponse response = getSearchResponse(searchRequestBuilder.execute()); SearchResponse response = getSearchResponse(searchRequestBuilder);
return resultsMapper.mapResults(response, clazz, criteriaQuery.getPageable()); return resultsMapper.mapResults(response, clazz, criteriaQuery.getPageable());
} }
@@ -348,7 +377,7 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
@Override @Override
public <T> Page<T> queryForPage(StringQuery query, Class<T> clazz, SearchResultMapper mapper) { public <T> Page<T> queryForPage(StringQuery query, Class<T> clazz, SearchResultMapper mapper) {
SearchResponse response = getSearchResponse(prepareSearch(query, clazz).setQuery(wrapperQuery(query.getSource())).execute()); SearchResponse response = getSearchResponse(prepareSearch(query, clazz).setQuery(wrapperQuery(query.getSource())));
return mapper.mapResults(response, clazz, query.getPageable()); return mapper.mapResults(response, clazz, query.getPageable());
} }
@@ -559,9 +588,9 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
} }
private UpdateRequestBuilder prepareUpdate(UpdateQuery query) { private UpdateRequestBuilder prepareUpdate(UpdateQuery query) {
String indexName = isNotBlank(query.getIndexName()) ? query.getIndexName() String indexName = !StringUtils.isEmpty(query.getIndexName()) ? query.getIndexName()
: getPersistentEntityFor(query.getClazz()).getIndexName(); : getPersistentEntityFor(query.getClazz()).getIndexName();
String type = isNotBlank(query.getType()) ? query.getType() String type = !StringUtils.isEmpty(query.getType()) ? query.getType()
: getPersistentEntityFor(query.getClazz()).getIndexType(); : getPersistentEntityFor(query.getClazz()).getIndexType();
Assert.notNull(indexName, "No index defined for Query"); Assert.notNull(indexName, "No index defined for Query");
Assert.notNull(type, "No type define for Query"); Assert.notNull(type, "No type define for Query");
@@ -661,9 +690,9 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
@Override @Override
public <T> void delete(DeleteQuery deleteQuery, Class<T> clazz) { public <T> void delete(DeleteQuery deleteQuery, Class<T> clazz) {
String indexName = isNotBlank(deleteQuery.getIndex()) ? deleteQuery.getIndex() String indexName = !StringUtils.isEmpty(deleteQuery.getIndex()) ? deleteQuery.getIndex()
: getPersistentEntityFor(clazz).getIndexName(); : getPersistentEntityFor(clazz).getIndexName();
String typeName = isNotBlank(deleteQuery.getType()) ? deleteQuery.getType() String typeName = !StringUtils.isEmpty(deleteQuery.getType()) ? deleteQuery.getType()
: getPersistentEntityFor(clazz).getIndexType(); : getPersistentEntityFor(clazz).getIndexType();
Integer pageSize = deleteQuery.getPageSize() != null ? deleteQuery.getPageSize() : 1000; Integer pageSize = deleteQuery.getPageSize() != null ? deleteQuery.getPageSize() : 1000;
Long scrollTimeInMillis = deleteQuery.getScrollTimeInMillis() != null ? deleteQuery.getScrollTimeInMillis() Long scrollTimeInMillis = deleteQuery.getScrollTimeInMillis() != null ? deleteQuery.getScrollTimeInMillis()
@@ -730,7 +759,10 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
private SearchRequestBuilder prepareScroll(Query query, long scrollTimeInMillis) { private SearchRequestBuilder prepareScroll(Query query, long scrollTimeInMillis) {
SearchRequestBuilder requestBuilder = client.prepareSearch(toArray(query.getIndices())) SearchRequestBuilder requestBuilder = client.prepareSearch(toArray(query.getIndices()))
.setTypes(toArray(query.getTypes())).setScroll(TimeValue.timeValueMillis(scrollTimeInMillis)).setFrom(0); .setTypes(toArray(query.getTypes()))
.setScroll(TimeValue.timeValueMillis(scrollTimeInMillis))
.setFrom(0)
.setVersion(true);
if(query.getPageable().isPaged()){ if(query.getPageable().isPaged()){
requestBuilder.setSize(query.getPageable().getPageSize()); requestBuilder.setSize(query.getPageable().getPageSize());
@@ -761,7 +793,7 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
requestBuilder.setPostFilter(elasticsearchFilter); requestBuilder.setPostFilter(elasticsearchFilter);
} }
return getSearchResponse(requestBuilder.execute()); return getSearchResponse(requestBuilder);
} }
private SearchResponse doScroll(SearchRequestBuilder requestBuilder, SearchQuery searchQuery) { private SearchResponse doScroll(SearchRequestBuilder requestBuilder, SearchQuery searchQuery) {
@@ -773,7 +805,7 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
requestBuilder.setPostFilter(searchQuery.getFilter()); requestBuilder.setPostFilter(searchQuery.getFilter());
} }
return getSearchResponse(requestBuilder.setQuery(searchQuery.getQuery()).execute()); return getSearchResponse(requestBuilder.setQuery(searchQuery.getQuery()));
} }
public <T> Page<T> startScroll(long scrollTimeInMillis, SearchQuery searchQuery, Class<T> clazz) { public <T> Page<T> startScroll(long scrollTimeInMillis, SearchQuery searchQuery, Class<T> clazz) {
@@ -817,8 +849,8 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
public <T> Page<T> moreLikeThis(MoreLikeThisQuery query, Class<T> clazz) { public <T> Page<T> moreLikeThis(MoreLikeThisQuery query, Class<T> clazz) {
ElasticsearchPersistentEntity persistentEntity = getPersistentEntityFor(clazz); ElasticsearchPersistentEntity persistentEntity = getPersistentEntityFor(clazz);
String indexName = isNotBlank(query.getIndexName()) ? query.getIndexName() : persistentEntity.getIndexName(); String indexName = !StringUtils.isEmpty(query.getIndexName()) ? query.getIndexName() : persistentEntity.getIndexName();
String type = isNotBlank(query.getType()) ? query.getType() : persistentEntity.getIndexType(); String type = !StringUtils.isEmpty(query.getType()) ? query.getType() : persistentEntity.getIndexType();
Assert.notNull(indexName, "No 'indexName' defined for MoreLikeThisQuery"); Assert.notNull(indexName, "No 'indexName' defined for MoreLikeThisQuery");
Assert.notNull(type, "No 'type' defined for MoreLikeThisQuery"); Assert.notNull(type, "No 'type' defined for MoreLikeThisQuery");
@@ -867,17 +899,22 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
} }
if (!searchQuery.getScriptFields().isEmpty()) { if (!searchQuery.getScriptFields().isEmpty()) {
//_source should be return all the time // _source should be return all the time
//searchRequest.addStoredField("_source"); // searchRequest.addStoredField("_source");
for (ScriptField scriptedField : searchQuery.getScriptFields()) { for (ScriptField scriptedField : searchQuery.getScriptFields()) {
searchRequest.addScriptField(scriptedField.fieldName(), scriptedField.script()); searchRequest.addScriptField(scriptedField.fieldName(), scriptedField.script());
} }
} }
if (searchQuery.getHighlightFields() != null) { if (searchQuery.getHighlightFields() != null || searchQuery.getHighlightBuilder() != null) {
for (HighlightBuilder.Field highlightField : searchQuery.getHighlightFields()) { HighlightBuilder highlightBuilder = searchQuery.getHighlightBuilder();
searchRequest.highlighter(new HighlightBuilder().field(highlightField)); if (highlightBuilder == null) {
highlightBuilder = new HighlightBuilder();
} }
for (HighlightBuilder.Field highlightField : searchQuery.getHighlightFields()) {
highlightBuilder.field(highlightField);
}
searchRequest.highlighter(highlightBuilder);
} }
if (!isEmpty(searchQuery.getIndicesBoost())) { if (!isEmpty(searchQuery.getIndicesBoost())) {
@@ -897,10 +934,19 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
searchRequest.addAggregation(aggregatedFacet.getFacet()); searchRequest.addAggregation(aggregatedFacet.getFacet());
} }
} }
return getSearchResponse(searchRequest.setQuery(searchQuery.getQuery()).execute()); return getSearchResponse(searchRequest.setQuery(searchQuery.getQuery()));
} }
private SearchResponse getSearchResponse(ListenableActionFuture<SearchResponse> response) { private SearchResponse getSearchResponse(SearchRequestBuilder requestBuilder) {
if (QUERY_LOGGER.isDebugEnabled()) {
QUERY_LOGGER.debug(requestBuilder.toString());
}
return getSearchResponse(requestBuilder.execute());
}
private SearchResponse getSearchResponse(ActionFuture<SearchResponse> response) {
return searchTimeout == null ? response.actionGet() : response.actionGet(searchTimeout); return searchTimeout == null ? response.actionGet() : response.actionGet(searchTimeout);
} }
@@ -911,13 +957,13 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
private <T> boolean createIndexWithSettings(Class<T> clazz) { private <T> boolean createIndexWithSettings(Class<T> clazz) {
if (clazz.isAnnotationPresent(Setting.class)) { if (clazz.isAnnotationPresent(Setting.class)) {
String settingPath = clazz.getAnnotation(Setting.class).settingPath(); String settingPath = clazz.getAnnotation(Setting.class).settingPath();
if (isNotBlank(settingPath)) { if (!StringUtils.isEmpty(settingPath)) {
String settings = readFileFromClasspath(settingPath); String settings = readFileFromClasspath(settingPath);
if (isNotBlank(settings)) { if (!StringUtils.isEmpty(settings)) {
return createIndex(getPersistentEntityFor(clazz).getIndexName(), settings); return createIndex(getPersistentEntityFor(clazz).getIndexName(), settings);
} }
} else { } else {
logger.info("settingPath in @Setting has to be defined. Using default instead."); LOGGER.info("settingPath in @Setting has to be defined. Using default instead.");
} }
} }
return createIndex(getPersistentEntityFor(clazz).getIndexName(), getDefaultSettings(getPersistentEntityFor(clazz))); return createIndex(getPersistentEntityFor(clazz).getIndexName(), getDefaultSettings(getPersistentEntityFor(clazz)));
@@ -927,7 +973,7 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
public boolean createIndex(String indexName, Object settings) { public boolean createIndex(String indexName, Object settings) {
CreateIndexRequestBuilder createIndexRequestBuilder = client.admin().indices().prepareCreate(indexName); CreateIndexRequestBuilder createIndexRequestBuilder = client.admin().indices().prepareCreate(indexName);
if (settings instanceof String) { if (settings instanceof String) {
createIndexRequestBuilder.setSettings(String.valueOf(settings)); createIndexRequestBuilder.setSettings(String.valueOf(settings), Requests.INDEX_CONTENT_TYPE);
} else if (settings instanceof Map) { } else if (settings instanceof Map) {
createIndexRequestBuilder.setSettings((Map) settings); createIndexRequestBuilder.setSettings((Map) settings);
} else if (settings instanceof XContentBuilder) { } else if (settings instanceof XContentBuilder) {
@@ -960,8 +1006,9 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
@Override @Override
public Map getSetting(String indexName) { public Map getSetting(String indexName) {
Assert.notNull(indexName, "No index defined for getSettings"); Assert.notNull(indexName, "No index defined for getSettings");
return client.admin().indices().getSettings(new GetSettingsRequest()).actionGet().getIndexToSettings() Settings settings = client.admin().indices().getSettings(new GetSettingsRequest()).actionGet()
.get(indexName).getAsMap(); .getIndexToSettings().get(indexName);
return settings.keySet().stream().collect(Collectors.toMap((key)->key, (key)->settings.get(key)));
} }
private <T> SearchRequestBuilder prepareSearch(Query query, Class<T> clazz) { private <T> SearchRequestBuilder prepareSearch(Query query, Class<T> clazz) {
@@ -975,7 +1022,10 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
int startRecord = 0; int startRecord = 0;
SearchRequestBuilder searchRequestBuilder = client.prepareSearch(toArray(query.getIndices())) SearchRequestBuilder searchRequestBuilder = client.prepareSearch(toArray(query.getIndices()))
.setSearchType(query.getSearchType()).setTypes(toArray(query.getTypes())); .setSearchType(query.getSearchType())
.setTypes(toArray(query.getTypes()))
.setVersion(true)
.setTrackScores(query.getTrackScores());
if (query.getSourceFilter() != null) { if (query.getSourceFilter() != null) {
SourceFilter sourceFilter = query.getSourceFilter(); SourceFilter sourceFilter = query.getSourceFilter();
@@ -989,13 +1039,36 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
searchRequestBuilder.setFrom(startRecord); searchRequestBuilder.setFrom(startRecord);
if (!query.getFields().isEmpty()) { if (!query.getFields().isEmpty()) {
searchRequestBuilder.setFetchSource(toArray(query.getFields()),null); searchRequestBuilder.setFetchSource(toArray(query.getFields()), null);
}
if (query.getIndicesOptions() != null) {
searchRequestBuilder.setIndicesOptions(query.getIndicesOptions());
} }
if (query.getSort() != null) { if (query.getSort() != null) {
for (Sort.Order order : query.getSort()) { for (Sort.Order order : query.getSort()) {
searchRequestBuilder.addSort(order.getProperty(), SortOrder sortOrder = order.getDirection().isDescending() ? SortOrder.DESC : SortOrder.ASC;
order.getDirection() == Sort.Direction.DESC ? SortOrder.DESC : SortOrder.ASC);
if (FIELD_SCORE.equals(order.getProperty())) {
ScoreSortBuilder sort = SortBuilders //
.scoreSort() //
.order(sortOrder);
searchRequestBuilder.addSort(sort);
} else {
FieldSortBuilder sort = SortBuilders //
.fieldSort(order.getProperty()) //
.order(sortOrder);
if (order.getNullHandling() == Sort.NullHandling.NULLS_FIRST) {
sort.missing("_first");
} else if (order.getNullHandling() == Sort.NullHandling.NULLS_LAST) {
sort.missing("_last");
}
searchRequestBuilder.addSort(sort);
}
} }
} }
@@ -1007,24 +1080,27 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
private IndexRequestBuilder prepareIndex(IndexQuery query) { private IndexRequestBuilder prepareIndex(IndexQuery query) {
try { try {
String indexName = isBlank(query.getIndexName()) String indexName = StringUtils.isEmpty(query.getIndexName())
? retrieveIndexNameFromPersistentEntity(query.getObject().getClass())[0] : query.getIndexName(); ? retrieveIndexNameFromPersistentEntity(query.getObject().getClass())[0]
String type = isBlank(query.getType()) ? retrieveTypeFromPersistentEntity(query.getObject().getClass())[0] : query.getIndexName();
String type = StringUtils.isEmpty(query.getType()) ? retrieveTypeFromPersistentEntity(query.getObject().getClass())[0]
: query.getType(); : query.getType();
IndexRequestBuilder indexRequestBuilder = null; IndexRequestBuilder indexRequestBuilder = null;
if (query.getObject() != null) { if (query.getObject() != null) {
String id = isBlank(query.getId()) ? getPersistentEntityId(query.getObject()) : query.getId(); String id = StringUtils.isEmpty(query.getId()) ? getPersistentEntityId(query.getObject()) : query.getId();
// If we have a query id and a document id, do not ask ES to generate one. // If we have a query id and a document id, do not ask ES to generate one.
if (id != null) { if (id != null) {
indexRequestBuilder = client.prepareIndex(indexName, type, id); indexRequestBuilder = client.prepareIndex(indexName, type, id);
} else { } else {
indexRequestBuilder = client.prepareIndex(indexName, type); indexRequestBuilder = client.prepareIndex(indexName, type);
} }
indexRequestBuilder.setSource(resultsMapper.getEntityMapper().mapToString(query.getObject())); indexRequestBuilder.setSource(resultsMapper.getEntityMapper().mapToString(query.getObject()),
Requests.INDEX_CONTENT_TYPE);
} else if (query.getSource() != null) { } else if (query.getSource() != null) {
indexRequestBuilder = client.prepareIndex(indexName, type, query.getId()).setSource(query.getSource()); indexRequestBuilder = client.prepareIndex(indexName, type, query.getId()).setSource(query.getSource(),
Requests.INDEX_CONTENT_TYPE);
} else { } else {
throw new ElasticsearchException( throw new ElasticsearchException(
"object or source is null, failed to index the document [id: " + query.getId() + "]"); "object or source is null, failed to index the document [id: " + query.getId() + "]");
@@ -1059,17 +1135,18 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
public Boolean addAlias(AliasQuery query) { public Boolean addAlias(AliasQuery query) {
Assert.notNull(query.getIndexName(), "No index defined for Alias"); Assert.notNull(query.getIndexName(), "No index defined for Alias");
Assert.notNull(query.getAliasName(), "No alias defined"); Assert.notNull(query.getAliasName(), "No alias defined");
final IndicesAliasesRequest.AliasActions aliasAction = IndicesAliasesRequest.AliasActions.add().alias(query.getAliasName()).index(query.getIndexName()); final IndicesAliasesRequest.AliasActions aliasAction = IndicesAliasesRequest.AliasActions.add()
.alias(query.getAliasName()).index(query.getIndexName());
if (query.getFilterBuilder() != null) { if (query.getFilterBuilder() != null) {
aliasAction.filter(query.getFilterBuilder()); aliasAction.filter(query.getFilterBuilder());
} else if (query.getFilter() != null) { } else if (query.getFilter() != null) {
aliasAction.filter(query.getFilter()); aliasAction.filter(query.getFilter());
} else if (isNotBlank(query.getRouting())) { } else if (!StringUtils.isEmpty(query.getRouting())) {
aliasAction.routing(query.getRouting()); aliasAction.routing(query.getRouting());
} else if (isNotBlank(query.getSearchRouting())) { } else if (!StringUtils.isEmpty(query.getSearchRouting())) {
aliasAction.searchRouting(query.getSearchRouting()); aliasAction.searchRouting(query.getSearchRouting());
} else if (isNotBlank(query.getIndexRouting())) { } else if (!StringUtils.isEmpty(query.getIndexRouting())) {
aliasAction.indexRouting(query.getIndexRouting()); aliasAction.indexRouting(query.getIndexRouting());
} }
return client.admin().indices().prepareAliases().addAliasAction(aliasAction).execute().actionGet().isAcknowledged(); return client.admin().indices().prepareAliases().addAliasAction(aliasAction).execute().actionGet().isAcknowledged();
@@ -1101,7 +1178,7 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
ElasticsearchPersistentEntity<?> persistentEntity = getPersistentEntityFor(entity.getClass()); ElasticsearchPersistentEntity<?> persistentEntity = getPersistentEntityFor(entity.getClass());
Object identifier = persistentEntity.getIdentifierAccessor(entity).getIdentifier(); Object identifier = persistentEntity.getIdentifierAccessor(entity).getIdentifier();
if (identifier != null){ if (identifier != null) {
return identifier.toString(); return identifier.toString();
} }
@@ -1189,14 +1266,14 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
stringBuilder.append(line).append(lineSeparator); stringBuilder.append(line).append(lineSeparator);
} }
} catch (Exception e) { } catch (Exception e) {
logger.debug(String.format("Failed to load file from url: %s: %s", url, e.getMessage())); LOGGER.debug(String.format("Failed to load file from url: %s: %s", url, e.getMessage()));
return null; return null;
} finally { } finally {
if (bufferedReader != null) if (bufferedReader != null)
try { try {
bufferedReader.close(); bufferedReader.close();
} catch (IOException e) { } catch (IOException e) {
logger.debug(String.format("Unable to close buffered reader.. %s", e.getMessage())); LOGGER.debug(String.format("Unable to close buffered reader.. %s", e.getMessage()));
} }
} }
@@ -1,5 +1,5 @@
/* /*
* Copyright 2014 the original author or authors. * Copyright 2014-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
/* /*
* Copyright 2014 the original author or authors. * Copyright 2014-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
/* /*
* Copyright 2014-2017 the original author or authors. * Copyright 2014-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -40,6 +40,8 @@ import org.springframework.data.elasticsearch.core.facet.result.StatisticalResul
import org.springframework.data.elasticsearch.core.facet.result.Term; import org.springframework.data.elasticsearch.core.facet.result.Term;
import org.springframework.data.elasticsearch.core.facet.result.TermResult; import org.springframework.data.elasticsearch.core.facet.result.TermResult;
import static java.util.Optional.ofNullable;
/** /**
* Container for query result and facet results * Container for query result and facet results
* *
@@ -48,6 +50,7 @@ import org.springframework.data.elasticsearch.core.facet.result.TermResult;
* @author Artur Konczak * @author Artur Konczak
* @author Jonathan Yan * @author Jonathan Yan
* @author Philipp Kräutli * @author Philipp Kräutli
* @author Remco Zigterman
*/ */
@Deprecated @Deprecated
public abstract class FacetedPageImpl<T> extends PageImpl<T> implements FacetedPage<T>, AggregatedPage<T> { public abstract class FacetedPageImpl<T> extends PageImpl<T> implements FacetedPage<T>, AggregatedPage<T> {
@@ -60,7 +63,7 @@ public abstract class FacetedPageImpl<T> extends PageImpl<T> implements FacetedP
} }
public FacetedPageImpl(List<T> content, Pageable pageable, long total) { public FacetedPageImpl(List<T> content, Pageable pageable, long total) {
super(content, Pageable.unpaged(), total); super(content, ofNullable(pageable).orElse(Pageable.unpaged()), total);
} }
@Override @Override
@@ -1,5 +1,5 @@
/* /*
* Copyright 2014 the original author or authors. * Copyright 2014-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -1,362 +1,395 @@
/* /*
* Copyright 2014-2017 the original author or authors. * Copyright 2014-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.data.elasticsearch.core; package org.springframework.data.elasticsearch.core;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.springframework.core.ResolvableType; import org.springframework.core.ResolvableType;
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.ClassPathResource;
import org.springframework.data.annotation.Transient; import org.springframework.data.annotation.Transient;
import org.springframework.data.elasticsearch.annotations.CompletionField; import org.springframework.data.elasticsearch.annotations.CompletionField;
import org.springframework.data.elasticsearch.annotations.DateFormat; import org.springframework.data.elasticsearch.annotations.DateFormat;
import org.springframework.data.elasticsearch.annotations.Field; import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType; import org.springframework.data.elasticsearch.annotations.FieldType;
import org.springframework.data.elasticsearch.annotations.GeoPointField; import org.springframework.data.elasticsearch.annotations.GeoPointField;
import org.springframework.data.elasticsearch.annotations.InnerField; import org.springframework.data.elasticsearch.annotations.InnerField;
import org.springframework.data.elasticsearch.annotations.Mapping; import org.springframework.data.elasticsearch.annotations.Mapping;
import org.springframework.data.elasticsearch.annotations.MultiField; import org.springframework.data.elasticsearch.annotations.MultiField;
import org.springframework.data.elasticsearch.core.completion.Completion; import org.springframework.data.elasticsearch.core.completion.Completion;
import org.springframework.data.elasticsearch.core.geo.GeoPoint; import org.springframework.data.elasticsearch.core.geo.GeoPoint;
import org.springframework.data.mapping.model.SimpleTypeHolder; import org.springframework.data.mapping.model.SimpleTypeHolder;
import org.springframework.data.util.ClassTypeInformation; import org.springframework.data.util.ClassTypeInformation;
import org.springframework.data.util.TypeInformation; import org.springframework.data.util.TypeInformation;
import static org.apache.commons.lang.StringUtils.*; import org.springframework.util.StringUtils;
import static org.elasticsearch.common.xcontent.XContentFactory.*;
import static org.springframework.util.StringUtils.*; import static org.elasticsearch.common.xcontent.XContentFactory.*;
import static org.springframework.util.StringUtils.*;
/**
* @author Rizwan Idrees /**
* @author Mohsin Husen * @author Rizwan Idrees
* @author Artur Konczak * @author Mohsin Husen
* @author Kevin Leturc * @author Artur Konczak
* @author Alexander Volz * @author Kevin Leturc
* @author Dennis Maaß * @author Alexander Volz
* @author Pavel Luhin * @author Dennis Maaß
* @author Mark Paluch * @author Pavel Luhin
*/ * @author Mark Paluch
class MappingBuilder { * @author Sascha Woo
* @author Nordine Bittich
public static final String FIELD_DATA = "fielddata"; */
public static final String FIELD_STORE = "store"; class MappingBuilder {
public static final String FIELD_TYPE = "type";
public static final String FIELD_INDEX = "index"; public static final String FIELD_DATA = "fielddata";
public static final String FIELD_FORMAT = "format"; public static final String FIELD_STORE = "store";
public static final String FIELD_SEARCH_ANALYZER = "search_analyzer"; public static final String FIELD_TYPE = "type";
public static final String FIELD_INDEX_ANALYZER = "analyzer"; public static final String FIELD_INDEX = "index";
public static final String FIELD_PROPERTIES = "properties"; public static final String FIELD_FORMAT = "format";
public static final String FIELD_PARENT = "_parent"; public static final String FIELD_SEARCH_ANALYZER = "search_analyzer";
public static final String FIELD_INDEX_ANALYZER = "analyzer";
public static final String COMPLETION_PRESERVE_SEPARATORS = "preserve_separators"; public static final String FIELD_NORMALIZER = "normalizer";
public static final String COMPLETION_PRESERVE_POSITION_INCREMENTS = "preserve_position_increments"; public static final String FIELD_PROPERTIES = "properties";
public static final String COMPLETION_MAX_INPUT_LENGTH = "max_input_length"; public static final String FIELD_PARENT = "_parent";
public static final String FIELD_COPY_TO = "copy_to";
public static final String INDEX_VALUE_NOT_ANALYZED = "not_analyzed";
public static final String TYPE_VALUE_STRING = "text"; public static final String COMPLETION_PRESERVE_SEPARATORS = "preserve_separators";
public static final String TYPE_VALUE_GEO_POINT = "geo_point"; public static final String COMPLETION_PRESERVE_POSITION_INCREMENTS = "preserve_position_increments";
public static final String TYPE_VALUE_COMPLETION = "completion"; public static final String COMPLETION_MAX_INPUT_LENGTH = "max_input_length";
public static final String TYPE_VALUE_GEO_HASH_PREFIX = "geohash_prefix";
public static final String TYPE_VALUE_GEO_HASH_PRECISION = "geohash_precision"; public static final String TYPE_VALUE_KEYWORD = "keyword";
public static final String TYPE_VALUE_GEO_POINT = "geo_point";
private static SimpleTypeHolder SIMPLE_TYPE_HOLDER = SimpleTypeHolder.DEFAULT; public static final String TYPE_VALUE_COMPLETION = "completion";
public static final String TYPE_VALUE_GEO_HASH_PREFIX = "geohash_prefix";
static XContentBuilder buildMapping(Class clazz, String indexType, String idFieldName, String parentType) throws IOException { public static final String TYPE_VALUE_GEO_HASH_PRECISION = "geohash_precision";
XContentBuilder mapping = jsonBuilder().startObject().startObject(indexType); private static SimpleTypeHolder SIMPLE_TYPE_HOLDER = SimpleTypeHolder.DEFAULT;
// Parent
if (hasText(parentType)) { static XContentBuilder buildMapping(Class clazz, String indexType, String idFieldName, String parentType) throws IOException {
mapping.startObject(FIELD_PARENT).field(FIELD_TYPE, parentType).endObject();
} XContentBuilder mapping = jsonBuilder().startObject().startObject(indexType);
// Parent
// Properties if (hasText(parentType)) {
XContentBuilder xContentBuilder = mapping.startObject(FIELD_PROPERTIES); mapping.startObject(FIELD_PARENT).field(FIELD_TYPE, parentType).endObject();
}
mapEntity(xContentBuilder, clazz, true, idFieldName, EMPTY, false, FieldType.Auto, null);
// Properties
return xContentBuilder.endObject().endObject().endObject(); XContentBuilder xContentBuilder = mapping.startObject(FIELD_PROPERTIES);
}
mapEntity(xContentBuilder, clazz, true, idFieldName, "", false, FieldType.Auto, null);
private static void mapEntity(XContentBuilder xContentBuilder, Class clazz, boolean isRootObject, String idFieldName,
String nestedObjectFieldName, boolean nestedOrObjectField, FieldType fieldType, Field fieldAnnotation) throws IOException { return xContentBuilder.endObject().endObject().endObject();
}
java.lang.reflect.Field[] fields = retrieveFields(clazz);
private static void mapEntity(XContentBuilder xContentBuilder, Class clazz, boolean isRootObject, String idFieldName,
if (!isRootObject && (isAnyPropertyAnnotatedAsField(fields) || nestedOrObjectField)) { String nestedObjectFieldName, boolean nestedOrObjectField, FieldType fieldType, Field fieldAnnotation) throws IOException {
String type = FieldType.Object.toString().toLowerCase();
if (nestedOrObjectField) { java.lang.reflect.Field[] fields = retrieveFields(clazz);
type = fieldType.toString().toLowerCase();
} if (!isRootObject && (isAnyPropertyAnnotatedAsField(fields) || nestedOrObjectField)) {
XContentBuilder t = xContentBuilder.startObject(nestedObjectFieldName).field(FIELD_TYPE, type); String type = FieldType.Object.toString().toLowerCase();
if (nestedOrObjectField) {
if (nestedOrObjectField && FieldType.Nested == fieldType && fieldAnnotation.includeInParent()) { type = fieldType.toString().toLowerCase();
t.field("include_in_parent", fieldAnnotation.includeInParent()); }
} XContentBuilder t = xContentBuilder.startObject(nestedObjectFieldName).field(FIELD_TYPE, type);
t.startObject(FIELD_PROPERTIES);
} if (nestedOrObjectField && FieldType.Nested == fieldType && fieldAnnotation.includeInParent()) {
t.field("include_in_parent", fieldAnnotation.includeInParent());
for (java.lang.reflect.Field field : fields) { }
t.startObject(FIELD_PROPERTIES);
if (field.isAnnotationPresent(Transient.class) || isInIgnoreFields(field, fieldAnnotation)) { }
continue;
} for (java.lang.reflect.Field field : fields) {
if (field.isAnnotationPresent(Mapping.class)) { if (field.isAnnotationPresent(Transient.class) || isInIgnoreFields(field, fieldAnnotation)) {
String mappingPath = field.getAnnotation(Mapping.class).mappingPath(); continue;
if (isNotBlank(mappingPath)) { }
ClassPathResource mappings = new ClassPathResource(mappingPath);
if (mappings.exists()) { if (field.isAnnotationPresent(Mapping.class)) {
xContentBuilder.rawField(field.getName(), mappings.getInputStream()); String mappingPath = field.getAnnotation(Mapping.class).mappingPath();
continue; if (!StringUtils.isEmpty(mappingPath)) {
} ClassPathResource mappings = new ClassPathResource(mappingPath);
} if (mappings.exists()) {
} xContentBuilder.rawField(field.getName(), mappings.getInputStream());
continue;
boolean isGeoPointField = isGeoPointField(field); }
boolean isCompletionField = isCompletionField(field); }
}
Field singleField = field.getAnnotation(Field.class);
if (!isGeoPointField && !isCompletionField && isEntity(field) && isAnnotated(field)) { boolean isGeoPointField = isGeoPointField(field);
if (singleField == null) { boolean isCompletionField = isCompletionField(field);
continue;
} Field singleField = field.getAnnotation(Field.class);
boolean nestedOrObject = isNestedOrObjectField(field); if (!isGeoPointField && !isCompletionField && isEntity(field) && isAnnotated(field)) {
mapEntity(xContentBuilder, getFieldType(field), false, EMPTY, field.getName(), nestedOrObject, singleField.type(), field.getAnnotation(Field.class)); if (singleField == null) {
if (nestedOrObject) { continue;
continue; }
} boolean nestedOrObject = isNestedOrObjectField(field);
} mapEntity(xContentBuilder, getFieldType(field), false, "", field.getName(), nestedOrObject, singleField.type(), field.getAnnotation(Field.class));
if (nestedOrObject) {
MultiField multiField = field.getAnnotation(MultiField.class); continue;
}
if (isGeoPointField) { }
applyGeoPointFieldMapping(xContentBuilder, field);
} MultiField multiField = field.getAnnotation(MultiField.class);
if (isCompletionField) { if (isGeoPointField) {
CompletionField completionField = field.getAnnotation(CompletionField.class); applyGeoPointFieldMapping(xContentBuilder, field);
applyCompletionFieldMapping(xContentBuilder, field, completionField); }
}
if (isCompletionField) {
if (isRootObject && singleField != null && isIdField(field, idFieldName)) { CompletionField completionField = field.getAnnotation(CompletionField.class);
applyDefaultIdFieldMapping(xContentBuilder, field); applyCompletionFieldMapping(xContentBuilder, field, completionField);
} else if (multiField != null) { }
addMultiFieldMapping(xContentBuilder, field, multiField, isNestedOrObjectField(field));
} else if (singleField != null) { if (isRootObject && singleField != null && isIdField(field, idFieldName)) {
addSingleFieldMapping(xContentBuilder, field, singleField, isNestedOrObjectField(field)); applyDefaultIdFieldMapping(xContentBuilder, field);
} } else if (multiField != null) {
} addMultiFieldMapping(xContentBuilder, field, multiField, isNestedOrObjectField(field));
} else if (singleField != null) {
if (!isRootObject && isAnyPropertyAnnotatedAsField(fields) || nestedOrObjectField) { addSingleFieldMapping(xContentBuilder, field, singleField, isNestedOrObjectField(field));
xContentBuilder.endObject().endObject(); }
} }
}
if (!isRootObject && isAnyPropertyAnnotatedAsField(fields) || nestedOrObjectField) {
private static java.lang.reflect.Field[] retrieveFields(Class clazz) { xContentBuilder.endObject().endObject();
// Create list of fields. }
List<java.lang.reflect.Field> fields = new ArrayList<>(); }
// Keep backing up the inheritance hierarchy. private static java.lang.reflect.Field[] retrieveFields(Class clazz) {
Class targetClass = clazz; // Create list of fields.
do { List<java.lang.reflect.Field> fields = new ArrayList<>();
fields.addAll(Arrays.asList(targetClass.getDeclaredFields()));
targetClass = targetClass.getSuperclass(); // Keep backing up the inheritance hierarchy.
} Class targetClass = clazz;
while (targetClass != null && targetClass != Object.class); do {
fields.addAll(Arrays.asList(targetClass.getDeclaredFields()));
return fields.toArray(new java.lang.reflect.Field[fields.size()]); targetClass = targetClass.getSuperclass();
} }
while (targetClass != null && targetClass != Object.class);
private static boolean isAnnotated(java.lang.reflect.Field field) {
return field.getAnnotation(Field.class) != null || return fields.toArray(new java.lang.reflect.Field[fields.size()]);
field.getAnnotation(MultiField.class) != null || }
field.getAnnotation(GeoPointField.class) != null ||
field.getAnnotation(CompletionField.class) != null; private static boolean isAnnotated(java.lang.reflect.Field field) {
} return field.getAnnotation(Field.class) != null ||
field.getAnnotation(MultiField.class) != null ||
private static void applyGeoPointFieldMapping(XContentBuilder xContentBuilder, java.lang.reflect.Field field) throws IOException { field.getAnnotation(GeoPointField.class) != null ||
xContentBuilder.startObject(field.getName()); field.getAnnotation(CompletionField.class) != null;
xContentBuilder.field(FIELD_TYPE, TYPE_VALUE_GEO_POINT); }
xContentBuilder.endObject();
} private static void applyGeoPointFieldMapping(XContentBuilder xContentBuilder, java.lang.reflect.Field field) throws IOException {
xContentBuilder.startObject(field.getName());
private static void applyCompletionFieldMapping(XContentBuilder xContentBuilder, java.lang.reflect.Field field, CompletionField annotation) throws IOException { xContentBuilder.field(FIELD_TYPE, TYPE_VALUE_GEO_POINT);
xContentBuilder.startObject(field.getName()); xContentBuilder.endObject();
xContentBuilder.field(FIELD_TYPE, TYPE_VALUE_COMPLETION); }
if (annotation != null) {
xContentBuilder.field(COMPLETION_MAX_INPUT_LENGTH, annotation.maxInputLength()); private static void applyCompletionFieldMapping(XContentBuilder xContentBuilder, java.lang.reflect.Field field, CompletionField annotation) throws IOException {
xContentBuilder.field(COMPLETION_PRESERVE_POSITION_INCREMENTS, annotation.preservePositionIncrements()); xContentBuilder.startObject(field.getName());
xContentBuilder.field(COMPLETION_PRESERVE_SEPARATORS, annotation.preserveSeparators()); xContentBuilder.field(FIELD_TYPE, TYPE_VALUE_COMPLETION);
if (isNotBlank(annotation.searchAnalyzer())) { if (annotation != null) {
xContentBuilder.field(FIELD_SEARCH_ANALYZER, annotation.searchAnalyzer()); xContentBuilder.field(COMPLETION_MAX_INPUT_LENGTH, annotation.maxInputLength());
} xContentBuilder.field(COMPLETION_PRESERVE_POSITION_INCREMENTS, annotation.preservePositionIncrements());
if (isNotBlank(annotation.analyzer())) { xContentBuilder.field(COMPLETION_PRESERVE_SEPARATORS, annotation.preserveSeparators());
xContentBuilder.field(FIELD_INDEX_ANALYZER, annotation.analyzer()); if (!StringUtils.isEmpty(annotation.searchAnalyzer())) {
} xContentBuilder.field(FIELD_SEARCH_ANALYZER, annotation.searchAnalyzer());
} }
xContentBuilder.endObject(); if (!StringUtils.isEmpty(annotation.analyzer())) {
} xContentBuilder.field(FIELD_INDEX_ANALYZER, annotation.analyzer());
}
private static void applyDefaultIdFieldMapping(XContentBuilder xContentBuilder, java.lang.reflect.Field field) }
throws IOException { xContentBuilder.endObject();
xContentBuilder.startObject(field.getName()) }
.field(FIELD_TYPE, TYPE_VALUE_STRING)
.field(FIELD_INDEX, INDEX_VALUE_NOT_ANALYZED); private static void applyDefaultIdFieldMapping(XContentBuilder xContentBuilder, java.lang.reflect.Field field)
xContentBuilder.endObject(); throws IOException {
} xContentBuilder.startObject(field.getName())
.field(FIELD_TYPE, TYPE_VALUE_KEYWORD)
/** .field(FIELD_INDEX, true);
* Apply mapping for a single @Field annotation xContentBuilder.endObject();
* }
* @throws IOException
*/ /**
private static void addSingleFieldMapping(XContentBuilder xContentBuilder, java.lang.reflect.Field field, * Add mapping for @Field annotation
Field fieldAnnotation, boolean nestedOrObjectField) throws IOException { *
xContentBuilder.startObject(field.getName()); * @throws IOException
if(!nestedOrObjectField) { */
xContentBuilder.field(FIELD_STORE, fieldAnnotation.store()); private static void addSingleFieldMapping(XContentBuilder builder, java.lang.reflect.Field field, Field annotation, boolean nestedOrObjectField) throws IOException {
} builder.startObject(field.getName());
if(fieldAnnotation.fielddata()) { addFieldMappingParameters(builder, annotation, nestedOrObjectField);
xContentBuilder.field(FIELD_DATA, fieldAnnotation.fielddata()); builder.endObject();
} }
if (FieldType.Auto != fieldAnnotation.type()) { /**
xContentBuilder.field(FIELD_TYPE, fieldAnnotation.type().name().toLowerCase()); * Add mapping for @MultiField annotation
if (FieldType.Date == fieldAnnotation.type() && DateFormat.none != fieldAnnotation.format()) { *
xContentBuilder.field(FIELD_FORMAT, DateFormat.custom == fieldAnnotation.format() * @throws IOException
? fieldAnnotation.pattern() : fieldAnnotation.format()); */
} private static void addMultiFieldMapping(
} XContentBuilder builder,
if(!fieldAnnotation.index()) { java.lang.reflect.Field field,
xContentBuilder.field(FIELD_INDEX, fieldAnnotation.index()); MultiField annotation,
} boolean nestedOrObjectField) throws IOException {
if (isNotBlank(fieldAnnotation.searchAnalyzer())) {
xContentBuilder.field(FIELD_SEARCH_ANALYZER, fieldAnnotation.searchAnalyzer()); // main field
} builder.startObject(field.getName());
if (isNotBlank(fieldAnnotation.analyzer())) { addFieldMappingParameters(builder, annotation.mainField(), nestedOrObjectField);
xContentBuilder.field(FIELD_INDEX_ANALYZER, fieldAnnotation.analyzer());
} // inner fields
xContentBuilder.endObject(); builder.startObject("fields");
} for (InnerField innerField : annotation.otherFields()) {
builder.startObject(innerField.suffix());
/** addFieldMappingParameters(builder, innerField, false);
* Apply mapping for a single nested @Field annotation builder.endObject();
* }
* @throws IOException builder.endObject();
*/
private static void addNestedFieldMapping(XContentBuilder builder, java.lang.reflect.Field field, builder.endObject();
InnerField annotation) throws IOException { }
builder.startObject(annotation.suffix());
//builder.field(FIELD_STORE, annotation.store()); private static void addFieldMappingParameters(XContentBuilder builder, Object annotation, boolean nestedOrObjectField) throws IOException {
if (FieldType.Auto != annotation.type()) { boolean index = true;
builder.field(FIELD_TYPE, annotation.type().name().toLowerCase()); boolean store = false;
} boolean fielddata = false;
if(!annotation.index()) { FieldType type = null;
builder.field(FIELD_INDEX, annotation.index()); DateFormat dateFormat = null;
} String datePattern = null;
if (isNotBlank(annotation.searchAnalyzer())) { String analyzer = null;
builder.field(FIELD_SEARCH_ANALYZER, annotation.searchAnalyzer()); String searchAnalyzer = null;
} String normalizer = null;
if (isNotBlank(annotation.indexAnalyzer())) { String[] copyTo = null;
builder.field(FIELD_INDEX_ANALYZER, annotation.indexAnalyzer());
} if (annotation instanceof Field) {
if (annotation.fielddata()) { // @Field
builder.field(FIELD_DATA, annotation.fielddata()); Field fieldAnnotation = (Field) annotation;
} index = fieldAnnotation.index();
builder.endObject(); store = fieldAnnotation.store();
} fielddata = fieldAnnotation.fielddata();
type = fieldAnnotation.type();
/** dateFormat = fieldAnnotation.format();
* Multi field mappings for string type fields, support for sorts and facets datePattern = fieldAnnotation.pattern();
* analyzer = fieldAnnotation.analyzer();
* @throws IOException searchAnalyzer = fieldAnnotation.searchAnalyzer();
*/ normalizer = fieldAnnotation.normalizer();
private static void addMultiFieldMapping(XContentBuilder builder, java.lang.reflect.Field field, copyTo = fieldAnnotation.copyTo();
MultiField annotation, boolean nestedOrObjectField) throws IOException { } else if (annotation instanceof InnerField) {
builder.startObject(field.getName()); // @InnerField
builder.field(FIELD_TYPE, annotation.mainField().type()); InnerField fieldAnnotation = (InnerField) annotation;
builder.startObject("fields"); index = fieldAnnotation.index();
//add standard field store = fieldAnnotation.store();
//addSingleFieldMapping(builder, field, annotation.mainField(), nestedOrObjectField); fielddata = fieldAnnotation.fielddata();
for (InnerField innerField : annotation.otherFields()) { type = fieldAnnotation.type();
addNestedFieldMapping(builder, field, innerField); dateFormat = fieldAnnotation.format();
} datePattern = fieldAnnotation.pattern();
builder.endObject(); analyzer = fieldAnnotation.analyzer();
builder.endObject(); searchAnalyzer = fieldAnnotation.searchAnalyzer();
} normalizer = fieldAnnotation.normalizer();
} else {
protected static boolean isEntity(java.lang.reflect.Field field) { throw new IllegalArgumentException("annotation must be an instance of @Field or @InnerField");
TypeInformation typeInformation = ClassTypeInformation.from(field.getType()); }
Class<?> clazz = getFieldType(field);
boolean isComplexType = !SIMPLE_TYPE_HOLDER.isSimpleType(clazz); if (!nestedOrObjectField) {
return isComplexType && !Map.class.isAssignableFrom(typeInformation.getType()); builder.field(FIELD_STORE, store);
} }
if (fielddata) {
protected static Class<?> getFieldType(java.lang.reflect.Field field) { builder.field(FIELD_DATA, fielddata);
}
ResolvableType resolvableType = ResolvableType.forField(field); if (type != FieldType.Auto) {
builder.field(FIELD_TYPE, type.name().toLowerCase());
if (resolvableType.isArray()) {
return resolvableType.getComponentType().getRawClass(); if (type == FieldType.Date && dateFormat != DateFormat.none) {
} builder.field(FIELD_FORMAT, dateFormat == DateFormat.custom ? datePattern : dateFormat.toString());
}
ResolvableType componentType = resolvableType.getGeneric(0); }
if (Iterable.class.isAssignableFrom(field.getType()) if (!index) {
&& componentType != ResolvableType.NONE) { builder.field(FIELD_INDEX, index);
return componentType.getRawClass(); }
} if (!StringUtils.isEmpty(analyzer)) {
builder.field(FIELD_INDEX_ANALYZER, analyzer);
return resolvableType.getRawClass(); }
} if (!StringUtils.isEmpty(searchAnalyzer)) {
builder.field(FIELD_SEARCH_ANALYZER, searchAnalyzer);
private static boolean isAnyPropertyAnnotatedAsField(java.lang.reflect.Field[] fields) { }
if (fields != null) { if (!StringUtils.isEmpty(normalizer)) {
for (java.lang.reflect.Field field : fields) { builder.field(FIELD_NORMALIZER, normalizer);
if (field.isAnnotationPresent(Field.class)) { }
return true; if (copyTo != null && copyTo.length > 0) {
} builder.field(FIELD_COPY_TO, copyTo);
} }
} }
return false;
} protected static boolean isEntity(java.lang.reflect.Field field) {
TypeInformation typeInformation = ClassTypeInformation.from(field.getType());
private static boolean isIdField(java.lang.reflect.Field field, String idFieldName) { Class<?> clazz = getFieldType(field);
return idFieldName.equals(field.getName()); boolean isComplexType = !SIMPLE_TYPE_HOLDER.isSimpleType(clazz);
} return isComplexType && !Map.class.isAssignableFrom(typeInformation.getType());
}
private static boolean isInIgnoreFields(java.lang.reflect.Field field, Field parentFieldAnnotation) {
if (null != parentFieldAnnotation) { protected static Class<?> getFieldType(java.lang.reflect.Field field) {
String[] ignoreFields = parentFieldAnnotation.ignoreFields();
return Arrays.asList(ignoreFields).contains(field.getName()); ResolvableType resolvableType = ResolvableType.forField(field);
}
return false; if (resolvableType.isArray()) {
} return resolvableType.getComponentType().getRawClass();
}
private static boolean isNestedOrObjectField(java.lang.reflect.Field field) {
Field fieldAnnotation = field.getAnnotation(Field.class); ResolvableType componentType = resolvableType.getGeneric(0);
return fieldAnnotation != null && (FieldType.Nested == fieldAnnotation.type() || FieldType.Object == fieldAnnotation.type()); if (Iterable.class.isAssignableFrom(field.getType())
} && componentType != ResolvableType.NONE) {
return componentType.getRawClass();
private static boolean isGeoPointField(java.lang.reflect.Field field) { }
return field.getType() == GeoPoint.class || field.getAnnotation(GeoPointField.class) != null;
} return resolvableType.getRawClass();
}
private static boolean isCompletionField(java.lang.reflect.Field field) {
return field.getType() == Completion.class; private static boolean isAnyPropertyAnnotatedAsField(java.lang.reflect.Field[] fields) {
} if (fields != null) {
} for (java.lang.reflect.Field field : fields) {
if (field.isAnnotationPresent(Field.class)) {
return true;
}
}
}
return false;
}
private static boolean isIdField(java.lang.reflect.Field field, String idFieldName) {
return idFieldName.equals(field.getName());
}
private static boolean isInIgnoreFields(java.lang.reflect.Field field, Field parentFieldAnnotation) {
if (null != parentFieldAnnotation) {
String[] ignoreFields = parentFieldAnnotation.ignoreFields();
return Arrays.asList(ignoreFields).contains(field.getName());
}
return false;
}
private static boolean isNestedOrObjectField(java.lang.reflect.Field field) {
Field fieldAnnotation = field.getAnnotation(Field.class);
return fieldAnnotation != null && (FieldType.Nested == fieldAnnotation.type() || FieldType.Object == fieldAnnotation.type());
}
private static boolean isGeoPointField(java.lang.reflect.Field field) {
return field.getType() == GeoPoint.class || field.getAnnotation(GeoPointField.class) != null;
}
private static boolean isCompletionField(java.lang.reflect.Field field) {
return field.getType() == Completion.class;
}
}
@@ -1,5 +1,5 @@
/* /*
* Copyright 2014 the original author or authors. * Copyright 2014-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -1,24 +1,24 @@
/* /*
* Copyright 2013-2014 the original author or authors. * Copyright 2013-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.data.elasticsearch.core; package org.springframework.data.elasticsearch.core;
import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.search.SearchResponse;
public interface ResultsExtractor<T> { public interface ResultsExtractor<T> {
T extract(SearchResponse response); T extract(SearchResponse response);
} }
@@ -1,29 +1,29 @@
/* /*
* Copyright 2013-2014 the original author or authors. * Copyright 2013-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.data.elasticsearch.core; package org.springframework.data.elasticsearch.core;
/** /**
* ResultsMapper * ResultsMapper
* *
* @author Rizwan Idrees * @author Rizwan Idrees
* @author Mohsin Husen * @author Mohsin Husen
* @author Artur Konczak * @author Artur Konczak
*/ */
public interface ResultsMapper extends SearchResultMapper, GetResultMapper, MultiGetResultMapper { public interface ResultsMapper extends SearchResultMapper, GetResultMapper, MultiGetResultMapper {
EntityMapper getEntityMapper(); EntityMapper getEntityMapper();
} }
@@ -1,5 +1,5 @@
/* /*
* Copyright 2014 the original author or authors. * Copyright 2018-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -13,14 +13,17 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.data.elasticsearch.annotations; package org.springframework.data.elasticsearch.core;
import org.springframework.data.domain.Page;
/** /**
* @author Artur Konczak * A score-aware page gaining information about max score.
* @author Mohsin Husen *
* @author Alexander Volz * @param <T>
* @author Dennis Maaß * @author Sascha Woo
*/ */
public enum FieldIndex { public interface ScoredPage<T> extends Page<T> {
not_analyzed, analyzed, no
float getMaxScore();
} }
@@ -1,5 +1,5 @@
/* /*
* Copyright 2014 the original author or authors. * Copyright 2014-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -3,12 +3,14 @@ package org.springframework.data.elasticsearch.core.aggregation;
import org.elasticsearch.search.aggregations.Aggregation; import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.Aggregations; import org.elasticsearch.search.aggregations.Aggregations;
import org.springframework.data.elasticsearch.core.FacetedPage; import org.springframework.data.elasticsearch.core.FacetedPage;
import org.springframework.data.elasticsearch.core.ScoredPage;
import org.springframework.data.elasticsearch.core.ScrolledPage; import org.springframework.data.elasticsearch.core.ScrolledPage;
/** /**
* @author Petar Tahchiev * @author Petar Tahchiev
* @author Sascha Woo
*/ */
public interface AggregatedPage<T> extends FacetedPage<T>, ScrolledPage<T> { public interface AggregatedPage<T> extends FacetedPage<T>, ScrolledPage<T>, ScoredPage<T> {
boolean hasAggregations(); boolean hasAggregations();
@@ -1,5 +1,5 @@
/* /*
* Copyright 2017 the original author or authors. * Copyright 2017-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -15,9 +15,7 @@
*/ */
package org.springframework.data.elasticsearch.core.aggregation.impl; package org.springframework.data.elasticsearch.core.aggregation.impl;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import org.elasticsearch.search.aggregations.Aggregation; import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.Aggregations; import org.elasticsearch.search.aggregations.Aggregations;
@@ -29,55 +27,77 @@ import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
* @author Petar Tahchiev * @author Petar Tahchiev
* @author Artur Konczak * @author Artur Konczak
* @author Mohsin Husen * @author Mohsin Husen
* @author Sascha Woo
*/ */
public class AggregatedPageImpl<T> extends FacetedPageImpl<T> implements AggregatedPage<T> { public class AggregatedPageImpl<T> extends FacetedPageImpl<T> implements AggregatedPage<T> {
private Aggregations aggregations; private Aggregations aggregations;
private Map<String, Aggregation> mapOfAggregations = new HashMap<>(); private String scrollId;
private String scrollId; private float maxScore;
public AggregatedPageImpl(List<T> content) { public AggregatedPageImpl(List<T> content) {
super(content); super(content);
} }
public AggregatedPageImpl(List<T> content, float maxScore) {
super(content);
this.maxScore = maxScore;
}
public AggregatedPageImpl(List<T> content, String scrollId) { public AggregatedPageImpl(List<T> content, String scrollId) {
super(content); super(content);
this.scrollId = scrollId; this.scrollId = scrollId;
} }
public AggregatedPageImpl(List<T> content, String scrollId, float maxScore) {
this(content, scrollId);
this.maxScore = maxScore;
}
public AggregatedPageImpl(List<T> content, Pageable pageable, long total) { public AggregatedPageImpl(List<T> content, Pageable pageable, long total) {
super(content, pageable, total); super(content, pageable, total);
} }
public AggregatedPageImpl(List<T> content, Pageable pageable, long total, float maxScore) {
super(content, pageable, total);
this.maxScore = maxScore;
}
public AggregatedPageImpl(List<T> content, Pageable pageable, long total, String scrollId) { public AggregatedPageImpl(List<T> content, Pageable pageable, long total, String scrollId) {
super(content, pageable, total); super(content, pageable, total);
this.scrollId = scrollId; this.scrollId = scrollId;
} }
public AggregatedPageImpl(List<T> content, Pageable pageable, long total, String scrollId, float maxScore) {
this(content, pageable, total, scrollId);
this.maxScore = maxScore;
}
public AggregatedPageImpl(List<T> content, Pageable pageable, long total, Aggregations aggregations) { public AggregatedPageImpl(List<T> content, Pageable pageable, long total, Aggregations aggregations) {
super(content, pageable, total); super(content, pageable, total);
this.aggregations = aggregations; this.aggregations = aggregations;
if (aggregations != null) {
for (Aggregation aggregation : aggregations) {
mapOfAggregations.put(aggregation.getName(), aggregation);
}
}
} }
public AggregatedPageImpl(List<T> content, Pageable pageable, long total, Aggregations aggregations, String scrollId) { public AggregatedPageImpl(List<T> content, Pageable pageable, long total, Aggregations aggregations, float maxScore) {
super(content, pageable, total); this(content, pageable, total, aggregations);
this.aggregations = aggregations; this.maxScore = maxScore;
}
public AggregatedPageImpl(List<T> content, Pageable pageable, long total, Aggregations aggregations,
String scrollId) {
this(content, pageable, total, aggregations);
this.scrollId = scrollId; this.scrollId = scrollId;
if (aggregations != null) { }
for (Aggregation aggregation : aggregations) {
mapOfAggregations.put(aggregation.getName(), aggregation); public AggregatedPageImpl(List<T> content, Pageable pageable, long total, Aggregations aggregations, String scrollId,
} float maxScore) {
} this(content, pageable, total, aggregations, scrollId);
this.maxScore = maxScore;
} }
@Override @Override
public boolean hasAggregations() { public boolean hasAggregations() {
return aggregations != null && mapOfAggregations.size() > 0; return aggregations != null;
} }
@Override @Override
@@ -94,4 +114,9 @@ public class AggregatedPageImpl<T> extends FacetedPageImpl<T> implements Aggrega
public String getScrollId() { public String getScrollId() {
return scrollId; return scrollId;
} }
@Override
public float getMaxScore() {
return maxScore;
}
} }
@@ -1,77 +1,77 @@
/* /*
* Copyright 2013 the original author or authors. * Copyright 2013-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.data.elasticsearch.core.convert; package org.springframework.data.elasticsearch.core.convert;
import java.util.Date; import java.util.Date;
import org.joda.time.DateTimeZone; import org.joda.time.DateTimeZone;
import org.joda.time.LocalDateTime; import org.joda.time.LocalDateTime;
import org.joda.time.ReadableInstant; import org.joda.time.ReadableInstant;
import org.joda.time.format.DateTimeFormatter; import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat; import org.joda.time.format.ISODateTimeFormat;
import org.springframework.core.convert.converter.Converter; import org.springframework.core.convert.converter.Converter;
/** /**
* DateTimeConverters * DateTimeConverters
* *
* @author Rizwan Idrees * @author Rizwan Idrees
* @author Mohsin Husen * @author Mohsin Husen
*/ */
public final class DateTimeConverters { public final class DateTimeConverters {
private static DateTimeFormatter formatter = ISODateTimeFormat.dateTime().withZone(DateTimeZone.UTC); private static DateTimeFormatter formatter = ISODateTimeFormat.dateTime().withZone(DateTimeZone.UTC);
public enum JodaDateTimeConverter implements Converter<ReadableInstant, String> { public enum JodaDateTimeConverter implements Converter<ReadableInstant, String> {
INSTANCE; INSTANCE;
@Override @Override
public String convert(ReadableInstant source) { public String convert(ReadableInstant source) {
if (source == null) { if (source == null) {
return null; return null;
} }
return formatter.print(source); return formatter.print(source);
} }
} }
public enum JodaLocalDateTimeConverter implements Converter<LocalDateTime, String> { public enum JodaLocalDateTimeConverter implements Converter<LocalDateTime, String> {
INSTANCE; INSTANCE;
@Override @Override
public String convert(LocalDateTime source) { public String convert(LocalDateTime source) {
if (source == null) { if (source == null) {
return null; return null;
} }
return formatter.print(source.toDateTime(DateTimeZone.UTC)); return formatter.print(source.toDateTime(DateTimeZone.UTC));
} }
} }
public enum JavaDateConverter implements Converter<Date, String> { public enum JavaDateConverter implements Converter<Date, String> {
INSTANCE; INSTANCE;
@Override @Override
public String convert(Date source) { public String convert(Date source) {
if (source == null) { if (source == null) {
return null; return null;
} }
return formatter.print(source.getTime()); return formatter.print(source.getTime());
} }
} }
} }
@@ -1,45 +1,45 @@
/* /*
* Copyright 2013 the original author or authors. * Copyright 2013-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.data.elasticsearch.core.convert; package org.springframework.data.elasticsearch.core.convert;
import org.springframework.core.convert.ConversionService; import org.springframework.core.convert.ConversionService;
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity; import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity;
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty; import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty;
import org.springframework.data.mapping.context.MappingContext; import org.springframework.data.mapping.context.MappingContext;
/** /**
* ElasticsearchConverter * ElasticsearchConverter
* *
* @author Rizwan Idrees * @author Rizwan Idrees
* @author Mohsin Husen * @author Mohsin Husen
*/ */
public interface ElasticsearchConverter { public interface ElasticsearchConverter {
/** /**
* Returns the underlying {@link org.springframework.data.mapping.context.MappingContext} used by the converter. * Returns the underlying {@link org.springframework.data.mapping.context.MappingContext} used by the converter.
* *
* @return never {@literal null} * @return never {@literal null}
*/ */
MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> getMappingContext(); MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> getMappingContext();
/** /**
* Returns the underlying {@link org.springframework.core.convert.ConversionService} used by the converter. * Returns the underlying {@link org.springframework.core.convert.ConversionService} used by the converter.
* *
* @return never {@literal null}. * @return never {@literal null}.
*/ */
ConversionService getConversionService(); ConversionService getConversionService();
} }
@@ -1,70 +1,70 @@
/* /*
* Copyright 2013-2017 the original author or authors. * Copyright 2013-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.data.elasticsearch.core.convert; package org.springframework.data.elasticsearch.core.convert;
import org.springframework.beans.BeansException; import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware; import org.springframework.context.ApplicationContextAware;
import org.springframework.core.convert.ConversionService; import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.support.DefaultConversionService; import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.core.convert.support.GenericConversionService; import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity; import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity;
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty; import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty;
import org.springframework.data.mapping.context.MappingContext; import org.springframework.data.mapping.context.MappingContext;
import org.springframework.util.Assert; import org.springframework.util.Assert;
/** /**
* MappingElasticsearchConverter * MappingElasticsearchConverter
* *
* @author Rizwan Idrees * @author Rizwan Idrees
* @author Mohsin Husen * @author Mohsin Husen
* @author Mark Paluch * @author Mark Paluch
*/ */
public class MappingElasticsearchConverter implements ElasticsearchConverter, ApplicationContextAware { public class MappingElasticsearchConverter implements ElasticsearchConverter, ApplicationContextAware {
private final MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext; private final MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext;
private final GenericConversionService conversionService; private final GenericConversionService conversionService;
@SuppressWarnings("unused") @SuppressWarnings("unused")
private ApplicationContext applicationContext; private ApplicationContext applicationContext;
public MappingElasticsearchConverter( public MappingElasticsearchConverter(
MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext) { MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext) {
Assert.notNull(mappingContext, "MappingContext must not be null!"); Assert.notNull(mappingContext, "MappingContext must not be null!");
this.mappingContext = mappingContext; this.mappingContext = mappingContext;
this.conversionService = new DefaultConversionService(); this.conversionService = new DefaultConversionService();
} }
@Override @Override
public MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> getMappingContext() { public MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> getMappingContext() {
return mappingContext; return mappingContext;
} }
@Override @Override
public ConversionService getConversionService() { public ConversionService getConversionService() {
return this.conversionService; return this.conversionService;
} }
@Override @Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext; this.applicationContext = applicationContext;
if (mappingContext instanceof ApplicationContextAware) { if (mappingContext instanceof ApplicationContextAware) {
((ApplicationContextAware) mappingContext).setApplicationContext(applicationContext); ((ApplicationContextAware) mappingContext).setApplicationContext(applicationContext);
} }
} }
} }
@@ -1,5 +1,5 @@
/* /*
* Copyright 2014 the original author or authors. * Copyright 2014-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -47,4 +47,4 @@ public abstract class AbstractFacetRequest implements FacetRequest {
public boolean applyQueryFilter() { public boolean applyQueryFilter() {
return applyQueryFilter; return applyQueryFilter;
} }
} }
@@ -1,5 +1,5 @@
/* /*
* Copyright 2014 the original author or authors. * Copyright 2014-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
/* /*
* Copyright 2014 the original author or authors. * Copyright 2014-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
/* /*
* Copyright 2014 the original author or authors. * Copyright 2014-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
/* /*
* Copyright 2014 the original author or authors. * Copyright 2014-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
/* /*
* Copyright 2014 the original author or authors. * Copyright 2014-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -16,14 +16,13 @@
package org.springframework.data.elasticsearch.core.facet.request; package org.springframework.data.elasticsearch.core.facet.request;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.search.aggregations.AbstractAggregationBuilder; import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders; import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramAggregationBuilder; import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramAggregationBuilder;
import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval; import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval;
import org.springframework.data.elasticsearch.core.facet.AbstractFacetRequest; import org.springframework.data.elasticsearch.core.facet.AbstractFacetRequest;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/** /**
* @author Artur Konczak * @author Artur Konczak
@@ -54,7 +53,7 @@ public class HistogramFacetRequest extends AbstractFacetRequest {
public AbstractAggregationBuilder getFacet() { public AbstractAggregationBuilder getFacet() {
Assert.notNull(getName(), "Facet name can't be a null !!!"); Assert.notNull(getName(), "Facet name can't be a null !!!");
Assert.isTrue(StringUtils.isNotBlank(field), "Please select field on which to build the facet !!!"); Assert.isTrue(!StringUtils.isEmpty(field), "Please select field on which to build the facet !!!");
Assert.isTrue(interval > 0, "Please provide interval as positive value greater them zero !!!"); Assert.isTrue(interval > 0, "Please provide interval as positive value greater them zero !!!");
DateHistogramAggregationBuilder dateHistogramBuilder = AggregationBuilders.dateHistogram(getName()); DateHistogramAggregationBuilder dateHistogramBuilder = AggregationBuilders.dateHistogram(getName());
@@ -70,4 +69,4 @@ public class HistogramFacetRequest extends AbstractFacetRequest {
return dateHistogramBuilder; return dateHistogramBuilder;
} }
} }
@@ -1,5 +1,5 @@
/* /*
* Copyright 2014 the original author or authors. * Copyright 2014-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -55,4 +55,4 @@ public class HistogramFacetRequestBuilder {
result.setApplyQueryFilter(true); result.setApplyQueryFilter(true);
return this; return this;
} }
} }
@@ -1,5 +1,5 @@
/* /*
* Copyright 2014 the original author or authors. * Copyright 2014-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
/* /*
* Copyright 2014-2017 the original author or authors. * Copyright 2014-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -19,13 +19,12 @@ package org.springframework.data.elasticsearch.core.facet.request;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.search.aggregations.AbstractAggregationBuilder; import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders; import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.range.RangeAggregationBuilder; import org.elasticsearch.search.aggregations.bucket.range.RangeAggregationBuilder;
import org.springframework.data.elasticsearch.core.facet.AbstractFacetRequest; import org.springframework.data.elasticsearch.core.facet.AbstractFacetRequest;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/** /**
* Range facet for numeric fields * Range facet for numeric fields
@@ -77,7 +76,7 @@ public class RangeFacetRequest extends AbstractFacetRequest {
Assert.notNull(getName(), "Facet name can't be a null !!!"); Assert.notNull(getName(), "Facet name can't be a null !!!");
RangeAggregationBuilder rangeBuilder = AggregationBuilders.range(getName()); RangeAggregationBuilder rangeBuilder = AggregationBuilders.range(getName());
final String field = StringUtils.isNotBlank(keyField) ? keyField : this.field; final String field = !StringUtils.isEmpty(keyField) ? keyField : this.field;
rangeBuilder.field(field); rangeBuilder.field(field);
for (Entry entry : entries) { for (Entry entry : entries) {
@@ -86,7 +85,7 @@ public class RangeFacetRequest extends AbstractFacetRequest {
} }
rangeBuilder.subAggregation(AggregationBuilders.extendedStats(INTERNAL_STATS).field(field)); rangeBuilder.subAggregation(AggregationBuilders.extendedStats(INTERNAL_STATS).field(field));
if(StringUtils.isNotBlank(valueField)){ if(!StringUtils.isEmpty(valueField)){
rangeBuilder.subAggregation(AggregationBuilders.sum(RANGE_INTERNAL_SUM).field(valueField)); rangeBuilder.subAggregation(AggregationBuilders.sum(RANGE_INTERNAL_SUM).field(valueField));
} }
@@ -1,5 +1,5 @@
/* /*
* Copyright 2014 the original author or authors. * Copyright 2014-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -82,4 +82,4 @@ public class RangeFacetRequestBuilder {
public FacetRequest build() { public FacetRequest build() {
return result; return result;
} }
} }
@@ -1,5 +1,5 @@
/* /*
* Copyright 2014 the original author or authors. * Copyright 2014-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -16,12 +16,11 @@
package org.springframework.data.elasticsearch.core.facet.request; package org.springframework.data.elasticsearch.core.facet.request;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.search.aggregations.AbstractAggregationBuilder; import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders; import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.springframework.data.elasticsearch.core.facet.AbstractFacetRequest; import org.springframework.data.elasticsearch.core.facet.AbstractFacetRequest;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/** /**
* @author Petar Tahchiev * @author Petar Tahchiev
@@ -47,7 +46,7 @@ public class StatisticalFacetRequest extends AbstractFacetRequest {
public AbstractAggregationBuilder getFacet() { public AbstractAggregationBuilder getFacet() {
Assert.notNull(getName(), "Facet name can't be a null !!!"); Assert.notNull(getName(), "Facet name can't be a null !!!");
Assert.isTrue(StringUtils.isNotBlank(field) && fields == null, "Please select field or fields on which to build the facets !!!"); Assert.isTrue(!StringUtils.isEmpty(field) && fields == null, "Please select field or fields on which to build the facets !!!");
return AggregationBuilders.extendedStats(getName()).field(field); return AggregationBuilders.extendedStats(getName()).field(field);
} }
} }
@@ -1,5 +1,5 @@
/* /*
* Copyright 2014 the original author or authors. * Copyright 2014-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -49,4 +49,4 @@ public class StatisticalFacetRequestBuilder {
public FacetRequest build() { public FacetRequest build() {
return result; return result;
} }
} }
@@ -1,5 +1,5 @@
/* /*
* Copyright 2014 the original author or authors. * Copyright 2014-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
/* /*
* Copyright 2014 the original author or authors. * Copyright 2014-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -16,22 +16,22 @@
package org.springframework.data.elasticsearch.core.facet.request; package org.springframework.data.elasticsearch.core.facet.request;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.lucene.util.automaton.RegExp; import org.apache.lucene.util.automaton.RegExp;
import org.elasticsearch.search.aggregations.AbstractAggregationBuilder; import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders; import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.terms.Terms; import org.elasticsearch.search.aggregations.BucketOrder;
import org.elasticsearch.search.aggregations.bucket.terms.IncludeExclude;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder; import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.aggregations.bucket.terms.support.IncludeExclude;
import org.springframework.data.elasticsearch.core.facet.AbstractFacetRequest; import org.springframework.data.elasticsearch.core.facet.AbstractFacetRequest;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
/** /**
* Term facet * Term facet
* *
* @author Artur Konczak * @author Artur Konczak
* @author Ilkang Na
*/ */
@Deprecated @Deprecated
public class TermFacetRequest extends AbstractFacetRequest { public class TermFacetRequest extends AbstractFacetRequest {
@@ -48,8 +48,8 @@ public class TermFacetRequest extends AbstractFacetRequest {
} }
public void setFields(String... fields) { public void setFields(String... fields) {
Assert.isTrue(ArrayUtils.isNotEmpty(fields), "Term agg need one field only"); Assert.isTrue(!ObjectUtils.isEmpty(fields), "Term agg need one field only");
Assert.isTrue(ArrayUtils.getLength(fields) == 1, "Term agg need one field only"); Assert.isTrue(fields.length == 1, "Term agg need one field only");
this.fields = fields; this.fields = fields;
} }
@@ -81,29 +81,29 @@ public class TermFacetRequest extends AbstractFacetRequest {
switch (order) { switch (order) {
case descTerm: case descTerm:
termsBuilder.order(Terms.Order.term(false)); termsBuilder.order(BucketOrder.key(false));
break; break;
case ascTerm: case ascTerm:
termsBuilder.order(Terms.Order.term(true)); termsBuilder.order(BucketOrder.key(true));
break; break;
case descCount: case descCount:
termsBuilder.order(Terms.Order.count(false)); termsBuilder.order(BucketOrder.count(false));
break; break;
default: default:
termsBuilder.order(Terms.Order.count(true)); termsBuilder.order(BucketOrder.count(true));
} }
if (ArrayUtils.isNotEmpty(excludeTerms)) { if (!ObjectUtils.isEmpty(excludeTerms)) {
termsBuilder.includeExclude(new IncludeExclude(null,excludeTerms)); termsBuilder.includeExclude(new IncludeExclude(null, excludeTerms));
} }
if (allTerms) { if (allTerms) {
termsBuilder.size(Integer.MAX_VALUE); termsBuilder.size(Integer.MAX_VALUE);
} }
if (StringUtils.isNotBlank(regex)) { if (!StringUtils.isEmpty(regex)) {
termsBuilder.includeExclude(new IncludeExclude(new RegExp(regex),null)); termsBuilder.includeExclude(new IncludeExclude(new RegExp(regex), null));
} }
return termsBuilder; return termsBuilder;
} }
} }
@@ -1,5 +1,5 @@
/* /*
* Copyright 2014 the original author or authors. * Copyright 2014-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -85,4 +85,4 @@ public class TermFacetRequestBuilder {
public FacetRequest build() { public FacetRequest build() {
return result; return result;
} }
} }
@@ -1,5 +1,5 @@
/* /*
* Copyright 2014 the original author or authors. * Copyright 2014-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
/* /*
* Copyright 2014 the original author or authors. * Copyright 2014-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
/* /*
* Copyright 2014 the original author or authors. * Copyright 2014-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
/* /*
* Copyright 2014 the original author or authors. * Copyright 2014-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
/* /*
* Copyright 2014 the original author or authors. * Copyright 2014-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
/* /*
* Copyright 2014 the original author or authors. * Copyright 2014-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
/* /*
* Copyright 2014 the original author or authors. * Copyright 2014-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
/* /*
* Copyright 2013 the original author or authors. * Copyright 2013-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
/* /*
* Copyright 2013-2016 the original author or authors. * Copyright 2013-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
/* /*
* Copyright 2013-2017 the original author or authors. * Copyright 2013-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
package org.springframework.data.elasticsearch.core.mapping; package org.springframework.data.elasticsearch.core.mapping;
import org.springframework.data.mapping.PersistentEntity; import org.springframework.data.mapping.PersistentEntity;
import org.springframework.lang.Nullable;
/** /**
* ElasticsearchPersistentEntity * ElasticsearchPersistentEntity
@@ -23,6 +24,8 @@ import org.springframework.data.mapping.PersistentEntity;
* @author Rizwan Idrees * @author Rizwan Idrees
* @author Mohsin Husen * @author Mohsin Husen
* @author Mark Paluch * @author Mark Paluch
* @author Sascha Woo
* @author Oliver Gierke
*/ */
public interface ElasticsearchPersistentEntity<T> extends PersistentEntity<T, ElasticsearchPersistentProperty> { public interface ElasticsearchPersistentEntity<T> extends PersistentEntity<T, ElasticsearchPersistentProperty> {
@@ -49,4 +52,24 @@ public interface ElasticsearchPersistentEntity<T> extends PersistentEntity<T, El
String settingPath(); String settingPath();
boolean isCreateIndexAndMapping(); boolean isCreateIndexAndMapping();
/**
* Returns whether the {@link ElasticsearchPersistentEntity} has an score property. If this call returns
* {@literal true}, {@link #getScoreProperty()} will return a non-{@literal null} value.
*
* @return false when {@link ElasticsearchPersistentEntity} does not define a score property.
* @since 3.1
*/
boolean hasScoreProperty();
/**
* Returns the score property of the {@link ElasticsearchPersistentEntity}. Can be {@literal null} in case no score
* property is available on the entity.
*
* @return the score {@link ElasticsearchPersistentProperty} of the {@link PersistentEntity} or {@literal null} if not
* defined.
* @since 3.1
*/
@Nullable
ElasticsearchPersistentProperty getScoreProperty();
} }
@@ -1,40 +1,65 @@
/* /*
* Copyright 2013 the original author or authors. * Copyright 2013-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.data.elasticsearch.core.mapping; package org.springframework.data.elasticsearch.core.mapping;
import org.springframework.core.convert.converter.Converter; import org.springframework.core.convert.converter.Converter;
import org.springframework.data.mapping.PersistentProperty; import org.springframework.data.mapping.PersistentProperty;
/** /**
* ElasticsearchPersistentProperty * ElasticsearchPersistentProperty
* *
* @author Rizwan Idrees * @author Rizwan Idrees
* @author Mohsin Husen * @author Mohsin Husen
*/ * @author Sascha Woo
* @author Oliver Gierke
public interface ElasticsearchPersistentProperty extends PersistentProperty<ElasticsearchPersistentProperty> { */
public interface ElasticsearchPersistentProperty extends PersistentProperty<ElasticsearchPersistentProperty> {
String getFieldName();
String getFieldName();
public enum PropertyToFieldNameConverter implements Converter<ElasticsearchPersistentProperty, String> {
/**
INSTANCE; * Returns whether the current property is a <em>potential</em> score property of the owning
* {@link ElasticsearchPersistentEntity}. This method is mainly used by {@link ElasticsearchPersistentEntity}
public String convert(ElasticsearchPersistentProperty source) { * implementation to discover score property candidates on {@link ElasticsearchPersistentEntity} creation you should
return source.getFieldName(); * rather call {@link ElasticsearchPersistentEntity#isScoreProperty(PersistentProperty)} to determine whether the
} * current property is the score property of that {@link ElasticsearchPersistentEntity} under consideration.
} *
} * @return
* @since 3.1
*/
boolean isScoreProperty();
/**
* Returns whether the current property is a <em>potential</em> parent property of the owning
* {@link ElasticsearchPersistentEntity}. This method is mainly used by {@link ElasticsearchPersistentEntity}
* implementation to discover parent property candidates on {@link ElasticsearchPersistentEntity} creation you should
* rather call {@link ElasticsearchPersistentEntity#isParentProperty()} to determine whether the current property is
* the parent property of that {@link ElasticsearchPersistentEntity} under consideration.
*
* @return
* @since 3.1
*/
boolean isParentProperty();
public enum PropertyToFieldNameConverter implements Converter<ElasticsearchPersistentProperty, String> {
INSTANCE;
public String convert(ElasticsearchPersistentProperty source) {
return source.getFieldName();
}
}
}
@@ -1,58 +1,58 @@
/* /*
* Copyright 2013-2017 the original author or authors. * Copyright 2013-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.data.elasticsearch.core.mapping; package org.springframework.data.elasticsearch.core.mapping;
import org.springframework.beans.BeansException; import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware; import org.springframework.context.ApplicationContextAware;
import org.springframework.data.mapping.context.AbstractMappingContext; import org.springframework.data.mapping.context.AbstractMappingContext;
import org.springframework.data.mapping.model.Property; import org.springframework.data.mapping.model.Property;
import org.springframework.data.mapping.model.SimpleTypeHolder; import org.springframework.data.mapping.model.SimpleTypeHolder;
import org.springframework.data.util.TypeInformation; import org.springframework.data.util.TypeInformation;
/** /**
* SimpleElasticsearchMappingContext * SimpleElasticsearchMappingContext
* *
* @author Rizwan Idrees * @author Rizwan Idrees
* @author Mohsin Husen * @author Mohsin Husen
* @author Mark Paluch * @author Mark Paluch
*/ */
public class SimpleElasticsearchMappingContext extends public class SimpleElasticsearchMappingContext extends
AbstractMappingContext<SimpleElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> implements ApplicationContextAware { AbstractMappingContext<SimpleElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> implements ApplicationContextAware {
private ApplicationContext context; private ApplicationContext context;
@Override @Override
protected <T> SimpleElasticsearchPersistentEntity<?> createPersistentEntity(TypeInformation<T> typeInformation) { protected <T> SimpleElasticsearchPersistentEntity<?> createPersistentEntity(TypeInformation<T> typeInformation) {
final SimpleElasticsearchPersistentEntity<T> persistentEntity = new SimpleElasticsearchPersistentEntity<>( final SimpleElasticsearchPersistentEntity<T> persistentEntity = new SimpleElasticsearchPersistentEntity<>(
typeInformation); typeInformation);
if (context != null) { if (context != null) {
persistentEntity.setApplicationContext(context); persistentEntity.setApplicationContext(context);
} }
return persistentEntity; return persistentEntity;
} }
@Override @Override
protected ElasticsearchPersistentProperty createPersistentProperty(Property property, protected ElasticsearchPersistentProperty createPersistentProperty(Property property,
SimpleElasticsearchPersistentEntity<?> owner, SimpleTypeHolder simpleTypeHolder) { SimpleElasticsearchPersistentEntity<?> owner, SimpleTypeHolder simpleTypeHolder) {
return new SimpleElasticsearchPersistentProperty(property, owner, simpleTypeHolder); return new SimpleElasticsearchPersistentProperty(property, owner, simpleTypeHolder);
} }
@Override @Override
public void setApplicationContext(ApplicationContext context) throws BeansException { public void setApplicationContext(ApplicationContext context) throws BeansException {
this.context = context; this.context = context;
} }
} }
@@ -1,5 +1,5 @@
/* /*
* Copyright 2013-2017 the original author or authors. * Copyright 2013-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -18,7 +18,6 @@ package org.springframework.data.elasticsearch.core.mapping;
import static org.springframework.util.StringUtils.*; import static org.springframework.util.StringUtils.*;
import java.util.Locale; import java.util.Locale;
import java.util.Optional;
import org.springframework.beans.BeansException; import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
@@ -28,12 +27,16 @@ import org.springframework.context.expression.BeanFactoryResolver;
import org.springframework.data.elasticsearch.annotations.Document; import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Parent; import org.springframework.data.elasticsearch.annotations.Parent;
import org.springframework.data.elasticsearch.annotations.Setting; import org.springframework.data.elasticsearch.annotations.Setting;
import org.springframework.data.mapping.MappingException;
import org.springframework.data.mapping.PersistentPropertyAccessor;
import org.springframework.data.mapping.model.BasicPersistentEntity; import org.springframework.data.mapping.model.BasicPersistentEntity;
import org.springframework.data.mapping.model.PersistentPropertyAccessorFactory;
import org.springframework.data.util.TypeInformation; import org.springframework.data.util.TypeInformation;
import org.springframework.expression.Expression; import org.springframework.expression.Expression;
import org.springframework.expression.ParserContext; import org.springframework.expression.ParserContext;
import org.springframework.expression.spel.standard.SpelExpressionParser; import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext; import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert; import org.springframework.util.Assert;
/** /**
@@ -43,6 +46,7 @@ import org.springframework.util.Assert;
* @author Rizwan Idrees * @author Rizwan Idrees
* @author Mohsin Husen * @author Mohsin Husen
* @author Mark Paluch * @author Mark Paluch
* @author Sascha Woo
*/ */
public class SimpleElasticsearchPersistentEntity<T> extends BasicPersistentEntity<T, ElasticsearchPersistentProperty> public class SimpleElasticsearchPersistentEntity<T> extends BasicPersistentEntity<T, ElasticsearchPersistentProperty>
implements ElasticsearchPersistentEntity<T>, ApplicationContextAware { implements ElasticsearchPersistentEntity<T>, ApplicationContextAware {
@@ -59,6 +63,7 @@ public class SimpleElasticsearchPersistentEntity<T> extends BasicPersistentEntit
private String indexStoreType; private String indexStoreType;
private String parentType; private String parentType;
private ElasticsearchPersistentProperty parentIdProperty; private ElasticsearchPersistentProperty parentIdProperty;
private ElasticsearchPersistentProperty scoreProperty;
private String settingPath; private String settingPath;
private boolean createIndexAndMapping; private boolean createIndexAndMapping;
@@ -150,22 +155,58 @@ public class SimpleElasticsearchPersistentEntity<T> extends BasicPersistentEntit
return createIndexAndMapping; return createIndexAndMapping;
} }
@Override
public boolean hasScoreProperty() {
return scoreProperty != null;
}
@Nullable
@Override
public ElasticsearchPersistentProperty getScoreProperty() {
return scoreProperty;
}
@Override @Override
public void addPersistentProperty(ElasticsearchPersistentProperty property) { public void addPersistentProperty(ElasticsearchPersistentProperty property) {
super.addPersistentProperty(property); super.addPersistentProperty(property);
Parent annotation = property.findAnnotation(Parent.class); if (property.isParentProperty()) {
ElasticsearchPersistentProperty parentProperty = this.parentIdProperty;
if (annotation != null) { if (parentProperty != null) {
Assert.isNull(this.parentIdProperty, "Only one field can hold a @Parent annotation"); throw new MappingException(
Assert.isNull(this.parentType, "Only one field can hold a @Parent annotation"); String.format("Attempt to add parent property %s but already have property %s registered "
Assert.isTrue(property.getType() == String.class, "Parent ID property should be String"); + "as parent property. Check your mapping configuration!", property.getField(), parentProperty.getField()));
}
Parent parentAnnotation = property.findAnnotation(Parent.class);
this.parentIdProperty = property; this.parentIdProperty = property;
this.parentType = annotation.type(); this.parentType = parentAnnotation.type();
} }
if (property.isVersionProperty()) { if (property.isScoreProperty()) {
Assert.isTrue(property.getType() == Long.class, "Version property should be Long");
ElasticsearchPersistentProperty scoreProperty = this.scoreProperty;
if (scoreProperty != null) {
throw new MappingException(
String.format("Attempt to add score property %s but already have property %s registered "
+ "as score property. Check your mapping configuration!", property.getField(), scoreProperty.getField()));
}
this.scoreProperty = property;
} }
} }
/*
* (non-Javadoc)
* @see org.springframework.data.mapping.model.BasicPersistentEntity#setPersistentPropertyAccessorFactory(org.springframework.data.mapping.model.PersistentPropertyAccessorFactory)
*/
@Override
public void setPersistentPropertyAccessorFactory(PersistentPropertyAccessorFactory factory) {
// Do nothing to avoid the usage of ClassGeneratingPropertyAccessorFactory for now
// DATACMNS-1322 switches to proper immutability behavior which Spring Data Elasticsearch
// cannot yet implement
}
} }
@@ -1,65 +1,124 @@
/* /*
* Copyright 2013-2017 the original author or authors. * Copyright 2013-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.data.elasticsearch.core.mapping; package org.springframework.data.elasticsearch.core.mapping;
import java.util.HashSet; import java.util.Arrays;
import java.util.Set; import java.util.List;
import org.springframework.data.mapping.Association; import org.springframework.data.elasticsearch.annotations.Parent;
import org.springframework.data.mapping.PersistentEntity; import org.springframework.data.elasticsearch.annotations.Score;
import org.springframework.data.mapping.model.AnnotationBasedPersistentProperty; import org.springframework.data.mapping.Association;
import org.springframework.data.mapping.model.Property; import org.springframework.data.mapping.MappingException;
import org.springframework.data.mapping.model.SimpleTypeHolder; import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.mapping.model.AnnotationBasedPersistentProperty;
/** import org.springframework.data.mapping.model.Property;
* Elasticsearch specific {@link org.springframework.data.mapping.PersistentProperty} implementation processing import org.springframework.data.mapping.model.SimpleTypeHolder;
*
* @author Rizwan Idrees /**
* @author Mohsin Husen * Elasticsearch specific {@link org.springframework.data.mapping.PersistentProperty} implementation processing
* @author Mark Paluch *
*/ * @author Rizwan Idrees
public class SimpleElasticsearchPersistentProperty extends * @author Mohsin Husen
AnnotationBasedPersistentProperty<ElasticsearchPersistentProperty> implements ElasticsearchPersistentProperty { * @author Mark Paluch
* @author Sascha Woo
private static final Set<Class<?>> SUPPORTED_ID_TYPES = new HashSet<>(); * @author Oliver Gierke
private static final Set<String> SUPPORTED_ID_PROPERTY_NAMES = new HashSet<>(); */
public class SimpleElasticsearchPersistentProperty extends
static { AnnotationBasedPersistentProperty<ElasticsearchPersistentProperty> implements ElasticsearchPersistentProperty {
SUPPORTED_ID_TYPES.add(String.class);
SUPPORTED_ID_PROPERTY_NAMES.add("id"); private static final List<String> SUPPORTED_ID_PROPERTY_NAMES = Arrays.asList("id", "document");
SUPPORTED_ID_PROPERTY_NAMES.add("documentId");
} private final boolean isScore;
private final boolean isParent;
public SimpleElasticsearchPersistentProperty(Property property, private final boolean isId;
PersistentEntity<?, ElasticsearchPersistentProperty> owner, SimpleTypeHolder simpleTypeHolder) {
super(property, owner, simpleTypeHolder); public SimpleElasticsearchPersistentProperty(Property property,
} PersistentEntity<?, ElasticsearchPersistentProperty> owner, SimpleTypeHolder simpleTypeHolder) {
@Override super(property, owner, simpleTypeHolder);
public String getFieldName() {
return getProperty().getName(); this.isId = super.isIdProperty() || SUPPORTED_ID_PROPERTY_NAMES.contains(getFieldName());
} this.isScore = isAnnotationPresent(Score.class);
this.isParent = isAnnotationPresent(Parent.class);
@Override
public boolean isIdProperty() { if (isVersionProperty() && getType() != Long.class) {
return super.isIdProperty() || SUPPORTED_ID_PROPERTY_NAMES.contains(getFieldName()); throw new MappingException(String.format("Version property %s must be of type Long!", property.getName()));
} }
@Override if (isScore && !Arrays.asList(Float.TYPE, Float.class).contains(getType())) {
protected Association<ElasticsearchPersistentProperty> createAssociation() { throw new MappingException(
return null; String.format("Score property %s must be either of type float or Float!", property.getName()));
} }
}
if (isParent && getType() != String.class) {
throw new MappingException(String.format("Parent property %s must be of type String!", property.getName()));
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty#getFieldName()
*/
@Override
public String getFieldName() {
return getProperty().getName();
}
/*
* (non-Javadoc)
* @see org.springframework.data.mapping.model.AnnotationBasedPersistentProperty#isIdProperty()
*/
@Override
public boolean isIdProperty() {
return isId;
}
/*
* (non-Javadoc)
* @see org.springframework.data.mapping.model.AbstractPersistentProperty#createAssociation()
*/
@Override
protected Association<ElasticsearchPersistentProperty> createAssociation() {
return null;
}
/*
* (non-Javadoc)
* @see org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty#isScoreProperty()
*/
@Override
public boolean isScoreProperty() {
return isScore;
}
/*
* (non-Javadoc)
* @see org.springframework.data.mapping.model.AbstractPersistentProperty#isImmutable()
*/
@Override
public boolean isImmutable() {
return false;
}
/*
* (non-Javadoc)
* @see org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty#isParentProperty()
*/
@Override
public boolean isParentProperty() {
return isParent;
}
}
@@ -1,5 +1,5 @@
/* /*
* Copyright 2013-2017 the original author or authors. * Copyright 2013-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -15,11 +15,14 @@
*/ */
package org.springframework.data.elasticsearch.core.query; package org.springframework.data.elasticsearch.core.query;
import static java.util.Collections.addAll; import static java.util.Collections.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import org.elasticsearch.action.search.SearchType; import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.action.support.IndicesOptions;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort;
import org.springframework.util.Assert; import org.springframework.util.Assert;
@@ -30,6 +33,8 @@ import org.springframework.util.Assert;
* @author Rizwan Idrees * @author Rizwan Idrees
* @author Mohsin Husen * @author Mohsin Husen
* @author Mark Paluch * @author Mark Paluch
* @author Alen Turkovic
* @author Sascha Woo
*/ */
abstract class AbstractQuery implements Query { abstract class AbstractQuery implements Query {
@@ -43,6 +48,8 @@ abstract class AbstractQuery implements Query {
protected Collection<String> ids; protected Collection<String> ids;
protected String route; protected String route;
protected SearchType searchType = SearchType.DFS_QUERY_THEN_FETCH; protected SearchType searchType = SearchType.DFS_QUERY_THEN_FETCH;
protected IndicesOptions indicesOptions;
protected boolean trackScores;
@Override @Override
public Sort getSort() { public Sort getSort() {
@@ -149,4 +156,31 @@ abstract class AbstractQuery implements Query {
public SearchType getSearchType() { public SearchType getSearchType() {
return searchType; return searchType;
} }
public IndicesOptions getIndicesOptions() {
return indicesOptions;
}
public void setIndicesOptions(IndicesOptions indicesOptions) {
this.indicesOptions = indicesOptions;
}
/*
* (non-Javadoc)
* @see org.springframework.data.elasticsearch.core.query.Query#getTrackScores()
*/
@Override
public boolean getTrackScores() {
return trackScores;
}
/**
* Configures whether to track scores.
*
* @param trackScores
* @since 3.1
*/
public void setTrackScores(boolean trackScores) {
this.trackScores = trackScores;
}
} }
@@ -1,81 +1,81 @@
/* /*
* Copyright 2013-2014 the original author or authors. * Copyright 2013-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.data.elasticsearch.core.query; package org.springframework.data.elasticsearch.core.query;
import java.util.Map; import java.util.Map;
import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilder;
/** /**
* @author Mohsin Husen * @author Mohsin Husen
*/ */
public class AliasBuilder { public class AliasBuilder {
private String indexName; private String indexName;
private String aliasName; private String aliasName;
private QueryBuilder filterBuilder; private QueryBuilder filterBuilder;
private Map<String, Object> filter; private Map<String, Object> filter;
private String searchRouting; private String searchRouting;
private String indexRouting; private String indexRouting;
private String routing; private String routing;
public AliasBuilder withIndexName(String indexName) { public AliasBuilder withIndexName(String indexName) {
this.indexName = indexName; this.indexName = indexName;
return this; return this;
} }
public AliasBuilder withAliasName(String aliasName) { public AliasBuilder withAliasName(String aliasName) {
this.aliasName = aliasName; this.aliasName = aliasName;
return this; return this;
} }
public AliasBuilder withFilterBuilder(QueryBuilder filterBuilder) { public AliasBuilder withFilterBuilder(QueryBuilder filterBuilder) {
this.filterBuilder = filterBuilder; this.filterBuilder = filterBuilder;
return this; return this;
} }
public AliasBuilder withFilter(Map<String, Object> filter) { public AliasBuilder withFilter(Map<String, Object> filter) {
this.filter = filter; this.filter = filter;
return this; return this;
} }
public AliasBuilder withSearchRouting(String searchRouting) { public AliasBuilder withSearchRouting(String searchRouting) {
this.searchRouting = searchRouting; this.searchRouting = searchRouting;
return this; return this;
} }
public AliasBuilder withIndexRouting(String indexRouting) { public AliasBuilder withIndexRouting(String indexRouting) {
this.indexRouting = indexRouting; this.indexRouting = indexRouting;
return this; return this;
} }
public AliasBuilder withRouting(String routing) { public AliasBuilder withRouting(String routing) {
this.routing = routing; this.routing = routing;
return this; return this;
} }
public AliasQuery build() { public AliasQuery build() {
AliasQuery aliasQuery = new AliasQuery(); AliasQuery aliasQuery = new AliasQuery();
aliasQuery.setIndexName(indexName); aliasQuery.setIndexName(indexName);
aliasQuery.setAliasName(aliasName); aliasQuery.setAliasName(aliasName);
aliasQuery.setFilterBuilder(filterBuilder); aliasQuery.setFilterBuilder(filterBuilder);
aliasQuery.setFilter(filter); aliasQuery.setFilter(filter);
aliasQuery.setSearchRouting(searchRouting); aliasQuery.setSearchRouting(searchRouting);
aliasQuery.setIndexRouting(indexRouting); aliasQuery.setIndexRouting(indexRouting);
aliasQuery.setRouting(routing); aliasQuery.setRouting(routing);
return aliasQuery; return aliasQuery;
} }
} }
@@ -1,92 +1,92 @@
/* /*
* Copyright 2013-2014 the original author or authors. * Copyright 2013-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.data.elasticsearch.core.query; package org.springframework.data.elasticsearch.core.query;
import java.util.Map; import java.util.Map;
import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilder;
/** /**
* AliasQuery is useful for creating new alias or deleting existing ones * AliasQuery is useful for creating new alias or deleting existing ones
* *
* @author Mohsin Husen * @author Mohsin Husen
*/ */
public class AliasQuery { public class AliasQuery {
private String indexName; private String indexName;
private String aliasName; private String aliasName;
private QueryBuilder filterBuilder; private QueryBuilder filterBuilder;
private Map<String, Object> filter; private Map<String, Object> filter;
private String searchRouting; private String searchRouting;
private String indexRouting; private String indexRouting;
private String routing; private String routing;
public String getIndexName() { public String getIndexName() {
return indexName; return indexName;
} }
public void setIndexName(String indexName) { public void setIndexName(String indexName) {
this.indexName = indexName; this.indexName = indexName;
} }
public String getAliasName() { public String getAliasName() {
return aliasName; return aliasName;
} }
public void setAliasName(String aliasName) { public void setAliasName(String aliasName) {
this.aliasName = aliasName; this.aliasName = aliasName;
} }
public QueryBuilder getFilterBuilder() { public QueryBuilder getFilterBuilder() {
return filterBuilder; return filterBuilder;
} }
public void setFilterBuilder(QueryBuilder filterBuilder) { public void setFilterBuilder(QueryBuilder filterBuilder) {
this.filterBuilder = filterBuilder; this.filterBuilder = filterBuilder;
} }
public Map<String, Object> getFilter() { public Map<String, Object> getFilter() {
return filter; return filter;
} }
public void setFilter(Map<String, Object> filter) { public void setFilter(Map<String, Object> filter) {
this.filter = filter; this.filter = filter;
} }
public String getSearchRouting() { public String getSearchRouting() {
return searchRouting; return searchRouting;
} }
public void setSearchRouting(String searchRouting) { public void setSearchRouting(String searchRouting) {
this.searchRouting = searchRouting; this.searchRouting = searchRouting;
} }
public String getIndexRouting() { public String getIndexRouting() {
return indexRouting; return indexRouting;
} }
public void setIndexRouting(String indexRouting) { public void setIndexRouting(String indexRouting) {
this.indexRouting = indexRouting; this.indexRouting = indexRouting;
} }
public String getRouting() { public String getRouting() {
return routing; return routing;
} }
public void setRouting(String routing) { public void setRouting(String routing) {
this.routing = routing; this.routing = routing;
} }
} }
File diff suppressed because it is too large Load Diff
@@ -1,82 +1,82 @@
/* /*
* Copyright 2013-2017 the original author or authors. * Copyright 2013-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.data.elasticsearch.core.query; package org.springframework.data.elasticsearch.core.query;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
import org.springframework.util.Assert; import org.springframework.util.Assert;
/** /**
* CriteriaQuery * CriteriaQuery
* *
* @author Rizwan Idrees * @author Rizwan Idrees
* @author Mohsin Husen * @author Mohsin Husen
* @author Mark Paluch * @author Mark Paluch
*/ */
public class CriteriaQuery extends AbstractQuery { public class CriteriaQuery extends AbstractQuery {
private Criteria criteria; private Criteria criteria;
private CriteriaQuery() {} private CriteriaQuery() {}
public CriteriaQuery(Criteria criteria) { public CriteriaQuery(Criteria criteria) {
this(criteria, Pageable.unpaged()); this(criteria, Pageable.unpaged());
} }
public CriteriaQuery(Criteria criteria, Pageable pageable) { public CriteriaQuery(Criteria criteria, Pageable pageable) {
Assert.notNull(criteria, "Criteria must not be null!"); Assert.notNull(criteria, "Criteria must not be null!");
Assert.notNull(pageable, "Pageable must not be null!"); Assert.notNull(pageable, "Pageable must not be null!");
this.criteria = criteria; this.criteria = criteria;
this.pageable = pageable; this.pageable = pageable;
this.addSort(pageable.getSort()); this.addSort(pageable.getSort());
} }
public static final Query fromQuery(CriteriaQuery source) { public static final Query fromQuery(CriteriaQuery source) {
return fromQuery(source, new CriteriaQuery()); return fromQuery(source, new CriteriaQuery());
} }
public static <T extends CriteriaQuery> T fromQuery(CriteriaQuery source, T destination) { public static <T extends CriteriaQuery> T fromQuery(CriteriaQuery source, T destination) {
if (source == null || destination == null) { if (source == null || destination == null) {
return null; return null;
} }
if (source.getCriteria() != null) { if (source.getCriteria() != null) {
destination.addCriteria(source.getCriteria()); destination.addCriteria(source.getCriteria());
} }
if (source.getSort() != null) { if (source.getSort() != null) {
destination.addSort(source.getSort()); destination.addSort(source.getSort());
} }
return destination; return destination;
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public final <T extends CriteriaQuery> T addCriteria(Criteria criteria) { public final <T extends CriteriaQuery> T addCriteria(Criteria criteria) {
Assert.notNull(criteria, "Cannot add null criteria."); Assert.notNull(criteria, "Cannot add null criteria.");
if (this.criteria == null) { if (this.criteria == null) {
this.criteria = criteria; this.criteria = criteria;
} else { } else {
this.criteria.and(criteria); this.criteria.and(criteria);
} }
return (T) this; return (T) this;
} }
public Criteria getCriteria() { public Criteria getCriteria() {
return this.criteria; return this.criteria;
} }
} }
@@ -1,73 +1,73 @@
/* /*
* Copyright 2013-2016 the original author or authors. * Copyright 2013-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.data.elasticsearch.core.query; package org.springframework.data.elasticsearch.core.query;
import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilder;
/** /**
* DeleteQuery * DeleteQuery
* *
* @author Rizwan Idrees * @author Rizwan Idrees
* @author Mohsin Husen * @author Mohsin Husen
*/ */
public class DeleteQuery { public class DeleteQuery {
private QueryBuilder query; private QueryBuilder query;
private String index; private String index;
private String type; private String type;
private Integer pageSize; private Integer pageSize;
private Long scrollTimeInMillis; private Long scrollTimeInMillis;
public QueryBuilder getQuery() { public QueryBuilder getQuery() {
return query; return query;
} }
public void setQuery(QueryBuilder query) { public void setQuery(QueryBuilder query) {
this.query = query; this.query = query;
} }
public String getIndex() { public String getIndex() {
return index; return index;
} }
public void setIndex(String index) { public void setIndex(String index) {
this.index = index; this.index = index;
} }
public String getType() { public String getType() {
return type; return type;
} }
public void setType(String type) { public void setType(String type) {
this.type = type; this.type = type;
} }
public Integer getPageSize() { public Integer getPageSize() {
return pageSize; return pageSize;
} }
public void setPageSize(Integer pageSize) { public void setPageSize(Integer pageSize) {
this.pageSize = pageSize; this.pageSize = pageSize;
} }
public Long getScrollTimeInMillis() { public Long getScrollTimeInMillis() {
return scrollTimeInMillis; return scrollTimeInMillis;
} }
public void setScrollTimeInMillis(Long scrollTimeInMillis) { public void setScrollTimeInMillis(Long scrollTimeInMillis) {
this.scrollTimeInMillis = scrollTimeInMillis; this.scrollTimeInMillis = scrollTimeInMillis;
} }
} }
@@ -1,5 +1,5 @@
/* /*
* Copyright 2016 the original author or authors. * Copyright 2016-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
/* /*
* Copyright 2016 the original author or authors. * Copyright 2016-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -1,32 +1,32 @@
/* /*
* Copyright 2013 the original author or authors. * Copyright 2013-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.data.elasticsearch.core.query; package org.springframework.data.elasticsearch.core.query;
/** /**
* Defines a Field that can be used within a Criteria. * Defines a Field that can be used within a Criteria.
* *
* @author Rizwan Idrees * @author Rizwan Idrees
* @author Mohsin Husen * @author Mohsin Husen
*/ */
public interface Field { public interface Field {
/** /**
* Get the name of the field used in schema.xml of elasticsearch server * Get the name of the field used in schema.xml of elasticsearch server
* *
* @return * @return
*/ */
String getName(); String getName();
} }
@@ -1,35 +1,35 @@
/* /*
* Copyright 2013 the original author or authors. * Copyright 2013-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.data.elasticsearch.core.query; package org.springframework.data.elasticsearch.core.query;
/** /**
* GetQuery * GetQuery
* *
* @author Rizwan Idrees * @author Rizwan Idrees
* @author Mohsin Husen * @author Mohsin Husen
*/ */
public class GetQuery { public class GetQuery {
private String id; private String id;
public String getId() { public String getId() {
return id; return id;
} }
public void setId(String id) { public void setId(String id) {
this.id = id; this.id = id;
} }
} }
@@ -1,5 +1,5 @@
/* /*
* Copyright 2016 the original author or authors. * Copyright 2016-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -1,90 +1,90 @@
/* /*
* Copyright 2013 the original author or authors. * Copyright 2013-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.data.elasticsearch.core.query; package org.springframework.data.elasticsearch.core.query;
/** /**
* IndexQuery * IndexQuery
* *
* @author Rizwan Idrees * @author Rizwan Idrees
* @author Mohsin Husen * @author Mohsin Husen
*/ */
public class IndexQuery { public class IndexQuery {
private String id; private String id;
private Object object; private Object object;
private Long version; private Long version;
private String indexName; private String indexName;
private String type; private String type;
private String source; private String source;
private String parentId; private String parentId;
public String getId() { public String getId() {
return id; return id;
} }
public void setId(String id) { public void setId(String id) {
this.id = id; this.id = id;
} }
public Object getObject() { public Object getObject() {
return object; return object;
} }
public void setObject(Object object) { public void setObject(Object object) {
this.object = object; this.object = object;
} }
public Long getVersion() { public Long getVersion() {
return version; return version;
} }
public void setVersion(Long version) { public void setVersion(Long version) {
this.version = version; this.version = version;
} }
public String getIndexName() { public String getIndexName() {
return indexName; return indexName;
} }
public void setIndexName(String indexName) { public void setIndexName(String indexName) {
this.indexName = indexName; this.indexName = indexName;
} }
public String getType() { public String getType() {
return type; return type;
} }
public void setType(String type) { public void setType(String type) {
this.type = type; this.type = type;
} }
public String getSource() { public String getSource() {
return source; return source;
} }
public void setSource(String source) { public void setSource(String source) {
this.source = source; this.source = source;
} }
public String getParentId() { public String getParentId() {
return parentId; return parentId;
} }
public void setParentId(String parentId) { public void setParentId(String parentId) {
this.parentId = parentId; this.parentId = parentId;
} }
} }
@@ -1,5 +1,5 @@
/* /*
* Copyright 2014 the original author or authors. * Copyright 2014-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -1,187 +1,187 @@
/* /*
* Copyright 2013-2017 the original author or authors. * Copyright 2013-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.data.elasticsearch.core.query; package org.springframework.data.elasticsearch.core.query;
import static java.util.Collections.addAll; import static java.util.Collections.addAll;
import static org.springframework.data.elasticsearch.core.query.AbstractQuery.*; import static org.springframework.data.elasticsearch.core.query.AbstractQuery.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
/** /**
* MoreLikeThisQuery * MoreLikeThisQuery
* *
* @author Rizwan Idrees * @author Rizwan Idrees
* @author Mohsin Husen * @author Mohsin Husen
*/ */
public class MoreLikeThisQuery { public class MoreLikeThisQuery {
private String id; private String id;
private String indexName; private String indexName;
private String type; private String type;
private List<String> searchIndices = new ArrayList<>(); private List<String> searchIndices = new ArrayList<>();
private List<String> searchTypes = new ArrayList<>(); private List<String> searchTypes = new ArrayList<>();
private List<String> fields = new ArrayList<>(); private List<String> fields = new ArrayList<>();
private String routing; private String routing;
private Float percentTermsToMatch; private Float percentTermsToMatch;
private Integer minTermFreq; private Integer minTermFreq;
private Integer maxQueryTerms; private Integer maxQueryTerms;
private List<String> stopWords = new ArrayList<>(); private List<String> stopWords = new ArrayList<>();
private Integer minDocFreq; private Integer minDocFreq;
private Integer maxDocFreq; private Integer maxDocFreq;
private Integer minWordLen; private Integer minWordLen;
private Integer maxWordLen; private Integer maxWordLen;
private Float boostTerms; private Float boostTerms;
private Pageable pageable = DEFAULT_PAGE; private Pageable pageable = DEFAULT_PAGE;
public String getId() { public String getId() {
return id; return id;
} }
public void setId(String id) { public void setId(String id) {
this.id = id; this.id = id;
} }
public String getIndexName() { public String getIndexName() {
return indexName; return indexName;
} }
public void setIndexName(String indexName) { public void setIndexName(String indexName) {
this.indexName = indexName; this.indexName = indexName;
} }
public String getType() { public String getType() {
return type; return type;
} }
public void setType(String type) { public void setType(String type) {
this.type = type; this.type = type;
} }
public List<String> getSearchIndices() { public List<String> getSearchIndices() {
return searchIndices; return searchIndices;
} }
public void addSearchIndices(String... searchIndices) { public void addSearchIndices(String... searchIndices) {
addAll(this.searchIndices, searchIndices); addAll(this.searchIndices, searchIndices);
} }
public List<String> getSearchTypes() { public List<String> getSearchTypes() {
return searchTypes; return searchTypes;
} }
public void addSearchTypes(String... searchTypes) { public void addSearchTypes(String... searchTypes) {
addAll(this.searchTypes, searchTypes); addAll(this.searchTypes, searchTypes);
} }
public List<String> getFields() { public List<String> getFields() {
return fields; return fields;
} }
public void addFields(String... fields) { public void addFields(String... fields) {
addAll(this.fields, fields); addAll(this.fields, fields);
} }
public String getRouting() { public String getRouting() {
return routing; return routing;
} }
public void setRouting(String routing) { public void setRouting(String routing) {
this.routing = routing; this.routing = routing;
} }
public Float getPercentTermsToMatch() { public Float getPercentTermsToMatch() {
return percentTermsToMatch; return percentTermsToMatch;
} }
public void setPercentTermsToMatch(Float percentTermsToMatch) { public void setPercentTermsToMatch(Float percentTermsToMatch) {
this.percentTermsToMatch = percentTermsToMatch; this.percentTermsToMatch = percentTermsToMatch;
} }
public Integer getMinTermFreq() { public Integer getMinTermFreq() {
return minTermFreq; return minTermFreq;
} }
public void setMinTermFreq(Integer minTermFreq) { public void setMinTermFreq(Integer minTermFreq) {
this.minTermFreq = minTermFreq; this.minTermFreq = minTermFreq;
} }
public Integer getMaxQueryTerms() { public Integer getMaxQueryTerms() {
return maxQueryTerms; return maxQueryTerms;
} }
public void setMaxQueryTerms(Integer maxQueryTerms) { public void setMaxQueryTerms(Integer maxQueryTerms) {
this.maxQueryTerms = maxQueryTerms; this.maxQueryTerms = maxQueryTerms;
} }
public List<String> getStopWords() { public List<String> getStopWords() {
return stopWords; return stopWords;
} }
public void addStopWords(String... stopWords) { public void addStopWords(String... stopWords) {
addAll(this.stopWords, stopWords); addAll(this.stopWords, stopWords);
} }
public Integer getMinDocFreq() { public Integer getMinDocFreq() {
return minDocFreq; return minDocFreq;
} }
public void setMinDocFreq(Integer minDocFreq) { public void setMinDocFreq(Integer minDocFreq) {
this.minDocFreq = minDocFreq; this.minDocFreq = minDocFreq;
} }
public Integer getMaxDocFreq() { public Integer getMaxDocFreq() {
return maxDocFreq; return maxDocFreq;
} }
public void setMaxDocFreq(Integer maxDocFreq) { public void setMaxDocFreq(Integer maxDocFreq) {
this.maxDocFreq = maxDocFreq; this.maxDocFreq = maxDocFreq;
} }
public Integer getMinWordLen() { public Integer getMinWordLen() {
return minWordLen; return minWordLen;
} }
public void setMinWordLen(Integer minWordLen) { public void setMinWordLen(Integer minWordLen) {
this.minWordLen = minWordLen; this.minWordLen = minWordLen;
} }
public Integer getMaxWordLen() { public Integer getMaxWordLen() {
return maxWordLen; return maxWordLen;
} }
public void setMaxWordLen(Integer maxWordLen) { public void setMaxWordLen(Integer maxWordLen) {
this.maxWordLen = maxWordLen; this.maxWordLen = maxWordLen;
} }
public Float getBoostTerms() { public Float getBoostTerms() {
return boostTerms; return boostTerms;
} }
public void setBoostTerms(Float boostTerms) { public void setBoostTerms(Float boostTerms) {
this.boostTerms = boostTerms; this.boostTerms = boostTerms;
} }
public Pageable getPageable() { public Pageable getPageable() {
return pageable; return pageable;
} }
public void setPageable(Pageable pageable) { public void setPageable(Pageable pageable) {
this.pageable = pageable; this.pageable = pageable;
} }
} }
@@ -1,140 +1,156 @@
/* /*
* Copyright 2013-2017 the original author or authors. * Copyright 2013-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.data.elasticsearch.core.query; package org.springframework.data.elasticsearch.core.query;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.search.aggregations.AbstractAggregationBuilder; import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder; import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortBuilder; import org.elasticsearch.search.sort.SortBuilder;
import org.springframework.data.elasticsearch.core.facet.FacetRequest; import org.springframework.data.elasticsearch.core.facet.FacetRequest;
import java.util.Arrays; import java.util.Arrays;
/** /**
* NativeSearchQuery * NativeSearchQuery
* *
* @author Rizwan Idrees * @author Rizwan Idrees
* @author Mohsin Husen * @author Mohsin Husen
* @author Artur Konczak * @author Artur Konczak
*/ * @author Jean-Baptiste Nizet
public class NativeSearchQuery extends AbstractQuery implements SearchQuery { */
public class NativeSearchQuery extends AbstractQuery implements SearchQuery {
private QueryBuilder query;
private QueryBuilder filter; private QueryBuilder query;
private List<SortBuilder> sorts; private QueryBuilder filter;
private final List<ScriptField> scriptFields = new ArrayList<>(); private List<SortBuilder> sorts;
private List<FacetRequest> facets; private final List<ScriptField> scriptFields = new ArrayList<>();
private List<AbstractAggregationBuilder> aggregations; private List<FacetRequest> facets;
private HighlightBuilder.Field[] highlightFields; private List<AbstractAggregationBuilder> aggregations;
private List<IndexBoost> indicesBoost; private HighlightBuilder highlightBuilder;
private HighlightBuilder.Field[] highlightFields;
private List<IndexBoost> indicesBoost;
public NativeSearchQuery(QueryBuilder query) {
this.query = query;
} public NativeSearchQuery(QueryBuilder query) {
this.query = query;
public NativeSearchQuery(QueryBuilder query, QueryBuilder filter) { }
this.query = query;
this.filter = filter; public NativeSearchQuery(QueryBuilder query, QueryBuilder filter) {
} this.query = query;
this.filter = filter;
public NativeSearchQuery(QueryBuilder query, QueryBuilder filter, List<SortBuilder> sorts) { }
this.query = query;
this.filter = filter; public NativeSearchQuery(QueryBuilder query, QueryBuilder filter, List<SortBuilder> sorts) {
this.sorts = sorts; this.query = query;
} this.filter = filter;
this.sorts = sorts;
public NativeSearchQuery(QueryBuilder query, QueryBuilder filter, List<SortBuilder> sorts, HighlightBuilder.Field[] highlightFields) { }
this.query = query;
this.filter = filter; public NativeSearchQuery(QueryBuilder query, QueryBuilder filter, List<SortBuilder> sorts, HighlightBuilder.Field[] highlightFields) {
this.sorts = sorts; this.query = query;
this.highlightFields = highlightFields; this.filter = filter;
} this.sorts = sorts;
this.highlightFields = highlightFields;
public QueryBuilder getQuery() { }
return query;
} public NativeSearchQuery(QueryBuilder query, QueryBuilder filter, List<SortBuilder> sorts,
HighlightBuilder highlighBuilder, HighlightBuilder.Field[] highlightFields) {
public QueryBuilder getFilter() { this.query = query;
return filter; this.filter = filter;
} this.sorts = sorts;
this.highlightBuilder = highlighBuilder;
public List<SortBuilder> getElasticsearchSorts() { this.highlightFields = highlightFields;
return sorts; }
}
public QueryBuilder getQuery() {
@Override return query;
public HighlightBuilder.Field[] getHighlightFields() { }
return highlightFields;
} public QueryBuilder getFilter() {
return filter;
@Override }
public List<ScriptField> getScriptFields() { return scriptFields; }
public List<SortBuilder> getElasticsearchSorts() {
public void setScriptFields(List<ScriptField> scriptFields) { return sorts;
this.scriptFields.addAll(scriptFields); }
}
@Override
public void addScriptField(ScriptField... scriptField) { public HighlightBuilder getHighlightBuilder() {
scriptFields.addAll(Arrays.asList(scriptField)); return highlightBuilder;
} }
public void addFacet(FacetRequest facetRequest) { @Override
if (facets == null) { public HighlightBuilder.Field[] getHighlightFields() {
facets = new ArrayList<>(); return highlightFields;
} }
facets.add(facetRequest);
} @Override
public List<ScriptField> getScriptFields() { return scriptFields; }
public void setFacets(List<FacetRequest> facets) {
this.facets = facets; public void setScriptFields(List<ScriptField> scriptFields) {
} this.scriptFields.addAll(scriptFields);
}
@Override
public List<FacetRequest> getFacets() { public void addScriptField(ScriptField... scriptField) {
return facets; scriptFields.addAll(Arrays.asList(scriptField));
} }
@Override public void addFacet(FacetRequest facetRequest) {
public List<AbstractAggregationBuilder> getAggregations() { if (facets == null) {
return aggregations; facets = new ArrayList<>();
} }
facets.add(facetRequest);
}
public void addAggregation(AbstractAggregationBuilder aggregationBuilder) {
if (aggregations == null) { public void setFacets(List<FacetRequest> facets) {
aggregations = new ArrayList<>(); this.facets = facets;
} }
aggregations.add(aggregationBuilder);
} @Override
public List<FacetRequest> getFacets() {
public void setAggregations(List<AbstractAggregationBuilder> aggregations) { return facets;
this.aggregations = aggregations; }
}
@Override
@Override public List<AbstractAggregationBuilder> getAggregations() {
public List<IndexBoost> getIndicesBoost() { return aggregations;
return indicesBoost; }
}
public void setIndicesBoost(List<IndexBoost> indicesBoost) { public void addAggregation(AbstractAggregationBuilder aggregationBuilder) {
this.indicesBoost = indicesBoost; if (aggregations == null) {
} aggregations = new ArrayList<>();
}
} aggregations.add(aggregationBuilder);
}
public void setAggregations(List<AbstractAggregationBuilder> aggregations) {
this.aggregations = aggregations;
}
@Override
public List<IndexBoost> getIndicesBoost() {
return indicesBoost;
}
public void setIndicesBoost(List<IndexBoost> indicesBoost) {
this.indicesBoost = indicesBoost;
}
}
@@ -1,197 +1,231 @@
/* /*
* Copyright 2013-2017 the original author or authors. * Copyright 2013-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.data.elasticsearch.core.query; package org.springframework.data.elasticsearch.core.query;
import static org.springframework.util.CollectionUtils.isEmpty; import static org.springframework.util.CollectionUtils.isEmpty;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import org.elasticsearch.action.search.SearchType; import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.search.aggregations.AbstractAggregationBuilder; import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder; import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
import org.elasticsearch.search.sort.SortBuilder; import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.springframework.data.domain.Pageable; import org.elasticsearch.search.sort.SortBuilder;
import org.springframework.data.elasticsearch.core.facet.FacetRequest; import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.facet.FacetRequest;
/**
* NativeSearchQuery /**
* * NativeSearchQuery
* @author Rizwan Idrees *
* @author Mohsin Husen * @author Rizwan Idrees
* @author Artur Konczak * @author Mohsin Husen
* @author Mark Paluch * @author Artur Konczak
*/ * @author Mark Paluch
public class NativeSearchQueryBuilder { * @author Alen Turkovic
* @author Sascha Woo
private QueryBuilder queryBuilder; * @author Jean-Baptiste Nizet
private QueryBuilder filterBuilder; */
private List<ScriptField> scriptFields = new ArrayList<>(); public class NativeSearchQueryBuilder {
private List<SortBuilder> sortBuilders = new ArrayList<>();
private List<FacetRequest> facetRequests = new ArrayList<>(); private QueryBuilder queryBuilder;
private List<AbstractAggregationBuilder> aggregationBuilders = new ArrayList<>(); private QueryBuilder filterBuilder;
private HighlightBuilder.Field[] highlightFields; private List<ScriptField> scriptFields = new ArrayList<>();
private Pageable pageable = Pageable.unpaged(); private List<SortBuilder> sortBuilders = new ArrayList<>();
private String[] indices; private List<FacetRequest> facetRequests = new ArrayList<>();
private String[] types; private List<AbstractAggregationBuilder> aggregationBuilders = new ArrayList<>();
private String[] fields; private HighlightBuilder highlightBuilder;
private SourceFilter sourceFilter; private HighlightBuilder.Field[] highlightFields;
private List<IndexBoost> indicesBoost; private Pageable pageable = Pageable.unpaged();
private float minScore; private String[] indices;
private Collection<String> ids; private String[] types;
private String route; private String[] fields;
private SearchType searchType; private SourceFilter sourceFilter;
private List<IndexBoost> indicesBoost;
public NativeSearchQueryBuilder withQuery(QueryBuilder queryBuilder) { private float minScore;
this.queryBuilder = queryBuilder; private boolean trackScores;
return this; private Collection<String> ids;
} private String route;
private SearchType searchType;
public NativeSearchQueryBuilder withFilter(QueryBuilder filterBuilder) { private IndicesOptions indicesOptions;
this.filterBuilder = filterBuilder;
return this; public NativeSearchQueryBuilder withQuery(QueryBuilder queryBuilder) {
} this.queryBuilder = queryBuilder;
return this;
public NativeSearchQueryBuilder withSort(SortBuilder sortBuilder) { }
this.sortBuilders.add(sortBuilder);
return this; public NativeSearchQueryBuilder withFilter(QueryBuilder filterBuilder) {
} this.filterBuilder = filterBuilder;
return this;
public NativeSearchQueryBuilder withScriptField(ScriptField scriptField) { }
this.scriptFields.add(scriptField);
return this; public NativeSearchQueryBuilder withSort(SortBuilder sortBuilder) {
} this.sortBuilders.add(sortBuilder);
return this;
public NativeSearchQueryBuilder addAggregation(AbstractAggregationBuilder aggregationBuilder) { }
this.aggregationBuilders.add(aggregationBuilder);
return this; public NativeSearchQueryBuilder withScriptField(ScriptField scriptField) {
} this.scriptFields.add(scriptField);
return this;
public NativeSearchQueryBuilder withFacet(FacetRequest facetRequest) { }
facetRequests.add(facetRequest);
return this; public NativeSearchQueryBuilder addAggregation(AbstractAggregationBuilder aggregationBuilder) {
} this.aggregationBuilders.add(aggregationBuilder);
return this;
public NativeSearchQueryBuilder withHighlightFields(HighlightBuilder.Field... highlightFields) { }
this.highlightFields = highlightFields;
return this; public NativeSearchQueryBuilder withFacet(FacetRequest facetRequest) {
} facetRequests.add(facetRequest);
return this;
public NativeSearchQueryBuilder withIndicesBoost(List<IndexBoost> indicesBoost) { }
this.indicesBoost = indicesBoost;
return this; public NativeSearchQueryBuilder withHighlightBuilder(HighlightBuilder highlightBuilder) {
} this.highlightBuilder = highlightBuilder;
return this;
public NativeSearchQueryBuilder withPageable(Pageable pageable) { }
this.pageable = pageable;
return this; public NativeSearchQueryBuilder withHighlightFields(HighlightBuilder.Field... highlightFields) {
} this.highlightFields = highlightFields;
return this;
public NativeSearchQueryBuilder withIndices(String... indices) { }
this.indices = indices;
return this; public NativeSearchQueryBuilder withIndicesBoost(List<IndexBoost> indicesBoost) {
} this.indicesBoost = indicesBoost;
return this;
public NativeSearchQueryBuilder withTypes(String... types) { }
this.types = types;
return this; public NativeSearchQueryBuilder withPageable(Pageable pageable) {
} this.pageable = pageable;
return this;
public NativeSearchQueryBuilder withFields(String... fields) { }
this.fields = fields;
return this; public NativeSearchQueryBuilder withIndices(String... indices) {
} this.indices = indices;
return this;
public NativeSearchQueryBuilder withSourceFilter(SourceFilter sourceFilter) { }
this.sourceFilter = sourceFilter;
return this; public NativeSearchQueryBuilder withTypes(String... types) {
} this.types = types;
return this;
public NativeSearchQueryBuilder withMinScore(float minScore) { }
this.minScore = minScore;
return this; public NativeSearchQueryBuilder withFields(String... fields) {
} this.fields = fields;
return this;
public NativeSearchQueryBuilder withIds(Collection<String> ids) { }
this.ids = ids;
return this; public NativeSearchQueryBuilder withSourceFilter(SourceFilter sourceFilter) {
} this.sourceFilter = sourceFilter;
return this;
public NativeSearchQueryBuilder withRoute(String route) { }
this.route = route;
return this; public NativeSearchQueryBuilder withMinScore(float minScore) {
} this.minScore = minScore;
return this;
public NativeSearchQueryBuilder withSearchType(SearchType searchType) { }
this.searchType = searchType;
return this; /**
} * @param trackScores whether to track scores.
* @return
public NativeSearchQuery build() { * @since 3.1
NativeSearchQuery nativeSearchQuery = new NativeSearchQuery(queryBuilder, filterBuilder, sortBuilders, highlightFields); */
nativeSearchQuery.setPageable(pageable); public NativeSearchQueryBuilder withTrackScores(boolean trackScores) {
this.trackScores = trackScores;
if (indices != null) { return this;
nativeSearchQuery.addIndices(indices); }
}
public NativeSearchQueryBuilder withIds(Collection<String> ids) {
if (types != null) { this.ids = ids;
nativeSearchQuery.addTypes(types); return this;
} }
if (fields != null) { public NativeSearchQueryBuilder withRoute(String route) {
nativeSearchQuery.addFields(fields); this.route = route;
} return this;
}
if (sourceFilter != null) {
nativeSearchQuery.addSourceFilter(sourceFilter); public NativeSearchQueryBuilder withSearchType(SearchType searchType) {
} this.searchType = searchType;
return this;
if(indicesBoost != null) { }
nativeSearchQuery.setIndicesBoost(indicesBoost);
} public NativeSearchQueryBuilder withIndicesOptions(IndicesOptions indicesOptions) {
this.indicesOptions = indicesOptions;
if (!isEmpty(scriptFields)) { return this;
nativeSearchQuery.setScriptFields(scriptFields); }
}
public NativeSearchQuery build() {
if (!isEmpty(facetRequests)) { NativeSearchQuery nativeSearchQuery = new NativeSearchQuery(queryBuilder, filterBuilder, sortBuilders,
nativeSearchQuery.setFacets(facetRequests); highlightBuilder, highlightFields);
}
nativeSearchQuery.setPageable(pageable);
if (!isEmpty(aggregationBuilders)) { nativeSearchQuery.setTrackScores(trackScores);
nativeSearchQuery.setAggregations(aggregationBuilders);
} if (indices != null) {
nativeSearchQuery.addIndices(indices);
if (minScore > 0) { }
nativeSearchQuery.setMinScore(minScore);
} if (types != null) {
nativeSearchQuery.addTypes(types);
if (ids != null) { }
nativeSearchQuery.setIds(ids);
} if (fields != null) {
nativeSearchQuery.addFields(fields);
if (route != null) { }
nativeSearchQuery.setRoute(route);
} if (sourceFilter != null) {
nativeSearchQuery.addSourceFilter(sourceFilter);
if (searchType != null) { }
nativeSearchQuery.setSearchType(searchType);
} if (indicesBoost != null) {
nativeSearchQuery.setIndicesBoost(indicesBoost);
return nativeSearchQuery; }
}
} if (!isEmpty(scriptFields)) {
nativeSearchQuery.setScriptFields(scriptFields);
}
if (!isEmpty(facetRequests)) {
nativeSearchQuery.setFacets(facetRequests);
}
if (!isEmpty(aggregationBuilders)) {
nativeSearchQuery.setAggregations(aggregationBuilders);
}
if (minScore > 0) {
nativeSearchQuery.setMinScore(minScore);
}
if (ids != null) {
nativeSearchQuery.setIds(ids);
}
if (route != null) {
nativeSearchQuery.setRoute(route);
}
if (searchType != null) {
nativeSearchQuery.setSearchType(searchType);
}
if (indicesOptions != null) {
nativeSearchQuery.setIndicesOptions(indicesOptions);
}
return nativeSearchQuery;
}
}
@@ -1,150 +1,166 @@
/* /*
* Copyright 2013-2017 the original author or authors. * Copyright 2013-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.data.elasticsearch.core.query; package org.springframework.data.elasticsearch.core.query;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import org.elasticsearch.action.search.SearchType; import org.elasticsearch.action.search.SearchType;
import org.springframework.data.domain.PageRequest; import org.elasticsearch.action.support.IndicesOptions;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort; import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
/**
* Query /**
* * Query
* @author Rizwan Idrees *
* @author Mohsin Husen * @author Rizwan Idrees
* @author Mark Paluch * @author Mohsin Husen
*/ * @author Mark Paluch
public interface Query { * @author Alen Turkovic
* @author Sascha Woo
int DEFAULT_PAGE_SIZE = 10; */
Pageable DEFAULT_PAGE = PageRequest.of(0, DEFAULT_PAGE_SIZE); public interface Query {
/** int DEFAULT_PAGE_SIZE = 10;
* restrict result to entries on given page. Corresponds to the 'start' and 'rows' parameter in elasticsearch Pageable DEFAULT_PAGE = PageRequest.of(0, DEFAULT_PAGE_SIZE);
*
* @param pageable /**
* @return * restrict result to entries on given page. Corresponds to the 'start' and 'rows' parameter in elasticsearch
*/ *
<T extends Query> T setPageable(Pageable pageable); * @param pageable
* @return
/** */
* Get page settings if defined <T extends Query> T setPageable(Pageable pageable);
*
* @return /**
*/ * Get page settings if defined
Pageable getPageable(); *
* @return
/** */
* Add {@link org.springframework.data.domain.Sort} to query Pageable getPageable();
*
* @param sort /**
* @return * Add {@link org.springframework.data.domain.Sort} to query
*/ *
<T extends Query> T addSort(Sort sort); * @param sort
* @return
/** */
* @return null if not set <T extends Query> T addSort(Sort sort);
*/
Sort getSort(); /**
* @return null if not set
/** */
* Get Indices to be searched Sort getSort();
*
* @return /**
*/ * Get Indices to be searched
List<String> getIndices(); *
* @return
/** */
* Add Indices to be added as part of search request List<String> getIndices();
*
* @param indices /**
*/ * Add Indices to be added as part of search request
void addIndices(String... indices); *
* @param indices
/** */
* Add types to be searched void addIndices(String... indices);
*
* @param types /**
*/ * Add types to be searched
void addTypes(String... types); *
* @param types
/** */
* Get types to be searched void addTypes(String... types);
*
* @return /**
*/ * Get types to be searched
List<String> getTypes(); *
* @return
/** */
* Add fields to be added as part of search request List<String> getTypes();
*
* @param fields /**
*/ * Add fields to be added as part of search request
void addFields(String... fields); *
* @param fields
/** */
* Get fields to be returned as part of search request void addFields(String... fields);
*
* @return /**
*/ * Get fields to be returned as part of search request
List<String> getFields(); *
* @return
/** */
* Add source filter to be added as part of search request List<String> getFields();
*
* @param sourceFilter /**
*/ * Add source filter to be added as part of search request
void addSourceFilter(SourceFilter sourceFilter); *
* @param sourceFilter
/** */
* Get SourceFilter to be returned to get include and exclude source void addSourceFilter(SourceFilter sourceFilter);
* fields as part of search request.
* /**
* @return SourceFilter * Get SourceFilter to be returned to get include and exclude source fields as part of search request.
*/ *
SourceFilter getSourceFilter(); * @return SourceFilter
*/
/** SourceFilter getSourceFilter();
* Get minimum score
* /**
* @return * Get minimum score
*/ *
float getMinScore(); * @return
*/
/** float getMinScore();
* Get Ids
* /**
* @return * Get if scores will be computed and tracked, regardless of whether sorting on a field. Defaults to <tt>false</tt>.
*/ *
Collection<String> getIds(); * @return
* @since 3.1
/** */
* Get route boolean getTrackScores();
*
* @return /**
*/ * Get Ids
String getRoute(); *
* @return
*/
/** Collection<String> getIds();
* Type of search
* /**
* @return * Get route
*/ *
SearchType getSearchType(); * @return
} */
String getRoute();
/**
* Type of search
*
* @return
*/
SearchType getSearchType();
/**
* Get indices options
*
* @return null if not set
*/
IndicesOptions getIndicesOptions();
}
@@ -1,52 +1,55 @@
/* /*
* Copyright 2013-2016 the original author or authors. * Copyright 2013-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.data.elasticsearch.core.query; package org.springframework.data.elasticsearch.core.query;
import java.util.List; import java.util.List;
import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.search.aggregations.AbstractAggregationBuilder; import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder; import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortBuilder; import org.elasticsearch.search.sort.SortBuilder;
import org.springframework.data.elasticsearch.core.facet.FacetRequest; import org.springframework.data.elasticsearch.core.facet.FacetRequest;
/** /**
* NativeSearchQuery * NativeSearchQuery
* *
* @author Rizwan Idrees * @author Rizwan Idrees
* @author Mohsin Husen * @author Mohsin Husen
* @author Artur Konczak * @author Artur Konczak
*/ * @author Jean-Baptiste Nizet
public interface SearchQuery extends Query { */
public interface SearchQuery extends Query {
QueryBuilder getQuery();
QueryBuilder getQuery();
QueryBuilder getFilter();
QueryBuilder getFilter();
List<SortBuilder> getElasticsearchSorts();
List<SortBuilder> getElasticsearchSorts();
@Deprecated
List<FacetRequest> getFacets(); @Deprecated
List<FacetRequest> getFacets();
List<AbstractAggregationBuilder> getAggregations();
List<AbstractAggregationBuilder> getAggregations();
HighlightBuilder.Field[] getHighlightFields();
HighlightBuilder getHighlightBuilder();
List<IndexBoost> getIndicesBoost();
HighlightBuilder.Field[] getHighlightFields();
List<ScriptField> getScriptFields();
List<IndexBoost> getIndicesBoost();
}
List<ScriptField> getScriptFields();
}
@@ -1,41 +1,41 @@
/* /*
* Copyright 2013 the original author or authors. * Copyright 2013-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.data.elasticsearch.core.query; package org.springframework.data.elasticsearch.core.query;
/** /**
* The most trivial implementation of a Field * The most trivial implementation of a Field
* *
* @author Rizwan Idrees * @author Rizwan Idrees
* @author Mohsin Husen * @author Mohsin Husen
*/ */
public class SimpleField implements Field { public class SimpleField implements Field {
private final String name; private final String name;
public SimpleField(String name) { public SimpleField(String name) {
this.name = name; this.name = name;
} }
@Override @Override
public String getName() { public String getName() {
return this.name; return this.name;
} }
@Override @Override
public String toString() { public String toString() {
return this.name; return this.name;
} }
} }
@@ -1,5 +1,5 @@
/* /*
* Copyright 2016 the original author or authors. * Copyright 2016-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -1,49 +1,49 @@
/* /*
* Copyright 2013 the original author or authors. * Copyright 2013-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.data.elasticsearch.core.query; package org.springframework.data.elasticsearch.core.query;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort;
/** /**
* StringQuery * StringQuery
* *
* @author Rizwan Idrees * @author Rizwan Idrees
* @author Mohsin Husen * @author Mohsin Husen
*/ */
public class StringQuery extends AbstractQuery { public class StringQuery extends AbstractQuery {
private String source; private String source;
public StringQuery(String source) { public StringQuery(String source) {
this.source = source; this.source = source;
} }
public StringQuery(String source, Pageable pageable) { public StringQuery(String source, Pageable pageable) {
this.source = source; this.source = source;
this.pageable = pageable; this.pageable = pageable;
} }
public StringQuery(String source, Pageable pageable, Sort sort) { public StringQuery(String source, Pageable pageable, Sort sort) {
this.pageable = pageable; this.pageable = pageable;
this.sort = sort; this.sort = sort;
this.source = source; this.source = source;
} }
public String getSource() { public String getSource() {
return source; return source;
} }
} }
@@ -1,5 +1,5 @@
/* /*
* Copyright 2013 the original author or authors. * Copyright 2013-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
/* /*
* Copyright 2013 the original author or authors. * Copyright 2013-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -1,33 +1,33 @@
/* /*
* Copyright 2013-2014 the original author or authors. * Copyright 2013-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.data.elasticsearch.repository; package org.springframework.data.elasticsearch.repository;
import java.io.Serializable; import java.io.Serializable;
import org.springframework.data.repository.NoRepositoryBean; import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.data.repository.PagingAndSortingRepository; import org.springframework.data.repository.PagingAndSortingRepository;
/** /**
* @param <T> * @param <T>
* @param <ID> * @param <ID>
* @author Rizwan Idrees * @author Rizwan Idrees
* @author Mohsin Husen * @author Mohsin Husen
* @author Oliver Gierke * @author Oliver Gierke
*/ */
@NoRepositoryBean @NoRepositoryBean
public interface ElasticsearchCrudRepository<T, ID extends Serializable> extends PagingAndSortingRepository<T, ID> { public interface ElasticsearchCrudRepository<T, ID extends Serializable> extends PagingAndSortingRepository<T, ID> {
} }

Some files were not shown because too many files have changed in this diff Show More