Search in sources :

Example 46 with IndexCoordinates

use of org.springframework.data.elasticsearch.core.mapping.IndexCoordinates in project spring-data-elasticsearch by spring-projects.

the class ElasticsearchStringQuery method execute.

@Override
public Object execute(Object[] parameters) {
    Class<?> clazz = queryMethod.getResultProcessor().getReturnedType().getDomainType();
    ParametersParameterAccessor accessor = new ParametersParameterAccessor(queryMethod.getParameters(), parameters);
    StringQuery stringQuery = createQuery(accessor);
    Assert.notNull(stringQuery, "unsupported query");
    if (queryMethod.hasAnnotatedHighlight()) {
        stringQuery.setHighlightQuery(queryMethod.getAnnotatedHighlightQuery());
    }
    IndexCoordinates index = elasticsearchOperations.getIndexCoordinatesFor(clazz);
    Object result = null;
    if (isCountQuery()) {
        result = elasticsearchOperations.count(stringQuery, clazz, index);
    } else if (queryMethod.isPageQuery()) {
        stringQuery.setPageable(accessor.getPageable());
        SearchHits<?> searchHits = elasticsearchOperations.search(stringQuery, clazz, index);
        if (queryMethod.isSearchPageMethod()) {
            result = SearchHitSupport.searchPageFor(searchHits, stringQuery.getPageable());
        } else {
            result = SearchHitSupport.unwrapSearchHits(SearchHitSupport.searchPageFor(searchHits, stringQuery.getPageable()));
        }
    } else if (queryMethod.isStreamQuery()) {
        stringQuery.setPageable(accessor.getPageable().isPaged() ? accessor.getPageable() : PageRequest.of(0, DEFAULT_STREAM_BATCH_SIZE));
        result = StreamUtils.createStreamFromIterator(elasticsearchOperations.searchForStream(stringQuery, clazz, index));
    } else if (queryMethod.isCollectionQuery()) {
        stringQuery.setPageable(accessor.getPageable().isPaged() ? accessor.getPageable() : Pageable.unpaged());
        result = elasticsearchOperations.search(stringQuery, clazz, index);
    } else {
        result = elasticsearchOperations.searchOne(stringQuery, clazz, index);
    }
    return (queryMethod.isNotSearchHitMethod() && queryMethod.isNotSearchPageMethod()) ? SearchHitSupport.unwrapSearchHits(result) : result;
}
Also used : ParametersParameterAccessor(org.springframework.data.repository.query.ParametersParameterAccessor) StringQuery(org.springframework.data.elasticsearch.core.query.StringQuery) SearchHits(org.springframework.data.elasticsearch.core.SearchHits) IndexCoordinates(org.springframework.data.elasticsearch.core.mapping.IndexCoordinates)

Example 47 with IndexCoordinates

use of org.springframework.data.elasticsearch.core.mapping.IndexCoordinates in project spring-data-elasticsearch by spring-projects.

the class NestedObjectTests method shouldIndexInitialLevelNestedObject.

