Hibernate Search 4.5.1 및 Spring 4.0.5.RELEASE를 사용하여 응용 프로그램을 빌드 중입니다.Hibernate Search 결과를 반환하지 않음
@Entity
@Indexed
@Analyzer(impl= org.apache.lucene.analysis.standard.StandardAnalyzer.class)
@Table(name="SONG")
@XmlRootElement(name="song")
public class Song
{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID", updatable = false, nullable = false)
private Long id;
@Field(store = Store.YES)
@Column(name="NAME", length=255)
private String name;
@Field(store = Store.YES)
@Column(name="ALBUM", length=255)
private String album;
@Field(store = Store.YES)
@Column(name="ARTIST", length=255)
private String artist;
@NotNull
@Column(name="PATH", length=255)
private String path;
@NotNull
@Column(name="PATH_COVER", length=255)
private String cover;
@NotNull
@Column(name="LAST_VOTE")
private Date date;
@Field(store = Store.YES)
@NotNull
@Column(name="N_VOTES")
private int nvotes;
@NotNull
@Column(name="ACTIVE", nullable=false, columnDefinition="TINYINT(1) default 0")
private boolean active;
@OneToOne(fetch=FetchType.LAZY)
@JoinColumn(name="IMAGE_ID",insertable=true,updatable=true,nullable=false,unique=false)
private Image image;
@IndexedEmbedded
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "PLAYLIST_ID", nullable = false)
private PlayList playList;
@OneToMany(mappedBy = "song")
private Set<UserVotes> userServices = new HashSet<UserVotes>();
I는 다음과 같습니다 JUnit 테스트 케이스 건물입니다 :
이@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:jukebox-servlet-test.xml"})
@Transactional
public class SongDaoTest {
@Autowired
public I_PlaceDao placeDao;
@Autowired
public I_PlayListDao playListDao;
@Autowired
public I_SongDao songDao;
@Before
public void prepare() throws Exception
{
Operation operation = sequenceOf(CommonOperations.DISABLE_CONTRAINTS, CommonOperations.DELETE_ALL,CommonOperations.INSERT_SONG_DATA, CommonOperations.ENABLE_CONTRAINTS);
DbSetup dbSetup = new DbSetup(new DriverManagerDestination("jdbc:mysql://localhost:3306/jukebox", "root", "mpsbart"), operation);
dbSetup.launch();
FullTextSession fullTextSession = Search.getFullTextSession(placeDao.getSession());
fullTextSession.createIndexer().startAndWait();
}
@Test
@Rollback(false)
public void searchTest()
{
PlayList playList = playListDao.read(1l);
List<Song> songs = songDao.search(playList, "offspring", 1, 10);
assertEquals(10, songs.size());
}
검색 메소드 구현은 다음과 같습니다
@SuppressWarnings("unchecked")
public List<Song> search(PlayList playlist, String searchTerm,int page,int limit)
{
FullTextSession fullTextSession = Search.getFullTextSession(getSession());
QueryBuilder queryBuilder = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(Song.class).get();
BooleanQuery luceneQuery = new BooleanQuery();
luceneQuery.add(queryBuilder.keyword().onFields("name","album","artist").matching("*"+searchTerm+"*").createQuery(), BooleanClause.Occur.MUST);
luceneQuery.add(queryBuilder.phrase().onField("playList.place.id").sentence("\""+playlist.getPlace().getId()+"\"").createQuery(), BooleanClause.Occur.MUST);
luceneQuery.add(queryBuilder.phrase().onField("playList.id").sentence("\""+playlist.getId()+"\"").createQuery(), BooleanClause.Occur.MUST);
// wrap Lucene query in a javax.persistence.Query
FullTextQuery query = fullTextSession.createFullTextQuery(luceneQuery, Song.class);
org.apache.lucene.search.Sort sort = new Sort(new SortField("n_votes",SortField.INT));
query.setSort(sort);
List<Song> songs = query.setFirstResult(page*limit).setMaxResults(limit).list();
return songs;
}
테스트를 나는 인덱스에 다음 클래스를 시도하고있다 결과가 실패하면 일치하는 개체를 찾지 못합니다. luke lucene을 사용할 때 luke에서 최대 절전 모드로 생성 된 쿼리가 요소를 반환하면 결과가 있음을 알 수 있습니다. 최대 절전 모드에 의해 생성 된 쿼리는 다음과 같습니다 + (이름 : 메탈리카 앨범 : 메탈리카 아티스트 : 메탈리카) + playList.place.id : ""+ playList.id : "1"나는 또한 루크의 루씬에 나타났습니다
일부 색인 용어는 최대 6 자까지, 인스턴스의 경우 한 곡의 아티스트는 "The Offspring"이고 색인에 저장된 용어는 "the"와 "offspr"입니다. 첫 번째 것은 괜찮지 만 두 번째 용어는 "후손"이어서는 안됩니다. 이름이 왜 잘리는 거지? 이 사람을 도움이 경우