1.fuzzy search from journalSong to songInfo

main
Gary 9 months ago
parent df98d5aef7
commit d39a95bf3c

@ -23,10 +23,12 @@ import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import com.luoo.music.pojo.Journal; import com.luoo.music.pojo.Journal;
import com.luoo.music.pojo.JournalSong; import com.luoo.music.pojo.JournalSong;
import com.luoo.music.pojo.SongInfo;
import com.luoo.music.pojo.Tag; import com.luoo.music.pojo.Tag;
import com.luoo.music.service.JournalService; import com.luoo.music.service.JournalService;
import com.luoo.music.service.JournalSongService; import com.luoo.music.service.JournalSongService;
import com.luoo.music.service.SearchService; import com.luoo.music.service.SearchService;
import com.luoo.music.service.SongInfoService;
import com.luoo.music.service.TagService; import com.luoo.music.service.TagService;
import annotation.GlobalInterceptor; import annotation.GlobalInterceptor;
import annotation.VerifyParam; import annotation.VerifyParam;
@ -50,7 +52,7 @@ public class SearchController {
private JournalService journalService; private JournalService journalService;
@Autowired @Autowired
private JournalSongService journalSongService; private SongInfoService songInfoService;
@Autowired @Autowired
private SearchService searchService; private SearchService searchService;
@ -76,7 +78,7 @@ public class SearchController {
@VerifyParam(required = true) FuzzySearchReq query) throws InterruptedException, ExecutionException { @VerifyParam(required = true) FuzzySearchReq query) throws InterruptedException, ExecutionException {
PageRequest pageRequest = PageRequest.of(getPageNum(query.getPageNum()), getPageSize(query.getPageSize())); PageRequest pageRequest = PageRequest.of(getPageNum(query.getPageNum()), getPageSize(query.getPageSize()));
Page<JournalSong> pageResults=journalSongService.fuzzySearch(query.getKeyword(),pageRequest); Page<SongInfo> pageResults=songInfoService.fuzzySearch(query.getKeyword(),pageRequest);
List<SongRespDTO> list = pageResults.stream().map(SongMapper::getSongRespDTO) List<SongRespDTO> list = pageResults.stream().map(SongMapper::getSongRespDTO)
.collect(Collectors.toList()); .collect(Collectors.toList());
return Result.success(new PageResult<SongRespDTO>(Long.valueOf(list.size()), list)); return Result.success(new PageResult<SongRespDTO>(Long.valueOf(list.size()), list));

@ -156,26 +156,4 @@ public class JournalSongService {
public List<JournalSong> orderByField(List<String> objectIds) { public List<JournalSong> orderByField(List<String> objectIds) {
return journalSongDao.orderByField(objectIds); return journalSongDao.orderByField(objectIds);
} }
public Page<JournalSong> fuzzySearch(String keyword, PageRequest pageRequest) {
Specification<JournalSong> specification = fuzzySpecification(keyword);
return journalSongDao.findAll(specification,pageRequest);
}
private Specification<JournalSong> fuzzySpecification(String keyword) {
return new Specification<JournalSong>() {
private static final long serialVersionUID = 1L;
@Override
public Predicate toPredicate(Root<JournalSong> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
List<Predicate> predicateList = new ArrayList<Predicate>();
predicateList.add(criteriaBuilder.like(root.get("name").as(String.class),"%"+keyword+"%"));
predicateList.add(criteriaBuilder.like(root.get("artist").as(String.class),"%"+keyword+"%"));
predicateList.add(criteriaBuilder.like(root.get("album").as(String.class),"%"+keyword+"%"));
//predicateList.add(criteriaBuilder.like(root.get("journalNo").as(String.class),"%"+keyword+"%"));
query.orderBy(criteriaBuilder.asc(root.get("name")));
return criteriaBuilder.or(predicateList.toArray(new Predicate[predicateList.size()]));
}
};
}
} }

@ -4,9 +4,19 @@ import com.luoo.music.dao.SongInfoDao;
import com.luoo.music.pojo.SongInfo; import com.luoo.music.pojo.SongInfo;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
@Service @Service
public class SongInfoService { public class SongInfoService {
@ -24,4 +34,24 @@ public class SongInfoService {
public SongInfo findById(String id) { public SongInfo findById(String id) {
return songInfoDao.getSongById(id); return songInfoDao.getSongById(id);
} }
public Page<SongInfo> fuzzySearch(String keyword, PageRequest pageRequest) {
Specification<SongInfo> specification = fuzzySpecification(keyword);
return songInfoDao.findAll(specification,pageRequest);
}
private Specification<SongInfo> fuzzySpecification(String keyword) {
return new Specification<SongInfo>() {
private static final long serialVersionUID = 1L;
@Override
public Predicate toPredicate(Root<SongInfo> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
List<Predicate> predicateList = new ArrayList<Predicate>();
predicateList.add(criteriaBuilder.like(root.get("name").as(String.class),"%"+keyword+"%"));
predicateList.add(criteriaBuilder.like(root.get("artist").as(String.class),"%"+keyword+"%"));
predicateList.add(criteriaBuilder.like(root.get("album").as(String.class),"%"+keyword+"%"));
query.orderBy(criteriaBuilder.asc(root.get("name")));
return criteriaBuilder.or(predicateList.toArray(new Predicate[predicateList.size()]));
}
};
}
} }

Loading…
Cancel
Save