问题
I want to implement Fulltextsearch in MongoDB using Hibernate OGM. I wrote the code, but the code returns me an empty result. I have checked two files, which were produced by lucene with Luke, but it seems that both of them are empty. I don't know what is the cause of my problem.
I have enabled fulltext search in my collection with this command:
db.adminCommand( { setParameter : "*", textSearchEnabled : true } );
and also I have put the index on the UserID field in Users collection.
db.Users.ensureIndex({UserID:1 })
also I have this entity class:
@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;
and in my DAO class:
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());
If I open the segment.gen with Luke, I see this information:
Could you please help me to solve this problem? or how can I implement fulltext search using Hibernate and Lucene with MongoDB
thank you so much
回答1:
I am not quite sure what your overall goal is here, but you are mixing two things. There is a built-in mongodb fulltext search capability and there is Hibernate OGM which you can use in combination with Hibernate Search. However, they are two different things. Setting the mongodb parameter textSearchEnabled is not going to create a Lucene index in case that's what you expect. You will need to use the Hibernate Search indexing API or mass indexer to create the initial index.
来源:https://stackoverflow.com/questions/18444717/fulltext-search-in-mongodb-using-hibernate-ogm