@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));
  }
  @Override
  public <T> Page<T> moreLikeThis(MoreLikeThisQuery query, Class<T> clazz) {
    int startRecord = 0;
    ElasticsearchPersistentEntity persistentEntity = getPersistentEntityFor(clazz);
    String indexName =
        isNotBlank(query.getIndexName()) ? query.getIndexName() : persistentEntity.getIndexName();
    String type = isNotBlank(query.getType()) ? query.getType() : persistentEntity.getIndexType();

    Assert.notNull(indexName, "No 'indexName' defined for MoreLikeThisQuery");
    Assert.notNull(type, "No 'type' defined for MoreLikeThisQuery");
    Assert.notNull(query.getId(), "No document id defined for MoreLikeThisQuery");

    MoreLikeThisRequestBuilder requestBuilder =
        client.prepareMoreLikeThis(indexName, type, query.getId());

    if (query.getPageable() != null) {
      startRecord = query.getPageable().getPageNumber() * query.getPageable().getPageSize();
      requestBuilder.setSearchSize(query.getPageable().getPageSize());
    }
    requestBuilder.setSearchFrom(startRecord);

    if (isNotEmpty(query.getSearchIndices())) {
      requestBuilder.setSearchIndices(toArray(query.getSearchIndices()));
    }
    if (isNotEmpty(query.getSearchTypes())) {
      requestBuilder.setSearchTypes(toArray(query.getSearchTypes()));
    }
    if (isNotEmpty(query.getFields())) {
      requestBuilder.setField(toArray(query.getFields()));
    }
    if (isNotBlank(query.getRouting())) {
      requestBuilder.setRouting(query.getRouting());
    }
    if (query.getPercentTermsToMatch() != null) {
      requestBuilder.setPercentTermsToMatch(query.getPercentTermsToMatch());
    }
    if (query.getMinTermFreq() != null) {
      requestBuilder.setMinTermFreq(query.getMinTermFreq());
    }
    if (query.getMaxQueryTerms() != null) {
      requestBuilder.maxQueryTerms(query.getMaxQueryTerms());
    }
    if (isNotEmpty(query.getStopWords())) {
      requestBuilder.setStopWords(toArray(query.getStopWords()));
    }
    if (query.getMinDocFreq() != null) {
      requestBuilder.setMinDocFreq(query.getMinDocFreq());
    }
    if (query.getMaxDocFreq() != null) {
      requestBuilder.setMaxDocFreq(query.getMaxDocFreq());
    }
    if (query.getMinWordLen() != null) {
      requestBuilder.setMinWordLen(query.getMinWordLen());
    }
    if (query.getMaxWordLen() != null) {
      requestBuilder.setMaxWordLen(query.getMaxWordLen());
    }
    if (query.getBoostTerms() != null) {
      requestBuilder.setBoostTerms(query.getBoostTerms());
    }

    SearchResponse response = requestBuilder.execute().actionGet();
    return resultsMapper.mapResults(response, clazz, query.getPageable());
  }