2013-08-26 50 views
0

我想在MongoDB中使用Hibernate OGM來實現Fulltextsearch。我編寫了代碼,但代碼返回了一個空的結果。我檢查了兩個文件,這些文件是由lucene與Luke一起製作的,但似乎它們都是空的。我不知道我的問題是什麼原因。在Mongodb中使用Hibernate進行全文搜索Ogm

我已經啓用了全文搜索我收藏在該命令:

db.adminCommand({ setParameter : "*", textSearchEnabled : true }); 

而且我已經把指數上用戶名場中的用戶集合。

db.Users.ensureIndex({UserID:1 }) 

也是我有這樣的實體類:

@Entity 
@Indexed 
@Table(name="Users") 
@GenericGenerator(name="mongodb_uuidgg",strategy = "uuid2") 
public class User implements Serializable{ 
    private static final long serialVersionUID=1L; 
    @DocumentId 
    private String id; 

    @Column(name="City") 
    @Field(index = Index.NO,analyze = Analyze.YES,store = Store.YES) 
    private String city; 

    @Column(name="UserID") 
    @NumericField 
    @Field(index = Index.YES,analyze = Analyze.NO,store = Store.YES) 
    private int IdU; 

,並在我的DAO類:

OgmConfiguration cfgogm=new OgmConfiguration(); 
cfgogm.configure("hibernate.cfg.xml"); 
serviceregistry=new ServiceRegistryBuilder().applySettings(cfgogm.getProperties()).buildServiceRegistry(); 
sessionfactory=cfgogm.buildSessionFactory(serviceregistry); 

sessionfactory.openSession(); 
FullTextSession fulltextsession= Search.getFullTextSession(sessionfactory.getCurrentSession()); 
QueryBuilder querybuilder=fulltextsession.getSearchFactory().buildQueryBuilder().forEntity(User.class).get(); 
org.apache.lucene.search.Query lucenequery=querybuilder.keyword().onField("IdU").matching(new Integer(87709)).createQuery(); 

org.hibernate.search.FullTextQuery fulltextquery=fulltextsession.createFullTextQuery(lucenequery,User.class); 
fulltextquery.initializeObjectsWith(ObjectLookupMethod.SKIP, DatabaseRetrievalMethod.FIND_BY_ID); 
List result=fulltextquery.list(); 
System.out.println(result.size()); 

如果我打開與盧克的segment.gen,我看到這樣的信息: enter image description here

你能幫我解決這個問題嗎?或者我如何能實現使用Hibernate和Lucene全文搜索與MongoDB的

太感謝你了

回答

1

我不太清楚你的總體目標是什麼在這裏,但你混合兩件事情。有一個內置的mongodb全文搜索功能,還有Hibernate OGM,您可以將其與Hibernate Search結合使用。但是,它們是兩碼事。設置mongodb參數textSearchEnabled不會創建Lucene索引,以防您期望的情況發生。您將需要使用Hibernate Search索引API或批量索引器來創建初始索引。