parent
33df3e7aef
commit
72bb00b392
@ -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…
Reference in new issue