Merge pull request #14 from javamachr/master

added format and pattern  support for date fieldtype + fixed nullpointer...
This commit is contained in:
akonczak
2013-10-08 16:41:36 -07:00
6 changed files with 130 additions and 3 deletions

View File

@@ -0,0 +1,73 @@
package org.springframework.data.elasticsearch;
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 java.util.Date;
import static org.springframework.data.elasticsearch.annotations.FieldIndex.not_analyzed;
import static org.springframework.data.elasticsearch.annotations.FieldType.String;
import static org.springframework.data.elasticsearch.annotations.FieldType.Date;
/**
* @author Jakub Vavrik
*/
@Document(indexName = "test-datemapping", type = "mapping", indexStoreType = "memory", shards = 1, replicas = 0, refreshInterval = "-1")
public class SampleDateMappingEntity {
@Id
private String id;
@Field(type = String, index = not_analyzed, store = true, searchAnalyzer = "standard", indexAnalyzer = "standard")
private String message;
@Field(type = Date, format = DateFormat.custom, pattern = "dd.MM.yyyy hh:mm")
private Date customFormatDate;
@Field(type = Date)
private Date defaultFormatDate;
@Field(type = Date, format = DateFormat.basic_date)
private Date basicFormatDate;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Date getCustomFormatDate() {
return customFormatDate;
}
public void setCustomFormatDate(Date customFormatDate) {
this.customFormatDate = customFormatDate;
}
public Date getDefaultFormatDate() {
return defaultFormatDate;
}
public void setDefaultFormatDate(Date defaultFormatDate) {
this.defaultFormatDate = defaultFormatDate;
}
public Date getBasicFormatDate() {
return basicFormatDate;
}
public void setBasicFormatDate(Date basicFormatDate) {
this.basicFormatDate = basicFormatDate;
}
}

View File

@@ -0,0 +1,26 @@
package org.springframework.data.elasticsearch.core;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.data.elasticsearch.SampleDateMappingEntity;
import java.beans.IntrospectionException;
import java.io.IOException;
/**
* @author Jakub Vavrik
*/
public class SimpleElasticsearchDateMappingTest {
private static final String EXPECTED_MAPPING = "{\"mapping\":{\"properties\":{\"message\":{\"store\":true," +
"\"type\":\"string\",\"index\":\"not_analyzed\",\"search_analyzer\":\"standard\",\"index_analyzer\"" +
":\"standard\"},\"customFormatDate\":{\"store\":false,\"type\":\"date\",\"format\":\"dd.MM.yyyy hh:mm\"}," +
"\"defaultFormatDate\":{\"store\":false,\"type\":\"date\"},\"basicFormatDate\":{\"store\":false,\"" +
"type\":\"date\",\"format\":\"basic_date\"}}}}";
@Test
public void testCorrectDateMappings() throws NoSuchFieldException, IntrospectionException, IOException {
XContentBuilder xContentBuilder = MappingBuilder.buildMapping(SampleDateMappingEntity.class, "mapping", "id");
Assert.assertEquals(EXPECTED_MAPPING, xContentBuilder.string());
}
}