-->
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.  [ 1 post ] 
Author Message
 Post subject: Hibernate and Lucene Integration Problem
PostPosted: Mon Jan 22, 2007 8:03 am 
Newbie

Joined: Mon Jan 22, 2007 7:51 am
Posts: 1
Hi

I am Integrating Lucene with hibernate. It is working fine if Indexes are populated with database directly. if we add,delete and updating Hibernate objects the indexes are not updating.

Hibernate version:3.1

Mapping documents:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>

<class name="Article" table="ARTICLE">

<id name="articleId" type="long" unsaved-value="null">
<column name="ARTICLEID" sql-type="number" not-null="true"/>
<generator class="sequence">
<param name="sequence">SEQ_ARTICLEID</param>
</generator>
</id>

<property name="sort"/>
<property name="title"/>
<property name="byline"/>
<property name="pubDate" column="PUB_DATE"/>
<property name="shortText" column="SHORT_TEXT"/>
<property name="body" type="text"/>
<property name="source"/>

</class>

</hibernate-mapping>


POJO:

import org.apache.lucene.analysis.*;
import org.apache.lucene.analysis.standard.*;
import org.apache.lucene.index.*;
import org.apache.lucene.document.*;
import org.apache.lucene.search.*;
import org.apache.lucene.queryParser.QueryParser;
import org.hibernate.*;
import java.io.*;
import java.util.Date;

public class Article
{
private Long articleId;
private String title;
private String shortText;
private String byline;
private Date pubDate;
private String body;
private int sort;
private String source;
private Boolean includeInTiles;

/** Directory containing Lucene index */
private File idx = new File("index");


public Long getArticleId() {
return articleId;
}

public void setArticleId(Long articleId) {
this.articleId = articleId;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getShortText() {
return shortText;
}

public void setShortText(String shortText) {
this.shortText = shortText;
}

public String getByline() {
return byline;
}

public void setByline(String byline) {
this.byline = byline;
}

public Date getPubDate() {
return pubDate;
}

public void setPubDate(Date pubDate) {
this.pubDate = pubDate;
}

public String getBody() {
return body;
}

public void setBody(String body) {
this.body = body;
}

public int getSort() {
return sort;
}

public void setSort(int sort) {
this.sort = sort;
}

public String getSource() {
return source;
}

public void setSource(String source) {
this.source = source;
}

public Boolean getIncludeInTiles() {
return includeInTiles;
}

public void setIncludeInTiles(Boolean includeInTiles) {
this.includeInTiles = includeInTiles;
}

/**
* Return a Lucene Document that provides the searchable elements
* of the object.
*/
public Document getDocument() {

Document d = new Document();
d.add(Field.Keyword("id", getArticleId().toString()));
d.add(Field.Keyword("classname", this.getClass().getName()));
d.add(Field.UnStored("body", getBody()));
d.add(Field.Keyword("title", getTitle()));
System.out.println("in getDocument"+ d.getField("title"));
return d;
}
/**
* Open up a Lucene IndexWriter.
*/
protected IndexWriter getIndexWriter() throws IOException{
return new IndexWriter(idx, new StandardAnalyzer(), false);
}

/**
* Open a Lucene IndexReader.
*/
protected IndexReader getIndexReader() throws IOException{
return IndexReader.open(idx);
}

/**
* Saving an object for the first time - add it to the Lucene
* index...
*/
public boolean onSave(Session s) throws CallbackException {
try {
//System.out.println("in save");
IndexWriter writer = getIndexWriter();
writer.addDocument(getDocument());
writer.close();
} catch (IOException e) {
throw new CallbackException(e.getMessage());
}
return false;
}

/**
* Updating an object - must delete old object and reinsert it.
*/
public boolean onUpdate(Session s) throws CallbackException {
try {
IndexReader reader = getIndexReader();
reader.delete(new Term("id", getArticleId().toString()));

IndexWriter writer = getIndexWriter();
writer.addDocument(getDocument());
writer.close();
} catch (IOException e) {
throw new CallbackException(e.getMessage());
}
return false;
}

/**
* Deleting an object.
*/
public boolean onDelete(Session s) throws CallbackException {
try {
IndexReader reader = getIndexReader();
reader.delete(new Term("id", getArticleId().toString()));
} catch (IOException e) {
throw new CallbackException(e.getMessage());
}
return false;
}

/**
* Loading an object - we don't have to do anything here.
*/
public void onLoad(Session s, Serializable id) {
}
}


Full stack trace of any exception that occurs:

Oracle10g:
Index Populating Java file:

import org.apache.lucene.analysis.SimpleAnalyzer;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.analysis.standard.*;
import org.hibernate.*;
import java.util.*;
public class PopulateIndex {
public static void main(String s[])throws Exception{
Session session = HibernateSessionFactory.currentSession();

Query query = session.createQuery("from Article");
List list = query.list();
IndexWriter writer = new IndexWriter("index", new StandardAnalyzer(), true);
int count = 0;

for ( int i=0; i<list.size();i++ ) {
Article article = (Article) list.get(i);
writer.addDocument(article.getDocument());
count++;
if (count % 20 == 0 ) {
session.flush();
session.clear();
}
}
writer.close();

System.out.println("processed : " + count);

HibernateSessionFactory.closeSession();
}
}

Hibernate Operaitons file:

import org.hibernate.*;
import java.util.*;
import java.io.*;
public class HibernateOperations {
public static void main(String s[])throws Exception{
Session session = HibernateSessionFactory.currentSession();
Transaction tx = session.beginTransaction();


Article article = new Article();
article.setTitle("Unindexed Blog");
article.setShortText("Integration Blog");
article.setByline("Blog");
article.setPubDate(new java.util.Date());
article.setBody("Unindexed Stored in the index verbatim, but unsearchable. These values are normally used to provide displayable text for search results.");
article.setSort(8);
article.setSource("www.asd.com");
session.save(article);

/*
Article artup = (Article)session.load(Article.class,new Long(8));
artup.setBody("Since we store the Hibernate ID as a keyword, we can immediately retrieve the full object from the database and don't need unindexed or text fields, although they may be useful for non-hibernated tools.");
session.update(artup);
*/
/*
Article artdel = (Article)session.get(Article.class,new Long(8));
session.delete(artdel);
*/
tx.commit();
HibernateSessionFactory.closeSession();
}
}

Lucene Searher file:

import org.apache.lucene.document.Document;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Searcher;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.analysis.Analyzer;
import org.hibernate.*;
import java.io.InputStreamReader;
import java.io.BufferedReader;

public class SearchIndex {
public static void main(String[] args) throws Exception {
Searcher searcher = new IndexSearcher("index");
Analyzer analyzer = new StandardAnalyzer();

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
while (true) {
System.out.print("Search query (enter a blank query to stop) : ");
String line = in.readLine();

if (line == null || line.length() < 1) break;

Query query = QueryParser.parse(line, "body", analyzer);
System.out.println("Searching for: " + query.toString("name"));

Hits hits = searcher.search(query);
System.out.println("Number of matching documents = " + hits.length());

for (int i = 0; i < hits.length(); i++) {
Document doc = hits.doc(i);
System.out.println("Title : " + doc.get("title") + " and Article Id : "+doc.get("id") +", score: " + hits.score(i));
}
}
searcher.close();
}
}


Could you please suggest me if i did any mistakes?

Regards,
Srini.


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

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.