【问题标题】:Fulltext search in Mongodb using Hibernate Ogm使用 Hibernate Ogm 在 Mongodb 中进行全文搜索
【发布时间】:2013-08-26 12:57:41
【问题描述】:

我想使用 Hibernate OGM 在 MongoDB 中实现全文搜索。我编写了代码,但代码返回了一个空结果。查了两个文件,都是lucene和Luke一起生成的,但是好像都是空的。我不知道我的问题是什么原因。

我已使用此命令在我的收藏中启用全文搜索:

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

我还把索引放在用户集合中的 UserID 字段上。

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());

如果我用 Luke 打开 segment.gen,我会看到以下信息:

你能帮我解决这个问题吗?或者如何使用 Hibernate 和 Lucene 和 MongoDB 实现全文搜索

非常感谢

【问题讨论】:

    标签: mongodb lucene hibernate-search hibernate-ogm


    【解决方案1】:

    我不太确定您的总体目标是什么,但您将两件事混为一谈。有一个内置的 mongodb 全文搜索功能,还有 Hibernate OGM,您可以将其与 Hibernate Search 结合使用。然而,它们是两种不同的东西。设置 mongodb 参数 textSearchEnabled 不会创建 Lucene 索引,以防万一这是您所期望的。您将需要使用 Hibernate Search 索引 API 或海量索引器来创建初始索引。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-19
      • 2020-05-06
      • 2013-08-27
      相关资源
      最近更新 更多