2016-02-19 8 views
5

Ich habe ein Feld in meiner Entität mit @Id Annotation definiert. Aber diese @Id wird nicht auf die _id in Elastic Search Documents abgebildet. Die _id-Werte werden automatisch von der elastischen Suche generiert.Wie _id-Feld für die elastische Suche mit Spring-Daten festlegen

Ich verwende "org.springframework.data.annotation.Id;". Wird diese @Id von spring-data-es unterstützt?

Meine Einheit:

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; 
    } 
} 

Mein Repository Klasse:

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

My Application Code:

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 Abfrage ausgeführt (nach Speichern):

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

Erwartetes Ergebnis:

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

Erhalten Ergebnis:

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

Sie die _id ich in meinem Ergebnis bekam sehen konnte, war, "_id": "AVL7WcnOXA6CTVbl_1Yl". Aber ich würde erwarten, dass das _id-Feld dem eventPk-Wert entspricht.

Bitte helfen. Vielen Dank.

Antwort

2

Das Problem ist in Ihrem Getter es public mit gleichem Typ wie @Id Feld sein sollte:

public long getEvent_pk() { 
    return event_pk; 
} 
0

Sie können es zuordnen, wenn Sie einen String-Typ-ID mit Feldnamen-ID verwenden

private String id; 

Das hat funktioniert. Sieht aus wie nur String-ID-Feld akzeptiert.