Search in sources :

Example 36 with IndexCoordinates

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

the class ElasticsearchTemplateTests method shouldReturnSortFieldsInSearchHits.

// DATAES-714
@Test
void shouldReturnSortFieldsInSearchHits() {
    IndexCoordinates index = IndexCoordinates.of(indexNameProvider.indexName());
    IndexOperations indexOperations = operations.indexOps(SearchHitsEntity.class);
    indexOperations.delete();
    indexOperations.createWithMapping();
    SearchHitsEntity entity = new SearchHitsEntity();
    entity.setId("1");
    entity.setNumber(1000L);
    entity.setKeyword("thousands");
    IndexQuery indexQuery = new IndexQueryBuilder().withId(entity.getId()).withObject(entity).build();
    operations.index(indexQuery, index);
    NativeSearchQuery query = // 
    new NativeSearchQueryBuilder().withQuery(// 
    matchAllQuery()).withSort(new FieldSortBuilder("keyword").order(SortOrder.ASC)).withSort(new FieldSortBuilder("number").order(SortOrder.DESC)).build();
    SearchHits<SearchHitsEntity> searchHits = operations.search(query, SearchHitsEntity.class);
    assertThat(searchHits).isNotNull();
    assertThat(searchHits.getSearchHits()).hasSize(1);
    SearchHit<SearchHitsEntity> searchHit = searchHits.getSearchHit(0);
    List<Object> sortValues = searchHit.getSortValues();
    assertThat(sortValues).hasSize(2);
    assertThat(sortValues.get(0)).isInstanceOf(String.class).isEqualTo("thousands");
    // transport client returns Long, rest client Integer
    java.lang.Object o = sortValues.get(1);
    if (o instanceof Integer) {
        Integer i = (Integer) o;
        assertThat(o).isInstanceOf(Integer.class).isEqualTo(1000);
    } else if (o instanceof Long) {
        Long l = (Long) o;
        assertThat(o).isInstanceOf(Long.class).isEqualTo(1000L);
    } else {
        fail("unexpected object type " + o);
    }
}
Also used : FieldSortBuilder(org.elasticsearch.search.sort.FieldSortBuilder) Integer(java.lang.Integer) Integer(org.springframework.data.elasticsearch.annotations.FieldType.Integer) Long(java.lang.Long) Object(java.lang.Object) Object(java.lang.Object) IndexCoordinates(org.springframework.data.elasticsearch.core.mapping.IndexCoordinates) Test(org.junit.jupiter.api.Test) SpringIntegrationTest(org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest)

Example 37 with IndexCoordinates

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

the class ElasticsearchTemplateTests method shouldTestResultsAcrossMultipleIndices.

@Test
public void shouldTestResultsAcrossMultipleIndices() {
    IndexCoordinates index1 = IndexCoordinates.of(INDEX_1_NAME);
    IndexCoordinates index2 = IndexCoordinates.of(INDEX_2_NAME);
    operations.indexOps(index1).delete();
    operations.indexOps(index2).delete();
    String documentId1 = nextIdAsString();
    SampleEntity sampleEntity1 = SampleEntity.builder().id(documentId1).message("some message").version(System.currentTimeMillis()).build();
    IndexQuery indexQuery1 = new IndexQueryBuilder().withId(sampleEntity1.getId()).withObject(sampleEntity1).build();
    String documentId2 = nextIdAsString();
    SampleEntity sampleEntity2 = SampleEntity.builder().id(documentId2).message("some test message").version(System.currentTimeMillis()).build();
    IndexQuery indexQuery2 = new IndexQueryBuilder().withId(sampleEntity2.getId()).withObject(sampleEntity2).build();
    operations.index(indexQuery1, index1);
    operations.index(indexQuery2, index2);
    NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery()).build();
    // when
    SearchHits<SampleEntity> sampleEntities = operations.search(searchQuery, SampleEntity.class, IndexCoordinates.of(INDEX_1_NAME, INDEX_2_NAME));
    // then
    assertThat(sampleEntities).hasSize(2);
}
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 38 with IndexCoordinates

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

the class ElasticsearchTemplateTests method shouldReturnDifferentEntityForMultiSearch.

