-->
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.  [ 8 posts ] 
Author Message
 Post subject: dynamic hibernate search?
PostPosted: Mon Mar 23, 2009 1:10 pm 
Newbie

Joined: Mon Mar 23, 2009 12:46 pm
Posts: 6
recently i implemented hibernate search on a project i'm working on, but then i was asked if there was a way to generalize it. I tried looking online for the answer, but couldn't find anything and thought the search forums would be my best bet.

Currently I create my query like:
Code:
FullTextQuery query = entityManager.createFullTextQuery(luceneQuery, Foo.class, Bar.class);


but I would like to not explicitly name the classes I want to search over. Is there any way to construct the query to get the class information that needs to be search over? Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 24, 2009 4:03 am 
Pro
Pro

Joined: Wed Oct 03, 2007 2:31 pm
Posts: 205
Just a thought (not sure this will help much and more for the hibernate Search Team). Would it be possible to do the following:

Code:
FullTextQuery query = entityManager.createFullTextQuery(luceneQuery, org...Entity );


I don't know... I guess it would help people who may forget to put their class in the createFullTextQuery and be wondering why they don't get a result.

It's early in the morning and I haven't had my coffee so I may not be making sense...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 24, 2009 4:04 am 
Pro
Pro

Joined: Wed Oct 03, 2007 2:31 pm
Posts: 205
Just a thought (not sure this will help much and more for the hibernate Search Team). Would it be possible to do the following:

Code:
FullTextQuery query = entityManager.createFullTextQuery(luceneQuery, org...Indexed);


I don't know... I guess it would help people who may forget to put their class in the createFullTextQuery and be wondering why they don't get a result.

It's early in the morning and I haven't had my coffee so I may not be making sense...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 24, 2009 11:59 am 
Newbie

Joined: Mon Mar 23, 2009 12:46 pm
Posts: 6
thanks for the reply, but i think i figured it out. I was able to get a set of class names as strings and then i used Class.forName() to convert them into an array of classes. then i just pass in the array of classes instead of the explicit class name and it works.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 25, 2009 4:48 am 
Pro
Pro

Joined: Wed Oct 03, 2007 2:31 pm
Posts: 205
Hi

Is it possible to see a snippet of code of how you have done this?

Cheers
Amin


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 25, 2009 10:04 am 
Newbie

Joined: Mon Mar 23, 2009 12:46 pm
Posts: 6
sure no problem, but the HibernateUtil is from a framework I'm using so you'll need to get the session another way.

Code:
   Session session = HibernateUtil.getSession(entityManager);
   Map map = session.getSessionFactory().getAllClassMetadata();
   Set<String> entities = map.keySet();
   Iterator<String> entitiesI = entities.iterator();

   Class[] classes = new Class[entities.size()];
   int j=0;
      //converts the set of strings into an array of classes
      try{
         while(entitiesI.hasNext()){
            classes[j] = Class.forName(entitiesI.next());
            j++;
         }
      }catch(ClassNotFoundException cnfe){
         cnfe.printStackTrace();
         numberOfResults = -1;
         return;
      }
........
FullTextQuery query = entityManager.createFullTextQuery(luceneQuery, classes);


that's all you need to do to get the search to work, but there is still an issue with what to do with the results because you get a list of objects and they can be a number of different class types.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 25, 2009 11:15 am 
Newbie

Joined: Mon Mar 23, 2009 12:46 pm
Posts: 6
sorry for the double post, but i have another question, if I have a couple of classes like

Code:
Foo{
  String name
  String summary
}

Bar{
  String header
  String desc
}


if I put the @Field Annotation in them so the field names are the same (so the field name would be like title and description) would I be access them when I'm going to display the information? I would think this is possible, but I'm not sure how to do it.

I'm talking about something like
Code:
Foo {

  String name;
  String summary;

  @Field(name = "title", index = Index.TOKENIZED)
  String getName(){
    return this.string;
  }
 
  @Field(name = "description", index = Index.TOKENIZED)
  String getSummary(){
    return this.summary
  }

}

Bar {

  String header;
  String desc;

  @Field(name = "title", index = Index.TOKENIZED)
  String getHeader(){
    return this.string;
  }
 
  @Field(name = "description", index = Index.TOKENIZED)
  String getDesc(){
    return this.summary
  }

}


and then i would be able to access the title and description of the objects no matter what their class is. is this possible?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 26, 2009 3:59 am 
Pro
Pro

Joined: Wed Oct 03, 2007 2:31 pm
Posts: 205
possym wrote:
sure no problem, but the HibernateUtil is from a framework I'm using so you'll need to get the session another way.

Code:
   Session session = HibernateUtil.getSession(entityManager);
   Map map = session.getSessionFactory().getAllClassMetadata();
   Set<String> entities = map.keySet();
   Iterator<String> entitiesI = entities.iterator();

   Class[] classes = new Class[entities.size()];
   int j=0;
      //converts the set of strings into an array of classes
      try{
         while(entitiesI.hasNext()){
            classes[j] = Class.forName(entitiesI.next());
            j++;
         }
      }catch(ClassNotFoundException cnfe){
         cnfe.printStackTrace();
         numberOfResults = -1;
         return;
      }
........
FullTextQuery query = entityManager.createFullTextQuery(luceneQuery, classes);


that's all you need to do to get the search to work, but there is still an issue with what to do with the results because you get a list of objects and they can be a number of different class types.


Yeah, that was what I was wondering. I guess some kind of annotation to specify what the return type should be...for example:

@Indexed
@SearchResultType (something)

And then the search code could be:

Code:
FullTextQuery query = entityManager.createFullTextQuery(luceneQuery, ..SearchResultType.class);


Under the hood some processor collects the classes marketed with the annotation and populates the code..just an idea..


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