2016-02-19 16 views
5

@Idアノテーションを使用してエンティティ内のフィールドを定義しました。しかし、この@ IdはElastic Search Documentsの_idにマッピングされていません。 _id値は、Elastic Searchによって自動的に生成されます。Springデータを使用した弾性検索の_idフィールドの設定方法

"org.springframework.data.annotation.Id;"を使用しています。これは、@ spring-data-esでサポートされていますか?

マイエンティティ:

import org.springframework.data.annotation.Id; 
import org.springframework.data.elasticsearch.annotations.Document; 
@Document(
    indexName = "my_event_db", type = "my_event_type" 
) 
public class EventTransactionEntity { 
    @Id 
    private long event_pk; 

    public EventTransactionEntity(long event_pk) { 
     this.event_pk = event_pk; 
    } 

    private String getEventPk() { 
     return event_pk; 
    } 
} 

マイリポジトリクラス:

import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; 
import com.softwareag.pg.pgmen.events.audit.myPackage.domain.EventTransactionEntity; 
public interface EventTransactionRepository extends ElasticsearchRepository<EventTransactionEntity, Long> { 
} 

マイアプリケーションコード:

public class Application { 
    @Autowired 
    EventTransactionRepository eventTransactionRepository; 

    public void setEventTransactionRepository(EventTransactionRepository repo) 
    { 
     this.eventTransactionRepository = repo; 
    } 

    public void saveRecord(EventTransactionEntity entity) { 
     eventTransactionRepository.save(entity); 
    } 

    public void testSave() { 
     EventTransactionEntity entity = new EventTransactionEntity(12345); 
     saveRecord(entity); 
    } 
} 

ESクエリ(保存後)実行:

http://localhost:9200/my_event_db/my_event_type/_search 

期待される結果:

{ 
"hits": { 
    "total": 1, 
    "max_score": 1, 
    "hits": [ 
    { 
    "_index": "my_event_db", 
    "_type": "my_event_type", 
    "_id": "12345", 
    "_score": 1, 
    "_source": { 
     "eventPk": 12345, 
    } 
    }] 
}} 

得られた結果: "AVL7WcnOXA6CTVbl_1Yl":

{ 
"hits": { 
    "total": 1, 
    "max_score": 1, 
    "hits": [ 
    { 
    "_index": "my_event_db", 
    "_type": "my_event_type", 
    "_id": "AVL7WcnOXA6CTVbl_1Yl", 
    "_score": 1, 
    "_source": { 
     "eventPk": 12345, 
    } 
    }] 
}} 

あなたは私が私の結果になった_idは、 "_id" だった見ることができました。しかし、私は_idフィールドがeventPk値と同等であると期待します。

助けてください。ありがとう。

答えて

2

問題は、それが@Idフィールドと同じタイプでpublicである必要があり、あなたのゲッターである:

public long getEvent_pk() { 
    return event_pk; 
} 
0

あなたは

private String id; 

フィールド名IDと文字列型IDを使用している場合は、それをマッピングすることができますこれはうまくいった。文字列型IDフィールドだけが受け入れられるように見えます。

関連する問題