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;
}
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;
}
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;
}
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);
}
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);
}
Aggregations