Search in sources :

Example 1 with NativeSearchQueryBuilder

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

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

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

Example 4 with NativeSearchQueryBuilder

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

the class ElasticSearchQueryIntegrationTest method givenPartTitle_whenRunMatchQuery_thenDocIsFound.

@Test
public void givenPartTitle_whenRunMatchQuery_thenDocIsFound() {
    final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "elasticsearch data")).build();
    final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
    assertEquals(3, 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 NativeSearchQueryBuilder

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

the class ElasticSearchQueryIntegrationTest method givenFullTitle_whenRunMatchQuery_thenDocIsFound.

@Test
public void givenFullTitle_whenRunMatchQuery_thenDocIsFound() {
    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)

Aggregations

NativeSearchQueryBuilder (org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder)15 Article (com.baeldung.spring.data.es.model.Article)13 Test (org.junit.Test)13 SearchQuery (org.springframework.data.elasticsearch.core.query.SearchQuery)13 MultiMatchQueryBuilder (org.elasticsearch.index.query.MultiMatchQueryBuilder)2 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)2 lombok.val (lombok.val)1 NativeSearchQuery (org.springframework.data.elasticsearch.core.query.NativeSearchQuery)1