Search in sources :

Example 41 with Document

use of org.springframework.data.elasticsearch.core.document.Document in project spring-data-elasticsearch by spring-projects.

the class MappingElasticsearchConverterUnitTests method shouldReadListOfLocalDate.

// DATAES-924
@Test
@DisplayName("should read list of LocalDate")
void shouldReadListOfLocalDate() {
    Document document = Document.create();
    document.put("id", "4711");
    document.put("dates", new String[] { "15.09.2020", "01.05.2019" });
    LocalDatesEntity entity = mappingElasticsearchConverter.read(LocalDatesEntity.class, document);
    assertThat(entity.getId()).isEqualTo("4711");
    assertThat(entity.getDates()).hasSize(2).containsExactly(LocalDate.of(2020, 9, 15), LocalDate.of(2019, 5, 1));
}
Also used : Document(org.springframework.data.elasticsearch.core.document.Document) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 42 with Document

use of org.springframework.data.elasticsearch.core.document.Document in project spring-data-elasticsearch by spring-projects.

the class MappingElasticsearchConverterUnitTests method shouldReadUsingValueConverters.

// #1945
@Test
@DisplayName("should read using ValueConverters")
void shouldReadUsingValueConverters() throws JSONException {
    String json = // 
    "{\n" + // 
    "  \"id\": \"42\",\n" + // 
    "  \"fieldWithClassBasedConverter\": \"desabssalc\",\n" + // 
    "  \"fieldWithEnumBasedConverter\": \"desabmune\",\n" + // 
    "  \"dontConvert\": \"Monty Python's Flying Circus\"\n" + // 
    "}\n";
    Document source = Document.parse(json);
    // when
    EntityWithCustomValueConverters entity = mappingElasticsearchConverter.read(EntityWithCustomValueConverters.class, source);
    assertThat(entity.getId()).isEqualTo("42");
    assertThat(entity.getFieldWithClassBasedConverter()).isEqualTo("classbased");
    assertThat(entity.getFieldWithEnumBasedConverter()).isEqualTo("enumbased");
    assertThat(entity.getDontConvert()).isEqualTo("Monty Python's Flying Circus");
}
Also used : GeoJsonLineString(org.springframework.data.elasticsearch.core.geo.GeoJsonLineString) GeoJsonMultiLineString(org.springframework.data.elasticsearch.core.geo.GeoJsonMultiLineString) Document(org.springframework.data.elasticsearch.core.document.Document) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 43 with Document

use of org.springframework.data.elasticsearch.core.document.Document in project spring-data-elasticsearch by spring-projects.

the class MappingElasticsearchConverterUnitTests method readGenericMap.

// DATAES-530
@Test
public void readGenericMap() {
    Document source = Document.create();
    source.put("objectMap", Collections.singletonMap("glock19", gunAsMap));
    Skynet target = mappingElasticsearchConverter.read(Skynet.class, source);
    assertThat(target.getObjectMap()).containsEntry("glock19", gun);
}
Also used : Document(org.springframework.data.elasticsearch.core.document.Document) Test(org.junit.jupiter.api.Test)

Example 44 with Document

use of org.springframework.data.elasticsearch.core.document.Document in project spring-data-elasticsearch by spring-projects.

the class MappingElasticsearchConverterUnitTests method shouldNotWriteTypeHintsIfNotConfigured.

// #1454
@Test
@DisplayName("should not write type hints if configured")
void shouldNotWriteTypeHintsIfNotConfigured() throws JSONException {
    ((SimpleElasticsearchMappingContext) mappingElasticsearchConverter.getMappingContext()).setWriteTypeHints(false);
    PersonWithCars person = new PersonWithCars();
    person.setId("42");
    person.setName("Smith");
    Car car1 = new Car();
    car1.setModel("Ford Mustang");
    Car car2 = new ElectricCar();
    car2.setModel("Porsche Taycan");
    person.setCars(Arrays.asList(car1, car2));
    String expected = // 
    "{\n" + // 
    "  \"id\": \"42\",\n" + // 
    "  \"name\": \"Smith\",\n" + // 
    "  \"cars\": [\n" + // 
    "    {\n" + // 
    "      \"model\": \"Ford Mustang\"\n" + // 
    "    },\n" + // 
    "    {\n" + // 
    "      \"model\": \"Porsche Taycan\"\n" + // 
    "    }\n" + // 
    "  ]\n" + // 
    "}\n";
    Document document = Document.create();
    mappingElasticsearchConverter.write(person, document);
    assertEquals(expected, document.toJson(), true);
}
Also used : SimpleElasticsearchMappingContext(org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext) GeoJsonLineString(org.springframework.data.elasticsearch.core.geo.GeoJsonLineString) GeoJsonMultiLineString(org.springframework.data.elasticsearch.core.geo.GeoJsonMultiLineString) Document(org.springframework.data.elasticsearch.core.document.Document) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 45 with Document

use of org.springframework.data.elasticsearch.core.document.Document in project spring-data-elasticsearch by spring-projects.

the class MappingElasticsearchConverterUnitTests method shouldWriteEntityWithMapAsObject.

// DATAES-865
@Test
void shouldWriteEntityWithMapAsObject() throws JSONException {
    Map<String, Object> map = new LinkedHashMap<>();
    map.put("foo", "bar");
    EntityWithObject entity = new EntityWithObject();
    entity.setId("42");
    entity.setContent(map);
    String expected = // 
    "{\n" + // 
    "  \"id\": \"42\",\n" + // 
    "  \"content\": {\n" + // 
    "    \"foo\": \"bar\"\n" + // 
    "  }\n" + // 
    "}\n";
    Document document = Document.create();
    mappingElasticsearchConverter.write(entity, document);
    assertEquals(expected, document.toJson(), false);
}
Also used : GeoJsonLineString(org.springframework.data.elasticsearch.core.geo.GeoJsonLineString) GeoJsonMultiLineString(org.springframework.data.elasticsearch.core.geo.GeoJsonMultiLineString) Document(org.springframework.data.elasticsearch.core.document.Document) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.jupiter.api.Test)

Aggregations

Document (org.springframework.data.elasticsearch.core.document.Document)58 Test (org.junit.jupiter.api.Test)48 GeoJsonLineString (org.springframework.data.elasticsearch.core.geo.GeoJsonLineString)15 GeoJsonMultiLineString (org.springframework.data.elasticsearch.core.geo.GeoJsonMultiLineString)15 DisplayName (org.junit.jupiter.api.DisplayName)7 HashMap (java.util.HashMap)6 ArrayList (java.util.ArrayList)5 LinkedHashMap (java.util.LinkedHashMap)5 SpringIntegrationTest (org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest)5 GetResult (org.elasticsearch.index.get.GetResult)4 SearchDocument (org.springframework.data.elasticsearch.core.document.SearchDocument)4 Object (java.lang.Object)2 GetResponse (org.elasticsearch.action.get.GetResponse)2 UpdateRequest (org.elasticsearch.action.update.UpdateRequest)2 MappingMetadata (org.elasticsearch.cluster.metadata.MappingMetadata)2 BytesArray (org.elasticsearch.common.bytes.BytesArray)2 DocumentField (org.elasticsearch.common.document.DocumentField)2 GeoPoint (org.springframework.data.elasticsearch.core.geo.GeoPoint)2 AliasData (org.springframework.data.elasticsearch.core.index.AliasData)2 Settings (org.springframework.data.elasticsearch.core.index.Settings)2