2014-06-15 5 views
1

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"입니다. 첫 번째 것은 괜찮지 만 두 번째 용어는 "후손"이어서는 안됩니다. 이름이 왜 잘리는 거지? 이 사람을 도움이 경우

답변

0

, 나는이에 쿼리를 변경하여 문제를 해결할 수 있었다 :

해당 필드 값이 다음 다음 줄을 추가해야합니다 널 또는 널이 아닌 확인이의 경우
 FullTextSession fullTextSession = org.hibernate.search.Search.getFullTextSession(getSession()); 
    QueryBuilder qb = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(Song.class).get(); 

    if(searchTerm==null || searchTerm.equals("")) 
     searchTerm="*"; 
    else 
     searchTerm="*"+searchTerm+"*"; 
    Query luceneQuery1 = qb.bool() 
      .should(qb.keyword().wildcard().onField("name").matching(searchTerm).createQuery()) 
      .should(qb.keyword().wildcard().onField("album").matching(searchTerm).createQuery()) 
      .should(qb.keyword().wildcard().onField("artist").matching(searchTerm).createQuery()).createQuery(); 
    Query luceneQuery2 = qb.bool() 
      .must(qb.keyword().wildcard().onField("playList.place.id").matching(playlist.getPlace().getId()).createQuery()) 
      .must(qb.keyword().wildcard().onField("playList.id").matching(playlist.getId()).createQuery()) 
      .createQuery(); 
    BooleanQuery finalLuceneQuery=new BooleanQuery(); 
    finalLuceneQuery.add(luceneQuery1, BooleanClause.Occur.MUST); 
    finalLuceneQuery.add(luceneQuery2, BooleanClause.Occur.MUST); 
    FullTextQuery query = fullTextSession.createFullTextQuery(finalLuceneQuery, Song.class); 
    org.apache.lucene.search.Sort sort = new Sort(new SortField("nvotes",SortField.INT,true)); 
    query.setSort(sort); 
    List<Song> songs = query.setFirstResult(page*limit).setMaxResults(limit).list(); 
0

필드에 어디 클래스

@Field에서 색인 필드 (인덱스 = Index.YES 분석 = Analyze.NO, 저장 = Store.YES, indexNullAs = Field.DEFAULT_NULL_TOKEN) 필드 에

검색이 경우 null 값이 필요하다. booleanQuery.must (qb.keyword(). onField ("callReminder") 일치 ("null") .createQuery());

원하지 않는 경우는 null 값

booleanQuery.must (qb.keyword(). onField ("callReminder"). 일치 ("") .createQuery()).하지() ;

참조 문서 : http://docs.jboss.org/hibernate/search/4.1/reference/en-US/html/search-mapping.html#search-mapping-entity

관련 문제