java - Hibernate Search doesn't reindex lucene index after insert -


i'm using hibernate (with jpa) , hibernate search spring web application. when application starts on server, create indexes following code:

entitymanager em = emf.createentitymanager();  fulltextentitymanager fulltextentitymanager = search.getfulltextentitymanager(em); fulltextentitymanager.createindexer().startandwait();  em.close(); 

that works charm. however, when insert new entities through hibernate, indexes aren't getting modified contain new entities. according hibernate documentation; should happen automatically.

this how insert entity:

    entitytransaction tx = null;     entitymanager em = emf.createentitymanager();       try {         tx = em.gettransaction();         tx.begin();          em.persist(account);         em.flush();           tx.commit();     }     catch (runtimeexception e) {         if ( tx != null && tx.isactive() ) tx.rollback();         return null;     } 

and how use hibernate search:

    entitymanager em = emf.createentitymanager();        fulltextentitymanager fulltextentitymanager = search.getfulltextentitymanager(em);      em.gettransaction().begin();       querybuilder qb = fulltextentitymanager.getsearchfactory()             .buildquerybuilder().forentity(accountpojo.class).get();     org.apache.lucene.search.query lucenequery = qb     .keyword()     .onfields("id", "user.email", "user.firstname", "user.lastname", "user.phonenumber", "user.streetaddress")     .matching(term)     .createquery();       // wrap lucene query in javax.persistence.query     org.hibernate.search.jpa.fulltextquery jpaquery =                     fulltextentitymanager.createfulltextquery(lucenequery, accountpojo.class);      jpaquery.setprojection(fulltextquery.score, fulltextquery.this, "id", "user.email", "user.firstname", "user.lastname", "user.phonenumber");      // execute search             list result = jpaquery.getresultlist();               em.gettransaction().commit();             em.close();      return result; 

when insert account, it's not searchable (indexed) before restart application. said, configuration made jpa annotations.

is there i'm missing here?

as turns out, solution answer quite simple. in example above, saving account, , tried search afterwards, properties on related entity (user). when persist object related entities included in 1 of queries, need do:

em.refresh(account) 

after persist it. otherwise related entities' indexes won't rebuilt, , hence won't able find new entity realted entities' properties.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -