2017-05-08 2 views
1

使用Spring Data ESLocalDateTimeパラメータに等しい私はちょうど前に船の位置を検索したい入れ子オブジェクト日付フィールドによるSpringデータESクエリ

import javax.persistence.Temporal; 
import javax.persistence.TemporalType; 
import java.time.LocalDateTime; 

public class ShipLocation { 

    private double latitude; 
    private double longitude; 

    @Temporal(TemporalType.TIMESTAMP) 
    private LocalDateTime recordedOnTime; 

    //setters and getters 
} 

か:

import org.springframework.data.annotation.Id; 
import org.springframework.data.elasticsearch.annotations.Document; 
import org.springframework.data.elasticsearch.annotations.Field; 
import org.springframework.data.elasticsearch.annotations.FieldType; 

@Document(indexName = "ship", type = "journey") 
public class ShipJourney { 

    @Id 
    private String shipId; 

    @Field(type = FieldType.Nested) 
    private List<ShipLocation> shipLocation; 

    //getters and setters 
} 

ShipLocationのように定義されたネストされたオブジェクトです:

私は、次の定義とshipという名前のインデックスを持っています。

私はこの試みた:私だけが唯一の原因shipId's一意に最終的に一つのレコードになりますどのShipJourney自体にTopを使用していたことを後で実現するために

ShipJourney findTopByShipIdAndShipLocationRecordedOnTimeLessThanOrderByShipLocationRecordedOnTimeDesc(
     String shipId, LocalDateTime recordedOnTime); 

を。

ネストされた要素の属性を属性に基づいて取得する方法を制限するにはどうすればよいですか。

+0

このレポメソッド名は非常にストーリーです。私を覚えてますか? –

答えて

0

Spring Data ElasticSearchリポジトリで可能かどうかはわかりません。 ElasticSearchTemplateをオートワイヤできる場合、クエリは次のようになります(テストされていません)。

String shipId = "A"; 
LocalDateTime recordedOnTime = LocalDateTime.now(); 
int maxShipLocations = 3; // <-- TOP 

NestedQueryBuilder findByLocation = nestedQuery("shipLocation", rangeQuery("shipLocation.recordedOnTime").lt(recordedOnTime)); 
TermQueryBuilder findById = termQuery("id", shipId); 
QueryBuilder findByIdAndLocation = QueryBuilders.boolQuery() 
     .filter(findById) 
     .filter(findByLocation.innerHit(new QueryInnerHitBuilder().setSize(maxShipLocations))); 

SearchQuery query = new NativeSearchQueryBuilder() 
     .withIndices("ship") 
     .withTypes("journey") 
     .withQuery(findByIdAndLocation) 
     .withSort(SortBuilders.fieldSort("shipLocation.recordedOnTime").order(SortOrder.DESC)) 
     .build(); 
List<ShipJourney> shipJourney = elasticSearchTemplate.queryForList(query, ShipJourney.class); 
関連する問題