diff --git a/luoo_music/src/main/java/com/luoo/music/controller/SearchController.java b/luoo_music/src/main/java/com/luoo/music/controller/SearchController.java index a3cfb7b..07a3372 100644 --- a/luoo_music/src/main/java/com/luoo/music/controller/SearchController.java +++ b/luoo_music/src/main/java/com/luoo/music/controller/SearchController.java @@ -1,65 +1,28 @@ package com.luoo.music.controller; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.time.LocalDateTime; -import java.util.Arrays; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; import java.util.List; -import java.util.Map; -import java.util.Set; import java.util.stream.Collectors; -import javax.annotation.PostConstruct; - -import com.apifan.common.random.RandomSource; -import com.apifan.common.random.entity.Poem; -import com.luoo.music.dao.CommentDao; -import com.luoo.music.dao.TagDao; import com.luoo.music.dto.mapper.JournalMapper; import com.luoo.music.dto.mapper.SongMapper; -import com.luoo.music.dto.request.JournalQueryReq; -import com.luoo.music.dto.response.JournalFilterDTO; -import com.luoo.music.dto.response.JournalRespDTO; import com.luoo.music.dto.response.SearchCategoryDTO; import com.luoo.music.dto.response.SearchResultDTO; -import com.luoo.music.dto.response.TotalCommentVo; -import com.luoo.music.pojo.Comment; import io.swagger.annotations.*; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.core.io.ClassPathResource; -import org.springframework.data.domain.PageRequest; -import org.springframework.data.domain.Pageable; -import org.springframework.data.mongodb.core.MongoTemplate; -import org.springframework.data.mongodb.core.aggregation.Aggregation; -import org.springframework.data.mongodb.core.aggregation.AggregationResults; -import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.web.bind.annotation.*; import com.luoo.music.pojo.Journal; import com.luoo.music.pojo.JournalSong; import com.luoo.music.pojo.Tag; import com.luoo.music.service.JournalService; import com.luoo.music.service.JournalSongService; +import com.luoo.music.service.SearchService; import com.luoo.music.service.TagService; -import com.luoo.music.service.UserCollectInfoService; import annotation.GlobalInterceptor; import annotation.VerifyParam; -import api.PageResult; import api.Result; import constants.Constants; -import dto.UserLoginDto; -import enums.CollectTypeEnum; -import enums.DateTimePatternEnum; -import lombok.SneakyThrows; -import util.DateUtil; -import util.JwtUtil; -import util.StringTools; /** * 控制器层 @@ -78,6 +41,9 @@ public class SearchController { @Autowired private JournalSongService journalSongService; + @Autowired + private SearchService searchService; + @Autowired private TagService tagService; @@ -106,6 +72,16 @@ public class SearchController { return Result.success(searchResultDTO); } + @ApiOperation(value = "3.搜索自动补全") + @ApiImplicitParams({ @ApiImplicitParam(name = "query", value = "搜索词", required = true) }) + @GetMapping("/autoComplete/{query}") + @GlobalInterceptor(checkAppUserLogin = true) + public Result> autoComplete( + @RequestHeader(value = "Authorization", required = true) String authorization, + @PathVariable @VerifyParam(required = true) String query) { + return Result.success(searchService.autoComplete(query)); + } + private SearchCategoryDTO getSearchCategoryDTO(Tag tag) { SearchCategoryDTO searchCategoryDTO = new SearchCategoryDTO(); BeanUtils.copyProperties(tag, searchCategoryDTO); diff --git a/luoo_music/src/main/java/com/luoo/music/dao/JournalDao.java b/luoo_music/src/main/java/com/luoo/music/dao/JournalDao.java index 374a226..313989d 100644 --- a/luoo_music/src/main/java/com/luoo/music/dao/JournalDao.java +++ b/luoo_music/src/main/java/com/luoo/music/dao/JournalDao.java @@ -79,5 +79,8 @@ public interface JournalDao extends JpaRepository,JpaSpecificati @Query(value = "select id from tb_journal where state='1' and is_publish='1' order by rand() limit ?1", nativeQuery = true) List random(int limit); + + @Query(value = "select * from tb_journal where state='1' and is_publish='1'", nativeQuery = true) + List findValidJournals(); } diff --git a/luoo_music/src/main/java/com/luoo/music/service/SearchService.java b/luoo_music/src/main/java/com/luoo/music/service/SearchService.java new file mode 100644 index 0000000..a44e842 --- /dev/null +++ b/luoo_music/src/main/java/com/luoo/music/service/SearchService.java @@ -0,0 +1,102 @@ +package com.luoo.music.service; + +import java.nio.file.Files; +import java.nio.file.Paths; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.ListIterator; +import java.util.Map; +import java.util.Set; +import java.util.UUID; + +import javax.annotation.PostConstruct; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.connection.RedisZSetCommands.Limit; +import org.springframework.data.redis.connection.RedisZSetCommands.Range; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.stereotype.Service; +import org.springframework.util.StringUtils; + +import com.luoo.music.dao.JournalDao; +import com.luoo.music.dao.JournalSongDao; + +import enums.DateTimePatternEnum; +import lombok.SneakyThrows; +import util.StringTools; + +@Service +public class SearchService { + private static final String REDIS_AUTO_COMPLETE = "redis_auto_complete:"; + + @Autowired + private RedisTemplate redisTemplate; + @Autowired + private JournalDao journalDao; + @Autowired + private JournalSongDao journalSongDao; + + @PostConstruct + private void init() { + /* + * DateTimeFormatter formatter = + * DateTimeFormatter.ofPattern(DateTimePatternEnum.YYYY_MM_DD_HH_MM_SS. + * getPattern()); + * + * Map map=getMap(); journalDao.findAll().forEach(j->{ + * if(map.containsKey(j.getJournalNo())) { + * j.setPubTime(LocalDateTime.parse(map.get(j.getJournalNo()), formatter)); + * journalDao.save(j); } + * + * }); System.exit(-1); + */ + //addKeyWord("中国"); + //searchKey("中"); + //System.exit(-1); + //searchKey("绵"); + //System.exit(-1); + /* + * journalDao.findValidJournals().forEach(j->{ addKeyWord(j.getTitle()); + * addKeyWord(j.getJournalNo()); }); System.exit(-1); + */ + /* + * journalDao.findEnableJournals().parallelStream().forEach(j->{ + * //addKeyWord(j.getTitle()); addKeyWord(j.getJournalNo()); }); + */ + + /* + * journalSongDao.findAll().parallelStream().forEach(s->{ + * addKeyWord(s.getName()); addKeyWord(s.getArtist()); addKeyWord(s.getAlbum()); + * }); System.exit(-1); + */ + } + + + public List autoComplete(String query) { + Set values=redisTemplate.opsForZSet().rangeByScore(REDIS_AUTO_COMPLETE+query, 0, Double.MAX_VALUE, 0, 10); + return new ArrayList<>(values); + } + + @SneakyThrows + public void addKeyWord(String keyword) { + if(!StringTools.isEmpty(keyword)) { + for(int i=1;i values=redisTemplate.opsForZSet().reverseRangeByScore(REDIS_AUTO_COMPLETE+query, 0, Double.MAX_VALUE, 0, 10); + //Set values=redisTemplate.opsForZSet().rangeWithScores(REDIS_AUTO_COMPLETE+query, 0,10); + //Set values=redisTemplate.opsForZSet().rangeByLex(REDIS_AUTO_COMPLETE+query, Range.unbounded(), Limit.limit().count(10)); + //Set values=redisTemplate.opsForZSet().range(REDIS_AUTO_COMPLETE+query, 0, -1); + values.forEach(System.out::println); + } +} diff --git a/luoo_music/src/test/java/com/luoo/music/service/SearchServiceTest.java b/luoo_music/src/test/java/com/luoo/music/service/SearchServiceTest.java new file mode 100644 index 0000000..e7b3005 --- /dev/null +++ b/luoo_music/src/test/java/com/luoo/music/service/SearchServiceTest.java @@ -0,0 +1,29 @@ +package com.luoo.music.service; + +import static org.junit.Assert.*; + +import java.io.UnsupportedEncodingException; +import java.nio.charset.Charset; + +import org.junit.Test; + +import lombok.SneakyThrows; + +public class SearchServiceTest { + + @Test + public void test() { + String chKeyword="1012"; + print(chKeyword); + } + @SneakyThrows + private void print(String keyword) { + for(int i=1;i