[英]How to return distance and all fields using elasticsearch and spring-boot (springframework.data.elasticsearch)
I found a way to get my data using elasticsearch query.我找到了一种使用 elasticsearch 查询获取数据的方法。 But my current problem is to translate it in spring boot query.
但我目前的问题是在 spring 引导查询中翻译它。
My query is the following:我的查询如下:
{
"from": 0,
"size": 10,
"query": {
"bool": {
"should": [
{
"constant_score": {
"filter": {
"term": {
"my_field": "my_value"
}
},
"boost": 10
}
},
{
"constant_score": {
"filter": {
"term": {
"my_field": "my_value"
}
},
"boost": 8
}
}
],
"filter": [
{
"terms": {
"my_array_field": [
"first_value",
"second_value"
]
}
},
{
"geo_distance": {
"distance": "10km",
"location": {
"lat": 40,
"lon": 2
}
}
}
]
}
},
"script_fields": {
"distance": {
"script": "doc['location'].arcDistance(40, 2) / 1000"
}
},
"_source": true,
"sort": [
{
"_score": "desc"
}
]
}
All documents stored in the index have a fields location
, with a type defined as a geo_point
存储在索引中的所有文档都有一个字段
location
,类型定义为geo_point
My question is:我的问题是:
How to translate the script_fields
, which is returning me the distance, using spring-boot.如何使用 spring-boot 翻译返回距离的
script_fields
。
My current code is:我当前的代码是:
BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
NativeSearchQuery nativeSearchQuery = new NativeSearchQueryBuilder()
.withQuery(boolQueryBuilder)
.withPageable(PageRequest.of(page, size))
.build();
elasticsearchOperations.search(nativeSearchQuery, MyIndexedDocument.class);
I tried to use ScriptField from here and saw an example here but didn't find any solution for now.我尝试从这里使用 ScriptField 并在这里看到了一个示例,但目前没有找到任何解决方案。
_source
field in return._source
字段。 Is it possible to precise it using NativeSearchQuery
?NativeSearchQuery
对其进行精确化?Answer as a workaround作为解决方法回答
I let the question without any validated answer in case of somebody has a better answer.如果有人有更好的答案,我会在没有任何经过验证的答案的情况下提出问题。
In case it's a question which is relevant for somebody, I finally choose to deal the distance return by using geo distance sort.如果这是一个与某人相关的问题,我最终选择使用地理距离排序来处理距离返回。
The query is something like this:查询是这样的:
{
"from": 0,
"size": 10,
"query": {
"bool": {
"should": [
{
"constant_score": {
"filter": {
"term": {
"my_field": "my_value"
}
},
"boost": 10
}
},
{
"constant_score": {
"filter": {
"term": {
"my_field": "my_value"
}
},
"boost": 8
}
}
],
"filter": [
{
"terms": {
"my_array_field": [
"first_value",
"second_value"
]
}
},
{
"geo_distance": {
"distance": "10km",
"location": {
"lat": 40,
"lon": 2
}
}
}
]
}
},
"sort" : [
{ "_score": "desc"},{
"_geo_distance": {
"location": {
"lat": 40,
"lon": 2
},
"order": "asc",
"unit": "km",
"mode": "min",
"distance_type": "arc",
"ignore_unmapped": true
}
}
]
}
So I can get my distance in return, but not as a field of content , but as a value in sortvalues所以我可以获得我的距离作为回报,但不是作为content的字段,而是作为sortvalues中的值
Using spring data elasticsearch:使用spring数据elasticsearch:
BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
ScoreSortBuilder scoreSortBuilder = new ScoreSortBuilder();
GeoDistanceSortBuilder geoDistanceSortBuilder = new GeoDistanceSortBuilder("location", Double.parseDouble(location.getLatitude()), Double.parseDouble(location.getLongitude()));
geoDistanceSortBuilder.unit(DistanceUnit.KILOMETERS);
geoDistanceSortBuilder.ignoreUnmapped(true);
geoDistanceSortBuilder.sortMode(SortMode.MIN);
geoDistanceSortBuilder.order(SortOrder.ASC);
NativeSearchQuery nativeSearchQuery = new NativeSearchQueryBuilder()
.withQuery(boolQueryBuilder)
.withPageable(PageRequest.of(page, size))
.withSort(scoreSortBuilder)
.withSort(geoDistanceSortBuilder)
.build();
elasticsearchOperations.search(nativeSearchQuery, MyIndexedDocument.class);
you can use the annotation @ScriptedField你可以使用注解@ScriptedField
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.