-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 12 posts ] 
Author Message
 Post subject: Hibernate Search
PostPosted: Wed Jan 30, 2008 2:49 am 
Beginner
Beginner

Joined: Mon Oct 01, 2007 8:21 am
Posts: 40
Hi,

Iam new to Hibernate Search, i would like to get the list of books as a result from the given hibernate search example in the getting started guide, i think everything is going right but iam not able to get the list.

My code:

Code:
public class Example {
  public static void main(String[] args) {
   

    try{
           Session session = null;
       SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
       session =sessionFactory.openSession();
       FullTextSession fullTextSession = Search.createFullTextSession(session);

       Transaction tx = fullTextSession.beginTransaction();

       MultiFieldQueryParser parser = new MultiFieldQueryParser( new String[]{"summary", "body"}, new StandardAnalyzer());
       Query query = parser.parse("oracle");
       org.hibernate.Query hibQuery = fullTextSession.createFullTextQuery(query, Book.class);
       List<Book> result = hibQuery.list();
              
       for(int i=0;i<result.size();i++)
       {   System.out.println("Result-->"+result.get(i).getBody());
          System.out.println("Result-->"+result.get(i).getSummary());
                
       }
          
       tx.commit();
       session.close(); 
    }catch(Exception e){
      System.out.println(e.getMessage());
    }finally{
      }
   
  }
}


If i check this out in debug mode the value of hibQuery is FullTextQueryImpl(summary:oracle body:oracle)

The value of result is []

result is empty.

I have given data in the database it is also creating the index, i didnt get error but no result.

I really dont no what is happening, Could anybody please suggest how would i be able to get the result out.

Thanks,
Ambika.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 31, 2008 3:21 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
How does your indexing code look like and what type of DirectoryProvider are you using? If it is a FSDirectoryProvider have you checked whether the Lucene index files got successfully created on the file system? And if so have you tried to inspect the Lucene index using eg Luke?

--Hardy


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 04, 2008 7:48 am 
Beginner
Beginner

Joined: Mon Oct 01, 2007 8:21 am
Posts: 40
Thanks Hardy,

This is my hibernate.cfg.xml file
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
      "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
      "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- @author Anees -->
<hibernate-configuration>
<session-factory>
      <property name="hibernate.connection.driver_class">
         com.mysql.jdbc.Driver
      </property>
      <property name="hibernate.connection.password">root</property>
      <property name="hibernate.connection.url">
         jdbc:mysql://localhost/hibernate_book
      </property>
      <property name="hibernate.connection.username">root</property>
      <property name="hibernate.default_schema">hibernate_book</property>
      <property name="hibernate.dialect">
         org.hibernate.dialect.MySQLDialect
      </property>
      <property name="show_sql">true</property>
      <property name="hibernate.hbm2ddl.auto">update</property>
      
      <property name="hibernate.search.default.directory_provider">org.hibernate.search.store.FSDirectoryProvider</property>
        <property name="hibernate.search.default.indexBase">Index</property>
        <property name="hibernate.search.Rules.directory_provider">org.hibernate.search.store.RAMDirectoryProvider</property>
      
       <mapping resource="com/i3l/sample/model/Book.hbm.xml"/>
      
      <event type="post-update">
            <listener type="post-update" class="org.hibernate.search.event.FullTextIndexEventListener"/>
      </event>
      <event type="post-insert">
         <listener type="post-insert" class="org.hibernate.search.event.FullTextIndexEventListener"/>
      </event>
      <event type="post-delete">
         <listener  type="post-delete" class="org.hibernate.search.event.FullTextIndexEventListener"/>
      </event>
   </session-factory>
</hibernate-configuration>


It is creating me a file structured directory for the index,

-Index
- com.i3l.sample.model.book
_0.cfs
segments_3
segments.gen

How would i be able to check whether the index created is right or wrong,
right now for me the result array is getting loaded.. the problem is whenever i change or add some data in the database externally index is updated it seems.. it is not giving me the output..

Should i use hibernate session to insert the data in the database to make it work..

One more thing i would like to ask, how can v do this in web application, like after fetching the object, how can i list my jsp's which is having that particular keyword..

Answering the above query of mine.. will help me a lot in my further proceedings, is there any document to get thru using hibernate search in web application..

Thanks,
Ambika


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 04, 2008 4:28 pm 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
ambika_devi wrote:

