Search in sources :

Example 1 with ShortUrlDTO

use of com.github.lybgeek.shorturl.dto.ShortUrlDTO in project springboot-learning by lyb-geek.

the class CustomShortUrlEsServiceImpl method pageShortUrl.

@Override
public PageResult<ShortUrlDTO> pageShortUrl(PageQuery<ShortUrlDTO> pageQuery) {
    int pageNo = ObjectUtils.isNotEmpty(pageQuery.getPageNo()) && pageQuery.getPageNo() > 0 ? pageQuery.getPageNo() : 1;
    int pageSize = ObjectUtils.isNotEmpty(pageQuery.getPageSize()) ? pageQuery.getPageSize() : 10;
    ShortUrlDTO queryParams = pageQuery.getQueryParams();
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    int from = (pageNo - 1) * pageSize;
    searchSourceBuilder.from(from).size(pageSize).sort("id", SortOrder.DESC);
    if (ObjectUtils.isNotEmpty(queryParams)) {
        BoolQueryBuilder boolBuilder = QueryBuilders.boolQuery();
        if (queryParams != null) {
            if (StringUtils.isNotBlank(queryParams.getLongUrl())) {
                QueryBuilder builder = QueryBuilders.matchQuery("longUrl", queryParams.getLongUrl());
                boolBuilder.must(builder);
            }
            if (StringUtils.isNotBlank(queryParams.getRemark())) {
                QueryBuilder builder = QueryBuilders.matchQuery("remark", queryParams.getRemark());
                boolBuilder.must(builder);
            }
            if (StringUtils.isNotBlank(queryParams.getUrlName())) {
                QueryBuilder builder = QueryBuilders.matchQuery("urlName", queryParams.getUrlName());
                boolBuilder.must(builder);
            }
            searchSourceBuilder.query(boolBuilder);
            // 设置高亮
            HighlightBuilder highlightBuilder = new HighlightBuilder();
            HighlightBuilder.Field highlightUrlName = new HighlightBuilder.Field("urlName").preTags("<strong>").postTags("</strong>");
            highlightUrlName.highlighterType("unified");
            highlightBuilder.field(highlightUrlName);
            searchSourceBuilder.highlighter(highlightBuilder);
        }
    }
    PageResult<ShortUrlDTO> pageResult = elasticsearchHelper.pageSearch(ElasticsearchConstant.SHORT_URL_INDEX, searchSourceBuilder, ShortUrlDTO.class);
    return pageResult;
}
Also used : BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) ShortUrlDTO(com.github.lybgeek.shorturl.dto.ShortUrlDTO) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) HighlightBuilder(org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder)

Example 2 with ShortUrlDTO

use of com.github.lybgeek.shorturl.dto.ShortUrlDTO in project springboot-learning by lyb-geek.

the class ShortUrlEsServiceImpl method pageShortUrl.

@Override
public PageResult<ShortUrlVO> pageShortUrl(PageQuery<ShortUrlDTO> pageQuery) {
    int pageNo = ObjectUtils.isNotEmpty(pageQuery.getPageNo()) ? pageQuery.getPageNo() - 1 : 0;
    int pageSize = ObjectUtils.isNotEmpty(pageQuery.getPageSize()) ? pageQuery.getPageSize() : 10;
    Sort sort = new Sort(Direction.DESC, "id");
    Pageable pageable = PageRequest.of(pageNo, pageSize, sort);
    ShortUrlDTO queryParams = pageQuery.getQueryParams();
    BoolQueryBuilder boolBuilder = QueryBuilders.boolQuery();
    if (queryParams != null) {
        if (StringUtils.isNotBlank(queryParams.getLongUrl())) {
            QueryBuilder builder = QueryBuilders.matchQuery("longUrl", queryParams.getLongUrl());
            boolBuilder.must(builder);
        }
        if (StringUtils.isNotBlank(queryParams.getRemark())) {
            QueryBuilder builder = QueryBuilders.matchQuery("remark", queryParams.getRemark());
            boolBuilder.must(builder);
        }
        if (StringUtils.isNotBlank(queryParams.getUrlName())) {
            QueryBuilder builder = QueryBuilders.matchQuery("urlName", queryParams.getUrlName());
            boolBuilder.must(builder);
        }
    }
    Page<ShortUrlVO> page = shortUrlEsRepository.search(boolBuilder, pageable);
    return PageUtil.INSTANCE.getPage(page, page.getContent());
}
Also used : Pageable(org.springframework.data.domain.Pageable) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) ShortUrlVO(com.github.lybgeek.elasticsearch.model.ShortUrlVO) Sort(org.springframework.data.domain.Sort) ShortUrlDTO(com.github.lybgeek.shorturl.dto.ShortUrlDTO) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) NativeSearchQueryBuilder(org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder)

