2016-02-19 18 views
5

Mam zdefiniowane pole w mojej jednostce przy użyciu adnotacji @Id. Ale ten @ID nie jest mapowany do _id w Elastic Search Documents. Wartości _id są generowane automatycznie przez Elastic Search.Jak ustawić pole _id dla wyszukiwania elastycznego za pomocą danych sprężystych

Używam "org.springframework.data.annotation.Id;". Czy ten @ID jest obsługiwany przez wiosenne dane?

Moja jednostka:

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

moim repozytorium Klasa:

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

Moje kod aplikacji:

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 Zapytanie wykonywane (Po Save):

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

Oczekiwany wynik:

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

Otrzymany wynik:

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

Można zobaczyć _id mam w rezultacie było "_id": "AVL7WcnOXA6CTVbl_1Yl". Ale oczekiwałbym, że pole _id będzie równoznaczne z wartością eventPk.

Proszę o pomoc. Dzięki.

Odpowiedz

2

Problem jest w getter powinno być public z tego samego rodzaju jak @Id polu:

public long getEvent_pk() { 
    return event_pk; 
} 
0

Można mapować go jeśli używasz id typu String o nazwie pole id

private String id; 

To zadziałało. Wygląda na to, że akceptowane jest tylko pole typu ciąg znaków.