Search in sources :

Example 1 with Article

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

the class ElasticSearchIntegrationTest method givenSavedDoc_whenOneTermMatches_thenFindByTitle.

@Test
public void givenSavedDoc_whenOneTermMatches_thenFindByTitle() {
    final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "Search engines").operator(AND)).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 2 with Article

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

the class ElasticSearchIntegrationTest method givenArticleService_whenSaveArticle_thenIdIsAssigned.

@Test
public void givenArticleService_whenSaveArticle_thenIdIsAssigned() {
    final List<Author> authors = asList(new Author("John Smith"), johnDoe);
    Article article = new Article("Making Search Elastic");
    article.setAuthors(authors);
    article = articleService.save(article);
    assertNotNull(article.getId());
}
Also used : Article(com.baeldung.spring.data.es.model.Article) Author(com.baeldung.spring.data.es.model.Author) Test(org.junit.Test)

Example 3 with Article

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

the class ElasticSearchIntegrationTest method before.

@Before
public void before() {
    elasticsearchTemplate.deleteIndex(Article.class);
    elasticsearchTemplate.createIndex(Article.class);
    // don't call putMapping() to test the default mappings
    Article article = new Article("Spring Data Elasticsearch");
    article.setAuthors(asList(johnSmith, johnDoe));
    article.setTags("elasticsearch", "spring data");
    articleService.save(article);
    article = new Article("Search engines");
    article.setAuthors(asList(johnDoe));
    article.setTags("search engines", "tutorial");
    articleService.save(article);
    article = new Article("Second Article About Elasticsearch");
    article.setAuthors(asList(johnSmith));
    article.setTags("elasticsearch", "spring data");
    articleService.save(article);
    article = new Article("Elasticsearch Tutorial");
    article.setAuthors(asList(johnDoe));
    article.setTags("elasticsearch");
    articleService.save(article);
}
Also used : Article(com.baeldung.spring.data.es.model.Article) Before(org.junit.Before)

Example 4 with Article

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

the class ElasticSearchQueryIntegrationTest method givenMultimatchQuery_whenDoSearch_thenAllProvidedFieldsMatch.

@Test
public void givenMultimatchQuery_whenDoSearch_thenAllProvidedFieldsMatch() {
    final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(multiMatchQuery("tutorial").field("title").field("tags").type(MultiMatchQueryBuilder.Type.BEST_FIELDS)).build();
    final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
    assertEquals(2, 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 5 with Article

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

the class ElasticSearchQueryIntegrationTest method givenBoolQuery_whenQueryByAuthorsName_thenFoundArticlesByThatAuthorAndFilteredTag.

@Test
public void givenBoolQuery_whenQueryByAuthorsName_thenFoundArticlesByThatAuthorAndFilteredTag() {
    final QueryBuilder builder = boolQuery().must(nestedQuery("authors", boolQuery().must(termQuery("authors.name", "doe")))).filter(termQuery("tags", "elasticsearch"));
    final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder).build();
    final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
    assertEquals(2, articles.size());
}
Also used : SearchQuery(org.springframework.data.elasticsearch.core.query.SearchQuery) Article(com.baeldung.spring.data.es.model.Article) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) MultiMatchQueryBuilder(org.elasticsearch.index.query.MultiMatchQueryBuilder) NativeSearchQueryBuilder(org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder) 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