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);
}
}
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);
}
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);
}
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>");
}
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();
}
Aggregations