I'm using Spring Boot 3.4.1, Elasticsearch 8.15.5, and Spring Data Elasticsearch with Kotlin. When trying to build a query using gte, lte, and .fields, I get unresolved reference errors in my code.
ERROR LINES;
val rangeQuery = RangeQuery.Builder()
.field("lastSeen") // ❌ Unresolved reference
.gte(JsonData.of(startDate)) // ❌ Unresolved reference
.lte(JsonData.of(endDate)) // ❌ Unresolved reference
.build()
I am new to ElasticSearch and I am lacking in creating these queries in spring boot. I am applying a dynamic filter system in the function below, my usage may be wrong but all my filtering works except for date range filter;
MY FILTER FUNCTION;
@Repository
class CustomCrashSummaryRepository(
private val elasticsearchClient: ElasticsearchClient,
private val elasticsearchOps: ElasticsearchOperations
) {
fun findCrashSummariesByFilters(
merchantId: String,
projectId: String,
osName: String,
isIssueClosed: Boolean?,
deviceModelList: List<String>?,
osVersionList: List<String>?,
appVersionList: List<String>?,
pageable: Pageable,
startDate: String?,
endDate: String?
): Page<CrashSummary> {
val query: NativeQuery = NativeQuery.builder()
.withQuery { q ->
q.bool { b ->
b.filter { f -> f.term { t -> t.field("merchantId.keyword").value(merchantId) } }
b.filter { f -> f.term { t -> t.field("projectId.keyword").value(projectId) } }
b.filter { f -> f.term { t -> t.field("osName.keyword").value(osName) } }
if (isIssueClosed != null) {
b.filter { f -> f.term { t -> t.field("isIssueClosed").value(isIssueClosed) } }
}
if (!deviceModelList.isNullOrEmpty()) {
b.filter { f ->
f.terms { t ->
t.field("devices.keyword")
.terms { terms -> terms.value(deviceModelList.map { FieldValue.of(it) }) }
}
}
}
if (!osVersionList.isNullOrEmpty()) {
b.filter { f ->
f.terms { t ->
t.field("osVersions.keyword")
.terms { terms -> terms.value(osVersionList.map { FieldValue.of(it) }) }
}
}
}
if (!appVersionList.isNullOrEmpty()) {
b.filter { f ->
f.terms { t ->
t.field("appVersions.keyword")
.terms { terms -> terms.value(appVersionList.map { FieldValue.of(it) }) }
}
}
}
if (startDate != null || endDate != null) {
val rangeQuery = RangeQuery.Builder()
.field("lastSeen")
.gte(JsonData.of(startDate))
.lte(JsonData.of(endDate))
.build()
b.filter(rangeQuery._toQuery())
}
b
}
}
.withPageable(pageable)
.build()
val searchResponse = elasticsearchOps.search(query, CrashSummary::class.java)
val contentList: List<CrashSummary> = searchResponse.map { it.content }.toList()
val totalHits = searchResponse.totalHits
return PageImpl(contentList, pageable, totalHits)
}
}
MY IMPORTS;
import co.elastic.clients.elasticsearch.ElasticsearchClient
import co.elastic.clients.elasticsearch.core.UpdateByQueryRequest
import co.elastic.clients.elasticsearch._types.Script
import co.elastic.clients.elasticsearch._types.FieldValue
import co.elastic.clients.elasticsearch._types.aggregations.TermsAggregation
import co.elastic.clients.elasticsearch._types.query_dsl.*
import co.elastic.clients.elasticsearch.core.SearchRequest
import co.elastic.clients.json.JsonData
import co.elastic.clients.json.jackson.JacksonJsonpMapper
import com.fasterxml.jackson.databind.ObjectMapper
import io.initial.logdrop.core.entity.CrashSummary
import io.initial.logdrop.core.entity.admin.CrashFilterResult
import .springframework.stereotype.Repository
import java.io.IOException
import .springframework.data.domain.PageImpl
import java.io.StringWriter
import .springframework.data.domain.Page
import .springframework.data.domain.Pageable
import .springframework.data.elasticsearch.client.elc.NativeQuery
import .springframework.data.elasticsearch.core.ElasticsearchOperations
CrashSummary Document Mapping;
...
"lastSeen": {
"type": "date",
"format": "epoch_millis"
}
...
Example Data;
{
"description": [
"java.lang.NullPointerException"
],
"firstSeen": [
"2025-03-17T16:12:46.887Z"
],
"isIssueClosed": [
false
],
"issueId": [
"1651584580"
],
"issueId.keyword": [
"1651584580"
],
"lastSeen": [
"2025-03-17T16:12:46.887Z"
]
}
I'm using Spring Boot 3.4.1, Elasticsearch 8.15.5, and Spring Data Elasticsearch with Kotlin. When trying to build a query using gte, lte, and .fields, I get unresolved reference errors in my code.
ERROR LINES;
val rangeQuery = RangeQuery.Builder()
.field("lastSeen") // ❌ Unresolved reference
.gte(JsonData.of(startDate)) // ❌ Unresolved reference
.lte(JsonData.of(endDate)) // ❌ Unresolved reference
.build()
I am new to ElasticSearch and I am lacking in creating these queries in spring boot. I am applying a dynamic filter system in the function below, my usage may be wrong but all my filtering works except for date range filter;
MY FILTER FUNCTION;
@Repository
class CustomCrashSummaryRepository(
private val elasticsearchClient: ElasticsearchClient,
private val elasticsearchOps: ElasticsearchOperations
) {
fun findCrashSummariesByFilters(
merchantId: String,
projectId: String,
osName: String,
isIssueClosed: Boolean?,
deviceModelList: List<String>?,
osVersionList: List<String>?,
appVersionList: List<String>?,
pageable: Pageable,
startDate: String?,
endDate: String?
): Page<CrashSummary> {
val query: NativeQuery = NativeQuery.builder()
.withQuery { q ->
q.bool { b ->
b.filter { f -> f.term { t -> t.field("merchantId.keyword").value(merchantId) } }
b.filter { f -> f.term { t -> t.field("projectId.keyword").value(projectId) } }
b.filter { f -> f.term { t -> t.field("osName.keyword").value(osName) } }
if (isIssueClosed != null) {
b.filter { f -> f.term { t -> t.field("isIssueClosed").value(isIssueClosed) } }
}
if (!deviceModelList.isNullOrEmpty()) {
b.filter { f ->
f.terms { t ->
t.field("devices.keyword")
.terms { terms -> terms.value(deviceModelList.map { FieldValue.of(it) }) }
}
}
}
if (!osVersionList.isNullOrEmpty()) {
b.filter { f ->
f.terms { t ->
t.field("osVersions.keyword")
.terms { terms -> terms.value(osVersionList.map { FieldValue.of(it) }) }
}
}
}
if (!appVersionList.isNullOrEmpty()) {
b.filter { f ->
f.terms { t ->
t.field("appVersions.keyword")
.terms { terms -> terms.value(appVersionList.map { FieldValue.of(it) }) }
}
}
}
if (startDate != null || endDate != null) {
val rangeQuery = RangeQuery.Builder()
.field("lastSeen")
.gte(JsonData.of(startDate))
.lte(JsonData.of(endDate))
.build()
b.filter(rangeQuery._toQuery())
}
b
}
}
.withPageable(pageable)
.build()
val searchResponse = elasticsearchOps.search(query, CrashSummary::class.java)
val contentList: List<CrashSummary> = searchResponse.map { it.content }.toList()
val totalHits = searchResponse.totalHits
return PageImpl(contentList, pageable, totalHits)
}
}
MY IMPORTS;
import co.elastic.clients.elasticsearch.ElasticsearchClient
import co.elastic.clients.elasticsearch.core.UpdateByQueryRequest
import co.elastic.clients.elasticsearch._types.Script
import co.elastic.clients.elasticsearch._types.FieldValue
import co.elastic.clients.elasticsearch._types.aggregations.TermsAggregation
import co.elastic.clients.elasticsearch._types.query_dsl.*
import co.elastic.clients.elasticsearch.core.SearchRequest
import co.elastic.clients.json.JsonData
import co.elastic.clients.json.jackson.JacksonJsonpMapper
import com.fasterxml.jackson.databind.ObjectMapper
import io.initial.logdrop.core.entity.CrashSummary
import io.initial.logdrop.core.entity.admin.CrashFilterResult
import .springframework.stereotype.Repository
import java.io.IOException
import .springframework.data.domain.PageImpl
import java.io.StringWriter
import .springframework.data.domain.Page
import .springframework.data.domain.Pageable
import .springframework.data.elasticsearch.client.elc.NativeQuery
import .springframework.data.elasticsearch.core.ElasticsearchOperations
CrashSummary Document Mapping;
...
"lastSeen": {
"type": "date",
"format": "epoch_millis"
}
...
Example Data;
{
"description": [
"java.lang.NullPointerException"
],
"firstSeen": [
"2025-03-17T16:12:46.887Z"
],
"isIssueClosed": [
false
],
"issueId": [
"1651584580"
],
"issueId.keyword": [
"1651584580"
],
"lastSeen": [
"2025-03-17T16:12:46.887Z"
]
}
Share
Improve this question
asked Mar 18 at 9:03
Ozgur BaykalOzgur Baykal
4232 silver badges13 bronze badges
1 Answer
Reset to default 0You have your types in a state. Try this
val rangeQuery = DateRangeQuery.of(r -> r
.field("lastSeen")
.gte(JsonData.of(startDate))
.lte(JsonData.of(endDate))
)._toRangeQuery()
}
b.filter(rangeQuery)
Would be easier to spot in Java.