It is creating me a file structured directory for the index,

-Index
- com.i3l.sample.model.book
_0.cfs
segments_3
segments.gen

Download Luke http://www.getopt.org/luke/ or start it via Java Web Start and open the segments.gen file. Luke will show you the contents in the index. However, your next question makes me believe the index will be empty.

ambika_devi wrote:
Should i use hibernate session to insert the data in the database to make it work..

Of course you have go via the hibernate session. Read more about indexing and how it works here http://www.hibernate.org/hib_docs/search/reference/en/html_single/#search-configuration-event.
In order for Hibernate Search to work you have to persist your objects via Hibernate. Inserting data into the database bypassing Hibernate won't work. Per default Hibernate Search is configured as event listener to insert/update/delete operations.

ambika_devi wrote:
One more thing i would like to ask, how can v do this in web application, like after fetching the object, how can i list my jsp's which is having that particular keyword..

Answering the above query of mine.. will help me a lot in my further proceedings, is there any document to get thru using hibernate search in web application..

I am not sure what you are after. The setup for a web application will depend on the framework you are using. Using Struts2 you can egwork with this setup: http://struts.apache.org/2.x/docs/struts-2-spring-2-jpa-ajax.html.

--Hardy


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 05, 2008 1:12 am 
Beginner
Beginner

Joined: Mon Oct 01, 2007 8:21 am
Posts: 40
Hi,

Thanks hardy. I just verified using luke im able to view the contents in the segments.gen file.

Now i will try out with hibernate session to insert and delete the records, will this update the index file automatically.

I would like to ask saying Im giving the pojo and Example test java file which i used.

Book.java
Code:
package com.i3l.sample.model;

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;


import org.hibernate.search.annotations.Analyzer;
import org.hibernate.search.annotations.DocumentId;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Index;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.annotations.Store;

@Entity
@Indexed
public class Book implements Serializable{
   @Id
   @DocumentId
   private long bookId;
   
   //@Field(index=Index.TOKENIZED, store=Store.NO)
   @Field(index=Index.TOKENIZED, store=Store.NO)
   private String body;
   
   @Field(index=Index.TOKENIZED, store=Store.NO)
   private String summary;

   
   
   public Book(){
      
   }
      // respective getters and setters
   
}


Example.java -- To create the index
Code:
package com.i3l.sample.test;

//Hibernate Imports
import java.util.List;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.queryParser.ParseException;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.search.FullTextSession;
import org.hibernate.search.Search;

import com.i3l.sample.model.Book;

public class Example {
public static void main(String[] args) {
   try {
   Session session = null;
     SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
     session =sessionFactory.openSession();
   FullTextSession fullTextSession = Search.createFullTextSession(session);
   Transaction tx = fullTextSession.beginTransaction();
   List<Book> books = session.createQuery("from Book").list();
   
   for (Book book : books) {
       fullTextSession.index(book);
   }
   
   MultiFieldQueryParser parser = new MultiFieldQueryParser( new String[]{"summary", "body"},
     new StandardAnalyzer());
   org.apache.lucene.search.Query query;
   
      query = parser.parse("Java");
   
   org.hibernate.Query hibQuery = fullTextSession.createFullTextQuery( query, Book.class );
   List result = hibQuery.list();
     System.out.println("Result : "+result.size()+result);
   tx.commit();
   session.close();
   } catch (ParseException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
   }
 
}
}


