Search in sources :

Example 11 with NativeSearchQueryBuilder

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

Example 12 with NativeSearchQueryBuilder

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

the class ElasticSearchIntegrationTest method givenPersistedArticles_whenUseRegexQuery_thenRightArticlesFound.

@Test
public void givenPersistedArticles_whenUseRegexQuery_thenRightArticlesFound() {
    final SearchQuery searchQuery = new NativeSearchQueryBuilder().withFilter(regexpQuery("title", ".*data.*")).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 13 with NativeSearchQueryBuilder

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

the class ElasticSearchQueryIntegrationTest method givenPhraseWithType_whenUseFuzziness_thenQueryMatches.

@Test
public void givenPhraseWithType_whenUseFuzziness_thenQueryMatches() {
    final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "spring date elasticserch").operator(AND).fuzziness(Fuzziness.ONE).prefixLength(3)).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 14 with NativeSearchQueryBuilder

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

the class ElasticSearchQueryIntegrationTest method givenFullTitle_whenRunMatchQueryOnVerbatimField_thenDocIsFound.

@Test
public void givenFullTitle_whenRunMatchQueryOnVerbatimField_thenDocIsFound() {
    SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title.verbatim", "Second Article About Elasticsearch")).build();
    List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
    assertEquals(1, articles.size());
    searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title.verbatim", "Second Article About")).build();
    articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
    assertEquals(0, 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 15 with NativeSearchQueryBuilder

use of org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder in project xm-ms-entity by xm-online.

the class XmEntityPermittedSearchRepository method searchByQueryAndTypeKey.

/**
 * Search for XmEntity by type key and query.
 * @param query the query
 * @param typeKey the type key
 * @param pageable the page info
 * @param privilegeKey the privilege key
 * @return permitted entities
 */
public Page<XmEntity> searchByQueryAndTypeKey(String query, String typeKey, Pageable pageable, String privilegeKey) {
    String permittedQuery = buildPermittedQuery(query, privilegeKey);
    // TODO disable elastic analized to field "typeKey" and fix this
    val prefix = (typeKey + ".").toLowerCase();
    val typeKeyQuery = boolQuery().should(matchQuery(TYPE_KEY, typeKey)).should(prefixQuery(TYPE_KEY, prefix)).minimumNumberShouldMatch(1);
    val esQuery = isEmpty(permittedQuery) ? boolQuery().must(typeKeyQuery) : typeKeyQuery.must(simpleQueryStringQuery(permittedQuery));
    log.debug("Executing DSL '{}'", esQuery);
    NativeSearchQuery queryBuilder = new NativeSearchQueryBuilder().withQuery(esQuery).withPageable(pageable).build();
    return getElasticsearchTemplate().queryForPage(queryBuilder, XmEntity.class);
}
Also used : lombok.val(lombok.val) NativeSearchQueryBuilder(org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder) NativeSearchQuery(org.springframework.data.elasticsearch.core.query.NativeSearchQuery)

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