-->
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.  [ 11 posts ] 
Author Message
 Post subject: Stuck with simple Hibernate Search...
PostPosted: Mon Mar 26, 2007 9:55 am 
Newbie

Joined: Mon Mar 26, 2007 9:22 am
Posts: 9
Hello,
I'm trying to make a very simple query on my data. I have the newest .jars so Hibernate Search should be functionable..

I put some lines into my persistence.xml to enable the indexing feature and I also annotated my entities like described here: http://www.hibernate.org/hib_docs/search/reference/en/html/search-mapping.html

Now this code gives me an exception that tells me that: "Lucene event listener not initialized"

Code:
Configuration cfg = new Configuration();
         cfg.setProperty("hibernate.search.autoregister_listeners", "true");
         cfg.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
         SessionFactory sessionFactory = cfg.buildSessionFactory();
         Session session = sessionFactory.openSession();
         FullTextSession fullTextSession = Search.createFullTextSession(session);   
         org.hibernate.Query fullTextQuery = fullTextSession.createFullTextQuery( new TermQuery(new Term("street", "mystreet")) );
         List result = fullTextQuery.list(); // exception at this point thrown
         request.setAttribute("resultList", result);


Can someone maybe give me a push in the right direction? I'm not quite sure how to go on...

I just want to search for mystreet in the field street.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 26, 2007 3:38 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
f you are using a persistence.xml file you shouldn't use the Configuration object

persistence.xml is sued if you use Hibernate EntityManager (Persistence.createEntityManagerFactory)

if you don;t want to use HEM, you can use Hibernate Annotations, and then use AnnotationConfiguration

In any case, there should be no need to describe the event listener (they are auto wired)

If you only want to use Hibernate Core, follow the event configuration described here http://www.hibernate.org/hib_docs/search/reference/en/html_single/#search-configuration-event
But this is a rare conbination (only Core and Search).

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 26, 2007 3:59 pm 
Newbie

Joined: Mon Mar 26, 2007 9:22 am
Posts: 9
Hello, thanks for your answer.

I am actually using Entitymanager as well as Annotations without problems, but how am I able to obtain a Session / SessionFactory using the properties I specified in the persistence.xml?

Or am I supposed to use a Query which is obtained through the EntityManager?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 26, 2007 4:16 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
rydl wrote:
but how am I able to obtain a Session / SessionFactory using the properties I specified in the persistence.xml?

I don't understand you, what properties?

rydl wrote:
Or am I supposed to use a Query which is obtained through the EntityManager?

Or maybe I do,
Session session = ( (Session) em.getDelegate() );
//then use the session to build the query

I need to implement the JPA version to that you can avoid such casting, it's on the todo list :-)

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 27, 2007 2:03 pm 
Newbie

Joined: Mon Mar 26, 2007 9:22 am
Posts: 9
thanks, that helped a lot! Now I only have to learn how these lucene queries are working ;)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 27, 2007 2:55 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
I strongly recommend Lucene in Action. It is old, but definitely a must have. You can by the electronic version for 25$ or so. I think it's on Manning's website.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 27, 2007 5:47 pm 
Newbie

Joined: Mon Mar 26, 2007 9:22 am
Posts: 9
I made some research already about Lucene and its syntax and I'm not sure if my problem is caused by a typo in the syntax. Can you quickly watch at my configurations and tell me if they are correct?

At first 2 entity classes. You can see that the ID of each entity is inherited by VersionableEntity.

VersionableEntity, which is the super class of contact
Code:
@Entity
@Indexed // I tried it with and without this annotation
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class VersionableEntity {
   
    @Id
    @DocumentId
    @GeneratedValue(strategy=GenerationType.AUTO)
    private long id;
...



Contact
Code:
import javax.persistence.*;

@Entity
@Indexed
@PrimaryKeyJoinColumn(name = "contactId")
public class Contact extends VersionableEntity {
   
    @Field(index=Index.UN_TOKENIZED)
   private String name;

    @Field(index=Index.UN_TOKENIZED)
   @Column(nullable = false)
   private String street;

...



Parts of the properties of my persistence.xml:
Code:
<property name="hibernate.search.default.directory_provider" value="org.hibernate.search.store.FSDirectoryProvider"/>
           <property name="hibernate.search.default.indexBase" value="/Users/rydl/indexes"/>
            <property name="hibernate.ejb.event.post-update" value="org.hibernate.search.event.FullTextIndexEventListener"/>
            <property name="hibernate.ejb.event.post-insert" value="org.hibernate.search.event.FullTextIndexEventListener"/>
            <property name="hibernate.ejb.event.post-delete" value="org.hibernate.search.event.FullTextIndexEventListener"/>


And my servlet is trying to query like this:
Code:
try {

            System.out.println("suche gestartet!");
            Session session = ((Session) getEM().getDelegate());
            FullTextSession fullTextSession = Search.createFullTextSession(session);
            QueryParser parser = new QueryParser("street", new StopAnalyzer()); // also tried it with contact.street as field name
            org.apache.lucene.search.Query luceneQuery;
            luceneQuery = parser.parse("w*"); // want to see all streets starting with the letter 'w'
            org.hibernate.Query fullTextQuery = fullTextSession.createFullTextQuery(luceneQuery);
            System.out.println("fulltextquery: " + fullTextQuery.toString());
            List result = fullTextQuery.list();
            System.out.println("result.size(): " + result.size());
            request.setAttribute("resultList", result);
         } catch (ParseException e) {
            e.printStackTrace();
         }



The two outputs are printing:

fulltextquery: FullTextQueryImpl(street:w*)
result.size(): 0

The directory indexes was created and even some subdirectories named like the entities with some files in it were created on my filesystem. I don't know whether there are wrong data indexed or some configuration went wrong or the query is wrong.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 28, 2007 11:53 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
the event listener config is not necessary in the latest version

What are the street names?

If they are like 32 Westford street or even Westford street, the result is correct.
In the first case because the street start with 32, in the second because the street start with W and not w

Usually, people want to Tokenize their data to avoid both issues.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 29, 2007 12:03 pm 
Newbie

Joined: Mon Mar 26, 2007 9:22 am
Posts: 9
Ah, now I got it. But I had also to index the entity by myself with this code: http://www.hibernate.org/hib_docs/searc ... index.html

I thought that listener would do this for me? So when I (re)start my application I have to reindex all records of my database, is that correct?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 30, 2007 6:26 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Not usre what you're talking about.
The first time (when the index is new/empty), you need to use sesison.index() to let the index be up to date with the database.

Then any later change made through hibernate will be propagated to both the DB and the index (thanks to the event listener).

When you stop your application, the index is persistent (at least if you use the FSDirectory provider).

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 02, 2007 3:03 pm 
Newbie

Joined: Mon Mar 26, 2007 9:22 am
Posts: 9
yes, you are right :D
thanks for helping me!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 11 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.