Search in sources :

Example 31 with Document

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

the class ElasticsearchTemplateTests method shouldDoUpsertIfDocumentDoesNotExist.

@Test
public void shouldDoUpsertIfDocumentDoesNotExist() {
    // given
    String documentId = nextIdAsString();
    org.springframework.data.elasticsearch.core.document.Document document = org.springframework.data.elasticsearch.core.document.Document.create();
    document.put("message", "test message");
    UpdateQuery updateQuery = // 
    UpdateQuery.builder(documentId).withDocument(// 
    document).withDocAsUpsert(// 
    true).build();
    // when
    operations.update(updateQuery, IndexCoordinates.of(indexNameProvider.indexName()));
    // then
    SampleEntity indexedEntity = operations.get(documentId, SampleEntity.class, IndexCoordinates.of(indexNameProvider.indexName()));
    assertThat(indexedEntity.getMessage()).isEqualTo("test message");
}
Also used : Document(org.springframework.data.elasticsearch.core.document.Document) Test(org.junit.jupiter.api.Test) SpringIntegrationTest(org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest)

Example 32 with Document

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

the class ReactiveIndexTemplate method getMapping.

@Override
public Mono<Document> getMapping() {
    IndexCoordinates indexCoordinates = getIndexCoordinates();
    GetMappingsRequest request = requestFactory.getMappingsRequest(indexCoordinates);
    return Mono.from(operations.executeWithIndicesClient(client -> client.getMapping(request))).flatMap(getMappingsResponse -> {
        Map<String, Object> source = getMappingsResponse.mappings().get(indexCoordinates.getIndexName()).getSourceAsMap();
        Document document = Document.from(source);
        return Mono.just(document);
    });
}
Also used : Document(org.springframework.data.elasticsearch.core.document.Document) GetMappingsRequest(org.elasticsearch.client.indices.GetMappingsRequest) IndexCoordinates(org.springframework.data.elasticsearch.core.mapping.IndexCoordinates)

Example 33 with Document

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

the class ResponseConverter method getIndexInformations.

// endregion
// region index informations
/**
 * get the index informations from a {@link GetIndexResponse}
 *
 * @param getIndexResponse the index response, must not be {@literal null}
 * @return list of {@link IndexInformation}s for the different indices
 */
public static List<IndexInformation> getIndexInformations(GetIndexResponse getIndexResponse) {
    Assert.notNull(getIndexResponse, "getIndexResponse must not be null");
    List<IndexInformation> indexInformationList = new ArrayList<>();
    for (String indexName : getIndexResponse.getIndices()) {
        Settings settings = settingsFromGetIndexResponse(getIndexResponse, indexName);
        Document mappings = mappingsFromGetIndexResponse(getIndexResponse, indexName);
        List<AliasData> aliases = aliasDataFromIndexResponse(getIndexResponse, indexName);
        indexInformationList.add(IndexInformation.of(indexName, settings, mappings, aliases));
    }
    return indexInformationList;
}
Also used : AliasData(org.springframework.data.elasticsearch.core.index.AliasData) ArrayList(java.util.ArrayList) Document(org.springframework.data.elasticsearch.core.document.Document) Settings(org.springframework.data.elasticsearch.core.index.Settings)

Example 34 with Document

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

the class ElasticsearchTemplateTests method toDocument.

private org.springframework.data.elasticsearch.core.document.Document toDocument(JoinField joinField) {
    org.springframework.data.elasticsearch.core.document.Document document = create();
    document.put("name", joinField.getName());
    document.put("parent", joinField.getParent());
    return document;
}
Also used : Document(org.springframework.data.elasticsearch.core.document.Document)

Example 35 with Document

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

the class ElasticsearchTemplateTests method shouldUseUpsertOnUpdate.

// DATAES-227
@Test
public void shouldUseUpsertOnUpdate() {
    // given
    Map<String, Object> doc = new HashMap<>();
    doc.put("id", "1");
    doc.put("message", "test");
    org.springframework.data.elasticsearch.core.document.Document document = org.springframework.data.elasticsearch.core.document.Document.from(doc);
    UpdateQuery updateQuery = // 
    UpdateQuery.builder("1").withDocument(// 
    document).withUpsert(// 
    document).build();
    // when
    UpdateRequest request = getRequestFactory().updateRequest(updateQuery, IndexCoordinates.of("index"));
    // then
    assertThat(request).isNotNull();
    assertThat(request.upsertRequest()).isNotNull();
}
Also used : HashMap(java.util.HashMap) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) Object(java.lang.Object) Document(org.springframework.data.elasticsearch.core.document.Document) Test(org.junit.jupiter.api.Test) SpringIntegrationTest(org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest)

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