Search in sources :

Example 41 with IndexCoordinates

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

the class CompletionIntegrationTests method shouldRetrieveEntityWithCompletion.

// DATAES-754
@Test
void shouldRetrieveEntityWithCompletion() {
    loadCompletionObjectEntities();
    IndexCoordinates index = IndexCoordinates.of("test-index-core-completion");
    operations.get("1", CompletionEntity.class, index);
}
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 42 with IndexCoordinates

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

the class CompletionIntegrationTests method loadCompletionObjectEntities.

private void loadCompletionObjectEntities() {
    List<IndexQuery> indexQueries = new ArrayList<>();
    indexQueries.add(new CompletionEntityBuilder("1").name("Rizwan Idrees").suggest(new String[] { "Rizwan Idrees" }).buildIndex());
    indexQueries.add(new CompletionEntityBuilder("2").name("Franck Marchand").suggest(new String[] { "Franck", "Marchand" }).buildIndex());
    indexQueries.add(new CompletionEntityBuilder("3").name("Mohsin Husen").suggest(new String[] { "Mohsin", "Husen" }).buildIndex());
    indexQueries.add(new CompletionEntityBuilder("4").name("Artur Konczak").suggest(new String[] { "Artur", "Konczak" }).buildIndex());
    IndexCoordinates index = IndexCoordinates.of("test-index-core-completion");
    operations.bulkIndex(indexQueries, index);
    operations.indexOps(CompletionEntity.class).refresh();
}
Also used : IndexQuery(org.springframework.data.elasticsearch.core.query.IndexQuery) ArrayList(java.util.ArrayList) IndexCoordinates(org.springframework.data.elasticsearch.core.mapping.IndexCoordinates)

Example 43 with IndexCoordinates

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

the class DynamicSettingAndMappingEntityRepositoryIntegrationTests method shouldSearchOnGivenTokenizerUsingGivenDynamicSettingsForGivenIndex.

// DATAES-64
@Test
public void shouldSearchOnGivenTokenizerUsingGivenDynamicSettingsForGivenIndex() {
    DynamicSettingAndMappingEntity dynamicSettingAndMappingEntity1 = new DynamicSettingAndMappingEntity();
    dynamicSettingAndMappingEntity1.setId(nextIdAsString());
    dynamicSettingAndMappingEntity1.setName("test-setting1");
    dynamicSettingAndMappingEntity1.setEmail("test_setting1@Test.com");
    repository.save(dynamicSettingAndMappingEntity1);
    DynamicSettingAndMappingEntity dynamicSettingAndMappingEntity2 = new DynamicSettingAndMappingEntity();
    dynamicSettingAndMappingEntity2.setId(nextIdAsString());
    dynamicSettingAndMappingEntity2.setName("test-setting2");
    dynamicSettingAndMappingEntity2.setEmail("test_setting2@Test.com");
    repository.save(dynamicSettingAndMappingEntity2);
    // use a term query to prevent the input from being analysed
    Query searchQuery = new StringQuery("{\"term\": {\"email\": \"" + dynamicSettingAndMappingEntity1.getEmail() + "\"}}\n");
    IndexCoordinates index = IndexCoordinates.of(indexNameProvider.indexName());
    SearchHits<DynamicSettingAndMappingEntity> entityList = operations.search(searchQuery, DynamicSettingAndMappingEntity.class, index);
    assertThat(entityList).hasSize(1);
    assertThat(entityList).isNotNull().hasSize(1);
    assertThat(entityList.getSearchHit(0).getContent().getEmail()).isEqualTo(dynamicSettingAndMappingEntity1.getEmail());
}
Also used : Query(org.springframework.data.elasticsearch.core.query.Query) StringQuery(org.springframework.data.elasticsearch.core.query.StringQuery) StringQuery(org.springframework.data.elasticsearch.core.query.StringQuery) IndexCoordinates(org.springframework.data.elasticsearch.core.mapping.IndexCoordinates) Test(org.junit.jupiter.api.Test) SpringIntegrationTest(org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest)

Example 44 with IndexCoordinates

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

the class MappingBuilderIntegrationTests method shouldAddStockPriceDocumentToIndex.

// DATAES-530
@Test
public void shouldAddStockPriceDocumentToIndex() {
    // Given
    IndexOperations indexOps = operations.indexOps(StockPrice.class);
    // When
    indexOps.create();
    indexOps.putMapping(StockPrice.class);
    String symbol = "AU";
    double price = 2.34;
    String id = "abc";
    IndexCoordinates index = IndexCoordinates.of("test-index-stock-mapping-builder");
    // 
    StockPrice stockPrice = new StockPrice();
    stockPrice.setId(id);
    stockPrice.setSymbol(symbol);
    stockPrice.setPrice(BigDecimal.valueOf(price));
    operations.index(buildIndex(stockPrice), index);
    operations.indexOps(StockPrice.class).refresh();
    NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery()).build();
    SearchHits<StockPrice> result = operations.search(searchQuery, StockPrice.class, index);
    // Then
    assertThat(result).hasSize(1);
    StockPrice entry = result.getSearchHit(0).getContent();
    assertThat(entry.getSymbol()).isEqualTo(symbol);
    assertThat(entry.getPrice()).isCloseTo(BigDecimal.valueOf(price), Percentage.withPercentage(0.01));
}
Also used : IndexOperations(org.springframework.data.elasticsearch.core.IndexOperations) NativeSearchQueryBuilder(org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder) IndexCoordinates(org.springframework.data.elasticsearch.core.mapping.IndexCoordinates) NativeSearchQuery(org.springframework.data.elasticsearch.core.query.NativeSearchQuery) Test(org.junit.jupiter.api.Test) SpringIntegrationTest(org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest)

Example 45 with IndexCoordinates

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

the class ElasticsearchPartQuery method countOrGetDocumentsForDelete.

@Nullable
private Object countOrGetDocumentsForDelete(CriteriaQuery query, ParametersParameterAccessor accessor) {
    Object result = null;
    Class<?> clazz = queryMethod.getEntityInformation().getJavaType();
    IndexCoordinates index = elasticsearchOperations.getIndexCoordinatesFor(clazz);
    if (queryMethod.isCollectionQuery()) {
        if (accessor.getPageable().isUnpaged()) {
            int itemCount = (int) elasticsearchOperations.count(query, clazz, index);
            query.setPageable(PageRequest.of(0, Math.max(1, itemCount)));
        } else {
            query.setPageable(accessor.getPageable());
        }
        result = elasticsearchOperations.search(query, clazz, index);
    }
    if (ClassUtils.isAssignable(Number.class, queryMethod.getReturnedObjectType())) {
        result = elasticsearchOperations.count(query, clazz, index);
    }
    return result;
}
Also used : IndexCoordinates(org.springframework.data.elasticsearch.core.mapping.IndexCoordinates) Nullable(org.springframework.lang.Nullable)

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