简体   繁体   中英

how rebuild lucene indexes over hibernate search jpa with mongodb

I have accidentally deleted my index directory, now i am trying to rebuild all indexes. I am using the hibernate search with JPA, lucene and MONGODB.

the following method is returning no results

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

the method is returning no result. how should I get all data from mongoDb to rebuild index?

as hibernate search didn't work with criteria neither with JPQL and has his own JP-QL parser. I couldn't create a findAll method to retrieve all objetcs the only way was use a native mongoDb query:

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
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM