use of org.springframework.data.elasticsearch.core.mapping.IndexCoordinates in project spring-data-elasticsearch by spring-projects.
the class ElasticsearchRestTemplate method multiSearch.
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public List<SearchHits<?>> multiSearch(List<? extends Query> queries, List<Class<?>> classes) {
Assert.notNull(queries, "queries must not be null");
Assert.notNull(classes, "classes must not be null");
Assert.isTrue(queries.size() == classes.size(), "queries and classes must have the same size");
MultiSearchRequest request = new MultiSearchRequest();
Iterator<Class<?>> it = classes.iterator();
for (Query query : queries) {
Class<?> clazz = it.next();
request.add(requestFactory.searchRequest(query, clazz, getIndexCoordinatesFor(clazz)));
}
MultiSearchResponse.Item[] items = getMultiSearchResult(request);
List<SearchHits<?>> res = new ArrayList<>(queries.size());
Iterator<Class<?>> it1 = classes.iterator();
for (int i = 0; i < queries.size(); i++) {
Class entityClass = it1.next();
IndexCoordinates index = getIndexCoordinatesFor(entityClass);
ReadDocumentCallback<?> documentCallback = new ReadDocumentCallback<>(elasticsearchConverter, entityClass, index);
SearchDocumentResponseCallback<SearchHits<?>> callback = new ReadSearchDocumentResponseCallback<>(entityClass, index);
SearchResponse response = items[i].getResponse();
res.add(callback.doWith(SearchDocumentResponse.from(response, getEntityCreator(documentCallback))));
}
return res;
}
use of org.springframework.data.elasticsearch.core.mapping.IndexCoordinates in project spring-data-elasticsearch by spring-projects.
the class ElasticsearchRestTemplate method doBulkOperation.
public List<IndexedObjectInformation> doBulkOperation(List<?> queries, BulkOptions bulkOptions, IndexCoordinates index) {
BulkRequest bulkRequest = prepareWriteRequest(requestFactory.bulkRequest(queries, bulkOptions, index));
List<IndexedObjectInformation> indexedObjectInformationList = checkForBulkOperationFailure(execute(client -> client.bulk(bulkRequest, RequestOptions.DEFAULT)));
updateIndexedObjectsWithQueries(queries, indexedObjectInformationList);
return indexedObjectInformationList;
}
use of org.springframework.data.elasticsearch.core.mapping.IndexCoordinates in project spring-data-elasticsearch by spring-projects.
the class ElasticsearchTemplateTests method shouldSaveEntityWithJoinFields.
// #1218
private void shouldSaveEntityWithJoinFields(String qId1, String qId2, String aId1, String aId2) throws Exception {
SampleJoinEntity sampleQuestionEntity1 = new SampleJoinEntity();
sampleQuestionEntity1.setUuid(qId1);
sampleQuestionEntity1.setText("This is a question");
JoinField<String> myQJoinField1 = new JoinField<>("question");
sampleQuestionEntity1.setMyJoinField(myQJoinField1);
SampleJoinEntity sampleQuestionEntity2 = new SampleJoinEntity();
sampleQuestionEntity2.setUuid(qId2);
sampleQuestionEntity2.setText("This is another question");
JoinField<String> myQJoinField2 = new JoinField<>("question");
sampleQuestionEntity2.setMyJoinField(myQJoinField2);
SampleJoinEntity sampleAnswerEntity1 = new SampleJoinEntity();
sampleAnswerEntity1.setUuid(aId1);
sampleAnswerEntity1.setText("This is an answer");
JoinField<String> myAJoinField1 = new JoinField<>("answer");
myAJoinField1.setParent(qId1);
sampleAnswerEntity1.setMyJoinField(myAJoinField1);
SampleJoinEntity sampleAnswerEntity2 = new SampleJoinEntity();
sampleAnswerEntity2.setUuid(aId2);
sampleAnswerEntity2.setText("This is another answer");
JoinField<String> myAJoinField2 = new JoinField<>("answer");
myAJoinField2.setParent(qId1);
sampleAnswerEntity2.setMyJoinField(myAJoinField2);
IndexCoordinates index = IndexCoordinates.of(indexNameProvider.indexName());
operations.save(Arrays.asList(sampleQuestionEntity1, sampleQuestionEntity2, sampleAnswerEntity1, sampleAnswerEntity2), index);
SearchHits<SampleJoinEntity> hits = operations.search(new NativeSearchQueryBuilder().withQuery(new ParentIdQueryBuilder("answer", qId1)).build(), SampleJoinEntity.class);
List<String> hitIds = hits.getSearchHits().stream().map(sampleJoinEntitySearchHit -> sampleJoinEntitySearchHit.getId()).collect(Collectors.toList());
assertThat(hitIds.size()).isEqualTo(2);
assertThat(hitIds.containsAll(Arrays.asList(aId1, aId2))).isTrue();
hits.forEach(searchHit -> {
assertThat(searchHit.getRouting()).isEqualTo(qId1);
});
}
use of org.springframework.data.elasticsearch.core.mapping.IndexCoordinates in project spring-data-elasticsearch by spring-projects.
the class ElasticsearchTemplateTests method shouldRunRescoreQueryInSearchQuery.
// #1686
@Test
void shouldRunRescoreQueryInSearchQuery() {
IndexCoordinates index = IndexCoordinates.of("test-index-rescore-entity-template");
// matches main query better
SampleEntity entity = //
SampleEntity.builder().id(//
"1").message(//
"some message").rate(java.lang.Integer.MAX_VALUE).version(//
System.currentTimeMillis()).build();
// high score from rescore query
SampleEntity entity2 = //
SampleEntity.builder().id(//
"2").message(//
"nothing").rate(1).version(//
System.currentTimeMillis()).build();
List<IndexQuery> indexQueries = getIndexQueries(Arrays.asList(entity, entity2));
operations.bulkIndex(indexQueries, index);
NativeSearchQuery query = //
new NativeSearchQueryBuilder().withQuery(//
boolQuery().filter(existsQuery("rate")).should(termQuery("message", "message"))).withRescorerQuery(new RescorerQuery(new NativeSearchQueryBuilder().withQuery(QueryBuilders.functionScoreQuery(new FunctionScoreQueryBuilder.FilterFunctionBuilder[] { new FilterFunctionBuilder(new GaussDecayFunctionBuilder("rate", 0, 10, null, 0.5).setWeight(1f)), new FilterFunctionBuilder(new GaussDecayFunctionBuilder("rate", 0, 10, null, 0.5).setWeight(100f)) }).scoreMode(FunctionScoreQuery.ScoreMode.SUM).maxBoost(80f).boostMode(CombineFunction.REPLACE)).build()).withScoreMode(ScoreMode.Max).withWindowSize(100)).build();
SearchHits<SampleEntity> searchHits = operations.search(query, SampleEntity.class, index);
assertThat(searchHits).isNotNull();
assertThat(searchHits.getSearchHits()).hasSize(2);
SearchHit<SampleEntity> searchHit = searchHits.getSearchHit(0);
assertThat(searchHit.getContent().getMessage()).isEqualTo("nothing");
// score capped to 80
assertThat(searchHit.getScore()).isEqualTo(80f);
}
use of org.springframework.data.elasticsearch.core.mapping.IndexCoordinates in project spring-data-elasticsearch by spring-projects.
the class ElasticsearchTemplateTests method shouldComposeObjectsReturnedFromHeterogeneousIndexes.
@Test
public /*
* This is basically a demonstration to show composing entities out of heterogeneous indexes.
*/
void shouldComposeObjectsReturnedFromHeterogeneousIndexes() {
IndexCoordinates index1 = IndexCoordinates.of(INDEX_1_NAME);
IndexCoordinates index2 = IndexCoordinates.of(INDEX_2_NAME);
operations.indexOps(index1).delete();
operations.indexOps(index2).delete();
HetroEntity1 entity1 = new HetroEntity1(nextIdAsString(), "aFirstName");
HetroEntity2 entity2 = new HetroEntity2(nextIdAsString(), "aLastName");
IndexQuery indexQuery1 = new IndexQueryBuilder().withId(entity1.getId()).withObject(entity1).build();
IndexQuery indexQuery2 = new IndexQueryBuilder().withId(entity2.getId()).withObject(entity2).build();
operations.index(indexQuery1, index1);
operations.index(indexQuery2, index2);
// when
NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery()).build();
SearchHits<ResultAggregator> page = operations.search(searchQuery, ResultAggregator.class, IndexCoordinates.of(INDEX_1_NAME, INDEX_2_NAME));
assertThat(page.getTotalHits()).isEqualTo(2);
}
Aggregations