Search in sources :

Example 11 with Article

use of com.baeldung.spring.data.es.model.Article in project tutorials by eugenp.

the class ElasticSearchQueryIntegrationTest method givenNotExactPhrase_whenUseSlop_thenQueryMatches.

@Test
public void givenNotExactPhrase_whenUseSlop_thenQueryMatches() {
    final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchPhraseQuery("title", "spring elasticsearch").slop(1)).build();
    final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
    assertEquals(1, articles.size());
}
Also used : SearchQuery(org.springframework.data.elasticsearch.core.query.SearchQuery) Article(com.baeldung.spring.data.es.model.Article) NativeSearchQueryBuilder(org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder) Test(org.junit.Test)

Example 12 with Article

use of com.baeldung.spring.data.es.model.Article in project tutorials by eugenp.

the class ElasticSearchIntegrationTest method givenTagFilterQuery_whenSearchByTag_thenArticleIsFound.

@Test
public void givenTagFilterQuery_whenSearchByTag_thenArticleIsFound() {
    final Page<Article> articleByAuthorName = articleService.findByFilteredTagQuery("elasticsearch", new PageRequest(0, 10));
    assertEquals(3L, articleByAuthorName.getTotalElements());
}
Also used : PageRequest(org.springframework.data.domain.PageRequest) Article(com.baeldung.spring.data.es.model.Article) Test(org.junit.Test)

Example 13 with Article

use of com.baeldung.spring.data.es.model.Article in project tutorials by eugenp.

the class ElasticSearchIntegrationTest method givenPersistedArticles_whenSearchByAuthorsName_thenRightFound.

@Test
public void givenPersistedArticles_whenSearchByAuthorsName_thenRightFound() {
    final Page<Article> articleByAuthorName = articleService.findByAuthorName(johnSmith.getName(), new PageRequest(0, 10));
    assertEquals(2L, articleByAuthorName.getTotalElements());
}
Also used : PageRequest(org.springframework.data.domain.PageRequest) Article(com.baeldung.spring.data.es.model.Article) Test(org.junit.Test)

Example 14 with Article

use of com.baeldung.spring.data.es.model.Article in project tutorials by eugenp.

the class ElasticSearchIntegrationTest method givenCustomQuery_whenSearchByAuthorsName_thenArticleIsFound.

@Test
public void givenCustomQuery_whenSearchByAuthorsName_thenArticleIsFound() {
    final Page<Article> articleByAuthorName = articleService.findByAuthorNameUsingCustomQuery("Smith", new PageRequest(0, 10));
    assertEquals(2L, articleByAuthorName.getTotalElements());
}
Also used : PageRequest(org.springframework.data.domain.PageRequest) Article(com.baeldung.spring.data.es.model.Article) Test(org.junit.Test)

Example 15 with Article

use of com.baeldung.spring.data.es.model.Article in project tutorials by eugenp.

the class ElasticSearchIntegrationTest method givenSavedDoc_whenTitleUpdated_thenCouldFindByUpdatedTitle.

@Test
public void givenSavedDoc_whenTitleUpdated_thenCouldFindByUpdatedTitle() {
    final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(fuzzyQuery("title", "serch")).build();
    final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
    assertEquals(1, articles.size());
    final Article article = articles.get(0);
    final String newTitle = "Getting started with Search Engines";
    article.setTitle(newTitle);
    articleService.save(article);
    assertEquals(newTitle, articleService.findOne(article.getId()).getTitle());
}
Also used : SearchQuery(org.springframework.data.elasticsearch.core.query.SearchQuery) Article(com.baeldung.spring.data.es.model.Article) NativeSearchQueryBuilder(org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder) Test(org.junit.Test)

Aggregations

Article (com.baeldung.spring.data.es.model.Article)20 Test (org.junit.Test)18 NativeSearchQueryBuilder (org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder)13 SearchQuery (org.springframework.data.elasticsearch.core.query.SearchQuery)13 PageRequest (org.springframework.data.domain.PageRequest)4 MultiMatchQueryBuilder (org.elasticsearch.index.query.MultiMatchQueryBuilder)2 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)2 Before (org.junit.Before)2 Author (com.baeldung.spring.data.es.model.Author)1