Example2.java
Code:
public class Example2 {
public static void main(String[] args) {


try{
    // This step will read hibernate.cfg.xml and prepare hibernate for use
     Session session = null;
     SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
     session =sessionFactory.openSession();
     FullTextSession fullTextSession = Search.createFullTextSession(session);

     Transaction tx = fullTextSession.beginTransaction();

     MultiFieldQueryParser parser = new MultiFieldQueryParser( new String[]{"summary", "body"}, new StandardAnalyzer());
   
     Query query = parser.parse("Java");
     org.hibernate.Query hibQuery = fullTextSession.createFullTextQuery(query, Book.class);
     List<Book> result = hibQuery.list();
      
     System.out.println("Result Size : "+result.size());
         
     for(int i=0;i<result.size();i++)
     {   
        System.out.println("Result::::::::::"+result.get(i).getBody());
        System.out.println("Result::::::::::"+result.get(i).getSummary());
     }
     
     tx.commit();
     session.close(); 
  }catch(Exception e){
    System.out.println(e.getMessage());
  }finally{
    }


Here by im able to get the record according to the Book name im passing"Query query = parser.parse("Java");"

If i want to try this out with more than tables then what should i do. Here im casting the result as List<Book> according to my pojo.

And also web application in the sense using Acegi,Spring MVC and Hibernate and JSP. Is there any doc or tutorial to get some idea regarding.

Thanks,
Ambika.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 05, 2008 2:37 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Quote:
If i want to try this out with more than tables then what should i do. Here im casting the result as List<Book> according to my pojo.

Really you should read http://www.hibernate.org/hib_docs/search/reference/en/html_single/#search-mapping
to understand the way entities are mapped. Depending on what you want to achieve you can either index multiple entities (tables) into different indexes and query them separately or you can use @IndexEmbedded to index associated entities together with the 'main' entity, for example Book. Per default Hibernate Search will create on index directory for each entity you annotate with @Indexed. At query time you can then specify which index to search by specifying the enitity class

Code:
fullTextSession.createFullTextQuery(query, Book.class);


Quote:
And also web application in the sense using Acegi,Spring MVC and Hibernate and JSP. Is there any doc or tutorial to get some idea regarding.

Sure there is. It seems to be a classic combination. If you have an application setup using the above framework Hibernate Search will 'just' work by adding the required configuration and annotations.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 05, 2008 8:12 am 
Beginner
Beginner

Joined: Mon Oct 01, 2007 8:21 am
Posts: 40
Hi,

Thanks Hardy.. i have gone thru the document some how.. but not able to get the clear idea about it, as im a fresher and i dont no about annotations, Not getting the clear idea..

Do u have any sample example where using the two or more tables to make a search and get the object specifically..

About the web application i have the application setup using these technologies, the thing is im confused how would i be able to get the list of jsp according to the resulting object from the search..

I think i was bugging u.. but im seriously unable to get the clear idea.. is there any other document to give some example regarding.. r if u have some idea or example code... could u ple share it with me..

Thanks
Ambika


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 05, 2008 11:24 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Quote:
Thanks Hardy.. i have gone thru the document some how.. but not able to get the clear idea about it, as im a fresher and i dont no about annotations, Not getting the clear idea..

Maybe you should start with reading the Hibernate annotation documentation and then design your entity model (using annotations). This might get you kick started with everything.

Quote:
Do u have any sample example where using the two or more tables to make a search and get the object specifically..

The best I can offer is http://www.hibernate.org/hib_docs/search/reference/en/html_single/#d0e1218.
It all really depends how and what you want to search. If you already have some entities maybe you can explain your use case in more detail.

Quote:
About the web application i have the application setup using these technologies, the thing is im confused how would i be able to get the list of jsp according to the resulting object from the search..

I am not quite sure what you mean with 'list of jsp'.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 06, 2008 12:59 am 
Beginner
Beginner

Joined: Mon Oct 01, 2007 8:21 am
Posts: 40
Hi,

Thanks Hardy, I will go thru the annotations first.

About that web application, list of jsp's is very similar to the google keyword search, entering the keyword to get the list of url or websites as search results.. similar kind of thing..

How would i be able to do that with my resulting object, to say these are the list of url's for the searched keyword..

Is there any tutorial or doc to get start with this.

Thanks
Ambika.


Top
 Profile  
 
 Post subject: I am newbie to hibernate search. Need help where to start
PostPosted: Tue Feb 12, 2008 5:28 am 
Newbie

Joined: Tue Feb 12, 2008 5:15 am
Posts: 3
Hi,
I am in to the application development using JBOSS and SPRING.
Need help in integrating hibernate search in to my web application.
Please point me to some useful step by step documents.


Top
 Profile  
 
 Post subject: I am newbie to hibernate search. Need help where to start
PostPosted: Tue Feb 12, 2008 5:28 am 
Newbie

Joined: Tue Feb 12, 2008 5:15 am
Posts: 3
Hi,
I am in to the application development using JBOSS 4.2.1 and SPRING.
(HIbernate version is 3.2.5.)
Need help in integrating hibernate search in to my web application.
Please point me to some useful step by step documents.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 12, 2008 10:12 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Maybe this helps: http://hibernate.org/441.html

--Hardy


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 12 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.