我已经使用 ElasticSearch 的 _plugin/head 接口成功创建了一个查询。该查询旨在返回特定位置的特定设备的最新时间戳。该查询如下所示:
{
"query":{
"bool":{
"must":[
{
"term":{
"deviceevent.location.id":"1"
}
},
{
"term":{
"deviceevent.deviceId":"AHE1LDD01"
}
}
]
}
},
"from":0,
"size":1,
"sort":{
"timestamp":{
"order":"desc"
}
}
}
上面的查询按预期工作。
现在使用 Spring-Boot 和 Spring-Data-ElasticSearch,我定义了自己的ElasticSearchRepository
如下所示:
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);
}
上面的代码被破坏主要是因为我希望它返回一个DeviceEvent
,但它实际上返回了 count = 10(默认页面大小)的设备事件。结果似乎也没有按时间戳降序排列。就好像size
and order
查询的部分内容未被获取。
我在这里做错了什么?