1 2 Previous Next 25 Replies Latest reply on Jun 13, 2011 3:32 PM by Leo van den berg

    Empty Result Hibernate search

    scope_talka Newbie

      Hi,


      Am using hibernate-search with my seam project, everything works fine except that am getting an empty result from the server when I search on the client-side. Help please.

        • 1. Re: Empty Result Hibernate search
          scope_talka Newbie

          Hi,


          Does anyone know why hibernate search is returning an empty list? Am thinking its a minor problem because everything works fine, annotations are well added to the entity class and persistence.xml is configured. But when a search request is sent to the app server, I get back an empty list. Please any help.



          Wale Scope wrote on Jun 09, 2011 22:28:


          Hi,

          Am using hibernate-search with my seam project, everything works fine except that am getting an empty result from the server when I search on the client-side. Help please.


          Click HELP for text formatting instructions. Then edit this text and check the preview.

          • 2. Re: Empty Result Hibernate search
            Leo van den berg Master

            Hi,


            if you want some help from us, provide some additional information, so basically your query code. If you get an empty list and not an error, it just could be that your query string is not ok.


            Leo

            • 3. Re: Empty Result Hibernate search
              scope_talka Newbie

              Thanks for replying Leo, the library versions included are Hibernate-search 3.1, hibernate-commons-annotations and Lucene-core 2.4.1
              here is the code :



              @SuppressWarnings( "unchecked" )
              public List<User> searchUsers( final String searchString ) throws ParseException {
                        final Query query;
                        try {
                          FullTextEntityManager fem = org.hibernate.search.jpa.Search.getFullTextEntityManager( em );
                          query = fem.createFullTextQuery( getLuceneQuery( searchString ), User.class );
                          return query.getResultList();
                      } catch ( NoResultException ex ) {
                          log.error( "search error", ex );
                          return null;
                      }
              }
              
              private org.apache.lucene.search.Query getLuceneQuery( final String text ) throws ParseException {
                         Map<String, Float> boostPerField = new HashMap<String, Float>(2);
                         boostPerField.put( "firstname", (float) 2 );
                         boostPerField.put( "lastname", (float) 1 );
              
                         String[] searchFields = { "firstname", "lastname" };
                         QueryParser parser = new MultiFieldQueryParser( searchFields, new StandardAnalyzer(), boostPerField );
                         parser.setDefaultOperator( QueryParser.Operator.OR );
                         org.apache.lucene.search.Query luceneQuery = null;
                         luceneQuery = parser.parse( text );
                         return luceneQuery;
              }



              Thanks again.

              • 4. Re: Empty Result Hibernate search
                Leo van den berg Master

                Hi,


                have you tried to see if everything works with the standard Lucene tool Luke?
                It helps you to create the correct query and have you tried it out with a simple string something like


                org.apache.lucene.queryParser.QueryParser parser =  new QueryParser("", new StandardAnalyzer() );
                org.apache.lucene.search.Query luceneQuery = parser.parse("firstname:<<PUT HERE WHAT YOU'RE SEARCHING>>");
                FullTextQuery fullTextQuery = entityManager.createFullTextQuery( luceneQuery );
                List result = fullTextQuery.getResultList();
                
                



                Leo


                • 5. Re: Empty Result Hibernate search
                  scope_talka Newbie

                  yes I tried using Luke but when I navigate to the index folder I get no result; I mean it says ( Number of fields:0, Number of documents:0, Number of terms:0 ) also am not good with the tool yet. Also I have just tried the snippet you sent but no luck. Thanks.

                  • 6. Re: Empty Result Hibernate search
                    Leo van den berg Master

                    Hi,


                    this sounds if you have a configuration problem. Does Luke show you the available data ? When you start up and define the dirs where your data is, it will give you the entries. If there is nothing, you have not configured Search correctly.


                    Take small steps. Luke is not really user-frinedly, but it helps you to get things working correctly.


                    Leo

                    • 7. Re: Empty Result Hibernate search
                      scope_talka Newbie

                      Thanks for replying, the only file configured is the persistence.xml file with the following :



                      <properties>
                      
                               <property name="hibernate.search.default.directory_provider" value="org.hibernate.search.store.FSDirectoryProvider"/>
                               <property name="hibernate.search.default.indexBase" value="/tmp/index"/>
                               <property name="hibernate.ejb.event.post-insert" value="org.hibernate.search.event.FullTextIndexEventListener"/>
                               <property name="hibernate.ejb.event.post-update" value="org.hibernate.search.event.FullTextIndexEventListener"/>
                               <property name="hibernate.ejb.event.post-delete" value="org.hibernate.search.event.FullTextIndexEventListener"/>
                      </properties>




                      am not sure if I am suppose to configure other file apart from the persistence file.

                      • 8. Re: Empty Result Hibernate search
                        Leo van den berg Master

                        Hi,


                        I have it working with only the first two lines, so there is something else happening. Try with Luke if there really is something stored. So when you start Luke and refence the dir, it should give you an overview of existing entries.


                        If they are not there there is maybe something wrong with you annotations.


                        Small example of a simple bean. It should have as a minimum the following annotations:




                        .....
                        import org.hibernate.annotations.Index; 
                        // Watch out for this simple error, this is the DB-one !!
                        // and not the annotation for Search !!!
                        
                        @Entity
                        @Table(name = "LOG_ENTRIES")
                        @Indexed
                        public class LogEntry implements Serializable {
                        ....
                        
                        @Id
                        @DocumentId // Shouldn't be needed because Search looks/uses also for the @Id
                        @GeneratedValue
                        @Column(name="LOG_ID")
                        private Long id;
                        
                        ....
                        // The following s one of the properties which has TWO Index annotations, check if you maybe are using the wrong one.
                        /** Unique System name which produced this Notification */
                        @Column(name = "LOG_SYSTEMNAME")
                        @Index(name = "sys_idx") // This is the Hibernate Index for the DB
                        @Field(index=org.hibernate.search.annotations.Index.UN_TOKENIZED)// This one is for Search
                        private String uniqueSystemName;
                        
                        ....
                        // Left out the rest for brevity
                        
                        }
                        
                        



                        Leo

                        • 9. Re: Empty Result Hibernate search
                          scope_talka Newbie

                          I have made the amendments, still no luck, i keep getting empty result with no errors and Luke tool shows no entries. here is the code both the entity and the DAO class.



                          Entity Class
                          ------------


                          import static javax.persistence.GenerationType.IDENTITY;
                          import java.util.HashSet;
                          import java.util.Set;
                          import javax.persistence.CascadeType;
                          import javax.persistence.Column;
                          import javax.persistence.Entity;
                          import javax.persistence.FetchType;
                          import javax.persistence.GeneratedValue;
                          import javax.persistence.Id;
                          import javax.persistence.JoinColumn;
                          import javax.persistence.JoinTable;
                          import javax.persistence.ManyToMany;
                          import javax.persistence.Table;
                          import org.hibernate.search.annotations.DocumentId;
                          import org.hibernate.search.annotations.Field;
                          import org.hibernate.search.annotations.Indexed;
                          import org.hibernate.search.annotations.Store;
                          
                          @Entity
                          @Table( name = "user" )
                          @Indexed( index="user" )
                          public class User implements java.io.Serializable {
                          
                               private static final long serialVersionUID = -1790633498716266718L;
                               private int id;
                               private String firstName;
                               private String lastName;
                               private String email;
                               
                               public User() {}
                               
                               @Id
                               @GeneratedValue( strategy = IDENTITY )
                               @Column( name = "id", unique = true, nullable = false )
                               @DocumentId
                               public int getId() {
                                    return this.id;
                               }
                               
                               public void setId( final int id ) {
                                    this.id = id;
                               }
                               
                               @Column( name = "firstName", nullable = false, length = 150 )
                               @org.hibernate.annotations.Index( name = "firstname" )
                               @Field( store = Store.YES )
                               public String getFirstName() {
                                    return this.firstName;
                               }
                               
                               public void setFirstName( final String firstName ) {
                                    this.firstName = firstName;
                               }
                               
                               @Column( name = "lastName", nullable = false, length = 150 )
                               @org.hibernate.annotations.Index( name = "lastname" )
                               @Field( store = Store.YES )
                               public String getLastName() {
                                    return this.lastName;
                               }
                               
                               public void setLastName( final String lastName ) {
                                    this.lastName = lastName;
                               }
                               
                          }




                          DAO Class
                          ---------


                          import java.util.List;
                          import javax.persistence.EntityManager;
                          import javax.persistence.NoResultException;
                          import org.apache.lucene.queryParser.QueryParser;
                          import org.hibernate.search.jpa.FullTextEntityManager;
                          import org.hibernate.search.jpa.FullTextQuery;
                          import org.apache.lucene.analysis.standard.StandardAnalyzer;
                          import org.jboss.seam.Component;
                          import org.jboss.seam.ScopeType;
                          import org.jboss.seam.annotations.AutoCreate;
                          import org.jboss.seam.annotations.Logger;
                          import org.jboss.seam.log.Log;
                          import org.jboss.seam.annotations.Name;
                          import org.jboss.seam.annotations.Scope;
                          import org.apache.lucene.queryParser.ParseException;
                          import com.sample.entity.User;
                          
                          @Name( "userDao" )
                          @Scope( ScopeType.STATELESS )
                          @AutoCreate
                          public class UserDao {
                               
                               @Logger Log log;
                               private final EntityManager em;
                               
                               public UserDao() {
                                    em = ( EntityManager )Component.getInstance( "sampleEntityManager", true );
                               }
                               
                               @SuppressWarnings( "unchecked" )
                               public List<User> searchUsers( final String searchString ) throws ParseException {
                                    org.apache.lucene.queryParser.QueryParser parser =  new QueryParser( "", new StandardAnalyzer() );
                                    org.apache.lucene.search.Query luceneQuery = parser.parse( "firstname:pete" );
                                    FullTextEntityManager fem = org.hibernate.search.jpa.Search.getFullTextEntityManager( em );
                                    FullTextQuery fullTextQuery = fem.createFullTextQuery( luceneQuery );
                                    List<User> result = fullTextQuery.getResultList();
                                    return result;
                               }
                               
                          }



                          Thanks for all your help.

                          • 10. Re: Empty Result Hibernate search
                            Leo van den berg Master

                            Hi,


                            Read my previous email again where I explained a possible confusion between two different Hibernate  Index annotations. I don't know if it's your intention, but you've annotated the field with a Database Index, which is NOT the same as getting stuff in the Lucene store.


                            I don't know if Search has defaults, if not you need to provide the attribute which indictes HOW things must be indexed by Lucene (again, I am NOT talking DB-index here).
                            Lucene will take the property name (because you didn't provide another name(!). In your case the values are firstName and lastName. See the subtle difference of the capitals N in the same. Java is case sensitive. Sop provided that there is something stored by Lucene, it will be name firstName and lastName and these are also the names you must use in queries.


                            Leo

                            • 11. Re: Empty Result Hibernate search
                              Leo van den berg Master

                              Hi,


                              and additionally, don't complicate things. You can just use:




                              @In      FullTextEntityManager entityManager;



                              Which gives you the needed entitymanager directly (without looking it up, casting etc.)



                              Leo

                              • 12. Re: Empty Result Hibernate search
                                scope_talka Newbie

                                Hi,
                                Thanks again, adding the index annotation was a mistake. I fixed up the entity Class, injected FullTextEntityManager and still it returns an empty list. The new query code is



                                @SuppressWarnings( "unchecked" )
                                     public List<User> searchUsers( final String searchString ) throws ParseException {
                                          org.apache.lucene.queryParser.QueryParser parser =  new QueryParser( "", new StandardAnalyzer() );
                                          org.apache.lucene.search.Query luceneQuery = parser.parse( "firstName:pete" );
                                          FullTextQuery fullTextQuery = entityManager.createFullTextQuery( luceneQuery );
                                          List<User> result = fullTextQuery.getResultList();
                                          return result;
                                     }



                                Regarding indexing, the @Indexed class annotation should take care of the indexing. Or could all this be a bug issue in JBoss? Am using: JBoss 5.1 with Seam 2.2.1 Final

                                • 13. Re: Empty Result Hibernate search
                                  Leo van den berg Master

                                  Hi,


                                  I keep repeating this question, but are you 100% sure something gets inserted in de Lucene index? The only way to check this (1) with Luke or (2) by looking at the directories where the data is stored, because you use a Store true setting, even the complete value of the property should be there.


                                  So: You should have an entity saved in  the database AND the textvalue of first- and lastName saved somewhere in /tmp/index. Youre still missing the Tokenizer setting, so try to find the name with a wildchar



                                  something like --- >  ("firstName:pet*") 
                                  




                                  I strongly believe that you're not saving anything in the Lucene store, only en entity in the DB. The whole lot works for me with JBoss 5.1 (or 6) and with Seam up to version 2.2.2.Final


                                  Be patient and try to learn the basics of Luke, it will save you a lot of time !


                                  Leo

                                  • 14. Re: Empty Result Hibernate search
                                    scope_talka Newbie

                                    Just tried using the wildcard, still no result. The index directory structure is C:/tmp/index/user with two files in it (1) segments.gen and (2) segments-1. Judging by the results in Luke, nothing is inserted into lucene index. Thanks.


                                    1 2 Previous Next