Example usage for org.springframework.data.domain Page getTotalElements

List of usage examples for org.springframework.data.domain Page getTotalElements

Introduction

In this page you can find the example usage for org.springframework.data.domain Page getTotalElements.

Prototype

long getTotalElements();

Source Link

Document

Returns the total amount of elements.

Usage

From source file:org.springframework.data.elasticsearch.repositories.CustomMethodRepositoryTests.java

@Test
public void shouldReturnPageableResultsWithQueryAnnotationExpectedPageSize() {
    // given//w ww  . j a  v a  2  s.c o  m
    for (int i = 0; i < 30; i++) {
        String documentId = String.valueOf(i);
        SampleEntity sampleEntity = new SampleEntity();
        sampleEntity.setId(documentId);
        sampleEntity.setMessage("message");
        sampleEntity.setVersion(System.currentTimeMillis());
        repository.save(sampleEntity);
    }
    // when
    Page<SampleEntity> pageResult = repository.findByMessage("message",
            new PageRequest(0, 23, new Sort(new Sort.Order(Sort.Direction.ASC, "message"))));
    // then
    assertThat(pageResult.getTotalElements(), is(equalTo(30L)));
    assertThat(pageResult.getContent().size(), is(equalTo(23)));
}

From source file:org.springframework.data.elasticsearch.repositories.NestedObjectTests.java

@Test
public void shouldSearchUsingNestedQueryOnMultipleLevelNestedObject() {
    //given/*  ww w . ja  v a2  s.c o  m*/
    List<IndexQuery> indexQueries = createPerson();

    //when
    elasticsearchTemplate.putMapping(PersonMultipleLevelNested.class);
    elasticsearchTemplate.bulkIndex(indexQueries);
    elasticsearchTemplate.refresh(PersonMultipleLevelNested.class, true);

    //then
    BoolQueryBuilder builder = boolQuery();
    builder.must(nestedQuery("girlFriends", termQuery("girlFriends.type", "temp")))
            .must(nestedQuery("girlFriends.cars", termQuery("girlFriends.cars.name", "Ford".toLowerCase())));

    SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder).build();

    Page<PersonMultipleLevelNested> personIndexed = elasticsearchTemplate.queryForPage(searchQuery,
            PersonMultipleLevelNested.class);
    assertThat(personIndexed, is(notNullValue()));
    assertThat(personIndexed.getTotalElements(), is(1L));
    assertThat(personIndexed.getContent().get(0).getId(), is("1"));
}

From source file:org.springframework.data.elasticsearch.repository.support.DoubleIDRepositoryTest.java

@Test
public void shouldReturnResultsForGivenSearchQuery() {
    //given//from   w  w w.  j  av  a2  s . c  o  m
    Double documentId = RandomUtils.nextDouble();
    DoubleIDEntity sampleEntity = new DoubleIDEntity();
    sampleEntity.setId(documentId);
    sampleEntity.setMessage("hello world.");
    sampleEntity.setVersion(System.currentTimeMillis());
    repository.save(sampleEntity);
    //when
    SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(fieldQuery("id", documentId)).build();
    Page<DoubleIDEntity> sampleEntities = repository.search(searchQuery);
    //then
    assertThat(sampleEntities.getTotalElements(), equalTo(1L));
}

From source file:org.springframework.data.elasticsearch.repository.support.DoubleIDRepositoryTest.java

@Test
public void shouldDeleteAll() {
    //when/*  w  w w . java 2  s.  c  o m*/
    repository.deleteAll();
    //then
    SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery()).build();
    Page<DoubleIDEntity> sampleEntities = repository.search(searchQuery);
    assertThat(sampleEntities.getTotalElements(), equalTo(0L));
}

From source file:org.springframework.data.elasticsearch.repository.support.DoubleIDRepositoryTest.java

