use of org.springframework.data.elasticsearch.core.mapping.IndexCoordinates in project spring-data-elasticsearch by spring-projects.
the class ElasticsearchTemplateTests method shouldPassIndicesOptionsForGivenSearchScrollQuery.
// DATAES-671
@Test
public void shouldPassIndicesOptionsForGivenSearchScrollQuery() {
// given
long scrollTimeInMillis = 3000;
String documentId = nextIdAsString();
SampleEntity sampleEntity = SampleEntity.builder().id(documentId).message("some message").version(System.currentTimeMillis()).build();
IndexQuery idxQuery = new IndexQueryBuilder().withId(sampleEntity.getId()).withObject(sampleEntity).build();
IndexCoordinates index = IndexCoordinates.of(INDEX_1_NAME);
operations.index(idxQuery, index);
// when
NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery()).withIndicesOptions(IndicesOptions.LENIENT_EXPAND_OPEN).build();
SearchScrollHits<SampleEntity> scroll = ((AbstractElasticsearchTemplate) operations).searchScrollStart(scrollTimeInMillis, searchQuery, SampleEntity.class, index);
List<SearchHit<SampleEntity>> entities = new ArrayList<>(scroll.getSearchHits());
while (scroll.hasSearchHits()) {
scroll = ((AbstractElasticsearchTemplate) operations).searchScrollContinue(scroll.getScrollId(), scrollTimeInMillis, SampleEntity.class, index);
entities.addAll(scroll.getSearchHits());
}
// then
assertThat(entities).isNotNull();
assertThat(entities.size()).isGreaterThanOrEqualTo(1);
}
use of org.springframework.data.elasticsearch.core.mapping.IndexCoordinates in project spring-data-elasticsearch by spring-projects.
the class ElasticsearchTemplateTests method shouldIndexDocumentForSpecifiedSource.
@Test
public void shouldIndexDocumentForSpecifiedSource() {
// given
String documentSource = "{\"id\":\"2333343434\",\"type\":null,\"message\":\"some message\",\"rate\":0,\"available\":false,\"highlightedMessage\":null,\"version\":1385208779482}";
IndexQuery indexQuery = new IndexQuery();
indexQuery.setId("2333343434");
indexQuery.setSource(documentSource);
// when
IndexCoordinates index = IndexCoordinates.of(indexNameProvider.indexName());
operations.index(indexQuery, index);
NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(termQuery("id", indexQuery.getId())).build();
// then
SearchHits<SampleEntity> searchHits = operations.search(searchQuery, SampleEntity.class, index);
assertThat(searchHits).isNotNull();
assertThat(searchHits.getTotalHits()).isEqualTo(1);
assertThat(searchHits.getSearchHit(0).getContent().getId()).isEqualTo(indexQuery.getId());
}
use of org.springframework.data.elasticsearch.core.mapping.IndexCoordinates in project spring-data-elasticsearch by spring-projects.
the class AggregationIntegrationTests method before.
@BeforeEach
public void before() {
indexOperations = operations.indexOps(ArticleEntity.class);
IndexInitializer.init(indexOperations);
IndexQuery article1 = new ArticleEntityBuilder("1").title("article four").subject("computing").addAuthor(RIZWAN_IDREES).addAuthor(ARTUR_KONCZAK).addAuthor(MOHSIN_HUSEN).addAuthor(JONATHAN_YAN).score(10).buildIndex();
IndexQuery article2 = new ArticleEntityBuilder("2").title("article three").subject("computing").addAuthor(RIZWAN_IDREES).addAuthor(ARTUR_KONCZAK).addAuthor(MOHSIN_HUSEN).addPublishedYear(YEAR_2000).score(20).buildIndex();
IndexQuery article3 = new ArticleEntityBuilder("3").title("article two").subject("computing").addAuthor(RIZWAN_IDREES).addAuthor(ARTUR_KONCZAK).addPublishedYear(YEAR_2001).addPublishedYear(YEAR_2000).score(30).buildIndex();
IndexQuery article4 = new ArticleEntityBuilder("4").title("article one").subject("accounting").addAuthor(RIZWAN_IDREES).addPublishedYear(YEAR_2002).addPublishedYear(YEAR_2001).addPublishedYear(YEAR_2000).score(40).buildIndex();
IndexCoordinates index = IndexCoordinates.of(INDEX_NAME);
operations.index(article1, index);
operations.index(article2, index);
operations.index(article3, index);
operations.index(article4, index);
indexOperations.refresh();
}
use of org.springframework.data.elasticsearch.core.mapping.IndexCoordinates in project fh by assecopl.
the class GenericDtoService method listDto.
@Override
public List<LIST> listDto(QUERY query) {
BoolQueryBuilder queryBuilder = createQueryBuilderInternal(query);
NativeSearchQueryBuilder searchQueryBuilder = new NativeSearchQueryBuilder().withQuery(queryBuilder);
if (query.getFirstRow() != null && query.getSize() != null) {
Sort sort;
if (query.getSortProperty() != null) {
sort = Sort.by((query.getAscending() != null && !query.getAscending()) ? Sort.Direction.DESC : Sort.Direction.ASC, query.getSortProperty());
} else {
sort = Sort.by(Sort.Direction.ASC, "id");
}
Pageable pageable;
// bez sortowania jesli SortProperty = "0"
if (query.getSortProperty() != null && query.getSortProperty().equals("0"))
pageable = PageRequest.of(query.getFirstRow() / query.getSize(), query.getSize());
else
pageable = PageRequest.of(query.getFirstRow() / query.getSize(), query.getSize(), sort);
searchQueryBuilder = searchQueryBuilder.withPageable(pageable);
}
NativeSearchQuery searchQuery = searchQueryBuilder.build();
List<LIST> list = new ArrayList<>();
try {
IndexCoordinates indexCoordinates = elasticsearchTemplate.getIndexCoordinatesFor(listClazz);
SearchHits<LIST> res = elasticsearchTemplate.search(searchQuery, listClazz, indexCoordinates);
list = res.getSearchHits().stream().map(SearchHit::getContent).collect(Collectors.toList());
} catch (Exception e) {
log.warn("{}", ExceptionUtils.getStackTrace(e));
}
return list;
}
use of org.springframework.data.elasticsearch.core.mapping.IndexCoordinates in project spring-data-elasticsearch by spring-projects.
the class AbstractReactiveElasticsearchRepositoryQuery method execute.
private Object execute(ElasticsearchParameterAccessor parameterAccessor) {
ResultProcessor processor = queryMethod.getResultProcessor().withDynamicProjection(parameterAccessor);
Query query = createQuery(new ConvertingParameterAccessor(elasticsearchOperations.getElasticsearchConverter(), parameterAccessor));
if (queryMethod.hasAnnotatedHighlight()) {
query.setHighlightQuery(queryMethod.getAnnotatedHighlightQuery());
}
Class<?> targetType = processor.getReturnedType().getTypeToRead();
String indexName = queryMethod.getEntityInformation().getIndexName();
IndexCoordinates index = IndexCoordinates.of(indexName);
ReactiveElasticsearchQueryExecution execution = getExecution(parameterAccessor, new ResultProcessingConverter(processor));
return execution.execute(query, processor.getReturnedType().getDomainType(), targetType, index);
}
Aggregations