Search in sources :

Example 16 with CriteriaQuery

use of org.springframework.data.elasticsearch.core.query.CriteriaQuery in project spring-study by backtony.

the class MemberSearchQueryRepository method findByCondition.

public List<MemberDocument> findByCondition(SearchCondition searchCondition, Pageable pageable) {
    CriteriaQuery query = createConditionCriteriaQuery(searchCondition).setPageable(pageable);
    SearchHits<MemberDocument> search = operations.search(query, MemberDocument.class);
    return search.stream().map(SearchHit::getContent).collect(Collectors.toList());
}
Also used : MemberDocument(com.example.elasticsearch.member.domain.MemberDocument) CriteriaQuery(org.springframework.data.elasticsearch.core.query.CriteriaQuery)

Example 17 with CriteriaQuery

use of org.springframework.data.elasticsearch.core.query.CriteriaQuery in project java-demos by powerLeePlus.

the class GoodsController method getList.

// 根据name搜索
@GetMapping("search/{name}")
public SearchHits<GoodsInfo> getList(String name) {
    // es搜索默认第一页页码是0
    // QueryBuilder queryBuilder = getEntitySearchQuery(pageNumber, PAGESIZE, query);
    CriteriaQuery query = new CriteriaQuery(Criteria.where("name").contains(name));
    SearchHits<GoodsInfo> goodsInfoSearchHits = elasticsearchRestTemplate.search(query, GoodsInfo.class);
    return goodsInfoSearchHits;
}
Also used : CriteriaQuery(org.springframework.data.elasticsearch.core.query.CriteriaQuery) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 18 with CriteriaQuery

use of org.springframework.data.elasticsearch.core.query.CriteriaQuery in project spring-study by backtony.

the class MemberSearchQueryRepository method findByStartWithNickname.

public List<MemberDocument> findByStartWithNickname(String nickname, Pageable pageable) {
    Criteria criteria = Criteria.where("nickname").startsWith(nickname);
    Query query = new CriteriaQuery(criteria).setPageable(pageable);
    SearchHits<MemberDocument> search = operations.search(query, MemberDocument.class);
    return search.stream().map(SearchHit::getContent).collect(Collectors.toList());
}
Also used : Query(org.springframework.data.elasticsearch.core.query.Query) CriteriaQuery(org.springframework.data.elasticsearch.core.query.CriteriaQuery) MemberDocument(com.example.elasticsearch.member.domain.MemberDocument) CriteriaQuery(org.springframework.data.elasticsearch.core.query.CriteriaQuery) Criteria(org.springframework.data.elasticsearch.core.query.Criteria)

Example 19 with CriteriaQuery

use of org.springframework.data.elasticsearch.core.query.CriteriaQuery in project spring-study by backtony.

the class MemberSearchQueryRepository method findByMatchesDescription.

/**
 * 일반적으로 원하는 description이 매칭되는 것들에 맞게 score 계산해서 찾아준다.
 */
public List<MemberDocument> findByMatchesDescription(String description, Pageable pageable) {
    Criteria criteria = Criteria.where("description").matches(description);
    Query query = new CriteriaQuery(criteria).setPageable(pageable);
    SearchHits<MemberDocument> search = operations.search(query, MemberDocument.class);
    return search.stream().map(SearchHit::getContent).collect(Collectors.toList());
}
Also used : Query(org.springframework.data.elasticsearch.core.query.Query) CriteriaQuery(org.springframework.data.elasticsearch.core.query.CriteriaQuery) MemberDocument(com.example.elasticsearch.member.domain.MemberDocument) CriteriaQuery(org.springframework.data.elasticsearch.core.query.CriteriaQuery) Criteria(org.springframework.data.elasticsearch.core.query.Criteria)

Example 20 with CriteriaQuery

use of org.springframework.data.elasticsearch.core.query.CriteriaQuery in project spring-study by backtony.

the class MemberSearchQueryRepository method findByContainsDescription.

/**
 * 형태소 분석기 nori를 사용할 경우 주의해야한다.
 * MemberDocument의 description이 text타입이기 때문에 노리 분석기가 토큰화 시키는데
 * member.json 파일에 보면 안녕하세요 ~~ 이런식으로 되어있는데 토큰화 될 때 안녕, 하, 시, 어요 이렇게 토큰화된다.
 * 따라서 요청 description에 "안녕하세요"로 들어올 경우 쿼리가 *안녕하세요*로 나가기 때문에 찾을 수 없게 된다.
 * 따라서 Contains를 사용할 경우 노리 분석기가 어떻게 동작하는지 잘 인지하고 사용해야 한다.
 *  contains는 앞쪽에 *가 붙어서 쿼리 성능을 급격히 저하시키기 때문에 웬만하면 사용하지 않는 것이 좋다.
 *
 *  nori 형태소가 어떻게 토큰화 하여 저장하는지는 키바나 콘솔에서 아래와 같이 검색하보면 어떤 식으로 토큰화 되는지 확인 가능
 *  {
 *   "tokenizer": "nori_tokenizer",
 *   "text": "원하는 내용"
 * }
 */
public List<MemberDocument> findByContainsDescription(String description, Pageable pageable) {
    Criteria criteria = Criteria.where("description").contains(description);
    Query query = new CriteriaQuery(criteria).setPageable(pageable);
    SearchHits<MemberDocument> search = operations.search(query, MemberDocument.class);
    return search.stream().map(SearchHit::getContent).collect(Collectors.toList());
}
Also used : Query(org.springframework.data.elasticsearch.core.query.Query) CriteriaQuery(org.springframework.data.elasticsearch.core.query.CriteriaQuery) MemberDocument(com.example.elasticsearch.member.domain.MemberDocument) CriteriaQuery(org.springframework.data.elasticsearch.core.query.CriteriaQuery) Criteria(org.springframework.data.elasticsearch.core.query.Criteria)

Aggregations

CriteriaQuery (org.springframework.data.elasticsearch.core.query.CriteriaQuery)38 Criteria (org.springframework.data.elasticsearch.core.query.Criteria)30 Test (org.junit.jupiter.api.Test)29 SpringIntegrationTest (org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest)16 DisplayName (org.junit.jupiter.api.DisplayName)10 Query (org.springframework.data.elasticsearch.core.query.Query)8 MemberDocument (com.example.elasticsearch.member.domain.MemberDocument)4 SearchRequest (org.elasticsearch.action.search.SearchRequest)4 SearchHits (org.springframework.data.elasticsearch.core.SearchHits)2 StringQuery (org.springframework.data.elasticsearch.core.query.StringQuery)2 ParametersParameterAccessor (org.springframework.data.repository.query.ParametersParameterAccessor)2 Method (java.lang.reflect.Method)1 LocalDate (java.time.LocalDate)1 Date (java.util.Date)1 List (java.util.List)1 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)1 SearchHitsImpl (org.springframework.data.elasticsearch.core.SearchHitsImpl)1 SearchPage (org.springframework.data.elasticsearch.core.SearchPage)1 GeoJsonPoint (org.springframework.data.elasticsearch.core.geo.GeoJsonPoint)1 GeoPoint (org.springframework.data.elasticsearch.core.geo.GeoPoint)1