// DATAES-487
@Test
public void shouldReturnDifferentEntityForMultiSearch() {
    // given
    Class<Book> clazz = Book.class;
    IndexOperations bookIndexOperations = operations.indexOps(Book.class);
    bookIndexOperations.delete();
    bookIndexOperations.create();
    indexOperations.putMapping(clazz);
    bookIndexOperations.refresh();
    IndexCoordinates bookIndex = IndexCoordinates.of("test-index-book-core-template");
    operations.index(buildIndex(SampleEntity.builder().id("1").message("ab").build()), IndexCoordinates.of(indexNameProvider.indexName()));
    operations.index(buildIndex(Book.builder().id("2").description("bc").build()), bookIndex);
    bookIndexOperations.refresh();
    // when
    List<NativeSearchQuery> queries = new ArrayList<>();
    queries.add(new NativeSearchQueryBuilder().withQuery(termQuery("message", "ab")).build());
    queries.add(new NativeSearchQueryBuilder().withQuery(termQuery("description", "bc")).build());
    List<SearchHits<?>> searchHitsList = operations.multiSearch(queries, Lists.newArrayList(SampleEntity.class, clazz), IndexCoordinates.of(indexNameProvider.indexName(), bookIndex.getIndexName()));
    // then
    SearchHits<?> searchHits0 = searchHitsList.get(0);
    assertThat(searchHits0.getTotalHits()).isEqualTo(1L);
    SearchHit<SampleEntity> searchHit0 = (SearchHit<SampleEntity>) searchHits0.getSearchHit(0);
    assertThat(searchHit0.getContent().getClass()).isEqualTo(SampleEntity.class);
    SearchHits<?> searchHits1 = searchHitsList.get(1);
    assertThat(searchHits1.getTotalHits()).isEqualTo(1L);
    SearchHit<Book> searchHit1 = (SearchHit<Book>) searchHits1.getSearchHit(0);
    assertThat(searchHit1.getContent().getClass()).isEqualTo(clazz);
}
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 39 with IndexCoordinates

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

the class ElasticsearchTemplateTests method shouldReturnHighlightFieldsInSearchHit.

// DATAES-715
@Test
void shouldReturnHighlightFieldsInSearchHit() {
    IndexCoordinates index = IndexCoordinates.of("test-index-highlight-entity-template");
    HighlightEntity entity = new HighlightEntity("1", "This message is a long text which contains the word to search for " + "in two places, the first being near the beginning and the second near the end of the message");
    IndexQuery indexQuery = new IndexQueryBuilder().withId(entity.getId()).withObject(entity).build();
    operations.index(indexQuery, index);
    operations.indexOps(index).refresh();
    NativeSearchQuery query = // 
    new NativeSearchQueryBuilder().withQuery(// 
    termQuery("message", "message")).withHighlightFields(// 
    new HighlightBuilder.Field("message")).build();
    SearchHits<HighlightEntity> searchHits = operations.search(query, HighlightEntity.class, index);
    assertThat(searchHits).isNotNull();
    assertThat(searchHits.getSearchHits()).hasSize(1);
    SearchHit<HighlightEntity> searchHit = searchHits.getSearchHit(0);
    List<String> highlightField = searchHit.getHighlightField("message");
    assertThat(highlightField).hasSize(2);
    assertThat(highlightField.get(0)).contains("<em>message</em>");
    assertThat(highlightField.get(1)).contains("<em>message</em>");
}
Also used : Field(org.springframework.data.elasticsearch.annotations.Field) ScriptedField(org.springframework.data.elasticsearch.annotations.ScriptedField) MultiField(org.springframework.data.elasticsearch.annotations.MultiField) JoinField(org.springframework.data.elasticsearch.core.join.JoinField) InnerField(org.springframework.data.elasticsearch.annotations.InnerField) IndexCoordinates(org.springframework.data.elasticsearch.core.mapping.IndexCoordinates) Test(org.junit.jupiter.api.Test) SpringIntegrationTest(org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest)

Example 40 with IndexCoordinates

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

the class ReactiveIndexOperationsTest method shouldCreateIndexOpsForIndexCoordinates.

// DATAES-678
@Test
void shouldCreateIndexOpsForIndexCoordinates() {
    IndexCoordinates indexCoordinates = IndexCoordinates.of(TESTINDEX);
    ReactiveIndexOperations indexOperations = operations.indexOps(indexCoordinates);
    assertThat(indexOperations).isNotNull();
}
Also used : IndexCoordinates(org.springframework.data.elasticsearch.core.mapping.IndexCoordinates) Test(org.junit.jupiter.api.Test) SpringIntegrationTest(org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest)

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