我正在使用Spring Data ElasticSearch来执行CRUD操作.默认情况下,当使用@Document注释的POJO写入ElasticSearch索引时,索引字段名称与POJO的Java属性名称相同.如何配置索引字段名称不同?例如,使用此文档POJO:
@Document(indexName = "areas", type = "area")
public class Area {
@Id
private String id;
private String countyName;
private String postOfficeName;
private String stateName;
Run Code Online (Sandbox Code Playgroud)
我如何配置它,以便ElasticSearch中的索引字段被序列化为county_name而不是countyName?
我参考了Spring Data Elasticsearch
org.springframework.data.elasticsearch.repository.ElasticsearchRepository org.springframework.data.elasticsearch.core.ElasticsearchTemplate它们似乎是两个不同的API,它们实现了相同的目标,但我不确定这两种类型之间的区别是什么,更重要的是何时使用哪种类型.
有人可以提供建议和指导吗?
是否可以动态地(在运行时)指定indexName每个@Document,例如,通过配置文件?或者是否可以使@DocumentSpring环境(dev,prod)依赖?
谢谢!
spring-mvc spring-data spring-boot spring-data-elasticsearch
嗨朋友我正在开发弹簧启动项目,elastic search我在本地机器上设置弹性搜索,我已经Head在弹性搜索中安装了插件.我的弹性搜索设置正确显示绿色标志.我的项目中的application-dev.yml文件如下:
server:
port: 8080
liquibase:
context: dev
spring:
profiles:
active: dev
datasource:
dataSourceClassName: org.h2.jdbcx.JdbcDataSource
url: jdbc:h2:mem:jhipster;DB_CLOSE_DELAY=-1
databaseName:
serverName:
username:
password:
jpa:
database-platform: com.aquevix.demo.domain.util.FixedH2Dialect
database: H2
openInView: false
show_sql: true
generate-ddl: false
hibernate:
ddl-auto: none
naming-strategy: org.hibernate.cfg.EJB3NamingStrategy
properties:
hibernate.cache.use_second_level_cache: true
hibernate.cache.use_query_cache: false
hibernate.generate_statistics: true
hibernate.cache.region.factory_class: org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory
data:
elasticsearch:
cluster-name: elasticsearch
cluster-nodes: localhost:9200
messages:
cache-seconds: 1
thymeleaf:
mode: XHTML
cache: false
activemq:
broker-url: tcp://localhost:61616
metrics:
jmx.enabled: true
spark:
enabled: false
host: localhost
port: 9999
graphite:
enabled: false …Run Code Online (Sandbox Code Playgroud) 我正在使用 elasticsearch spring 数据编写一个 api,我想添加排序。我无法在谷歌中找到解决方案。所以我写在这里问你们我怎么能做到这一点。如果您需要更多代码,请告诉我您需要什么,我会尝试添加更多代码。
我的代码看起来像:
拍卖查询控制器.java
@RequestMapping(value = "/auctions/search", produces = MediaType.APPLICATION_JSON_VALUE)
private List<Auction> search(
@RequestParam(value = "categoryId", required = false) Long categoryId,
@RequestParam(value = "treeCategoryId", required = false) Long treeCategoryId,
@RequestParam(value = "currency", required = false) String currency,
@RequestParam(value = "priceFrom", required = false) Long priceFrom,
@RequestParam(value = "priceTo", required = false) Long priceTo,
@RequestParam(value = "startDateFrom", required = false) Long startDateFrom,
@RequestParam(value = "startDateTo", required = false) Long startDateTo,
@RequestParam(value = "endDateFrom", required = false) Long endDateFrom, …Run Code Online (Sandbox Code Playgroud) java elasticsearch spring-data spring-boot spring-data-elasticsearch
我为两个父/子相关文档创建了两个Spring-Data elasticsearch存储库.您可以在此处查看这两个文档类.
借助于像这样的存储库元素,存储库将在XML中实例化
<elasticsearch:repositories base-package="com.acme.repositories" />
Run Code Online (Sandbox Code Playgroud)
您可以在此 github仓库中找到该问题的完整示例.我的源代码基于spring-data-elasticsearch repo中的测试.源代码只包含两个域类(父类,子类),相关的存储库,xml配置文件和单元测试类.
克隆并运行mvn test,抛出java.lang.IllegalArgumentException: can't add a _parent field that points to an already existing type, that isn't already a parent
似乎这个父/子关系产生了弹性搜索异常,这在Elasticsearch方面很明显,但我不知道在执行Spring Data测试时如何防止这种情况发生.
我不理解高级REST CLient(取代传输客户端)和Spring Data Elasticsearch之间的关系.
有一些教程(https://www.baeldung.com/spring-data-elasticsearch-tutorial)显示了如何使用自动实现的方法(如getSomethingById())进行JPA样式的查询.他们利用这个界面:
public interface ElasticsearchRepository<T, ID extends Serializable> extends ElasticsearchCrudRepository<T, ID> {
Run Code Online (Sandbox Code Playgroud)
但ElasticsearchRepository似乎需要一个@Configuration带有ElasticsearchTemplate-Bean的-Class并且需要TransportClient.据我所知,没有办法用高/低级别的Java Rest客户端替换它,不是吗?
所以如果我想使用最新的官方客户端,对我来说没有JPA的好处?
java spring elasticsearch spring-data spring-data-elasticsearch
RestHighLevelClient用于在 spring-boot 应用程序中连接到 ES 6.4(托管在 AWS 上)。当应用闲置一段时间,请求到达时,RestHighLevelClient抛出SocketTimeoutException:
[Request processing failed; nested exception is org.springframework.data.elasticsearch.ElasticsearchException: Error while bulk for request: org.elasticsearch.action.bulk.BulkRequest@21511b6c] w
java.net.SocketTimeoutException: 5,000 milliseconds timeout on connection http-outgoing-38 [ACTIVE]
at org.apache.http.nio.protocol.HttpAsyncRequestExecutor.timeout(HttpAsyncRequestExecutor.java:387) ~[httpcore-nio-4.4.11.jar!/:4.4.11]
at org.apache.http.impl.nio.client.InternalIODispatch.onTimeout(InternalIODispatch.java:92) ~[httpasyncclient-4.1.4.jar!/:4.1.4]
at org.apache.http.impl.nio.client.InternalIODispatch.onTimeout(InternalIODispatch.java:39) ~[httpasyncclient-4.1.4.jar!/:4.1.4]
at org.apache.http.impl.nio.reactor.AbstractIODispatch.timeout(AbstractIODispatch.java:175) ~[httpcore-nio-4.4.11.jar!/:4.4.11]
at org.apache.http.impl.nio.reactor.BaseIOReactor.sessionTimedOut(BaseIOReactor.java:263) ~[httpcore-nio-4.4.11.jar!/:4.4.11]
at org.apache.http.impl.nio.reactor.AbstractIOReactor.timeoutCheck(AbstractIOReactor.java:492) ~[httpcore-nio-4.4.11.jar!/:4.4.11]
at org.apache.http.impl.nio.reactor.BaseIOReactor.validate(BaseIOReactor.java:213) ~[httpcore-nio-4.4.11.jar!/:4.4.11]
at org.apache.http.impl.nio.reactor.AbstractIOReactor.execute(AbstractIOReactor.java:280) ~[httpcore-nio-4.4.11.jar!/:4.4.11]
at org.apache.http.impl.nio.reactor.BaseIOReactor.execute(BaseIOReactor.java:104) ~[httpcore-nio-4.4.11.jar!/:4.4.11]
at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor$Worker.run(AbstractMultiworkerIOReactor.java:591) ~[httpcore-nio-4.4.11.jar!/:4.4.11]
at java.base/java.lang.Thread.run(Thread.java:834) ~[na:na]
Run Code Online (Sandbox Code Playgroud)
RestHighLevelClient 是使用以下方法创建的:
@Bean
RestHighLevelClient client() {
ClientConfiguration clientConfiguration = ClientConfiguration.builder()
.connectedTo(elasticsearchHostAndPort)
.build();
return …Run Code Online (Sandbox Code Playgroud) 我有一个具有以下 Spring 依赖项的应用程序:
starterBase : 'org.springframework.boot:spring-boot-starter:2.2.1.RELEASE',
starterActuator: 'org.springframework.boot:spring-boot-starter-actuator:2.2.1.RELEASE',
starterJpa : 'org.springframework.boot:spring-boot-starter-data-jpa:2.2.1.RELEASE',
starterTest : 'org.springframework.boot:spring-boot-starter-test:2.2.1.RELEASE',
starterWeb : 'org.springframework.boot:spring-boot-starter-web:2.2.1.RELEASE',
elasticsearch : 'org.springframework.boot:spring-boot-starter-data-elasticsearch:2.2.1.RELEASE'
Run Code Online (Sandbox Code Playgroud)
在我添加elasticsearch依赖的那一刻,当我启动应用程序时出现了以下警告:
WARN [main] o.s.data.convert.CustomConversions.register - Registering converter from class org.springframework.data.geo.Point to interface java.util.Map as writing converter although it doesn't convert to a store-supported type! You might wanna check you annotation setup at the converter implementation.
WARN [main] o.s.data.convert.CustomConversions.register - Registering converter from interface java.util.Map to class org.springframework.data.geo.Point as reading converter although it doesn't convert from a store-supported type! You might …Run Code Online (Sandbox Code Playgroud) 我正在为我的应用程序使用 spring-data-elasticsearch 。我发现 spring 自动在我的所有文档中创建动态字段“_class”。我将索引配置为严格的动态映射。我希望某些属性能够从我的文档/映射中动态排除该字段。
有什么办法可以做到这一点吗?