@Test
public void shouldIndexInitialLevelNestedObject() {
    List<Car> cars = new ArrayList<>();
    Car saturn = new Car();
    saturn.setName("Saturn");
    saturn.setModel("SL");
    Car subaru = new Car();
    subaru.setName("Subaru");
    subaru.setModel("Imprezza");
    Car ford = new Car();
    ford.setName("Ford");
    ford.setModel("Focus");
    cars.add(saturn);
    cars.add(subaru);
    cars.add(ford);
    Person foo = new Person();
    foo.setName("Foo");
    foo.setId("1");
    foo.setCar(cars);
    Car car = new Car();
    car.setName("Saturn");
    car.setModel("Imprezza");
    Person bar = new Person();
    bar.setId("2");
    bar.setName("Bar");
    bar.setCar(Collections.singletonList(car));
    List<IndexQuery> indexQueries = new ArrayList<>();
    IndexQuery indexQuery1 = new IndexQuery();
    indexQuery1.setId(foo.getId());
    indexQuery1.setObject(foo);
    IndexQuery indexQuery2 = new IndexQuery();
    indexQuery2.setId(bar.getId());
    indexQuery2.setObject(bar);
    indexQueries.add(indexQuery1);
    indexQueries.add(indexQuery2);
    IndexCoordinates index = IndexCoordinates.of("test-index-person");
    operations.bulkIndex(indexQueries, index);
    operations.indexOps(Person.class).refresh();
    QueryBuilder builder = nestedQuery("car", boolQuery().must(termQuery("car.name", "saturn")).must(termQuery("car.model", "imprezza")), ScoreMode.None);
    Query searchQuery = new NativeSearchQueryBuilder().withQuery(builder).build();
    SearchHits<Person> persons = operations.search(searchQuery, Person.class, index);
    assertThat(persons).hasSize(1);
}
Also used : IndexQuery(org.springframework.data.elasticsearch.core.query.IndexQuery) Query(org.springframework.data.elasticsearch.core.query.Query) IndexQuery(org.springframework.data.elasticsearch.core.query.IndexQuery) NativeSearchQuery(org.springframework.data.elasticsearch.core.query.NativeSearchQuery) ArrayList(java.util.ArrayList) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) NativeSearchQueryBuilder(org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) NativeSearchQueryBuilder(org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder) IndexCoordinates(org.springframework.data.elasticsearch.core.mapping.IndexCoordinates) Test(org.junit.jupiter.api.Test) SpringIntegrationTest(org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest)

Example 48 with IndexCoordinates

use of org.springframework.data.elasticsearch.core.mapping.IndexCoordinates in project spring-data-elasticsearch by spring-projects.

the class SimpleElasticsearchRepository method saveAll.

@Override
public <S extends T> Iterable<S> saveAll(Iterable<S> entities) {
    Assert.notNull(entities, "Cannot insert 'null' as a List.");
    IndexCoordinates indexCoordinates = getIndexCoordinates();
    executeAndRefresh(operations -> operations.save(entities, indexCoordinates));
    return entities;
}
Also used : IndexCoordinates(org.springframework.data.elasticsearch.core.mapping.IndexCoordinates)

Example 49 with IndexCoordinates

use of org.springframework.data.elasticsearch.core.mapping.IndexCoordinates in project fh by assecopl.

the class GenericDtoService method reindexPage.

public Slice<ENTITY> reindexPage(Pageable pageable, Class dtoClass, String indexName, String indexType) {
    Slice<ENTITY> slice = getJpaRepository().findAll(pageable);
    List<DTO> dtoList = mapEntityToDtoForBulkReindex(slice.getContent());
    List<IndexQuery> queries = new ArrayList<>();
    for (DTO dto : dtoList) {
        IndexQuery indexQuery = new IndexQuery();
        indexQuery.setId(dto.getId().toString());
        indexQuery.setObject(dto);
        // if (StringUtils.isNotBlank(indexType))
        // indexQuery.setType(getDtoEsIndexType(dtoClass));
        queries.add(indexQuery);
    }
    IndexCoordinates indexCoordinates = elasticsearchTemplate.getIndexCoordinatesFor(listClazz);
    elasticsearchTemplate.bulkIndex(queries, indexCoordinates);
    queries.clear();
    return slice;
}
Also used : IndexQuery(org.springframework.data.elasticsearch.core.query.IndexQuery) IndexCoordinates(org.springframework.data.elasticsearch.core.mapping.IndexCoordinates)

Example 50 with IndexCoordinates

use of org.springframework.data.elasticsearch.core.mapping.IndexCoordinates in project fh by assecopl.

the class GenericDtoService method listCount.

@Override
public Long listCount(QUERY query) {
    BoolQueryBuilder queryBuilder = createQueryBuilderInternal(query);
    NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(queryBuilder).build();
    IndexCoordinates indexCoordinates = elasticsearchTemplate.getIndexCoordinatesFor(listClazz);
    return elasticsearchTemplate.count(searchQuery, indexCoordinates);
}
Also used : BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) NativeSearchQueryBuilder(org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder) NativeSearchQuery(org.springframework.data.elasticsearch.core.query.NativeSearchQuery) 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