Mou*_*ane 3 elasticsearch elasticsearch-plugin spring-boot spring-data-elasticsearch
我已经使用ElasticSearch的_plugin / head接口成功创建了查询。该查询旨在返回特定位置的特定设备的最新时间戳。该查询如下所示:
{
"query":{
"bool":{
"must":[
{
"term":{
"deviceevent.location.id":"1"
}
},
{
"term":{
"deviceevent.deviceId":"AHE1LDD01"
}
}
]
}
},
"from":0,
"size":1,
"sort":{
"timestamp":{
"order":"desc"
}
}
}
Run Code Online (Sandbox Code Playgroud)
上面的查询按预期工作。现在使用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);
}
Run Code Online (Sandbox Code Playgroud)
上面的代码之所以中断,主要是因为我希望它返回一个DeviceEvent,但实际上它返回的是带有count = 10(默认页面大小)的设备事件。似乎时间戳也没有按降序对结果进行排序。好像查询的size和order部分没有被拾取。
我在这里做错了什么?
而不是在查询注释中控制结果大小。
使用Pageable界面,以下内容取自文档。
public interface BookRepository extends ElasticsearchRepository<Book, String> {
@Query("{"bool" : {"must" : {"field" : {"name" : "?0"}}}}")
Page<Book> findByName(String name,Pageable pageable);
}
Run Code Online (Sandbox Code Playgroud)
这将使您能够:
findByName("foo-name", new PageRequest(0,1));
Run Code Online (Sandbox Code Playgroud)
如果您还想排序:
findByName("foo-name", new PageRequest(0,1, new Sort(new Sort.Order(Sort.Direction.ASC,"name")))).getContent().get(0);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3365 次 |
| 最近记录: |