我在使用@Id註釋的實體中定義了一個字段。但是這個@Id沒有映射到彈性搜索文檔中的_id。 _id值由Elastic Search自動生成。如何使用彈簧數據爲彈性搜索設置_id字段
我正在使用「org.springframework.data.annotation.Id;」。這個@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,
}
}]
}}
得到的結果:
{
"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」: 「AVL7WcnOXA6CTVbl_1Yl」。但我希望_id字段等同於eventPk值。
請幫忙。謝謝。