Search in sources :

Example 1 with ShortUrlVO

use of com.github.lybgeek.elasticsearch.model.ShortUrlVO 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 2 with ShortUrlVO

use of com.github.lybgeek.elasticsearch.model.ShortUrlVO in project springboot-learning by lyb-geek.

the class ElasticsearchApplicationTest method testSaveShortUrlEs.

@Test
public void testSaveShortUrlEs() {
    ShortUrlDTO shortUrlDTO = ShortUrlDTO.builder().longUrl("https://www.baidu.com").urlName("百度").remark("百度一下,你就知道").build();
    ShortUrlVO shortUrlVO = shortUrlEsService.save(shortUrlDTO);
    Assert.assertNotNull(shortUrlVO);
    System.out.println(shortUrlVO);
}
Also used : ShortUrlVO(com.github.lybgeek.elasticsearch.model.ShortUrlVO) ShortUrlDTO(com.github.lybgeek.shorturl.dto.ShortUrlDTO) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

ShortUrlVO (com.github.lybgeek.elasticsearch.model.ShortUrlVO)2 ShortUrlDTO (com.github.lybgeek.shorturl.dto.ShortUrlDTO)2 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)1 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)1 Test (org.junit.Test)1 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)1 Pageable (org.springframework.data.domain.Pageable)1 Sort (org.springframework.data.domain.Sort)1 NativeSearchQueryBuilder (org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder)1