Quando si utilizzano i dati di primavera per inserire il documento Elasticsearch con Tipo di data, non riesco a ottenere il formato della data corretto, il formato della data è sempre Long.ElasticSearch Spring Data Il formato della data è sempre lungo
Ecco il codice Java: Entity.java
import java.util.Date;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.DateFormat;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldIndex;
import org.springframework.data.elasticsearch.annotations.FieldType;
import com.fasterxml.jackson.annotation.JsonProperty;
@Document(indexName = "entity-index", type = "entity-type")
public class Entity {
@Id
private String id;
@Field(type = FieldType.Date, index = FieldIndex.not_analyzed, store = true,
format = DateFormat.custom, pattern = "yyyy-MM-dd'T'hh:mm:ss.SSS'Z'")
private Date createDate;
private String system;
private double score;
@Field(type = FieldType.Date, format = DateFormat.date_optional_time)
@JsonProperty(value = "@timestamp")
private Date updateDate;
// omit setter and getter
}
Ecco la prova
public class EntityDAOTest {
@Autowired
private ElasticsearchTemplate template;
@Before
public void init() {
template.createIndex(Entity.class);
template.putMapping(Entity.class);
}
@Test
public void testCreate() {
Entity entity = new Entity();
entity.setId("5");
entity.setCreateDate(new DateTime(2015,05,27,0,0).toDate());
entity.setUpdateDate(new DateTime(2015,05,27,0,0).toDate());
entity.setSystem("systemC");
entity.setScore(5.7);
IndexQuery query = new IndexQueryBuilder().withObject(entity).withId(entity.getId()).build();
template.index(query);
}
posso ottenere la mappatura del soggetto costituito:
{
"entity-index": {
"mappings": {
"entity-type": {
"properties": {
"@timestamp": {
"type": "long"
},
"createDate": {
"type": "date",
"store": true,
"format": "yyyy-MM-dd'T'hh:mm:ss.SSS'Z'"
},
"id": {
"type": "string"
},
"score": {
"type": "double"
},
"system": {
"type": "string"
},
"updateDate": {
"type": "date",
"format": "date_optional_time"
}
}
}
}
}
}
Tuttavia, quando lo cerco curl -X GET /entity-index/_search
, ottengo il seguente documento:
{
"id": "5",
"createDate": 1432656000000,
"system": "systemC",
"score": 5.7,
"@timestamp": 1432656000000
}
e i campi data sono tutti di tipo lungo, come posso ottenere il formato della data: "2015-08-17T12: 00: 00.000"?
Si prega di provare con un formato data corretto 'aaaa-MM-gg'T'HH: mm: ss.SSSZZ', cioè le ore devono essere in maiuscolo e non devono essere raddoppiate le zecche intorno al fuso orario' Z' e 'Z' . Puoi anche semplicemente usare il formato 'date_time'. Tieni presente che è necessario eliminare prima il tuo indice per verificare questa modifica. – Val
Grazie per la tua risposta, ho provato il tuo suggerimento, eliminare l'indice, modificare il modello, ma ancora mostrare a lungo come timestamp – fudy
In realtà, la mappatura è stata creata correttamente. Il problema è più probabile che provenga dal serializzatore Jackson JSON. Dovresti provare ad aggiungere questa annotazione ai tuoi campi data: '@JsonFormat (shape = JsonFormat.Shape.STRING, pattern =" yyyy-MM-dd'T'HH: mm: ss.SSSZZ ")'. Vedi anche alcune [soluzioni alternative] (http://www.baeldung.com/jackson-serialize-dates) che potrebbero adattarsi meglio al tuo caso. – Val