コード例 #1
1
  @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)));
  }
コード例 #2
1
  @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);
  }
コード例 #3
0
  @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));
  }
コード例 #4
0
  @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));
  }
コード例 #5
0
  @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));
  }
コード例 #6
0
  @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));
  }
  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;
  }
コード例 #9
0
  @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));
  }
コード例 #10
0
 @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)));
 }
コード例 #11
0
  @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));
  }
コード例 #12
0
  @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));
  }
コード例 #13
0
  @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()));
  }
コード例 #14
0
  @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));
  }
コード例 #15
0
  @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)));
  }
コード例 #16
0
  @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));
  }