Example usage for org.springframework.data.elasticsearch.core.query MoreLikeThisQuery setId

List of usage examples for org.springframework.data.elasticsearch.core.query MoreLikeThisQuery setId

Introduction

In this page you can find the example usage for org.springframework.data.elasticsearch.core.query MoreLikeThisQuery setId.

Prototype

public void setId(String id) 

Source Link

Usage

From source file:org.bisen.chatamari.service.BlogService.java

@Transactional(readOnly = true)
public Page<ElasticBlog> moreLikeThis(String id) {
    MoreLikeThisQuery query = new MoreLikeThisQuery();
    query.setId(id);
    query.addFields("strippedContent");
    query.setMinDocFreq(1);/*from   w  w  w .jav a  2 s .  co  m*/
    return elasticsearchTemplate.moreLikeThis(query, ElasticBlog.class);

}

From source file:com.github.vanroy.springdata.jest.JestElasticsearchTemplateTests.java

@Test
public void shouldReturnSimilarResultsGivenMoreLikeThisQuery() {
    // given//from w ww .  j  a  va  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.";

    String documentId1 = randomNumeric(5);
    SampleEntity sampleEntity = SampleEntity.builder().id(documentId1).message(sampleMessage)
            .version(System.currentTimeMillis()).build();

    IndexQuery indexQuery = getIndexQuery(sampleEntity);

    elasticsearchTemplate.index(indexQuery);

    String documentId2 = randomNumeric(5);

    elasticsearchTemplate.index(getIndexQuery(SampleEntity.builder().id(documentId2).message(sampleMessage)
            .version(System.currentTimeMillis()).build()));
    elasticsearchTemplate.refresh(SampleEntity.class);

    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(sampleEntity));
}

From source file:org.springframework.data.elasticsearch.core.ElasticsearchTemplateTests.java

@Test
public void shouldReturnSimilarResultsGivenMoreLikeThisQuery() {
    // given/*from  www  . ja v a  2s  .  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.";

    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));
}