1.add autoComplete for search

main
Gary 9 months ago
parent 33df3e7aef
commit 72bb00b392

@ -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<List<String>> 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);

@ -79,5 +79,8 @@ public interface JournalDao extends JpaRepository<Journal,String>,JpaSpecificati
@Query(value = "select id from tb_journal where state='1' and is_publish='1' order by rand() limit ?1", nativeQuery = true)
List<String> random(int limit);
@Query(value = "select * from tb_journal where state='1' and is_publish='1'", nativeQuery = true)
List<Journal> findValidJournals();
}

@ -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<String,String> 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<String> autoComplete(String query) {
Set<String> 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<keyword.length()+1;i++) {
String sub=keyword.substring(0, i);
String encodedString=new String(sub.getBytes(),"UTF-8");
redisTemplate.opsForZSet().add(REDIS_AUTO_COMPLETE+encodedString,keyword,0);
}
}
}
private void searchKey(String query) {
//redisTemplate.opsForZSet().intersectAndStore(query, null, query)
Set<String> values=redisTemplate.opsForZSet().reverseRangeByScore(REDIS_AUTO_COMPLETE+query, 0, Double.MAX_VALUE, 0, 10);
//Set<String> values=redisTemplate.opsForZSet().rangeWithScores(REDIS_AUTO_COMPLETE+query, 0,10);
//Set<String> values=redisTemplate.opsForZSet().rangeByLex(REDIS_AUTO_COMPLETE+query, Range.unbounded(), Limit.limit().count(10));
//Set<String> values=redisTemplate.opsForZSet().range(REDIS_AUTO_COMPLETE+query, 0, -1);
values.forEach(System.out::println);
}
}

@ -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<keyword.length()+1;i++) {
String sub=keyword.substring(0, i);
String encodedString=new String(sub.getBytes(),"UTF-8");
System.out.println(encodedString);
//String utf8String=new String(encodedString.getBytes("UTF-8"),"UTF-8");
//System.out.println(utf8String);
}
}
}
Loading…
Cancel
Save