@Test
public void shouldDeleteEntity() {
    //given//from   www .  ja  v  a  2s .c  o  m
    Double documentId = RandomUtils.nextDouble();
    DoubleIDEntity sampleEntity = new DoubleIDEntity();
    sampleEntity.setId(documentId);
    sampleEntity.setMessage("hello world.");
    sampleEntity.setVersion(System.currentTimeMillis());
    repository.save(sampleEntity);
    //when
    repository.delete(sampleEntity);
    //then
    SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(fieldQuery("id", documentId)).build();
    Page<DoubleIDEntity> sampleEntities = repository.search(searchQuery);
    assertThat(sampleEntities.getTotalElements(), equalTo(0L));
}

From source file:org.springframework.data.elasticsearch.repository.support.DoubleIDRepositoryTest.java

@Test
public void shouldIndexEntity() {
    //given/* ww  w .j  a  v a2s .c  o m*/
    Double documentId = RandomUtils.nextDouble();
    DoubleIDEntity sampleEntity = new DoubleIDEntity();
    sampleEntity.setId(documentId);
    sampleEntity.setVersion(System.currentTimeMillis());
    sampleEntity.setMessage("some message");
    //when
    repository.index(sampleEntity);
    //then
    Page<DoubleIDEntity> entities = repository.search(fieldQuery("id", documentId), new PageRequest(0, 50));
    assertThat(entities.getTotalElements(), equalTo(1L));
}

From source file:org.springframework.data.elasticsearch.repository.support.DoubleIDRepositoryTest.java

@Test
public void shouldReturnSimilarEntities() {
    //given//from ww w  . ja  v a  2 s  . c  o m
    String sampleMessage = "So we build a web site or an application and want to add search to it, "
            + "and then it hits us: getting search working is hard. We want our search solution to be fast,"
            + " we want a painless setup and a completely free search schema, we want to be able to index data simply using JSON over HTTP, "
            + "we want our search server to be always available, we want to be able to start with one machine and scale to hundreds, "
            + "we want real-time search, we want simple multi-tenancy, and we want a solution that is built for the cloud.";

    List<DoubleIDEntity> sampleEntities = createSampleEntitiesWithMessage(sampleMessage, 30);
    repository.save(sampleEntities);

    //when
    Page<DoubleIDEntity> results = repository.searchSimilar(sampleEntities.get(0));

    //then
    assertThat(results.getTotalElements(), is(greaterThanOrEqualTo(1L)));
}

From source file:org.springframework.data.elasticsearch.repository.support.IntegerIDRepositoryTest.java

@Test
public void shouldReturnResultsForGivenSearchQuery() {
    //given//from   w  w  w  . j a va 2  s .  com
    Integer documentId = RandomUtils.nextInt();
    IntegerIDEntity sampleEntity = new IntegerIDEntity();
    sampleEntity.setId(documentId);
    sampleEntity.setMessage("hello world.");
    sampleEntity.setVersion(System.currentTimeMillis());
    repository.save(sampleEntity);
    //when
    SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(fieldQuery("id", documentId)).build();
    Page<IntegerIDEntity> sampleEntities = repository.search(searchQuery);
    //then
    assertThat(sampleEntities.getTotalElements(), equalTo(1L));
}

From source file:org.springframework.data.elasticsearch.repository.support.IntegerIDRepositoryTest.java

@Test
public void shouldDeleteAll() {
    //when/* w  w w .j a va 2s .c o m*/
    repository.deleteAll();
    //then
    SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery()).build();
    Page<IntegerIDEntity> sampleEntities = repository.search(searchQuery);
    assertThat(sampleEntities.getTotalElements(), equalTo(0L));
}

From source file:org.springframework.data.elasticsearch.repository.support.IntegerIDRepositoryTest.java

@Test
public void shouldDeleteEntity() {
    //given/* w w w.ja  va  2 s  .co m*/
    Integer documentId = RandomUtils.nextInt();
    IntegerIDEntity sampleEntity = new IntegerIDEntity();
    sampleEntity.setId(documentId);
    sampleEntity.setMessage("hello world.");
    sampleEntity.setVersion(System.currentTimeMillis());
    repository.save(sampleEntity);
    //when
    repository.delete(sampleEntity);
    //then
    SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(fieldQuery("id", documentId)).build();
    Page<IntegerIDEntity> sampleEntities = repository.search(searchQuery);
    assertThat(sampleEntities.getTotalElements(), equalTo(0L));
}