-->
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.  [ 6 posts ] 
Author Message
 Post subject: Document id on a non-id field
PostPosted: Thu Oct 20, 2011 12:30 am 
Newbie

Joined: Wed Oct 19, 2011 12:45 am
Posts: 5
I am implementing hibernate full text search on my jpa entities. In one of the jpa entities I want to specify the document id on a field other than id field, which is unique.
When I index, it indexes. But the document id field value is overwritten by the value of my pk field and hence when I search, it does not fetch the objects from the database as it does not find the matching records in the database.

Here is my sample class.

Code:
@Entity
@Indexed
@Table(name="employee")
public class Employee implements Serializable {
   private static final long serialVersionUID = 1L;
   @Id   
        @GeneratedValue(strategy = GenerationType.IDENTITY)
   @Column(name="id")
        @Field(name="id", index = Index.UN_TOKENIZED, store = Store.YES)
   private Integer id;

   @Field(name="name", index = Index.TOKENIZED, store = Store.YES)
   @Column(name="name")
   private String name;

        @DocumentId(name="entityid")
   @Column(name="entityid")
   private Integer entityId;

....
...


For eg:, If my database table has records as follows:
Code:
id       name                  entityid
-------------------------------------
1        empname1          101
2        empname2          102
3        empname3          103
4        empname4          104


Problem 1: Document id is overwritten by the value of id.
After indexing, if I search for name: empname*, it finds 4 matching objects, but then, it does not retrieve the objects from the database because, in the index, entities id values would be 1,2,3,4 and hence it does not find matching records which has entityid 1 or 2 or 3 or 4.

Problem 2: More than one @DocumentId specified on entity
When I move the property document id and related code on to the top before the @id property and code, while indexing it gives an error saying "More than one @DocumentId specified on entity"


Top
 Profile  
 
 Post subject: Re: Document id on a non-id field
PostPosted: Thu Oct 20, 2011 4:57 am 
Hibernate Team
Hibernate Team

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

I am not sure what you are saying here. First of I am wondering which version of Search you are using. The feature you are talking about is available since Search 3.3. There is even a test for it in our test suite - https://github.com/hibernate/hibernate- ... dTest.java

Quote:
After indexing, if I search for name: empname*, it finds 4 matching objects, but then, it does not retrieve the objects from the database because, in the index, entities id values would be 1,2,3,4 and hence it does not find matching records which has entityid 1 or 2 or 3 or 4.

You re saying that after indexing a search returns 4 matches? What search do you mean? A fulltext search? If so, you got 4 results. Unless you are using projections Search will return you a list of objects retrieved from the database. I am also not following your reasoning about why it does not work. Do you get an exception somewhere? How does your query code look like? Have you turned on debug logging and actually looked at the SQL executed against the database?

Quote:
When I move the property document id and related code on to the top before the @id property and code, while indexing it gives an error saying "More than one @DocumentId specified on entity"

What do you mean with that? The order in which you specify and annotate properties in a class is irrelevant. At least it should be.

--Hardy


Top
 Profile  
 
 Post subject: Re: Document id on a non-id field
PostPosted: Fri Oct 21, 2011 1:31 am 
Newbie

Joined: Wed Oct 19, 2011 12:45 am
Posts: 5
I am using full text search. Hibernate search jar version is 3.4.1.Final.jar My search code is as follows:
Code:
try {
         
         Session session = SessionFactoryUtil.getFactory().getCurrentSession();
         FullTextSession fSession = Search.getFullTextSession(session);
         fSession.beginTransaction();
         
          QueryParser parser = new QueryParser(Version.LUCENE_31, "name", new StandardAnalyzer(Version.LUCENE_31));
          Query query = parser.parse( "empname*" );

          List<Employee> objects = fSession.createFullTextQuery(query, Employee.class).list();
          System.out.println("No of objects found = " + objects.size());
            for (Employee object : objects) {
               System.out.println(object);         
               
            }
         fSession.getTransaction().commit();

      } catch (ParseException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
               throw new RuntimeException("Cannot search with query string",e);
       }


When I looked at the indexed data using Luke, I see that the value of entityid is same as the value of id.


Top
 Profile  
 
 Post subject: Re: Document id on a non-id field
PostPosted: Fri Oct 21, 2011 4:38 am 
Hibernate Team
Hibernate Team

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

as I say, we have a working test case for this scenario. My guess is something else is going on in your setup.

Quote:
List<Employee> objects = fSession.createFullTextQuery(query, Employee.class).list();
System.out.println("No of objects found = " + objects.size());
for (Employee object : objects) {
System.out.println(object);

}


Does this returns something? And if so, are the values for _entityid_ and _id_ set? Can you create a test case which we can run to verify the problem. Preferably a test which runs in our test suite (like a JUnit test case)?

--Hardy


Top
 Profile  
 
 Post subject: Re: Document id on a non-id field
PostPosted: Fri Oct 21, 2011 9:35 am 
Newbie

Joined: Wed Oct 19, 2011 12:45 am
Posts: 5
I get No of objects found = 4 which is right.
But, the object put in the list is null. Hence, System.out.println(object); returns null though I have toString() in my entity class (Employee) which prints all the attributes of the class. You can just copy-paste the code that I have and see the problem OR
In your test case, you are just checking for the number of objects returned by the query. Probably, you can check for other attributes of the objects as well.


Top
 Profile  
 
 Post subject: Re: Document id on a non-id field
PostPosted: Mon Oct 31, 2011 2:15 am 
Newbie

Joined: Wed Oct 19, 2011 12:45 am
Posts: 5
I hope you have seen the issue by now and I assume this is a bug. When will this be fixed?

Thanks
Shobha


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