Search in sources :

Example 11 with Document

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

the class ResponseConverter method mappingsFromGetIndexResponse.

private static Document mappingsFromGetIndexResponse(org.elasticsearch.action.admin.indices.get.GetIndexResponse getIndexResponse, String indexName) {
    Document document = Document.create();
    boolean responseHasMappings = getIndexResponse.getMappings().containsKey(indexName) && (getIndexResponse.getMappings().get(indexName).get("_doc") != null);
    if (responseHasMappings) {
        MappingMetadata mappings = getIndexResponse.getMappings().get(indexName).get("_doc");
        document = Document.from(mappings.getSourceAsMap());
    }
    return document;
}
Also used : Document(org.springframework.data.elasticsearch.core.document.Document) MappingMetadata(org.elasticsearch.cluster.metadata.MappingMetadata)

Example 12 with Document

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

the class ResponseConverter method getIndexInformations.

/**
 * get the index informations from a {@link org.elasticsearch.action.admin.indices.get.GetIndexResponse} (transport
 * client)
 *
 * @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(org.elasticsearch.action.admin.indices.get.GetIndexResponse getIndexResponse) {
    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 13 with Document

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

the class ResponseConverter method mappingsFromGetIndexResponse.

/**
 * extract the mappings information from a given index
 *
 * @param getIndexResponse the elastic GetIndexResponse
 * @param indexName the index name
 * @return a document that represents {@link MappingMetadata}
 */
private static Document mappingsFromGetIndexResponse(GetIndexResponse getIndexResponse, String indexName) {
    Document document = Document.create();
    if (getIndexResponse.getMappings().containsKey(indexName)) {
        MappingMetadata mappings = getIndexResponse.getMappings().get(indexName);
        document = Document.from(mappings.getSourceAsMap());
    }
    return document;
}
Also used : Document(org.springframework.data.elasticsearch.core.document.Document) MappingMetadata(org.elasticsearch.cluster.metadata.MappingMetadata)

Example 14 with Document

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

the class ElasticsearchTemplateTests method shouldUpdateEntityWithJoinFields.

private void shouldUpdateEntityWithJoinFields(String qId1, String qId2, String aId1, String aId2) throws Exception {
    org.springframework.data.elasticsearch.core.document.Document document = org.springframework.data.elasticsearch.core.document.Document.create();
    document.put("myJoinField", toDocument(new JoinField<>("answer", qId2)));
    UpdateQuery updateQuery = // 
    UpdateQuery.builder(aId2).withDocument(// 
    document).withRouting(qId2).build();
    List<UpdateQuery> queries = new ArrayList<>();
    queries.add(updateQuery);
    // when
    operations.bulkUpdate(queries, IndexCoordinates.of(indexNameProvider.indexName()));
    SearchHits<SampleJoinEntity> updatedHits = operations.search(new NativeSearchQueryBuilder().withQuery(new ParentIdQueryBuilder("answer", qId2)).build(), SampleJoinEntity.class);
    List<String> hitIds = updatedHits.getSearchHits().stream().map(new Function<SearchHit<SampleJoinEntity>, String>() {

        @Override
        public String apply(SearchHit<SampleJoinEntity> sampleJoinEntitySearchHit) {
            return sampleJoinEntitySearchHit.getId();
        }
    }).collect(Collectors.toList());
    assertThat(hitIds.size()).isEqualTo(1);
    assertThat(hitIds.get(0)).isEqualTo(aId2);
    updatedHits = operations.search(new NativeSearchQueryBuilder().withQuery(new ParentIdQueryBuilder("answer", qId1)).build(), SampleJoinEntity.class);
    hitIds = updatedHits.getSearchHits().stream().map(new Function<SearchHit<SampleJoinEntity>, String>() {

        @Override
        public String apply(SearchHit<SampleJoinEntity> sampleJoinEntitySearchHit) {
            return sampleJoinEntitySearchHit.getId();
        }
    }).collect(Collectors.toList());
    assertThat(hitIds.size()).isEqualTo(1);
    assertThat(hitIds.get(0)).isEqualTo(aId1);
}
Also used : ParentIdQueryBuilder(org.elasticsearch.join.query.ParentIdQueryBuilder) ArrayList(java.util.ArrayList) JoinField(org.springframework.data.elasticsearch.core.join.JoinField) CombineFunction(org.elasticsearch.common.lucene.search.function.CombineFunction) Function(java.util.function.Function) Document(org.springframework.data.elasticsearch.core.document.Document)

Example 15 with Document

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

the class ElasticsearchTemplateTests method shouldDoPartialUpdateForExistingDocument.

@Test
public void shouldDoPartialUpdateForExistingDocument() {
    // given
    String documentId = nextIdAsString();
    String messageBeforeUpdate = "some test message";
    String messageAfterUpdate = "test message";
    SampleEntity sampleEntity = SampleEntity.builder().id(documentId).message(messageBeforeUpdate).version(System.currentTimeMillis()).build();
    IndexQuery indexQuery = getIndexQuery(sampleEntity);
    operations.index(indexQuery, IndexCoordinates.of(indexNameProvider.indexName()));
    org.springframework.data.elasticsearch.core.document.Document document = org.springframework.data.elasticsearch.core.document.Document.create();
    document.put("message", messageAfterUpdate);
    UpdateQuery updateQuery = // 
    UpdateQuery.builder(documentId).withDocument(// 
    document).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(messageAfterUpdate);
}
Also used : 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