我正在使用以下查询:
NativeSearchQuery nsq = new NativeSearchQueryBuilder()
.withQuery(qb)
.withPageable(PageRequest.of(page, PAGE_SIZE))
.withSort(sb)
.build();
使用以下SortBuilder:
SortBuilders.geoDistanceSort("customer.address.geoLocation",
customer.getAddress().getGeoLocation().toGeoPoint())
.order(SortOrder.ASC)
.unit(DistanceUnit.KILOMETERS);
正在生成所需的排序查询:
"sort": [
{
"_geo_distance" : {
"customer.address.geoLocation" : [
{
"lat" : 40.4221663,
"lon" : -3.7148336
}
],
"unit" : "km",
"distance_type" : "arc",
"order" : "asc",
"validation_method" : "STRICT",
"ignore_unmapped" : false
}
}
]
通常每个结果文档都将距离返回到以km为单位的参考参数:
"sort": [2.4670609224864997]
我需要将此值作为域的一部分,但是我无法将其推入对象。我尝试了一种简单的方法,将其定义为float[]
进入我的域,但我一直为空。我正在使用Jackson进行(反序列化)。
private float[] sort;
public float[] getSort() {
return this.sort;
}
public void setSort(float[] score) {
this.sort = score;
}
我的存储库:
public interface ElasticsearchProductRepository
extends ElasticsearchRepository<Product, String> {
Page<Product> search(SearchQuery searchQuery);
}
产品:
@Entity
@Table(name = "product")
@Document(indexName = "product", createIndex = true, type = "_doc")
@Setting(settingPath = "elasticsearch/product-index.json")
@DynamicTemplates(mappingPath = "elasticsearch/product-dynamic-templates.json")
public class Product {
...
@org.springframework.data.annotation.Id
@javax.persistence.Id
@GeneratedValue(strategy = GenerationType.AUTO)
@JsonView(ResponseView.class)
private long id;
@Field(analyzer = "autocomplete", type = FieldType.Text, searchAnalyzer="standard")
@JsonView(CreationRequestView.class)
private String productName;
private float[] sort;
...
public float[] getSort() {
return this.sort;
}
public void setSort(float[] score) {
this.sort = score;
}
}
sort不属于此,而是针对每个搜索命中返回的一部分附加信息。
对于下一个版本(4.0),我们目前正在重写如何处理返回的匹配。下一个版本将具有SearchHit<T>
类,其中T
是实体域类,并且SearchHit
对象包含该实体以及其他信息,例如得分,突出显示,脚本字段,排序等。计划中的存储库方法然后可以例如返回这些SearchHit
对象的列表或页面。但是目前,查询中的排序将导致排序返回,但是您无法自己检索排序值-在您的情况下这很不好,因为它们是动态计算的。