@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 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));
  }
  @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 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));
  }
 @Override
 public String index(IndexQuery query) {
   String documentId = prepareIndex(query).execute().actionGet().getId();
   // We should call this because we are not going through a mapper.
   if (query.getObject() != null) {
     setPersistentEntityId(query.getObject(), documentId);
   }
   return documentId;
 }
  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;
  }
  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 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()));
  }
  @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 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 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));
  }
 private static List<IndexQuery> createSampleEntitiesWithMessage(
     String message, int numberOfEntities) {
   List<IndexQuery> indexQueries = new ArrayList<IndexQuery>();
   for (int i = 0; i < numberOfEntities; i++) {
     String documentId = randomNumeric(5);
     SampleEntity sampleEntity = new SampleEntity();
     sampleEntity.setId(documentId);
     sampleEntity.setMessage(message);
     sampleEntity.setRate(2);
     sampleEntity.setVersion(System.currentTimeMillis());
     IndexQuery indexQuery = new IndexQuery();
     indexQuery.setId(documentId);
     indexQuery.setObject(sampleEntity);
     indexQueries.add(indexQuery);
   }
   return indexQueries;
 }
 @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
  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 shouldExecuteGivenCriteriaQuery() {
    // given
    String documentId = randomNumeric(5);
    SampleEntity sampleEntity = new SampleEntity();
    sampleEntity.setId(documentId);
    sampleEntity.setMessage("some test message");
    sampleEntity.setVersion(System.currentTimeMillis());

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

    elasticsearchTemplate.index(indexQuery);
    elasticsearchTemplate.refresh(SampleEntity.class, true);
    CriteriaQuery criteriaQuery = new CriteriaQuery(new Criteria("message").contains("test"));

    // when
    SampleEntity sampleEntity1 =
        elasticsearchTemplate.queryForObject(criteriaQuery, SampleEntity.class);
    // then
    assertThat(sampleEntity1, is(notNullValue()));
  }
  @Test
  public void shouldExecuteStringQuery() {
    // 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());
    // when
    Page<SampleEntity> sampleEntities =
        elasticsearchTemplate.queryForPage(stringQuery, SampleEntity.class);
    // then
    assertThat(sampleEntities.getTotalElements(), equalTo(1L));
  }
  @Test
  public void shouldReturnPageForGivenSearchQuery() {
    // 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
    Page<SampleEntity> sampleEntities =
        elasticsearchTemplate.queryForPage(searchQuery, SampleEntity.class);
    // then
    assertThat(sampleEntities, is(notNullValue()));
    assertThat(sampleEntities.getTotalElements(), greaterThanOrEqualTo(1L));
  }
  @Test
  public void shouldReturnObjectMatchingGivenStringQuery() {
    // 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(fieldQuery("id", documentId).toString());
    // when
    SampleEntity sampleEntity1 =
        elasticsearchTemplate.queryForObject(stringQuery, SampleEntity.class);
    // then
    assertThat(sampleEntity1, is(notNullValue()));
    assertThat(sampleEntity1.getId(), is(equalTo(documentId)));
  }
  @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 shouldDoBulkIndex() {
    // given
    List<IndexQuery> indexQueries = new ArrayList<IndexQuery>();
    // first document
    String documentId = randomNumeric(5);
    SampleEntity sampleEntity1 = new SampleEntity();
    sampleEntity1.setId(documentId);
    sampleEntity1.setMessage("some 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("some message");
    sampleEntity2.setVersion(System.currentTimeMillis());

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

    indexQueries.add(indexQuery2);
    // when
    elasticsearchTemplate.bulkIndex(indexQueries);
    elasticsearchTemplate.refresh(SampleEntity.class, true);
    // then
    SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery()).build();
    Page<SampleEntity> sampleEntities =
        elasticsearchTemplate.queryForPage(searchQuery, SampleEntity.class);
    assertThat(sampleEntities.getTotalElements(), is(equalTo(2L)));
  }
  private IndexRequestBuilder prepareIndex(IndexQuery query) {
    try {
      String indexName =
          isBlank(query.getIndexName())
              ? retrieveIndexNameFromPersistentEntity(query.getObject().getClass())[0]
              : query.getIndexName();
      String type =
          isBlank(query.getType())
              ? retrieveTypeFromPersistentEntity(query.getObject().getClass())[0]
              : query.getType();

      IndexRequestBuilder indexRequestBuilder = null;

      if (query.getObject() != null) {
        String entityId = null;
        if (isDocument(query.getObject().getClass())) {
          entityId = getPersistentEntityId(query.getObject());
        }
        // If we have a query id and a document id, do not ask ES to generate one.
        if (query.getId() != null && entityId != null) {
          indexRequestBuilder = client.prepareIndex(indexName, type, query.getId());
        } else {
          indexRequestBuilder = client.prepareIndex(indexName, type);
        }
        indexRequestBuilder.setSource(
            resultsMapper.getEntityMapper().mapToString(query.getObject()));
      } else if (query.getSource() != null) {
        indexRequestBuilder =
            client.prepareIndex(indexName, type, query.getId()).setSource(query.getSource());
      } else {
        throw new ElasticsearchException(
            "object or source is null, failed to index the document [id: " + query.getId() + "]");
      }
      if (query.getVersion() != null) {
        indexRequestBuilder.setVersion(query.getVersion());
        indexRequestBuilder.setVersionType(EXTERNAL);
      }

      if (query.getParentId() != null) {
        indexRequestBuilder.setParent(query.getParentId());
      }

      return indexRequestBuilder;
    } catch (IOException e) {
      throw new ElasticsearchException(
          "failed to index the document [id: " + query.getId() + "]", e);
    }
  }