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.