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.repository.support.IntegerIDRepositoryTest.java

@Test
public void shouldIndexEntity() {
    //given//from w  ww  .  ja  va2s  .c  om
    Integer documentId = RandomUtils.nextInt();
    IntegerIDEntity sampleEntity = new IntegerIDEntity();
    sampleEntity.setId(documentId);
    sampleEntity.setVersion(System.currentTimeMillis());
    sampleEntity.setMessage("some message");
    //when
    repository.index(sampleEntity);
    //then
    Page<IntegerIDEntity> entities = repository.search(fieldQuery("id", documentId), new PageRequest(0, 50));
    assertThat(entities.getTotalElements(), equalTo(1L));
}

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

@Test
public void shouldReturnSimilarEntities() {
    //given//from www .  j a  va 2s. com
    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<IntegerIDEntity> sampleEntities = createSampleEntitiesWithMessage(sampleMessage, 30);
    repository.save(sampleEntities);

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

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

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

@Test
public void shouldReturnResultsForGivenSearchQuery() {
    //given//from www .j a  va2 s .  co m
    String documentId = randomNumeric(5);
    SampleEntity sampleEntity = new SampleEntity();
    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<SampleEntity> sampleEntities = repository.search(searchQuery);
    //then
    assertThat(sampleEntities.getTotalElements(), equalTo(1L));
}

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

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

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

@Test
public void shouldDeleteEntity() {
    //given/*  w w  w. ja  v a  2 s.c  om*/
    String documentId = randomNumeric(5);
    SampleEntity sampleEntity = new SampleEntity();
    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<SampleEntity> sampleEntities = repository.search(searchQuery);
    assertThat(sampleEntities.getTotalElements(), equalTo(0L));
}

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

@Test
public void shouldIndexEntity() {
    //given//from w w w  .  j a  va  2 s  . c  om
    String documentId = randomNumeric(5);
    SampleEntity sampleEntity = new SampleEntity();
    sampleEntity.setId(documentId);
    sampleEntity.setVersion(System.currentTimeMillis());
    sampleEntity.setMessage("some message");
    //when
    repository.index(sampleEntity);
    //then
    Page<SampleEntity> entities = repository.search(fieldQuery("id", documentId), new PageRequest(0, 50));
    assertThat(entities.getTotalElements(), equalTo(1L));
}

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

@Test
public void shouldReturnSimilarEntities() {
    //given//from  w ww .j a 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<SampleEntity> sampleEntities = createSampleEntitiesWithMessage(sampleMessage, 30);
    repository.save(sampleEntities);

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

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

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

@Test
public void shouldReturnResultsForGivenSearchQuery() {
    // given/*from   w ww.  j a v a2  s .  c  om*/
    String documentId = randomNumeric(5);
    SampleEntity sampleEntity = new SampleEntity();
    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<SampleEntity> sampleEntities = repository.search(searchQuery);
    // then
    assertThat(sampleEntities.getTotalElements(), equalTo(1L));
}

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

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

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

@Test
public void shouldDeleteEntity() {
    // given/*ww  w  .ja  v  a  2 s  .co  m*/
    String documentId = randomNumeric(5);
    SampleEntity sampleEntity = new SampleEntity();
    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<SampleEntity> sampleEntities = repository.search(searchQuery);
    assertThat(sampleEntities.getTotalElements(), equalTo(0L));
}