@Test
  @Ignore(
      "By default, the search request will fail if there is no mapping associated with a field. The ignore_unmapped option allows to ignore fields that have no mapping and not sort by them")
  public void shouldReturnSortedPageableResultsGivenStringQuery() {
    // todo
    // given
    String documentId = randomNumeric(5);
    SampleEntity sampleEntity = new SampleEntity();
    sampleEntity.setId(documentId);
    sampleEntity.setMessage("some message");
    sampleEntity.setVersion(System.currentTimeMillis());

    IndexQuery indexQuery = new IndexQuery();
    indexQuery.setId(documentId);
    indexQuery.setObject(sampleEntity);

    elasticsearchTemplate.index(indexQuery);
    elasticsearchTemplate.refresh(SampleEntity.class, true);

    StringQuery stringQuery =
        new StringQuery(
            matchAllQuery().toString(),
            new PageRequest(0, 10),
            new Sort(new Sort.Order(Sort.Direction.ASC, "messsage")));
    // when
    Page<SampleEntity> sampleEntities =
        elasticsearchTemplate.queryForPage(stringQuery, SampleEntity.class);
    // then
    assertThat(sampleEntities.getTotalElements(), is(greaterThanOrEqualTo(1L)));
  }
  @Test
  public void shouldReturnObjectForGivenId() {
    // given
    String documentId = randomNumeric(5);
    SampleEntity sampleEntity = new SampleEntity();
    sampleEntity.setId(documentId);
    sampleEntity.setMessage("some message");
    sampleEntity.setVersion(System.currentTimeMillis());

    IndexQuery indexQuery = new IndexQuery();
    indexQuery.setId(documentId);
    indexQuery.setObject(sampleEntity);

    elasticsearchTemplate.index(indexQuery);
    // when
    GetQuery getQuery = new GetQuery();
    getQuery.setId(documentId);
    SampleEntity sampleEntity1 = elasticsearchTemplate.queryForObject(getQuery, SampleEntity.class);
    // then
    assertNotNull("not null....", sampleEntity1);
    assertEquals(sampleEntity, sampleEntity1);
  }
  @Test
  public void shouldFilterSearchResultsForGivenFilter() {
    // given
    String documentId = randomNumeric(5);
    SampleEntity sampleEntity = new SampleEntity();
    sampleEntity.setId(documentId);
    sampleEntity.setMessage("some message");
    sampleEntity.setVersion(System.currentTimeMillis());

    IndexQuery indexQuery = new IndexQuery();
    indexQuery.setId(documentId);
    indexQuery.setObject(sampleEntity);
    elasticsearchTemplate.index(indexQuery);
    elasticsearchTemplate.refresh(SampleEntity.class, true);

    SearchQuery searchQuery =
        new NativeSearchQueryBuilder()
            .withQuery(matchAllQuery())
            .withFilter(boolFilter().must(termFilter("id", documentId)))
            .build();
    // when
    Page<SampleEntity> sampleEntities =
        elasticsearchTemplate.queryForPage(searchQuery, SampleEntity.class);
    // then
    assertThat(sampleEntities.getTotalElements(), equalTo(1L));
  }
  @Test
  public void shouldDoPartialUpdateForExistingDocument() {
    // given
    String documentId = randomNumeric(5);
    String messageBeforeUpdate = "some test message";
    String messageAfterUpdate = "test message";

    SampleEntity sampleEntity = new SampleEntity();
    sampleEntity.setId(documentId);
    sampleEntity.setMessage(messageBeforeUpdate);
    sampleEntity.setVersion(System.currentTimeMillis());

    IndexQuery indexQuery = new IndexQuery();
    indexQuery.setId(documentId);
    indexQuery.setObject(sampleEntity);

    elasticsearchTemplate.index(indexQuery);
    elasticsearchTemplate.refresh(SampleEntity.class, true);

    IndexRequest indexRequest = new IndexRequest();
    indexRequest.source("message", messageAfterUpdate);
    UpdateQuery updateQuery =
        new UpdateQueryBuilder()
            .withId(documentId)
            .withClass(SampleEntity.class)
            .withIndexRequest(indexRequest)
            .build();
    // when
    elasticsearchTemplate.update(updateQuery);
    // then
    GetQuery getQuery = new GetQuery();
    getQuery.setId(documentId);
    SampleEntity indexedEntity = elasticsearchTemplate.queryForObject(getQuery, SampleEntity.class);
    assertThat(indexedEntity.getMessage(), is(messageAfterUpdate));
  }
  @Before
  public void before() {
    elasticsearchTemplate.deleteIndex(Article.class);
    elasticsearchTemplate.createIndex(Article.class);
    elasticsearchTemplate.putMapping(Article.class);
    elasticsearchTemplate.refresh(Article.class, true);

    Article article = new Article("Spring Data Elasticsearch");
    article.setAuthors(asList(johnSmith, johnDoe));
    article.setTags("elasticsearch", "spring data");
    articleService.save(article);

    article = new Article("Search engines");
    article.setAuthors(asList(johnDoe));
    article.setTags("search engines", "tutorial");
    articleService.save(article);

    article = new Article("Second Article About Elasticsearch");
    article.setAuthors(asList(johnSmith));
    article.setTags("elasticsearch", "spring data");
    articleService.save(article);

    article = new Article("Elasticsearch Tutorial");
    article.setAuthors(asList(johnDoe));
    article.setTags("elasticsearch");
    articleService.save(article);
  }
  @Test
  public void shouldDeleteDocumentBySpecifiedTypeUsingDeleteQuery() {
    // given
    String documentId = randomNumeric(5);
    SampleEntity sampleEntity = new SampleEntity();
    sampleEntity.setId(documentId);
    sampleEntity.setMessage("some message");
    sampleEntity.setVersion(System.currentTimeMillis());

    IndexQuery indexQuery = new IndexQuery();
    indexQuery.setId(documentId);
    indexQuery.setObject(sampleEntity);

    elasticsearchTemplate.index(indexQuery);
    // when
    DeleteQuery deleteQuery = new DeleteQuery();
    deleteQuery.setQuery(fieldQuery("id", documentId));
    deleteQuery.setIndex("test-index");
    deleteQuery.setType("test-type");
    elasticsearchTemplate.delete(deleteQuery);
    // then
    SearchQuery searchQuery =
        new NativeSearchQueryBuilder().withQuery(fieldQuery("id", documentId)).build();
    Page<SampleEntity> sampleEntities =
        elasticsearchTemplate.queryForPage(searchQuery, SampleEntity.class);
    assertThat(sampleEntities.getTotalElements(), equalTo(0L));
  }
  @Test
  public void shouldSortResultsGivenSortCriteria() {
    // given
    List<IndexQuery> indexQueries = new ArrayList<IndexQuery>();
    // first document
    String documentId = randomNumeric(5);
    SampleEntity sampleEntity1 = new SampleEntity();
    sampleEntity1.setId(documentId);
    sampleEntity1.setMessage("abc");
    sampleEntity1.setRate(10);
    sampleEntity1.setVersion(System.currentTimeMillis());

    IndexQuery indexQuery1 = new IndexQuery();
    indexQuery1.setId(documentId);
    indexQuery1.setObject(sampleEntity1);

    // second document
    String documentId2 = randomNumeric(5);
    SampleEntity sampleEntity2 = new SampleEntity();
    sampleEntity2.setId(documentId2);
    sampleEntity2.setMessage("xyz");
    sampleEntity2.setRate(5);
    sampleEntity2.setVersion(System.currentTimeMillis());

    IndexQuery indexQuery2 = new IndexQuery();
    indexQuery2.setId(documentId2);
    indexQuery2.setObject(sampleEntity2);

    // third document
    String documentId3 = randomNumeric(5);
    SampleEntity sampleEntity3 = new SampleEntity();
    sampleEntity3.setId(documentId3);
    sampleEntity3.setMessage("xyz");
    sampleEntity3.setRate(15);
    sampleEntity3.setVersion(System.currentTimeMillis());

    IndexQuery indexQuery3 = new IndexQuery();
    indexQuery3.setId(documentId3);
    indexQuery3.setObject(sampleEntity3);

    indexQueries.add(indexQuery1);
    indexQueries.add(indexQuery2);
    indexQueries.add(indexQuery3);

    elasticsearchTemplate.bulkIndex(indexQueries);
    elasticsearchTemplate.refresh(SampleEntity.class, true);

    SearchQuery searchQuery =
        new NativeSearchQueryBuilder()
            .withQuery(matchAllQuery())
            .withSort(new FieldSortBuilder("rate").ignoreUnmapped(true).order(SortOrder.ASC))
            .build();
    // when
    Page<SampleEntity> sampleEntities =
        elasticsearchTemplate.queryForPage(searchQuery, SampleEntity.class);
    // then
    assertThat(sampleEntities.getTotalElements(), equalTo(3L));
    assertThat(sampleEntities.getContent().get(0).getRate(), is(sampleEntity2.getRate()));
  }
 @Before
 public void before() {
   clean();
   elasticsearchTemplate.createIndex(ParentEntity.class);
   elasticsearchTemplate.createIndex(ChildEntity.class);
   elasticsearchTemplate.putMapping(ParentEntity.class);
   elasticsearchTemplate.putMapping(ChildEntity.class);
 }
 @Test
 public void shouldPutMappingForGivenEntity() throws Exception {
   // given
   Class entity = SampleMappingEntity.class;
   elasticsearchTemplate.createIndex(entity);
   // when
   assertThat(elasticsearchTemplate.putMapping(entity), is(true));
 }
  @Test
  public void shouldReturnListForGivenCriteria() {
    // given
    List<IndexQuery> indexQueries = new ArrayList<IndexQuery>();
    // first document
    String documentId = randomNumeric(5);
    SampleEntity sampleEntity1 = new SampleEntity();
    sampleEntity1.setId(documentId);
    sampleEntity1.setMessage("test message");
    sampleEntity1.setVersion(System.currentTimeMillis());

    IndexQuery indexQuery1 = new IndexQuery();
    indexQuery1.setId(documentId);
    indexQuery1.setObject(sampleEntity1);
    indexQueries.add(indexQuery1);

    // second document
    String documentId2 = randomNumeric(5);
    SampleEntity sampleEntity2 = new SampleEntity();
    sampleEntity2.setId(documentId2);
    sampleEntity2.setMessage("test test");
    sampleEntity2.setVersion(System.currentTimeMillis());

    IndexQuery indexQuery2 = new IndexQuery();
    indexQuery2.setId(documentId2);
    indexQuery2.setObject(sampleEntity2);

    indexQueries.add(indexQuery2);

    // second document
    String documentId3 = randomNumeric(5);
    SampleEntity sampleEntity3 = new SampleEntity();
    sampleEntity3.setId(documentId3);
    sampleEntity3.setMessage("some message");
    sampleEntity3.setVersion(System.currentTimeMillis());

    IndexQuery indexQuery3 = new IndexQuery();
    indexQuery3.setId(documentId3);
    indexQuery3.setObject(sampleEntity3);

    indexQueries.add(indexQuery3);
    // when
    elasticsearchTemplate.bulkIndex(indexQueries);
    elasticsearchTemplate.refresh(SampleEntity.class, true);
    // when
    CriteriaQuery singleCriteriaQuery = new CriteriaQuery(new Criteria("message").contains("test"));
    CriteriaQuery multipleCriteriaQuery =
        new CriteriaQuery(
            new Criteria("message").contains("some").and("message").contains("message"));
    List<SampleEntity> sampleEntitiesForSingleCriteria =
        elasticsearchTemplate.queryForList(singleCriteriaQuery, SampleEntity.class);
    List<SampleEntity> sampleEntitiesForAndCriteria =
        elasticsearchTemplate.queryForList(multipleCriteriaQuery, SampleEntity.class);
    // then
    assertThat(sampleEntitiesForSingleCriteria.size(), is(2));
    assertThat(sampleEntitiesForAndCriteria.size(), is(1));
  }
 @Test
 public void shouldDeleteIndexForGivenEntity() {
   // given
   Class clazz = SampleEntity.class;
   // when
   elasticsearchTemplate.deleteIndex(clazz);
   // then
   assertThat(elasticsearchTemplate.indexExists(clazz), is(false));
 }
  @Test
  public void shouldReturnHighlightedFieldsForGivenQueryAndFields() {

    // given
    String documentId = randomNumeric(5);
    String actualMessage = "some test message";
    String highlightedMessage = "some <em>test</em> message";

    SampleEntity sampleEntity = new SampleEntity();
    sampleEntity.setId(documentId);
    sampleEntity.setMessage(actualMessage);
    sampleEntity.setVersion(System.currentTimeMillis());

    IndexQuery indexQuery = new IndexQuery();
    indexQuery.setId(documentId);
    indexQuery.setObject(sampleEntity);

    elasticsearchTemplate.index(indexQuery);
    elasticsearchTemplate.refresh(SampleEntity.class, true);

    SearchQuery searchQuery =
        new NativeSearchQueryBuilder()
            .withQuery(termQuery("message", "test"))
            .withHighlightFields(new HighlightBuilder.Field("message"))
            .build();

    Page<SampleEntity> sampleEntities =
        elasticsearchTemplate.queryForPage(
            searchQuery,
            new ResultsMapper<SampleEntity>() {
              @Override
              public FacetedPage<SampleEntity> mapResults(SearchResponse response) {
                List<SampleEntity> chunk = new ArrayList<SampleEntity>();
                for (SearchHit searchHit : response.getHits()) {
                  if (response.getHits().getHits().length <= 0) {
                    return null;
                  }
                  SampleEntity user = new SampleEntity();
                  user.setId(searchHit.getId());
                  user.setMessage((String) searchHit.getSource().get("message"));
                  user.setHighlightedMessage(
                      searchHit.getHighlightFields().get("message").fragments()[0].toString());
                  chunk.add(user);
                }
                if (chunk.size() > 0) {
                  return new FacetedPageImpl<SampleEntity>(chunk);
                }
                return null;
              }
            });

    assertThat(sampleEntities.getContent().get(0).getHighlightedMessage(), is(highlightedMessage));
  }
  @Test
  public void shouldReturnResultsWithScanAndScroll() {
    // given
    List<IndexQuery> entities = createSampleEntitiesWithMessage("Test message", 30);
    // when
    elasticsearchTemplate.bulkIndex(entities);
    elasticsearchTemplate.refresh(SampleEntity.class, true);
    // then

    SearchQuery searchQuery =
        new NativeSearchQueryBuilder()
            .withQuery(matchAllQuery())
            .withIndices("test-index")
            .withTypes("test-type")
            .withPageable(new PageRequest(0, 10))
            .build();

    String scrollId = elasticsearchTemplate.scan(searchQuery, 1000, false);
    List<SampleEntity> sampleEntities = new ArrayList<SampleEntity>();
    boolean hasRecords = true;
    while (hasRecords) {
      Page<SampleEntity> page =
          elasticsearchTemplate.scroll(
              scrollId,
              5000L,
              new ResultsMapper<SampleEntity>() {
                @Override
                public FacetedPage<SampleEntity> mapResults(SearchResponse response) {
                  List<SampleEntity> chunk = new ArrayList<SampleEntity>();
                  for (SearchHit searchHit : response.getHits()) {
                    if (response.getHits().getHits().length <= 0) {
                      return null;
                    }
                    SampleEntity user = new SampleEntity();
                    user.setId(searchHit.getId());
                    user.setMessage((String) searchHit.getSource().get("message"));
                    chunk.add(user);
                  }
                  if (chunk.size() > 0) {

                    return new FacetedPageImpl<SampleEntity>(chunk);
                  }
                  return null;
                }
              });
      if (page != null) {
        sampleEntities.addAll(page.getContent());
      } else {
        hasRecords = false;
      }
    }
    assertThat(sampleEntities.size(), is(equalTo(30)));
  }
  @Test
  public void shouldReturnListForGivenStringQuery() {
    // given
    List<IndexQuery> indexQueries = new ArrayList<IndexQuery>();
    // first document
    String documentId = randomNumeric(5);
    SampleEntity sampleEntity1 = new SampleEntity();
    sampleEntity1.setId(documentId);
    sampleEntity1.setMessage("test message");
    sampleEntity1.setVersion(System.currentTimeMillis());

    IndexQuery indexQuery1 = new IndexQuery();
    indexQuery1.setId(documentId);
    indexQuery1.setObject(sampleEntity1);
    indexQueries.add(indexQuery1);

    // second document
    String documentId2 = randomNumeric(5);
    SampleEntity sampleEntity2 = new SampleEntity();
    sampleEntity2.setId(documentId2);
    sampleEntity2.setMessage("test test");
    sampleEntity2.setVersion(System.currentTimeMillis());

    IndexQuery indexQuery2 = new IndexQuery();
    indexQuery2.setId(documentId2);
    indexQuery2.setObject(sampleEntity2);

    indexQueries.add(indexQuery2);

    // second document
    String documentId3 = randomNumeric(5);
    SampleEntity sampleEntity3 = new SampleEntity();
    sampleEntity3.setId(documentId3);
    sampleEntity3.setMessage("some message");
    sampleEntity3.setVersion(System.currentTimeMillis());

    IndexQuery indexQuery3 = new IndexQuery();
    indexQuery3.setId(documentId3);
    indexQuery3.setObject(sampleEntity3);

    indexQueries.add(indexQuery3);
    // when
    elasticsearchTemplate.bulkIndex(indexQueries);
    elasticsearchTemplate.refresh(SampleEntity.class, true);
    // when
    StringQuery stringQuery = new StringQuery(matchAllQuery().toString());
    List<SampleEntity> sampleEntities =
        elasticsearchTemplate.queryForList(stringQuery, SampleEntity.class);
    // then
    assertThat(sampleEntities.size(), is(3));
  }
  @Test
  public void givenFullTitle_whenRunMatchQueryOnVerbatimField_thenDocIsFound() {
    SearchQuery searchQuery =
        new NativeSearchQueryBuilder()
            .withQuery(matchQuery("title.verbatim", "Second Article About Elasticsearch"))
            .build();
    List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
    assertEquals(1, articles.size());

    searchQuery =
        new NativeSearchQueryBuilder()
            .withQuery(matchQuery("title.verbatim", "Second Article About"))
            .build();
    articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
    assertEquals(0, articles.size());
  }
 @Test
 public void shouldCreateIndexGivenEntityClass() {
   // when
   boolean created = elasticsearchTemplate.createIndex(SampleEntity.class);
   // then
   assertThat(created, is(true));
 }
 @Test
 public void givenPartTitle_whenRunMatchQuery_thenDocIsFound() {
   SearchQuery searchQuery =
       new NativeSearchQueryBuilder().withQuery(matchQuery("title", "elasticsearch data")).build();
   List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
   assertEquals(3, articles.size());
 }
  @Test
  public void shouldReturnSimilarResultsGivenMoreLikeThisQuery() {
    // given
    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.";

    String documentId1 = randomNumeric(5);
    SampleEntity sampleEntity1 = new SampleEntity();
    sampleEntity1.setId(documentId1);
    sampleEntity1.setMessage(sampleMessage);
    sampleEntity1.setVersion(System.currentTimeMillis());

    IndexQuery indexQuery1 = new IndexQuery();
    indexQuery1.setId(documentId1);
    indexQuery1.setObject(sampleEntity1);

    elasticsearchTemplate.index(indexQuery1);

    String documentId2 = randomNumeric(5);
    SampleEntity sampleEntity2 = new SampleEntity();
    sampleEntity2.setId(documentId2);
    sampleEntity2.setMessage(sampleMessage);
    sampleEntity2.setVersion(System.currentTimeMillis());

    IndexQuery indexQuery2 = new IndexQuery();
    indexQuery2.setId(documentId2);
    indexQuery2.setObject(sampleEntity2);

    elasticsearchTemplate.index(indexQuery2);
    elasticsearchTemplate.refresh(SampleEntity.class, true);

    MoreLikeThisQuery moreLikeThisQuery = new MoreLikeThisQuery();
    moreLikeThisQuery.setId(documentId2);
    moreLikeThisQuery.addFields("message");
    moreLikeThisQuery.setMinDocFreq(1);
    // when
    Page<SampleEntity> sampleEntities =
        elasticsearchTemplate.moreLikeThis(moreLikeThisQuery, SampleEntity.class);

    // then
    assertThat(sampleEntities.getTotalElements(), is(equalTo(1L)));
    assertThat(sampleEntities.getContent(), hasItem(sampleEntity1));
  }
 @Test
 public void givenOneTermFromTitle_whenRunMatchQuery_thenDocIsFound() {
   SearchQuery searchQuery =
       new NativeSearchQueryBuilder().withQuery(matchQuery("title", "Engines Solutions")).build();
   List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
   assertEquals(1, articles.size());
   assertEquals("Search engines", articles.get(0).getTitle());
 }
 @Test
 public void givenNotExactPhrase_whenUseSlop_thenQueryMatches() {
   SearchQuery searchQuery =
       new NativeSearchQueryBuilder()
           .withQuery(matchPhraseQuery("title", "spring elasticsearch").slop(1))
           .build();
   List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
   assertEquals(1, articles.size());
 }
 @Test
 public void givenFullTitle_whenRunMatchQuery_thenDocIsFound() {
   SearchQuery searchQuery =
       new NativeSearchQueryBuilder()
           .withQuery(matchQuery("title", "Search engines").operator(AND))
           .build();
   List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
   assertEquals(1, articles.size());
 }
  private ParentEntity index(String parentId, String name) {
    ParentEntity parent = new ParentEntity(parentId, name);
    IndexQuery index = new IndexQuery();
    index.setId(parent.getId());
    index.setObject(parent);
    elasticsearchTemplate.index(index);

    return parent;
  }
  @Test
  public void givenNestedObject_whenQueryByAuthorsName_thenFoundArticlesByThatAuthor() {
    QueryBuilder builder =
        nestedQuery("authors", boolQuery().must(termQuery("authors.name", "smith")));

    SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder).build();
    List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);

    assertEquals(2, articles.size());
  }
  private ChildEntity index(String childId, String parentId, String name) {
    ChildEntity child = new ChildEntity(childId, parentId, name);
    IndexQuery index = new IndexQuery();
    index.setId(child.getId());
    index.setObject(child);
    index.setParentId(child.getParentId());
    elasticsearchTemplate.index(index);

    return child;
  }
 @Test
 public void shouldReturnCountForGivenSearchQuery() {
   // given
   String documentId = randomNumeric(5);
   SampleEntity sampleEntity = new SampleEntity();
   sampleEntity.setId(documentId);
   sampleEntity.setMessage("some message");
   sampleEntity.setVersion(System.currentTimeMillis());
   IndexQuery indexQuery = new IndexQuery();
   indexQuery.setId(documentId);
   indexQuery.setObject(sampleEntity);
   elasticsearchTemplate.index(indexQuery);
   elasticsearchTemplate.refresh(SampleEntity.class, true);
   SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery()).build();
   // when
   long count = elasticsearchTemplate.count(searchQuery, SampleEntity.class);
   // then
   assertThat(count, is(equalTo(1L)));
 }
  @Test
  public void shouldReturnSpecifiedFields() {
    // given
    String documentId = randomNumeric(5);
    String message = "some test message";
    SampleEntity sampleEntity = new SampleEntity();
    sampleEntity.setId(documentId);
    sampleEntity.setMessage(message);
    sampleEntity.setVersion(System.currentTimeMillis());

    IndexQuery indexQuery = new IndexQuery();
    indexQuery.setId(documentId);
    indexQuery.setObject(sampleEntity);

    elasticsearchTemplate.index(indexQuery);
    elasticsearchTemplate.refresh(SampleEntity.class, true);
    SearchQuery searchQuery =
        new NativeSearchQueryBuilder()
            .withQuery(matchAllQuery())
            .withIndices("test-index")
            .withTypes("test-type")
            .withFields("message")
            .build();
    // when
    Page<String> page =
        elasticsearchTemplate.queryForPage(
            searchQuery,
            new ResultsMapper<String>() {
              @Override
              public FacetedPage<String> mapResults(SearchResponse response) {
                List<String> values = new ArrayList<String>();
                for (SearchHit searchHit : response.getHits()) {
                  values.add((String) searchHit.field("message").value());
                }
                return new FacetedPageImpl<String>(values);
              }
            });
    // then
    assertThat(page, is(notNullValue()));
    assertThat(page.getTotalElements(), is(equalTo(1L)));
    assertThat(page.getContent().get(0), is(message));
  }
 @Test(expected = DocumentMissingException.class)
 public void shouldThrowExceptionIfDocumentDoesNotExistWhileDoingPartialUpdate() {
   // when
   IndexRequest indexRequest = new IndexRequest();
   UpdateQuery updateQuery =
       new UpdateQueryBuilder()
           .withId(randomNumeric(5))
           .withClass(SampleEntity.class)
           .withIndexRequest(indexRequest)
           .build();
   elasticsearchTemplate.update(updateQuery);
 }
  @Test
  public void shouldDeleteSpecifiedTypeFromAnIndex() {
    // given
    String documentId = randomNumeric(5);
    SampleEntity sampleEntity = new SampleEntity();
    sampleEntity.setId(documentId);
    sampleEntity.setMessage("some message");
    sampleEntity.setVersion(System.currentTimeMillis());

    IndexQuery indexQuery = new IndexQuery();
    indexQuery.setId(documentId);
    indexQuery.setObject(sampleEntity);

    elasticsearchTemplate.index(indexQuery);

    // when
    elasticsearchTemplate.deleteType("test-index", "test-type");

    // then
    boolean typeExists = elasticsearchTemplate.typeExists("test-index", "test-type");
    assertThat(typeExists, is(false));
  }
 @Test
 public void shouldDoUpsertIfDocumentDoesNotExist() {
   // given
   String documentId = randomNumeric(5);
   String message = "test message";
   IndexRequest indexRequest = new IndexRequest();
   indexRequest.source("message", message);
   UpdateQuery updateQuery =
       new UpdateQueryBuilder()
           .withId(documentId)
           .withDoUpsert(true)
           .withClass(SampleEntity.class)
           .withIndexRequest(indexRequest)
           .build();
   // when
   elasticsearchTemplate.update(updateQuery);
   // then
   GetQuery getQuery = new GetQuery();
   getQuery.setId(documentId);
   SampleEntity indexedEntity = elasticsearchTemplate.queryForObject(getQuery, SampleEntity.class);
   assertThat(indexedEntity.getMessage(), is(message));
 }
Example #30
0
  @SuppressWarnings("unchecked")
  @Test
  public void shouldGet() {
    // given
    FacetedPage page = mock(FacetedPage.class);
    given(template.queryForPage(isA(SearchQuery.class), isA(Class.class))).willReturn(page);

    // when
    commentDao.get(0, 10);

    // then
    verify(template).queryForPage(isA(SearchQuery.class), isA(Class.class));
  }