Search in sources :

Example 6 with SearchQuery

use of org.springframework.data.elasticsearch.core.query.SearchQuery in project tutorials by eugenp.

the class ElasticSearchQueryIntegrationTest method givenOneTermFromTitle_whenRunMatchQuery_thenDocIsFound.

@Test
public void givenOneTermFromTitle_whenRunMatchQuery_thenDocIsFound() {
    final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "Engines Solutions")).build();
    final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
    assertEquals(1, articles.size());
    assertEquals("Search engines", articles.get(0).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)

Example 7 with SearchQuery

use of org.springframework.data.elasticsearch.core.query.SearchQuery in project tutorials by eugenp.

the class ElasticSearchQueryIntegrationTest method givenNestedObject_whenQueryByAuthorsName_thenFoundArticlesByThatAuthor.

@Test
public void givenNestedObject_whenQueryByAuthorsName_thenFoundArticlesByThatAuthor() {
    final QueryBuilder builder = nestedQuery("authors", boolQuery().must(termQuery("authors.name", "smith")));
    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)

Example 8 with SearchQuery

use of org.springframework.data.elasticsearch.core.query.SearchQuery 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 9 with SearchQuery

use of org.springframework.data.elasticsearch.core.query.SearchQuery 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)

Example 10 with SearchQuery

use of org.springframework.data.elasticsearch.core.query.SearchQuery in project tutorials by eugenp.

the class ElasticSearchIntegrationTest method givenSavedDoc_whenDelete_thenRemovedFromIndex.

@Test
public void givenSavedDoc_whenDelete_thenRemovedFromIndex() {
    final String articleTitle = "Spring Data Elasticsearch";
    final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", articleTitle).minimumShouldMatch("75%")).build();
    final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
    assertEquals(1, articles.size());
    final long count = articleService.count();
    articleService.delete(articles.get(0));
    assertEquals(count - 1, articleService.count());
}
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)13 Test (org.junit.Test)13 NativeSearchQueryBuilder (org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder)13 SearchQuery (org.springframework.data.elasticsearch.core.query.SearchQuery)13 MultiMatchQueryBuilder (org.elasticsearch.index.query.MultiMatchQueryBuilder)2 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)2