Example 3 with ShortUrlDTO

use of com.github.lybgeek.shorturl.dto.ShortUrlDTO in project springboot-learning by lyb-geek.

the class ElasticsearchApplicationTest method testListCustomShortUrls.

@Test
public void testListCustomShortUrls() {
    ShortUrlDTO dto = new ShortUrlDTO();
    dto.setUrlName("美团");
    // dto.setRemark("门户");
    List<ShortUrlDTO> shortUrlDTOS = customShortUrlEsService.listShortUrls(dto);
    System.out.println(shortUrlDTOS);
}
Also used : ShortUrlDTO(com.github.lybgeek.shorturl.dto.ShortUrlDTO) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 4 with ShortUrlDTO

use of com.github.lybgeek.shorturl.dto.ShortUrlDTO in project springboot-learning by lyb-geek.

the class ElasticsearchApplicationTest method testPage.

@Test
public void testPage() {
    PageQuery pageQuery = new PageQuery<>().setPageNo(1).setPageSize(5);
    ShortUrlDTO dto = new ShortUrlDTO();
    dto.setUrlName("门户");
    // dto.setRemark("门户");
    pageQuery.setQueryParams(dto);
    PageResult<ShortUrlDTO> pageResult = shortUrlService.pageShortUrl(pageQuery);
    System.out.println(pageResult);
}
Also used : PageQuery(com.github.lybgeek.common.model.PageQuery) ShortUrlDTO(com.github.lybgeek.shorturl.dto.ShortUrlDTO) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 5 with ShortUrlDTO

use of com.github.lybgeek.shorturl.dto.ShortUrlDTO in project springboot-learning by lyb-geek.

the class ElasticsearchApplicationTest method testCustomPageShortUrl.

@Test
public void testCustomPageShortUrl() {
    PageQuery pageQuery = new PageQuery<>().setPageNo(5).setPageSize(5);
    ShortUrlDTO dto = new ShortUrlDTO();
    // dto.setUrlName("优酷");
    // dto.setRemark("视频");
    // pageQuery.setQueryParams(dto);
    PageResult<ShortUrlDTO> pageResult = customShortUrlEsService.pageShortUrl(pageQuery);
    System.out.println(pageResult);
}
Also used : PageQuery(com.github.lybgeek.common.model.PageQuery) ShortUrlDTO(com.github.lybgeek.shorturl.dto.ShortUrlDTO) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

ShortUrlDTO (com.github.lybgeek.shorturl.dto.ShortUrlDTO)14 Test (org.junit.Test)11 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)11 PageQuery (com.github.lybgeek.common.model.PageQuery)2 ShortUrlVO (com.github.lybgeek.elasticsearch.model.ShortUrlVO)2 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)2 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)2 EsEntity (com.github.lybgeek.common.elasticsearch.model.EsEntity)1 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)1 HighlightBuilder (org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder)1 EventListener (org.springframework.context.event.EventListener)1 Pageable (org.springframework.data.domain.Pageable)1 Sort (org.springframework.data.domain.Sort)1 NativeSearchQueryBuilder (org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder)1 Async (org.springframework.scheduling.annotation.Async)1