Search in sources :

Example 16 with IndexCoordinates

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);
}
Also used : ArrayList(java.util.ArrayList) IndexCoordinates(org.springframework.data.elasticsearch.core.mapping.IndexCoordinates) Test(org.junit.jupiter.api.Test) SpringIntegrationTest(org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest)

Example 17 with IndexCoordinates

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());
}
Also used : IndexCoordinates(org.springframework.data.elasticsearch.core.mapping.IndexCoordinates) Test(org.junit.jupiter.api.Test) SpringIntegrationTest(org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest)

Example 18 with IndexCoordinates

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();
}
Also used : IndexQuery(org.springframework.data.elasticsearch.core.query.IndexQuery) IndexCoordinates(org.springframework.data.elasticsearch.core.mapping.IndexCoordinates) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 19 with IndexCoordinates

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;
}
Also used : Pageable(org.springframework.data.domain.Pageable) SearchHit(org.springframework.data.elasticsearch.core.SearchHit) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) Sort(org.springframework.data.domain.Sort) NativeSearchQueryBuilder(org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder) NativeSearchQuery(org.springframework.data.elasticsearch.core.query.NativeSearchQuery) IndexCoordinates(org.springframework.data.elasticsearch.core.mapping.IndexCoordinates)

Example 20 with IndexCoordinates

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);
}
Also used : Query(org.springframework.data.elasticsearch.core.query.Query) RepositoryQuery(org.springframework.data.repository.query.RepositoryQuery) ResultProcessingConverter(org.springframework.data.elasticsearch.repository.query.ReactiveElasticsearchQueryExecution.ResultProcessingConverter) ResultProcessor(org.springframework.data.repository.query.ResultProcessor) IndexCoordinates(org.springframework.data.elasticsearch.core.mapping.IndexCoordinates)

Aggregations

IndexCoordinates (org.springframework.data.elasticsearch.core.mapping.IndexCoordinates)50 Test (org.junit.jupiter.api.Test)27 SpringIntegrationTest (org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest)27 IndexQuery (org.springframework.data.elasticsearch.core.query.IndexQuery)20 NativeSearchQueryBuilder (org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder)16 Query (org.springframework.data.elasticsearch.core.query.Query)16 ArrayList (java.util.ArrayList)15 Nullable (org.springframework.lang.Nullable)11 HashMap (java.util.HashMap)10 List (java.util.List)10 Collectors (java.util.stream.Collectors)10 QueryBuilders (org.elasticsearch.index.query.QueryBuilders)10 Map (java.util.Map)9 SearchRequest (org.elasticsearch.action.search.SearchRequest)9 UpdateRequest (org.elasticsearch.action.update.UpdateRequest)9 Log (org.apache.commons.logging.Log)8 LogFactory (org.apache.commons.logging.LogFactory)8 Version (org.elasticsearch.Version)8 DocWriteResponse (org.elasticsearch.action.DocWriteResponse)8 BulkItemResponse (org.elasticsearch.action.bulk.BulkItemResponse)8