/
NativeQueryWithCriteriaQueryWithLocationBug.kt
73 lines (60 loc) · 3.28 KB
/
NativeQueryWithCriteriaQueryWithLocationBug.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package kern.elasticsearchplayground
import co.elastic.clients.elasticsearch._types.GeoLocation
import co.elastic.clients.elasticsearch._types.LatLonGeoLocation
import co.elastic.clients.elasticsearch._types.aggregations.Aggregation
import co.elastic.clients.elasticsearch._types.aggregations.TermsAggregation
import co.elastic.clients.elasticsearch._types.query_dsl.GeoDistanceQuery
import org.springframework.data.annotation.Id
import org.springframework.data.elasticsearch.annotations.Document
import org.springframework.data.elasticsearch.client.elc.NativeQueryBuilder
import org.springframework.data.elasticsearch.core.ElasticsearchOperations
import org.springframework.data.elasticsearch.core.SearchHits
import org.springframework.data.elasticsearch.core.geo.GeoPoint
import org.springframework.data.elasticsearch.core.query.Criteria
import org.springframework.data.elasticsearch.core.query.CriteriaQuery
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository
import org.springframework.stereotype.Service
import java.util.*
@Document(indexName = "with_geo_location_index", createIndex = true)
class WithGeoLocation(@Id val id: UUID, val location: GeoPoint, val aggregationId: Long)
interface WithGeoLocationRepository : ElasticsearchRepository<WithGeoLocation, UUID>
@Service
class WithGeoLocationService(val repository: WithGeoLocationRepository, val operations: ElasticsearchOperations) {
companion object {
val pointWeAreLookingFor = GeoPoint(60.0, 60.0)
val otherPoint = GeoPoint(70.0, 70.0)
}
fun seedSampleData() {
repository.saveAll(
listOf(
WithGeoLocation(UUID.randomUUID(), pointWeAreLookingFor, 1L),
WithGeoLocation(UUID.randomUUID(), pointWeAreLookingFor, 1L),
WithGeoLocation(UUID.randomUUID(), pointWeAreLookingFor, 2L),
WithGeoLocation(UUID.randomUUID(), otherPoint, 3L),
)
)
}
fun searchOnlyWithNativeQuery(): SearchHits<WithGeoLocation> {
val nativeGeoDistance = GeoDistanceQuery.Builder().field("location").distance("1km")
.location(GeoLocation.Builder().latlon(LatLonGeoLocation.Builder().lat(pointWeAreLookingFor.lat).lon(pointWeAreLookingFor.lon).build()).build())
.build()
val nq = NativeQueryBuilder().withQuery { q -> q.geoDistance(nativeGeoDistance) }.build()
return operations.search(nq, WithGeoLocation::class.java)
}
fun searchWithNativeQueryMixedWithCriteriaQueryAndGeoLocation(): SearchHits<WithGeoLocation> {
val cq = CriteriaQuery(Criteria("location").within(pointWeAreLookingFor, "1km"))
val nq = NativeQueryBuilder().withQuery(cq).build()
return operations.search(nq, WithGeoLocation::class.java)
}
fun searchWithNativeQueryMixedWithCriteriaQueryAndGeoLocationAndAggregation(): SearchHits<WithGeoLocation> {
val cq = CriteriaQuery(Criteria("location").within(pointWeAreLookingFor, "1km"))
val nq = NativeQueryBuilder()
.withQuery(cq)
.withAggregation(
"exampleAggregation",
Aggregation.of { it.terms(TermsAggregation.Builder().field("aggregationId").size(20).build()) },
)
.build()
return operations.search(nq, WithGeoLocation::class.java)
}
}