首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何用mongodb在hibernate搜索jpa上重建lucene索引

如何用mongodb在hibernate搜索jpa上重建lucene索引
EN

Stack Overflow用户
提问于 2014-06-03 17:39:14
回答 1查看 786关注 0票数 0

我无意中删除了我的索引目录,现在我正在尝试重建所有索引。我使用hibernate搜索与JPA,lucene和MONGODB。

下面的方法不返回结果

代码语言:javascript
复制
    public void rebuildIndex()throws Exception{
    org.hibernate.search.jpa.FullTextEntityManager fem = org.hibernate.search.jpa.Search.getFullTextEntityManager(entityManager);

    org.hibernate.search.query.dsl.QueryBuilder queryBuilder = fem.getSearchFactory().buildQueryBuilder().forEntity(Person.class).get();

    org.apache.lucene.search.Query query = queryBuilder.all().createQuery();

    FullTextQuery fullTextQuery = fem.createFullTextQuery(query, Person.class);

    //fullTextQuery.initializeObjectsWith(ObjectLookupMethod.SKIP, DatabaseRetrievalMethod.FIND_BY_ID);

    System.out.println(fullTextQuery.toString());

    List<Person> results = fullTextQuery.getResultList();

    fem.clear(); 
    System.out.println(results.size());
    for(Person p : results){
        fem.index( p );
        fem.flushToIndexes();
        fem.clear();
    }

    //fem.createIndexer().startAndWait();  
}

该方法不返回结果。如何从mongoDb获取所有数据以重建索引?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-06-03 19:20:55

由于hibernate搜索不符合标准,也不适用于JPQL,因此他有自己的JPQL解析器。我无法创建一个findAll方法来检索所有objetcs,唯一的方法是使用本机mongoDb查询:

代码语言:javascript
复制
org.hibernate.search.jpa.FullTextEntityManager fem = org.hibernate.search.jpa.Search.getFullTextEntityManager(entityManager);

    Mongo mongo = new Mongo("127.0.0.1", 27017);
    DB db = mongo.getDB("mainBase");
    DBCollection dbCollection = db.getCollection("Persons");
    DBCursor cursor = dbCollection.find();
    Collection<String> ids = new ArrayList<String>();
    String id = "";
    while (cursor.hasNext()) {
        id = cursor.next().get("_id").toString();
        System.out.println(id);
        ids.add(id);
    }

    System.out.println(">"+ids.size());

    Person pes;
    for(String p : ids){
        pes = new Person();
        pes.setId(p);
        pes = find(pes);
        System.out.println("indexing: "+pes.getId());
        fem.index( pes );//index each element
        fem.flushToIndexes();//apply changes to indexes
        fem.clear();//free memory since the queue is processed
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24021601

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档