Search in sources :

Example 46 with Document

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

the class MappingElasticsearchConverterUnitTests method shouldWriteTypeHintsIfConfigured.

// #1454
@Test
@DisplayName("should write type hints if configured")
void shouldWriteTypeHintsIfConfigured() throws JSONException {
    ((SimpleElasticsearchMappingContext) mappingElasticsearchConverter.getMappingContext()).setWriteTypeHints(true);
    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" + "  \"_class\": \"org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverterUnitTests$PersonWithCars\",\n" + // 
    "  \"id\": \"42\",\n" + // 
    "  \"name\": \"Smith\",\n" + // 
    "  \"cars\": [\n" + // 
    "    {\n" + // 
    "      \"model\": \"Ford Mustang\"\n" + // 
    "    },\n" + // 
    "    {\n" + "      \"_class\": \"org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverterUnitTests$ElectricCar\",\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 47 with Document

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

the class MappingElasticsearchConverterUnitTests method readGenericList.

// DATAES-530
@Test
public void readGenericList() {
    Document source = Document.create();
    source.put("objectList", Arrays.asList(t800AsMap, gunAsMap));
    Skynet target = mappingElasticsearchConverter.read(Skynet.class, source);
    assertThat(target.getObjectList()).containsExactly(t800, gun);
}
Also used : Document(org.springframework.data.elasticsearch.core.document.Document) Test(org.junit.jupiter.api.Test)

Example 48 with Document

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

the class MappingElasticsearchConverterUnitTests method readGenericMapWithSimpleTypes.

// DATAES-795
@Test
void readGenericMapWithSimpleTypes() {
    Map<String, Object> mapWithSimpleValues = new HashMap<>();
    mapWithSimpleValues.put("int", 1);
    mapWithSimpleValues.put("string", "string");
    mapWithSimpleValues.put("boolean", true);
    Document document = Document.create();
    document.put("schemaLessObject", mapWithSimpleValues);
    SchemaLessObjectWrapper wrapper = mappingElasticsearchConverter.read(SchemaLessObjectWrapper.class, document);
    assertThat(wrapper.getSchemaLessObject()).isEqualTo(mapWithSimpleValues);
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) 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)

Example 49 with Document

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

the class MappingElasticsearchConverterUnitTests method shouldNotReadSeqNoPrimaryTermProperty.

// DATAES-799
@Test
void shouldNotReadSeqNoPrimaryTermProperty() {
    Document document = Document.create().append("seqNoPrimaryTerm", emptyMap());
    EntityWithSeqNoPrimaryTerm entity = mappingElasticsearchConverter.read(EntityWithSeqNoPrimaryTerm.class, document);
    assertThat(entity.seqNoPrimaryTerm).isNull();
}
Also used : Document(org.springframework.data.elasticsearch.core.document.Document) Test(org.junit.jupiter.api.Test)

Example 50 with Document

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

the class MappingElasticsearchConverterUnitTests method shouldWriteUsingValueConverters.

// #1945
@Test
@DisplayName("should write using ValueConverters")
void shouldWriteUsingValueConverters() throws JSONException {
    EntityWithCustomValueConverters entity = new EntityWithCustomValueConverters();
    entity.setId("42");
    entity.setFieldWithClassBasedConverter("classbased");
    entity.setFieldWithEnumBasedConverter("enumbased");
    entity.setDontConvert("Monty Python's Flying Circus");
    String expected = // 
    "{\n" + // 
    "  \"id\": \"42\",\n" + // 
    "  \"fieldWithClassBasedConverter\": \"desabssalc\",\n" + // 
    "  \"fieldWithEnumBasedConverter\": \"desabmune\",\n" + // 
    "  \"dontConvert\": \"Monty Python's Flying Circus\"\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) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

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