most recent 30 from stackoverflow.com2026-01-01T11:43:02Zhttps://stackoverflow.com/feeds/tag?tagnames=spring-data-elasticsearchhttps://creativecommons.org/licenses/by-sa/4.0/rdfhttps://stackoverflow.com/q/625837872Swordfishhttps://stackoverflow.com/users/18391042020-06-25T20:26:15Z2025-12-31T15:00:20Z
<p>I am using Elasticsearch 6.8.10 with Spring Boot 2.2.7 and Spring Data Elasticsearch.</p>
<p>I have streams of stats and trends data being stored in Kafka topics. These topics are read using Spring Kafka and stored into MongoDB and Elasticsearch for analysis and reporting. The problem I am having is that when the queues are being processed and the data is written to Elasticsearch, Elasticsearch CPU consumption is continuously around 250%. This leads to sporadic timeout errors across the application. I understand that indexing is an intensive operation but I am trying to understand what I can do to reduce the CPU usage.</p>
<p>Data:</p>
<ul>
<li>Approx stats queue items (1.2M)</li>
<li>Stats document size (220 bytes)</li>
</ul>
<p>The VM config details are:</p>
<ul>
<li>4 CPU, 16GB Memory, 20GB disk (SSD)</li>
<li>Running on a VM in Google Cloud Platform.</li>
<li>VM only used for Elasticsearch</li>
</ul>
<p>The Docker Elasticsearch config details:</p>
<ul>
<li>I am using single node (for the moment)</li>
</ul>
<pre><code>version: '2.4'
services:
elasticsearch:
container_name: elasticsearch
image: 'docker.elastic.co/elasticsearch/elasticsearch:6.8.10'
ports:
- '9200:9200'
- '9300:9300'
mem_limit: 16GB
environment:
- discovery.type=single-node
- cluster.name=docker-cluster
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms8g -Xmx8g"
ulimits:
memlock:
soft: -1
hard: -1
nofile:
soft: 65536
hard: 65536
volumes:
- 'esdata1:/usr/share/elasticsearch/data'
restart: always
volumes:
esdata1:
driver: local
</code></pre>
<p>Example of Spring Stat document:</p>
<ul>
<li>Shards = 1, replicas = 0</li>
</ul>
<pre><code>@Document(indexName = "stats_test", type = "stat", shards = 1, replicas = 0)
public class EsStat {
@Id
@Field(type = FieldType.Keyword)
private String id;
@Field(type = FieldType.Keyword)
private String entityOrRelationshipId;
@Field(type = FieldType.Keyword)
private String articleId;
@Field(type = FieldType.Keyword)
private String status;
@Field(type = FieldType.Keyword)
private String type;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZ")
@Field(type = FieldType.Date, format = DateFormat.custom, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZ")
private ZonedDateTime date;
@JsonProperty("type")
@Field(type = FieldType.Keyword)
private String dataSource;
// getter and setters
}
</code></pre>
<p>Stats Spring repository:</p>
<ul>
<li>Indexing is done via Spring Data Elasticsearch repository:</li>
</ul>
<pre><code>public interface StatElasticsearchRepository extends ElasticsearchRepository<EsStat, String> {
}
</code></pre>
<p>Stats mapping:</p>
<pre><code>{
"stats": {
"mappings": {
"stat": {
"properties": {
"_class": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"articleId": {
"type": "keyword"
},
"dataSource": {
"type": "keyword"
},
"date": {
"type": "date",
"format": "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
},
"entityOrRelationshipId": {
"type": "keyword"
},
"id": {
"type": "keyword"
},
"status": {
"type": "keyword"
},
"type": {
"type": "keyword"
}
}
}
}
}
}
</code></pre>
<p>What can I do to identify why the CPU usage is so high and what can I do to reduce it?</p>
<p>Any advice or recommendations would be much appreciated. I'm happy to add more configuration/output if required.</p>
https://stackoverflow.com/q/798404440kernelhttps://stackoverflow.com/users/146863022025-12-07T19:11:57Z2025-12-07T19:11:57Z
<p>I am trying to implement <strong>multi-tenancy</strong> in a Spring Boot application using <strong>Spring Data Elasticsearch</strong>.</p>
<p>My goal is:</p>
<p>Automatically inject a <code>tenantId</code> filter into <strong>all</strong> Elasticsearch queries<br />
This must apply to:</p>
<ul>
<li><p>Repository-derived queries (<code>findBy…</code>)</p>
</li>
<li><p>Custom <code>@Query</code> JSON queries</p>
</li>
<li><p>CRUD operations like <code>findById</code>, <code>save</code>, etc.</p>
</li>
<li><p>Low-level operations triggered internally (because <code>findById()</code> does <strong>not</strong> call <code>search()</code>; it uses different ES ops under the hood)</p>
</li>
<li><p>Derived queries → findByPersonTypeName</p>
</li>
<li><p>Custom @Query → findByIdentityNo</p>
</li>
<li><p>findById / getById (which do not use a search query)</p>
</li>
<li><p>Low-level ES operations invoked by Spring Data Elasticsearch internally</p>
</li>
</ul>
<p>Basically, I want something like Hibernate Filters but for <code>ElasticsearchRepository</code>.</p>
<p>I want a <strong>global multi-tenant filter</strong> that automatically injects:</p>
<pre><code>{ "term": { "tenantId": <currentTenant> } }
</code></pre>
<h2><strong>Dependencies</strong></h2>
<p>Spring Boot brings:</p>
<pre><code><dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
<version>3.3.10</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
<version>5.3.10</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>8.13.4</version>
</dependency>
</code></pre>
<h2><strong>Document model</strong></h2>
<pre><code>@Document(indexName = PersonDocument.PERSON_INDEX)
public class PersonDocument extends TenantDocument {
public static final String PERSON_INDEX = "person_index";
@Id
@Field(type = FieldType.Long)
private Long personNo;
}
</code></pre>
<pre><code>public class TenantDocument {
@Field(type = FieldType.Long)
protected Long tenantId;
public Long getTenantId() { return tenantId; }
public void setTenantId(Long tenantId) { this.tenantId = tenantId; }
}
</code></pre>
<p>Repository</p>
<pre><code>@Repository
public interface PersonDocumentRepository
extends ElasticsearchRepository<PersonDocument, Long> {
List<PersonDocument> findPersonDocumentByPersonTypeName(String name);
@Query("{\"bool\":{\"must\":[{\"term\":{\"identityNo\":\"?0\"}}]}}")
PersonDocument findByIdentityNo(String identityNo);
}
</code></pre>
https://stackoverflow.com/q/791974861Karinhttps://stackoverflow.com/users/189392112024-11-17T15:17:00Z2025-11-01T22:04:48Z
<p>I am upgrading the elasticsearch version from 8.14.3 to 8.15.3 and there are some issues with this query-</p>
<p><em><strong>return new RangeQuery.Builder() .field(fieldName) .gte(JsonData.of(dateToday)) .build() ._toQuery;</strong></em></p>
<p>Here, .field seems to be deprecated. Is there any other way to rewrite the query in the new version?</p>
https://stackoverflow.com/q/797830080garyhttps://stackoverflow.com/users/62653702025-10-05T13:52:06Z2025-10-05T17:32:19Z
<p>Using SpringBoot data elasticsearch</p>
<p>Need to print the Elasticsearch JSON representation of the query</p>
<p>Creating a <code>org.springframework.data.elasticsearch.core.query.CriteriaQuery</code> that is passed to
<code>org.springframework.data.elasticsearch.client.elc.ElasticsearchTemplate::search()</code></p>
<p>Can print the <code>criteriaQuery.getCriteria().getCriteriaChain()</code> and get some good information but need to get the JSON query that is created (I believe) by the REST client</p>
<p>Looking at various page e.g., it seems to be a tricky affair
<a href="https://github.com/spring-projects/spring-data-elasticsearch/issues/1808" rel="nofollow noreferrer">https://github.com/spring-projects/spring-data-elasticsearch/issues/1808</a></p>
<p>Tried log level like:
<code>logging.level.org.springframework.data.elasticsearch.core=TRACE</code></p>
<p>But also no joy</p>
<p>What is the simplest/easiest way to get the JSON ?</p>
<p>Any help is much appreciated</p>
https://stackoverflow.com/q/797616111Thomas Muellerhttps://stackoverflow.com/users/11449502025-09-11T07:38:32Z2025-09-12T22:54:44Z
<p>I have a SpringBoot application in which I query an Elasticsearch cluster using an Elasticsearch repository. This is working fine at first.
I have the following method in my repository, among others:</p>
<pre><code>List<FooDocument> findByEvent_Guid(String guid);
</code></pre>
<p>This method also returns the desired results. However, there are problems: as soon as the result set is larger than 10,000, an "UncategorizedElasticsearchException (all shards failed)" is thrown.</p>
<p>This seems to be a known issue/feature because it can no longer paginate properly with such large data sets. I found a way to circumvent this by using a PageRequest with a pageSize max of 10,000, so that the error no longer occurs, but after 10,000 hits, no more results appear on subsequent pages.</p>
<p>How can I access the additional results?</p>
https://stackoverflow.com/q/797592870Usernamedrewhttps://stackoverflow.com/users/251156052025-09-08T19:49:19Z2025-09-08T19:49:19Z
<p>I faced a problem connected with Elasticsearch while implementing full text search. When I try to find documents in index I face this problem</p>
<pre><code>2025-08-27T09:15:59.544+03:00 ERROR 19622 --- [Muse] [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: org.springframework.data.elasticsearch.UncategorizedElasticsearchException: [es/search] failed: [search_phase_execution_exception] all shards failed] with root cause
co.elastic.clients.elasticsearch._types.ElasticsearchException: [es/search] failed: [search_phase_execution_exception] all shards failed
</code></pre>
<p><strong>This is the dependency tree</strong>:</p>
<pre><code>andrejburlacko@MacBook-Air-Andrej team28 % mvn dependency:tree | grep elasticsearch
[INFO] +- org.springframework.boot:spring-boot-starter-data-elasticsearch:jar:3.5.4:compile
[INFO] | \- org.springframework.data:spring-data-elasticsearch:jar:5.5.2:compile
[INFO] | +- co.elastic.clients:elasticsearch-java:jar:8.18.1:compile
[INFO] | +- org.elasticsearch.client:elasticsearch-rest-client:jar:8.18.1:compile
</code></pre>
<p><strong>This is how I run elastic in docker</strong>:</p>
<pre><code>docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 \
-e "discovery.type=single-node" \
-e "xpack.security.enabled=false" \
-e "ES_JAVA_OPTS=-Xms512m -Xmx512m" \
elasticsearch:8.18.1
</code></pre>
<p><strong>This how my indexes look like</strong>:</p>
<p>health status index uuid pri rep docs.count docs.deleted store.size pri.store.size dataset.size</p>
<p>yellow open questions _nkW3NzETNanGDBXuhiQyQ 1 1 50 0 23.7kb 23.7kb 23.7kb</p>
<p>yellow open tags BdaRQbRWTzSGQIWxod96VQ 1 1 0 0 249b 249b 249b</p>
<p><strong>This is how mapping structure looks like for question</strong>:</p>
<pre><code>{
"questions" : {
"mappings" : {
"properties" : {
"_class" : {
"type" : "keyword",
"index" : false,
"doc_values" : false
},
"id" : {
"type" : "keyword"
},
"questionText" : {
"type" : "text"
},
"score" : {
"type" : "integer"
},
"title" : {
"type" : "text"
},
"viewsCount" : {
"type" : "integer"
}
}
}
}
}
</code></pre>
<p>When I try search by hands through elastic api, I get my questions successfully</p>
<p>GET http://localhost:9200/questions/_search?q=title:производительность&pretty</p>
<pre><code>{
"took": 59,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 2.920971,
"hits": [
{
"_index": "questions",
"_id": "b23e4dcc-cc13-4ba3-a354-9aee75238375",
"_score": 2.920971,
"_source": {
"_class": "ru.t1.usernamedrew.muse.entity.elastic.SearchQuestion",
"id": "b23e4dcc-cc13-4ba3-a354-9aee75238375",
"title": "SQL индексы производительность",
"questionText": "Как индексы влияют на производительность SQL запросов?",
"viewsCount": 126,
"score": 12
}
},
{
"_index": "questions",
"_id": "d54852a3-b6bd-4e99-8376-faa19cfe9a45",
"_score": 2.5565581,
"_source": {
"_class": "ru.t1.usernamedrew.muse.entity.elastic.SearchQuestion",
"id": "d54852a3-b6bd-4e99-8376-faa19cfe9a45",
"title": "Java Stream API производительность",
"questionText": "Влияет ли использование Stream API на производительность Java приложений?",
"viewsCount": 95,
"score": 9
}
}
]
}
}
</code></pre>
<p><strong>This is how my search service looks like</strong>:</p>
<pre><code>@Service
@RequiredArgsConstructor
public class SearchServiceImpl implements SearchService {
private final SearchQuestionRepository searchQuestionRepository;
private final SearchTagRepository searchTagRepository;
private final QuestionService questionService;
private final TagService tagService;
private final ElasticsearchOperations elasticsearchOperations;
// Поиск вопросов по заголовку
@Override
public Slice<QuestionPreviewDTO> searchQuestions(String query, Pageable pageable) {
List<UUID> questionIds = searchQuestionRepository.findByTitleContainingIgnoreCase(query, pageable) // <- it falls then findByTitleIgnoreCase try’s to make a query
.getContent().stream()
.map(SearchQuestion::getId)
.toList();
return questionService.getQuestionsByIds(questionIds, pageable);
}
// Поиск тегов по заголовку
@Override
public Slice<TagDTO> searchTags(String query, Pageable pageable) {
List<UUID> tagsIds = searchTagRepository.findByTitleContaining(query, pageable)
.getContent().stream()
.map(SearchTag::getId)
.toList();
return tagService.findAllByIds(tagsIds, pageable);
}
// Индексация вопроса
@Override
public void indexQuestion(QuestionPreviewDTO question) {
searchQuestionRepository.save(convertToSearchQuestion(question));
}
@Override
public void indexTags(Tag tags) {
// TODO
}
@Override
@Transactional
public void reindexAllQuestions() {
searchQuestionRepository.deleteAll();
int batchSize = 100;
int offset = 0;
while (true) {
Pageable pageable = PageRequest.of(offset, batchSize);
Slice<QuestionPreviewDTO> questionSlice = questionService.getQuestions(pageable);
if (questionSlice.getContent().isEmpty()) {
break;
}
List<SearchQuestion> searchQuestions = questionSlice.getContent().stream()
.map(this::convertToSearchQuestion)
.toList();
searchQuestionRepository.saveAll(searchQuestions);
offset++;
}
}
private SearchQuestion convertToSearchQuestion(QuestionPreviewDTO question) {
return SearchQuestion.builder()
.id(question.getId())
.title(question.getTitle())
.questionText(question.getQuestionText())
.viewsCount(question.getViewsCount())
.score(question.getScore())
.build();
}
}
</code></pre>
<p><strong>My documents</strong>:</p>
<pre><code>@Document(indexName = "questions")
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class SearchQuestion {
@Id
@Field(type = FieldType.Keyword)
private UUID id;
@Field(type = FieldType.Text)
private String title;
@Field(type = FieldType.Text)
private String questionText;
@Field(type = FieldType.Integer)
private Integer viewsCount;
@Field(type = FieldType.Integer)
private Integer score;
}
@Document(indexName = "tags")
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class SearchTag {
@Id
@Field(type = FieldType.Keyword)
private UUID id;
@Field(type = FieldType.Text)
private String title;
@Field(type = FieldType.Text)
private String description;
}
</code></pre>
<p><strong>And elastic search repositories</strong>:</p>
<pre><code>public interface SearchQuestionRepository extends ElasticsearchRepository<SearchQuestion, UUID> {
Slice<SearchQuestion> findByTitleContainingIgnoreCase(String title, Pageable pageable);
}
public interface SearchTagRepository extends ElasticsearchRepository<SearchTag, UUID> {
Slice<SearchTag> findByTitleContaining(String title, Pageable pageable);
}
</code></pre>
<p><strong>Controller</strong>:</p>
<pre><code>@RestController
@RequestMapping("/api/search")
@RequiredArgsConstructor
@Tag(name = "Контроллер поиска", description = "Поиск вопросов по названию и тегам")
public class SearchController {
private final SearchService searchService;
@GetMapping("/questions")
@Operation(summary = "Поиск вопросов по заголовку с использованием ElasticSearch")
public ResponseEntity<Slice<QuestionPreviewDTO>> searchQuestions(
@RequestParam("q") String prefix,
@PageableDefault(size = 20, sort = "createdAt") Pageable pageable) {
return ResponseEntity.ok(searchService.searchQuestions(prefix, pageable)); // falls in search service, reindex works well
}
@GetMapping("/tags")
@Operation(summary = "Поиск тега по названию с использованием ElasticSearch")
public ResponseEntity<Slice<TagDTO>> searchTags(
@RequestParam("q") String prefix,
@PageableDefault(size = 20) Pageable pageable) {
return ResponseEntity.ok(searchService.searchTags(prefix, pageable));
}
@GetMapping("/reindex")
@Operation(summary = "Реиндексация вопросов в ElasticSearch")
public void reindex() {
searchService.reindexAllQuestions();
}
}
</code></pre>
<p><strong>Full text of exception</strong>:</p>
<pre><code>co.elastic.clients.elasticsearch._types.ElasticsearchException: [es/search] failed: [search_phase_execution_exception] all shards failed
at co.elastic.clients.transport.ElasticsearchTransportBase.getApiResponse(ElasticsearchTransportBase.java:357) ~[elasticsearch-java-8.18.1.jar:na]
at co.elastic.clients.transport.ElasticsearchTransportBase.performRequest(ElasticsearchTransportBase.java:141) ~[elasticsearch-java-8.18.1.jar:na]
at co.elastic.clients.elasticsearch.ElasticsearchClient.search(ElasticsearchClient.java:5374) ~[elasticsearch-java-8.18.1.jar:na]
at org.springframework.data.elasticsearch.client.elc.ElasticsearchTemplate.lambda$doSearch$14(ElasticsearchTemplate.java:356) ~[spring-data-elasticsearch-5.5.2.jar:5.5.2]
at org.springframework.data.elasticsearch.client.elc.ElasticsearchTemplate.execute(ElasticsearchTemplate.java:689) ~[spring-data-elasticsearch-5.5.2.jar:5.5.2]
at org.springframework.data.elasticsearch.client.elc.ElasticsearchTemplate.doSearch(ElasticsearchTemplate.java:356) ~[spring-data-elasticsearch-5.5.2.jar:5.5.2]
at org.springframework.data.elasticsearch.client.elc.ElasticsearchTemplate.search(ElasticsearchTemplate.java:349) ~[spring-data-elasticsearch-5.5.2.jar:5.5.2]
at org.springframework.data.elasticsearch.core.SearchOperations.searchOne(SearchOperations.java:91) ~[spring-data-elasticsearch-5.5.2.jar:5.5.2]
at org.springframework.data.elasticsearch.repository.query.AbstractElasticsearchRepositoryQuery.execute(AbstractElasticsearchRepositoryQuery.java:130) ~[spring-data-elasticsearch-5.5.2.jar:5.5.2]
at org.springframework.data.repository.core.support.RepositoryMethodInvoker.doInvoke(RepositoryMethodInvoker.java:170) ~[spring-data-commons-3.5.2.jar:3.5.2]
at org.springframework.data.repository.core.support.RepositoryMethodInvoker.invoke(RepositoryMethodInvoker.java:158) ~[spring-data-commons-3.5.2.jar:3.5.2]
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.doInvoke(QueryExecutorMethodInterceptor.java:170) ~[spring-data-commons-3.5.2.jar:3.5.2]
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.invoke(QueryExecutorMethodInterceptor.java:149) ~[spring-data-commons-3.5.2.jar:3.5.2]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184) ~[spring-aop-6.2.9.jar:6.2.9]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:223) ~[spring-aop-6.2.9.jar:6.2.9]
at jdk.proxy2/jdk.proxy2.$Proxy180.findByTitleContainingIgnoreCase(Unknown Source) ~[na:na]
at ru.t1.usernamedrew.muse.service.impl.SearchServiceImpl.searchQuestions(SearchServiceImpl.java:40) ~[classes/:na] **<-- it falls when it try's to make a query in SearchQuestionRepository**
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:580) ~[na:na]
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:360) ~[spring-aop-6.2.9.jar:6.2.9]
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:724) ~[spring-aop-6.2.9.jar:6.2.9]
at ru.t1.usernamedrew.muse.service.impl.SearchServiceImpl$$SpringCGLIB$$0.searchQuestions(<generated>) ~[classes/:na]
at ru.t1.usernamedrew.muse.controller.SearchController.searchQuestions(SearchController.java:30) ~[classes/:na]
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:580) ~[na:na]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:258) ~[spring-web-6.2.9.jar:6.2.9]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:191) ~[spring-web-6.2.9.jar:6.2.9]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:118) ~[spring-webmvc-6.2.9.jar:6.2.9]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:991) ~[spring-webmvc-6.2.9.jar:6.2.9]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:896) ~[spring-webmvc-6.2.9.jar:6.2.9]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-6.2.9.jar:6.2.9]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1089) ~[spring-webmvc-6.2.9.jar:6.2.9]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:979) ~[spring-webmvc-6.2.9.jar:6.2.9]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1014) ~[spring-webmvc-6.2.9.jar:6.2.9]
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:903) ~[spring-webmvc-6.2.9.jar:6.2.9]
at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:564) ~[tomcat-embed-core-10.1.43.jar:6.0]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:885) ~[spring-webmvc-6.2.9.jar:6.2.9]
at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:658) ~[tomcat-embed-core-10.1.43.jar:6.0]
</code></pre>
<p>I've tried different types of queries. Tried to use NativeQuery, repositories with Page and List return types. Tried to set number_of_replicas to 0. What can I do next to fix this problem, also I tried to configure elastic with java code. All my shards are green and yellow. May be I wrote something wrong in elastic's repositories but I can't identify what's the problem. Spring Data Elastic documentation says it should work with Page and Slice.</p>
https://stackoverflow.com/q/757527182spaceManhttps://stackoverflow.com/users/136318812023-03-16T06:21:17Z2025-09-02T13:14:18Z
<p>I have an existing index "my-index" in Elastic. I'm trying to update this existing index with new fields without losing data.</p>
<p>I also tried to update the existing index with .putMapping function of RestHighLevelClient but I still got an error like this</p>
<blockquote>
<p>Elasticsearch exception [type=mapper_parsing_exception, reason=Failed to parse mapping [_doc]: Root mapping definition has unsupported parameters:...</p>
</blockquote>
<p>Here are the mappings of my existing index</p>
<pre><code>{
"mappings": {
"_doc": {
"properties": {
"_class": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 420
}
}
},
"taskType": {
"type": "integer"
},
"updateTime": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss"
},
"workType": {
"type": "integer"
}
}
}
}
}
</code></pre>
<p>And my expected index is</p>
<pre><code>{
"mappings": {
"_doc": {
"mappings": {
"dynamic_templates": [
{
"strings": {
"match_mapping_type": "string",
"mapping": {
"type": "keyword"
}
}
},
{
"dates": {
"match_mapping_type": "date",
"mapping": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss"
}
}
}
],
"properties": {
"_class": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 420
}
}
},
"date": {
"type": "long"
},
"favorite_color": {
"type": "keyword"
},
"is_active": {
"type": "boolean"
},
"number_float": {
"type": "float"
},
"number_long": {
"type": "long"
},
"taskType": {
"type": "integer"
},
"updateTime": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss"
},
"updatedDate": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss"
},
"workType": {
"type": "integer"
}
}
}
}
}
</code></pre>
<p>Can I update the existing index without losing any data using spring-data-elasticsearch repository??? Thanks for helping.</p>
https://stackoverflow.com/q/783291032Каляшов Григорийhttps://stackoverflow.com/users/46643922024-04-15T14:28:30Z2025-06-14T05:19:47Z
<p>I have a test project with the following configuration:</p>
<ul>
<li>spring boot 2.7.18</li>
<li>spring-boot-starter-data-elasticsearch</li>
<li>opensearch 2.6.0</li>
</ul>
<p>The connection to OpenSearch is configured like this:</p>
<pre><code>@Configuration
class OpensearchConfig: ReactiveElasticsearchConfiguration() {
val httpHeaders = HttpHeaders()
httpHeaders.add("Content-type", "application/json")
override fun clientConfiguration(): ClientConfiguration {
val config = ClientConfiguration.builder()
.connectedTo("localhost:9200")
.usingSsl()
.withDefaultHeaders(httpHeaders)
.withBasicAuth("admin", "admin")
.build()
return config
}
}
</code></pre>
<p>I'm trying to save a simple entity:</p>
<pre><code>val isIndexExists = opensearch.indexOps(Person::class.java).exists().awaitSingle()
if (!isIndexExists) {
opensearch.indexOps(Person::class.java).create().subscribe()
}
opensearch.save(Person(UUID.randomUUID().toString(), name))
.doOnNext(System.out::println)
.subscribe()
</code></pre>
<p>And I got the following error:</p>
<blockquote>
<p>org.springframework.dao.DataAccessResourceFailureException: status:
200, [es/indices.exists] Missing [X-Elastic-Product] header. Please
check that you are connecting to an Elasticsearch instance, and that
any networking filters are preserving that header.; nested exception
is java.lang.RuntimeException: status: 200, [es/indices.exists]
Missing [X-Elastic-Product] header. Please check that you are
connecting to an Elasticsearch instance, and that any networking
filters are preserving that header.] with root cause</p>
</blockquote>
<p>I didn't find an answer in the documentation whether spring-boot-starter-data-elasticsearch supports OpenSearch, so I had a question - maybe I'm configuring the connection to OpenSearch incorrectly? Or do I need to use opensearch-rest-client instead of spring-boot-starter-data-elasticsearch?</p>
https://stackoverflow.com/q/694349890safarionehttps://stackoverflow.com/users/150910372021-10-04T11:10:10Z2025-06-09T04:04:46Z
<p>I am new to reactive programming and I´m trying to switch to <code>ReactiveElasticsearchClient</code>.</p>
<p>Now the old code uses <code>RestHighLevelClient</code> and does a scroll search with a do-while loop like the following:</p>
<pre><code>SearchRequest searchRequest = searchRequest(indexName)
.source(searchSource()
.query(QueryBuilders.matchAllQuery())
.fetchSource(true)
.sort("id", SortOrder.ASC)
.size(1000))
.scroll(TimeValue.timeValueSeconds(60L));
SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
SearchHits searchHits = searchResponse.getHits();
String scrollId = searchResponse.getScrollId();
do {
/*
* work with the search hits and
* and do some logic here
*
* */
SearchScrollRequest scrollRequest = new SearchScrollRequest(scrollId).scroll(TimeValue.timeValueSeconds(60));
try {
searchResponse = restHighLevelClient.scroll(scrollRequest, RequestOptions.DEFAULT);
searchHits = searchResponse.getHits();
scrollId = searchResponse.getScrollId();
} catch (Exception e) {
LOGGER.error(e.getMessage());
}
} while (searchHits.getHits().length > 0);
ClearScrollRequest clearScrollRequest = new ClearScrollRequest();
clearScrollRequest.addScrollId(scrollId);
restHighLevelClient.clearScroll(clearScrollRequest, RequestOptions.DEFAULT);
</code></pre>
<p>How can I do the same in a reactive pipeline with the <code>ReactiveElasticsearchClient</code>? Some of the classes that the <code>RestHighLevelClient</code> uses are not available for the reactive client.</p>
https://stackoverflow.com/q/675753080pjunghttps://stackoverflow.com/users/19971372021-05-17T18:36:40Z2025-05-20T04:30:56Z
<p>I am trying to connect to an existing Elasticsearch instance. The instance is set up and working fine, which I can confirm by accessing it through the browser under https://localhost:9200. I am using Spring Data Elasticsearch version 4.1.2 and I am following the official instructions from <a href="https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#elasticsearch.clients.rest" rel="nofollow noreferrer">https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#elasticsearch.clients.rest</a>.</p>
<p>Unfortunately, when starting the application, the initial health checks of the elasticsearch instance fail, because the code is trying to use http instead of https:</p>
<pre><code>o.s.w.r.f.client.ExchangeFunctions : [377e90b0] HTTP HEAD http://localhost:9200/
</code></pre>
<p>Therefore I added the ".usingSsl()" to the ClientConfiguration, but it does not seem to have any effect and I just don't understand why. The "funny" thing is, if I implement the reactive client as described here (<a href="https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#elasticsearch.clients.reactive" rel="nofollow noreferrer">https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#elasticsearch.clients.reactive</a>), https suddenly works and the applications starts perfectly fine. However, I want to use the regular high level client.</p>
<p>Can you help me change the configuration in a way so that it finally makes use of https instead of http?</p>
<pre><code>@Configuration
class ElasticsearchClientConfig: AbstractElasticsearchConfiguration() {
@Bean
override fun elasticsearchClient(): RestHighLevelClient {
val clientConfiguration = ClientConfiguration.builder()
.connectedTo("localhost:9200")
.usingSsl()
.build();
return RestClients.create(clientConfiguration).rest();
}
}
</code></pre>
https://stackoverflow.com/q/796177620neverhttps://stackoverflow.com/users/8740072025-05-12T11:35:32Z2025-05-12T11:35:32Z
<p>I have a Spring Boot 3.3.x application that uses Spring Data Elasticsearch to query a single Elasticsearch index.</p>
<p>The query is built dynamically using the Criteria API. Now, I need to add a new condition using a match_phrase query.</p>
<p>However, I couldn't find any documentation or examples showing how to use match_phrase with the Criteria API.</p>
<p>One option would be to refactor the entire solution to use native queries, but that doesn't seem reasonable or trivial in our case.</p>
<p>Another workaround I found is to use NativeQueryBuilder, set the CriteriaQuery as the main query, and then add a manually constructed match_phrase query as a filter:</p>
<pre><code>return new NativeQueryBuilder()
.withQuery(criteriaQuery)
.withFilter(nativeMatchPhraseQuery)
.withPageable(pageable)
.build();
</code></pre>
<p>While this kind of works, it doesn't feel clean or intuitive.</p>
<p>Is there a way to use match_phrase within a Criteria query directly? Or a better pattern to combine Criteria and native queries more elegantly?</p>
https://stackoverflow.com/q/795877270Spartacushttps://stackoverflow.com/users/49376242025-04-23T02:22:36Z2025-04-28T07:37:23Z
<p>This is a followup question to (<a href="https://stackoverflow.com/questions/79463022/is-there-a-way-to-have-field-level-audit-in-elastic-search">Is there a way to have field level audit in elastic search?</a>)</p>
<p>Step 1:</p>
<pre><code>#push a sample doc
PUT my_index/_doc/1
{
"created_at": "2025-02-24T13:00:00Z",
"email": "john@example.com",
"name": "dogan",
"field_update_timestamps": {
"email": "2025-02-24T13:00:00Z",
"name": "2025-02-24T13:00:00Z"
}
}
</code></pre>
<p>Step 2:</p>
<pre><code>#update the doc with `_update` API call
POST my_index/_update/1
{
"script": {
"source": """
if (!ctx._source.containsKey('field_update_timestamps')) {ctx._source.field_update_timestamps = [:]; }
for (entry in params.entrySet()) {
ctx._source[entry.key] = entry.value; ctx._source.field_update_timestamps[entry.key] =
new java.text.SimpleDateFormat('yyyy-MM-dd\'T\'HH:mm:ss\'Z\'').format(new java.util.Date());
}
""",
"lang": "painless",
"params": {
"name": "musab"
}
}
}
</code></pre>
<p>So can we update the same record in the index concurrently? I mean if we start adding different domain like attendance in office or worked from home. {"wfh": "", "wfo": "" concurrently. Please note no 2 separate threads will put the same fields concurrently ie there will be no another thread which will be updating "wfh" or "wfo", however there can be another thread writing "email".
Will there be any optimistic lock exception or data loss?</p>
<p>Thanks</p>
<p>Cc: @Musab Dugan</p>
https://stackoverflow.com/q/790534980Ramzihttps://stackoverflow.com/users/229951832024-10-04T08:52:35Z2025-04-14T18:09:32Z
<p>I have a custom Kafka library that for business reasons I'm forced to use. My use case is to read each record from Kafka and save to a repository. The entry to my repository is a callback I write that implements something like</p>
<pre class="lang-java prettyprint-override"><code>interface Listener<T> {
Response apply(T val);
}
</code></pre>
<p>The library takes my listener, iterates over the <code>ConsumerRecords<T></code> from Kafka, and away I go. I wanted to see if I could use a reactive repository in Spring Data to save everything. Something like</p>
<pre class="lang-java prettyprint-override"><code>Response apply(T val) {
myRepo.save(val)
.subscribe()
...
return response;
}
</code></pre>
<p>So far I've only tried calling subscribe on the returned value of the Repository.save, but I don't have a way to dispose of all of these Publishers</p>
<p>But I immediately run into questions:</p>
<ol>
<li>I'm creating a Mono for every iteration within <code>ConsumerRecords</code>, but I'm never disposing of any of them. I can't actually tell when to dispose of each Mono so this sounds like it would just create a memory leak</li>
<li>I know for a fact that the library starts with the <code>ConsumerRecords</code> collection, so it seems like this should naturally be a Flux, but since my entry is the callback I can't figure out how to make this happen</li>
</ol>
<p>What would be ideal is to have some way to declare a Flux an set it so that each payload passed in is added to the flux, then call MyRepository.saveAll(Flux vals) and return a flux I can dispose of later, but if I understand the Reactor guide, I can't just add something to an exising Flux.</p>
<p>Is this just a hopeless cause? If I alternatively just take the Mono instance that comes back from calling <code>MyRepository.save(T)</code>, when do I call dispose on it?</p>
https://stackoverflow.com/q/795192570Guoqing Leehttps://stackoverflow.com/users/299896482025-03-19T06:07:25Z2025-03-19T10:44:00Z
<p>How should I implement script sort in spring-data-elasticsearch 5.3.x</p>
<pre class="lang-java prettyprint-override"><code>NativeQueryBuilder nativeQueryBuilder= new NativeQueryBuilder();
nativeQueryBuilder.withQuery(query);
nativeQueryBuilder.withSort(Sort.Order.desc('key'));
return elasticsearchTemplate.search(nativeQueryBuilder.build(), xxx.class);
</code></pre>
<p>Now, I want to add a script-based sorting on top of this. I know how to write the script, but it seems that script-based sorting is not supported in the context of NativeQueryBuilder.</p>
https://stackoverflow.com/q/6703018713carlspringhttps://stackoverflow.com/users/7741832021-04-10T02:08:12Z2025-02-28T14:56:51Z
<p>Across the web, I can see that <code>spring-data-elasticsearch</code> has some configuration properties that you can define in your <code>application.properties</code>, such as:</p>
<pre><code>spring.data.elasticsearch.repositories.enabled=true
spring.data.elasticsearch.cluster-nodes=localhost:9300
spring.data.elasticsearch.cluster-name=elasticsearch
elasticsearch.index.name=my_index
elasticsearch.user.type=user
</code></pre>
<p>However, in IntelliJ, I can see that, for example:</p>
<pre><code>spring.data.elasticsearch.cluster-nodes=localhost:9300
spring.data.elasticsearch.cluster-name=elasticsearch
</code></pre>
<p>... are actually now deprecated. However, I can't seem to find anything in the <code>spring-data-elasticsearch</code> documentations that either lists what the available properies are, or, what the deprecated ones should be replaced with instead.</p>
<p>Any help would be welcome. Thanks in advance!</p>
https://stackoverflow.com/q/681015980Pierre Joneshttps://stackoverflow.com/users/77325402021-06-23T14:14:44Z2025-02-28T14:53:29Z
<p>I'm facing a weird issue. I have 2 laptops, one running under Windows where everything works fine and on the other side, a Mac Mini M1.</p>
<p>Assuming, I'm developing a basic Spring Boot application with Elasticsearch as Repository.
In the <code>application.yml</code>, as usual I have the following properties :</p>
<pre><code>spring:
elasticsearch:
rest:
uris: https://dummy-cloud-provider.cloud.com:12833
username: myusername
password: mypassword
</code></pre>
<p>But everytime I want to start the application on my Mac, I have the following error :</p>
<pre><code>2021-06-23 15:55:51.972 ERROR [reactor-http-nio-3] r.c.p.Operators - Operator called default onErrorDropped
reactor.core.Exceptions$ErrorCallbackNotImplemented: org.springframework.data.elasticsearch.client.NoReachableHostException: Host 'localhost:9200' not reachable. Cluster state is offline.
Caused by: org.springframework.data.elasticsearch.client.NoReachableHostException: Host 'localhost:9200' not reachable. Cluster state is offline.
at org.springframework.data.elasticsearch.client.reactive.SingleNodeHostProvider.lambda$lookupActiveHost$3(SingleNodeHostProvider.java:101)
at reactor.core.publisher.FluxHandle$HandleSubscriber.onNext(FluxHandle.java:102)
at reactor.core.publisher.FluxMap$MapConditionalSubscriber.onNext(FluxMap.java:220)
at reactor.core.publisher.FluxOnErrorResume$ResumeSubscriber.onNext(FluxOnErrorResume.java:79)
at reactor.core.publisher.Operators$ScalarSubscription.request(Operators.java:2397)
...
</code></pre>
<p>I don't understand why it always skip my <code>application.yml</code> variables and try to reach <code>localhost:9200</code>.</p>
<p>I hope someone can help me :)</p>
<p>Best regards</p>
https://stackoverflow.com/q/794329540solujichttps://stackoverflow.com/users/31775322025-02-12T11:52:20Z2025-02-25T08:45:14Z
<p>Custom @ReadingConverter isn't being triggered but when I was initially implementing this code it worked normally.</p>
<p>Configuration class:</p>
<pre><code>@Configuration
public class ElasticsearchConfig {
@Bean
public MappingElasticsearchConverter mappingElasticsearchConverter() {
return new MappingElasticsearchConverter(new SimpleElasticsearchMappingContext());
}
@Bean
public ElasticsearchCustomConversions elasticsearchCustomConversions(
MappingElasticsearchConverter mappingElasticsearchConverter, ObjectMapper objectMapper) {
return new ElasticsearchCustomConversions(List.of(
new LogEntryReadingConverter(mappingElasticsearchConverter, objectMapper)
));
}
}
</code></pre>
<p>Here is the implementation of the converter:</p>
<pre><code>@Component
@ReadingConverter
public class LogEntryReadingConverter implements Converter<Document, LogEntry> {
private final MappingElasticsearchConverter elasticsearchConverter;
private final ObjectMapper objectMapper;
public LogEntryReadingConverter(MappingElasticsearchConverter elasticsearchConverter, ObjectMapper objectMapper) {
this.elasticsearchConverter = elasticsearchConverter;
this.objectMapper = objectMapper;
}
public LogEntry convert(Document source) {
// Use Elasticsearch's internal conversion (respects @Field annotations)
LogEntry logEntry = elasticsearchConverter.read(LogEntry.class, source);
// some custom logic is here
return logEntry;
}
</code></pre>
<p>My document class:</p>
<pre><code>@Data
@JsonIgnoreProperties(ignoreUnknown = true)
@Document(indexName = "logstash")
public class LogEntry implements ILogModel {
@Id
private String id;
// more fields are here
}
</code></pre>
<p>My repository class:</p>
<pre><code>@Repository
public interface HaProxyLogRepository extends ElasticsearchRepository<LogEntry, String> {
Page<LogEntry> findByTimestampBetween(LocalDateTime from, LocalDateTime to, Pageable pageable);
}
</code></pre>
<p>I would expect the convert method to be called before execution of the repository method "findByTimestampBetween" on method call. It worked before, for some odd reason it never triggers now.</p>
<p>I am using Spring Boot 3.1.2. and Spring Data Elasticsearch 5.1.2.</p>
https://stackoverflow.com/q/794630220Spartacushttps://stackoverflow.com/users/49376242025-02-24T09:33:04Z2025-02-24T14:16:08Z
<p>I have an index with say 10 fields. These fields may get modified from time to time. Is there a way to add any field level timestamp, which can store the timestamp when it was last updated and also can be retrieved by api?</p>
<p>Thanks in advance.</p>
https://stackoverflow.com/q/395492231ariehttps://stackoverflow.com/users/68432292016-09-17T16:39:21Z2025-02-19T21:05:58Z
<p>I'm trying to use both Spring Data Cassandra & Spring Data ElasticSearch in an Spring Boot application.
I already put Cassandra Repository & ElasticSearch Repository in different packages.</p>
<pre><code>@EnableElasticsearchRepositories(basePackages = "com.my.app.es.repository")
public class ElasticsearchConfig {
...
}
@EnableCassandraRepositories(basePackages = { "com.my.app.cassandra.repository" })
public class CassandraConfig extends AbstractCassandraConfiguration {
...
}
</code></pre>
<p>When I tried to run, I got the following exception::</p>
<pre><code>org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'converter' defined in class path resource [com/my/app/config/CassandraConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.cassandra.convert.CassandraConverter]: Factory method 'converter' threw exception; nested exception is java.lang.IllegalStateException: @Bean method CassandraConfig.mappingContext called as a bean reference for type [org.springframework.data.cassandra.mapping.CassandraMappingContext] but overridden by non-compatible bean instance of type [org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext]. Overriding bean of same name declared in: class path resource [org/springframework/boot/autoconfigure/data/elasticsearch/ElasticsearchDataAutoConfiguration.class]
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1123) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1018) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:776) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:861) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759) [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:369) [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:313) [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1185) [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1174) [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE]
</code></pre>
https://stackoverflow.com/q/729579392Danhttps://stackoverflow.com/users/14137562022-07-12T20:17:51Z2025-02-12T20:01:24Z
<p>I am having an issue trying to implement optimistic locking when executing a document upsert.</p>
<p>I followed all the guides, however I am getting the following error:</p>
<pre><code>Validation Failed: 1: compare and write operations can not be used with upsert;
</code></pre>
<p>I can trace this error back to this <a href="https://github.com/elastic/elasticsearch/blob/master/server/src/main/java/org/elasticsearch/action/update/UpdateRequest.java#L175" rel="nofollow noreferrer">validate</a> method, however I cannot find an explanation as to why this cannot be done nor HOW to do an upsert without running into conflicts.</p>
<p>Here's where I am getting the error in my code:</p>
<pre><code>private UpdateResponse doAction(UUID esId, List<Comment> comments, dSeqNoPrimaryTerm seqPrimNo, String indexName) {
UpdateResponse response = null;
Map<String, Object> params = new HashMap<>();
params.put("comments", comments);
Builder builder = UpdateQuery
.builder(esId.toString())
.withDocAsUpsert(true)
.withDocument(Document.from(params))
.withRefresh(Refresh.True);
if (seqPrimNo != null) {
builder.withIfSeqNo((int) seqPrimNo.getSequenceNumber());
builder.withIfPrimaryTerm((int) seqPrimNo.getPrimaryTerm());
}
UpdateQuery query = builder.build();
response = elasticsearchTemplate.update(query, IndexCoordinates.of(indexName)); // <-- ERRORS HERE
return response;
}
</code></pre>
<p>Does that mean that we cannot do optimistic locking when doing an upsert? What am I missing?</p>
https://stackoverflow.com/q/762598170Carlyle CHANhttps://stackoverflow.com/users/219046242023-05-16T06:03:47Z2025-02-04T19:27:39Z
<p>I am new at stackoverflow and happy to learn from all</p>
<p>I am required to upgrade my Java spring boot program from:</p>
<ul>
<li>Java 11 to Java 17</li>
<li>Spring Boot 2.4.2 to Spring Boot 3.0.5</li>
</ul>
<p>After change the pom.xml:</p>
<ul>
<li>java.version: 17</li>
<li>org.springframework.boot:spring-boot-starter-parent:3.0.5</li>
</ul>
<p>My elasticsearch dependency (org.springframework.boot:spring-boot-starter-data-elasticsearch) also upgrade to 3.0.5. However, I found the following packages are missing:</p>
<ul>
<li>org.apache.lucene.search.join.ScoreMode</li>
<li>org.elasticsearch.index.query.BoolQueryBuilder</li>
<li>org.elasticsearch.index.query.QueryBuilder</li>
<li>org.elasticsearch.index.query.QueryBuilders</li>
<li>org.elasticsearch.index.query.TermsQueryBuilder</li>
</ul>
<p>So I have some questions about the new Elasticsearch client library:</p>
<ol>
<li>Whether not support using the QueryBuilder again, so I require to change all of service code about using above package?</li>
<li>Whether not support reactive mode at 5.0.5 version?
If both answer are YES, it is really painful for me to upgrade it.</li>
</ol>
<p>The following is the base information of my ES:
version: 7.10.1</p>
<p>Also, I have a stupid question want to confirm: according to the following information provided by Spring-Data-Elasticsearch:
<a href="https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#preface.versions" rel="nofollow noreferrer">https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#preface.versions</a></p>
<p>If my ES version is 7.10.1, I only can upgrade my spring boot to 2.7.x? or I upgrade to 3.0.5 also can support the older version.</p>
<p>If there are any things missing, I will provide again. Sorry for my poor English and terms. Thank you for your answers.</p>
<p>I had tried change the pom with following pattern:</p>
<p>Case 1:</p>
<ul>
<li>Upgrade Java from 11 to 17</li>
<li>Upgrade Java Spring Boot from 2.4.2 to 3.0.5 or 3.0.6</li>
<li>Let the org.springframework.boot:spring-boot-starter-data-elasticsearch version same as parent</li>
</ul>
<p><strong>Result</strong>: Some ES packages are missing and program cannot be run</p>
<p>Case 2:</p>
<ul>
<li>Upgrade Java from 11 to 17</li>
<li>Keep Java Spring Boot 2.4.2</li>
<li>Upgrade org.springframework.boot:spring-boot-starter-data-elasticsearch to 3.0.5 or 3.0.6</li>
</ul>
<p><strong>Result</strong>: program runs without error</p>
<p>Case 3:</p>
<ul>
<li>Upgrade Java from 11 to 17</li>
<li>Upgrade Java Spring Boot from 2.4.2 to 3.0.5 or 3.0.6</li>
<li>Keep org.springframework.boot:spring-boot-starter-data-elasticsearch:2.4.2</li>
</ul>
<p><strong>Result</strong>: some ES packages are missing and program cannot be run</p>
<p>Thus, It seems the missing package problems is caused by Upgrade Java Spring Boot from 2.4.2 to 3.0.x</p>
https://stackoverflow.com/q/793753750Raouf Zouaouihttps://stackoverflow.com/users/292825802025-01-21T17:25:51Z2025-01-29T12:26:47Z
<p>I'm using Elasticsearch filters and I would like to apply a must condition for selected regions (e.g., Occitanie and Île-de-France). Additionally, I want to ensure that profiles with the region "Toute la France" are also included in the search results, even if they are not part of the selected regions.</p>
<p>Currently, I am doing the following:</p>
<pre><code>if (regions != null && !regions.isEmpty()) {
BoolQuery.Builder finalQuery = new BoolQuery.Builder();
for (String regionName : regions) {
finalQuery.must(Query.of(q -> q.nested(n -> n
.path("regions")
.query(Query.of(inner -> inner.match(m -> m
.field("regions.regionName")
.query(regionName)
)))
)));
}
finalQuery.should(Query.of(q -> q.nested(n -> n
.path("regions")
.query(Query.of(inner -> inner.match(m -> m
.field("regions.regionName")
.query("Toute la France")
.boost(4.0f)
)))
)));
finalQuery.minimumShouldMatch("1");
boolQuery.filter(Query.of(q -> q.bool(finalQuery.build())));
}
</code></pre>
<p>However, this query only returns profiles that match the selected regions (must), but it does not include profiles with "Toute la France."</p>
<p>How can I modify the query to include profiles with "Toute la France" along with the selected regions?</p>
<p>Thanks in advance for your help!</p>
https://stackoverflow.com/q/287970756Mouhammed Soueidanehttps://stackoverflow.com/users/7008582015-03-01T18:04:07Z2025-01-22T17:22:58Z
<p>I have successfully created a query using ElasticSearch's _plugin/head interface. The query is meant to return the latest timestamp for a specific device at a specific location. The query looks as follows:</p>
<pre><code>{
"query":{
"bool":{
"must":[
{
"term":{
"deviceevent.location.id":"1"
}
},
{
"term":{
"deviceevent.deviceId":"AHE1LDD01"
}
}
]
}
},
"from":0,
"size":1,
"sort":{
"timestamp":{
"order":"desc"
}
}
}
</code></pre>
<p>The above query works as intended.
Now using Spring-Boot and Spring-Data-ElasticSearch, I defined my own <code>ElasticSearchRepository</code> which looks as follows:</p>
<pre><code>package com.repository.elasticsearch;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.annotations.Query;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import com.domain.DeviceEvent;
public interface DeviceEventRepository extends ElasticsearchRepository<DeviceEvent, String>
{
@Query("{\"bool\":{\"must\":[{\"term\":{\"deviceevent.location.id\": \"?0\"}},{\"term\":{\"deviceevent.deviceId\": \"?1\"}}]}},\"from\": 0,\"size\": 1,\"sort\":{\"timestamp\":{\"order\":\"desc\"}}")
DeviceEvent findLatestCheckInAtLocation(Long locationId, String deviceId);
}
</code></pre>
<p>The above code is breaking mainly because I would expect it to return one <code>DeviceEvent</code>, but it's actually returning a device events with count = 10 (The default Page size). It seems also that the results are not being ordered by the timestamp in a descending order. It's as if the <code>size</code> and <code>order</code> parts of the query are not being picked up. </p>
<p>What am I doing wrong here?</p>
https://stackoverflow.com/q/79338556-1Redwingshttps://stackoverflow.com/users/37622842025-01-08T09:24:08Z2025-01-09T06:45:28Z
<p>Now, <code>RestHighLevelClient</code> is deprecated.
So i want rewrite code with <a href="https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/introduction.html" rel="nofollow noreferrer">ElasticSearch Java Api Client</a> in my spring project.</p>
<p>Most of the functions are being modified properly, but I have not found an appropriate way to modify this function.</p>
<p>Below is my existing code.</p>
<pre><code>// import org.elasticsearch.index.reindex.DeleteByQueryRequest;
private String deleteLarge(String indexName, Long categoryNumber, Optional<Long> age) {
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery()
.filter(QueryBuilders.matchQuery("category", categoryNumber));
age.ifPresent(a -> boolQueryBuilder
.filter(QueryBuilders.matchQuery("age", a)));
DeleteByQueryRequest request = new DeleteByQueryRequest(indexName)
.setQuery(boolQueryBuilder)
.setSlices(10);
try {
TaskSubmissionResponse taskSubmissionResponse = restHighLevelClient.submitDeleteByQueryTask(request, RequestOptions.DEFAULT);
logger.info("DeleteByQueryTask task id: {}", taskSubmissionResponse.getTask());
return taskSubmissionResponse.getTask();
} catch (Exception e) {
logger.error(e.getMessage());
}
return null;
}
</code></pre>
<p>The purpose of this code is to delete a very large volume of documents (ranging from tens of thousands to millions). Since it was difficult to estimate the execution time, I submitted a task to be performed internally by ES.</p>
<p>However, it seems that the ElasticSearch Java API Client does not have such a feature. In the official documentation, there is a Class like <a href="https://artifacts.elastic.co/javadoc/co/elastic/clients/elasticsearch-java/8.17.0/co/elastic/clients/elasticsearch/tasks/ElasticsearchTasksClient.html" rel="nofollow noreferrer">ElasticsearchTasksClient</a>, which provides the ability to get info or cancel task, but not to submit new tasks.</p>
<p>Here are my questions:</p>
<ol>
<li>Does ES no longer support tasks? Is it gradually being deprecated?</li>
<li>If not, is there another way to submit new tasks?</li>
<li>If it is indeed unavailable, is there another method to achieve similar functionality? Would using <code>deleteByQuery</code> in <code>ElasticsearchAsyncClient</code> be equivalent to submitting a task?</li>
</ol>
https://stackoverflow.com/q/793397240lambdahttps://stackoverflow.com/users/220222602025-01-08T15:19:39Z2025-01-08T15:19:39Z
<p>The following code will throw an exception: <code>[es/search] failed: [parsing_exception] unknown query [query]</code>.</p>
<pre><code> @Query(value = """
{
"query": {
"script_score": {
"query": {
"match_all": {}
},
"script": {
"source": "cosineSimilarity(params.vector, doc['nameVector']) + cosineSimilarity(params.vector, doc['descriptionVector'])",
"params": {
"vector": ?0
}
}
}
},
"sort": [
{ "_score": { "order": "desc" } }
]
}
""")
Flux<SearchHit<ClubPO>> queryClubs(List<Double> vector);
</code></pre>
<p>The following code can run successfully.</p>
<pre><code> @Query("""
{
"script_score": {
"query": {
"match_all": {}
},
"script": {
"source": "cosineSimilarity(params.vector, doc['nameVector']) + cosineSimilarity(params.vector, doc['descriptionVector'])",
"params": {
"vector": ?0
}
}
}
}
""")
Flux<SearchHit<ClubPO>> queryClubs(List<Double> vector);
</code></pre>
<p>What should I do?</p>
<p>Are there any other annotations? For example, ones that support more raw query syntax?</p>
<p># {"error":{"root_cause":[{"type":"parsing_exception","reason":"unknown query [query]","line":2,"col":12}],"type":"parsing_exception","reason":"unknown query [query]","line":2,"col":12,"caused_by":{"type":"named_object_not_found_exception","reason":"[2:12] unknown field [query]"}},"status":400}</p>
https://stackoverflow.com/q/7929987000xJanAbehttps://stackoverflow.com/users/240350222024-12-21T17:57:37Z2024-12-21T18:01:02Z
<p>I want to use the following elastic search query in Java to search through all my documents in my index.</p>
<pre><code>{
"query": {
"query_string": {
"query": "my search input"
}
}
}
</code></pre>
<p>When I use CURL it works fine and i get results back, but when i try to use this query in Java, it complains and gives me the following exception:</p>
<pre><code>co.elastic.clients.elasticsearch._types.ElasticsearchException: [es/search] failed: [parsing_exception] unknown query [query]
</code></pre>
<p>This is what my code looks like in Java:</p>
<pre class="lang-java prettyprint-override"><code> @GetMapping("/search")
public void searchResume(@RequestParam(name = "search") String query) {
Query q = new StringQuery("""
{
"query": {
"query_string": {
"query": "my search input"
}
}
}
""");
System.out.println(operations.search(q, Resume.class));
}
</code></pre>
<p>Does someone know why it works using CURL but does not work when using Java and spring-data-elasticsearch?</p>
<p>I tried googling a lot and even asked chatgpt for help but I can't seem to find out why it doesn't work. Thank you in advance.</p>
https://stackoverflow.com/q/4422683415Vincehttps://stackoverflow.com/users/80772682017-05-28T11:31:59Z2024-12-03T13:59:09Z
<p>I'm new to the community so I apologise if I do something wrong.</p>
<p>I'm using spring data elasticsearch (2.0.4/2.4)
And I would like to make a bulk insert and delete.
But ElasticsearchTemplate only contains a method bulkInsert</p>
<pre><code>@Override
public void bulkIndex(List<IndexQuery> queries) {
BulkRequestBuilder bulkRequest = client.prepareBulk();
for (IndexQuery query : queries) {
bulkRequest.add(prepareIndex(query));
}
BulkResponse bulkResponse = bulkRequest.execute().actionGet();
if (bulkResponse.hasFailures()) {
Map<String, String> failedDocuments = new HashMap<String, String>();
for (BulkItemResponse item : bulkResponse.getItems()) {
if (item.isFailed())
failedDocuments.put(item.getId(), item.getFailureMessage());
}
throw new ElasticsearchException(
"Bulk indexing has failures. Use ElasticsearchException.getFailedDocuments() for detailed messages ["
+ failedDocuments + "]", failedDocuments
);
}
}
</code></pre>
<p>So I have created a bulk method to handle both but I can't access the method prepareIndex which is private.</p>
<p>Are you aware of any solution to, in one bulk, index and delete documents or should I use reflection to change visibility of the prepareIndex method
or is there any easy way to create an indexRequest from a model/POJO?</p>
https://stackoverflow.com/q/791429440Mustafa Yilmazhttps://stackoverflow.com/users/206967722024-10-30T22:06:14Z2024-11-09T13:21:13Z
<p>I had an ELK server that I needed to shut down and replace with a new one. I had a Java Spring Boot project that was connected to the original ELK server. After moving to the new server, all the indices were reset, and I lost all previous data.</p>
<p>Now, I am encountering issues trying to connect my Spring Boot application to the new ELK server. I have verified that the ELK server is running and is accessible. However, when I start my Spring Boot application, I receive the following error:</p>
<pre class="lang-none prettyprint-override"><code>2024-10-31T00:42:37.816+03:00 WARN 9020 --- [data-leak-service] [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'Controller' defined in file [Controller.class]: Unsatisfied dependency expressed through constructor parameter 0: Error creating bean with name 'Service' defined in file [Service.class]: Unsatisfied dependency expressed through constructor parameter 0: Error creating bean with name 'DataRepository' defined in DataRepository defined in @EnableElasticsearchRepositories declared on ElasticSearchConfiguration: Cannot resolve reference to bean 'elasticsearchTemplate' while setting bean property 'elasticsearchOperations' ...
</code></pre>
<p>I suspect that this issue may be related to the ELK configuration. Here is my current configuration:</p>
<pre class="lang-java prettyprint-override"><code>@Configuration
public class ElasticSearchConfiguration {
@Value("${ElasticUrl}")
private String elkUrl;
@Value("${elasticsearch.username}")
private String username;
@Value("${elasticsearch.password}")
private String password;
@Bean
public ElasticsearchClient elasticsearchClient() {
// Set up credentials provider for basic authentication
BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(
AuthScope.ANY,
new UsernamePasswordCredentials(username, password)
);
// Configure RestClient with authentication
RestClient restClient = RestClient.builder(HttpHost.create(elkUrl))
.setHttpClientConfigCallback(httpClientBuilder ->
httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider))
.build();
// Set up transport with Jackson JSON mapper
ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
// Return the authenticated Elasticsearch client
return new ElasticsearchClient(transport);
}
}
</code></pre>
<p>Additionally, I added the property for automatic index creation in my <code>application.properties</code>:</p>
<pre class="lang-ini prettyprint-override"><code>spring.elasticsearch.index.auto-create=true
</code></pre>
<p>I am using the following dependencies:</p>
<pre class="lang-xml prettyprint-override"><code><dependency>
<groupId>co.elastic.clients</groupId>
<artifactId>elasticsearch-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
</code></pre>
<p>I am using the Elasticsearch client version 8.15.1.</p>
<p>Previously, I had a different configuration that worked without authentication:</p>
<pre class="lang-java prettyprint-override"><code>@Configuration
public class ElasticsearchConfig {
@Bean
public ElasticsearchClient elasticsearchClient() {
// Create the low-level client (without authentication)
RestClient restClient = RestClient.builder(HttpHost.create("http://elkip:9200")).build();
// Create the transport with a Jackson JSON mapper
ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
// Create the Elasticsearch client
return new ElasticsearchClient(transport);
}
}
</code></pre>
<p>What could be causing this error, and how can I resolve it? Additionally, what steps do I need to take to enable automatic index creation?</p>
https://stackoverflow.com/q/677203580Hari Raohttps://stackoverflow.com/users/40682182021-05-27T10:33:17Z2024-10-18T14:19:58Z
<p>I read through <a href="https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#reference" rel="nofollow noreferrer">https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#reference</a> to begin with</p>
<p><strong>My requirements</strong></p>
<ol>
<li>I want to use percolator. Is there any support for it in spring data elasticsearch? I don't see any in the above link although I understand that percolating is same as indexing (technically from using spring data elasticsearch's perspective). So I can use the indexing part of spring data elasticsearch but just checking if there are any that are specific to percolator.</li>
<li>I want to create an index dynamically. I do understand I can achieve that using SpEL template expression as mentioned in <a href="https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#elasticsearch.mapping.meta-model.annotations" rel="nofollow noreferrer">https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#elasticsearch.mapping.meta-model.annotations</a> but my case is slightly different, I will get the index name via the RequestParam as part of the API call. So this means as of my knowledge I cannot use SpEL or try something like <a href="https://stackoverflow.com/a/33520421/4068218">https://stackoverflow.com/a/33520421/4068218</a></li>
<li>I see I can use ElasticsearchOperations or ElasticsearchRepository to create Index. Because of #2 (i.e index name via request parameter) I think ElasticsearchOperations better suits but I see IndexOperations facilitating createMapping, createSettings but not both together. I see putMapping too but I dont see anything that says both mapping and settings. The reason I want both is I want to create something like below to begin with</li>
</ol>
<blockquote>
<pre><code> "settings" : {
"index" : {
"number_of_shards" : 1,
"number_of_replicas" : 0
}
},
"mappings": {
"properties": {
"message": {
"type": "text"
},
"query": {
"type": "percolator"
}
}
}
</code></pre>
</blockquote>
<p><strong>Bottom line :</strong>- How do I create an index (name of the index will be dynamic via request param) with mappings, settings using ElasticsearchOperations?<br />
Any lead/help is much appreciated</p>
https://stackoverflow.com/q/698969420vr3w3c9https://stackoverflow.com/users/13858832021-11-09T10:59:41Z2024-10-07T07:29:16Z
<p>Current Im working on a spring boot project to fetch the data from specific Elastic search index using Spring Data ElasticSearch (version 4.1.7). Im able to connect to elastic search and fetch all the data associated with a specific index. But there are some fields in the index which are returning null value. Where as the actual value exist in the index in elastic search .<br />
Im having issues in retriving the specific fields due to incorrect field types. I have created a Document Class which</p>
<pre><code>@Document(indexName = "netstat*")
@Setting(settingPath = "static/es-settings.json")
public class NetworkStats {
@Id
@Field(type = FieldType.Keyword)
private String id;
@Field
private String timeStamp ;
@Field(type = FieldType.Text)
private String podName ;
@Field(type = FieldType.Text)
private String iporHost ;
@Field(type = FieldType.Auto)
private String connectionCount ;
@Field(type = FieldType.Text)
private String command ;
}
</code></pre>
<p>Apart from the above I have getters & setters for the field as well defined.Im unabel to retrieve the values for fields connectionCount, iporHost, podName, timeStamp. Though the fields have values in the elastic index.<br />
Sample data from ElasticSearch below</p>
<pre><code>{
"_index": "netstat-2021.11.09",
"_type": "_doc",
"_id": "0sJmA30BW7LXXVrK0nCg",
"_score": 1.0,
"_source": {
"podname": "logindeployment-5479f7-4f2qr",
"input": {
"type": "log"
},
"iporhost": "6200",
"tags": [],
"command": "OUTESTAB",
"agent": {
"version": "7.15.1",
"id": "e8feeb4e-1f9e-41b2-bcba-38e507d176db",
"type": "filebeat",
"hostname": "BL1726L",
"name": "BL1726L",
"ephemeral_id": "c2859ece-3e95-4204-909d-af59322b9fa2"
},
"timestamp": "05-10-21T13:17:25.014719626",
"ecs": {
"version": "1.11.0"
},
"connectionscount": "2",
"@timestamp": "2021-11-09T06:31:29.104Z",
"log": {
"offset": 1002,
"file": {
"path": "D:\\elastic_stack\\data\\log\\netStats.log"
}
}
}
},
</code></pre>
<p>Require help on how to retrieve the data for the above mentioned fields.I guess there is some issue with field mapping. Any help on this is much appreciated.</p>