wood burning stoves 2.0*
The moose likes Object Relational Mapping and the fly likes hibernate search returns no result Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "hibernate search returns no result" Watch "hibernate search returns no result" New topic
Author

hibernate search returns no result

Christian Bastian
Greenhorn

Joined: Aug 26, 2013
Posts: 12
I am trying to create a hibernate full text search using hibernate-search-4.3.0.Final.jar There is no errors in this application, but my Lucene query unsing the query DSL doesn't return any results. I mean It doesn't return any of rows in the table. can any one please help me.

This is my function:



and this is my entity class:

Karthik Shiraly
Ranch Hand

Joined: Apr 04, 2009
Posts: 501
    
    5
Fields which should be searchable should have @Field(index=Index.YES). http://docs.jboss.org/hibernate/search/4.3/reference/en-US/html/search-mapping.html#field-annotation

Unrelated to the problem, but I feel you don't need Analyze.YES for the numeric ID field; it's typically used only for text fields.

In general, for debugging issues in hibernate search and lucene , Luke is a very useful tool. It shows you all the contents of the search index.

Christian Bastian
Greenhorn

Joined: Aug 26, 2013
Posts: 12
Hi thank you so much for your answer

I have changed my code as what you said:


but I get an empty result

I am using Luke to see what is in the index files, If I open segment.gen with Luke, I see nothing in this file:



Do you have any idea? what should I do to solve this problem?
Karthik Shiraly
Ranch Hand

Joined: Apr 04, 2009
Posts: 501
    
    5
Do you see only "segments.gen" and "segments_<some-char>" in that index directory? If so, then no documents have been added after the code change and Luke output is correct.
Exercise the application such that some hibernate entities get created and transactions committed. Then you should see additional files in that index directory, with filenames like _a.fdt, _a.tis, ...
Christian Bastian
Greenhorn

Joined: Aug 26, 2013
Posts: 12
Hi karthik,
Thank you for your answer. I see another file segment_1. it contains likes the segment.gen

Do you have any Idea? how can I solve my problem?
Karthik Shiraly
Ranch Hand

Joined: Apr 04, 2009
Posts: 501
    
    5
I see another file segment_1. it contains likes the segment.gen

That means the index is currently empty, because no entities have been saved.
After the change to "Index.YES", have you saved some entities using hibernate API and committed the transaction? Unless that's done, the index will remain empty.

Note that if you already have some entities saved in DB, then you have to explicitly index those entities using index() method.
But for entities newly created and saved, hibernate will take care of indexing them automatically.
Karthik Shiraly
Ranch Hand

Joined: Apr 04, 2009
Posts: 501
    
    5
I see another file segment_1. it contains likes the segment.gen

That means the index is currently empty, because no entities have been saved.
After the change to "Index.YES", have you saved some entities using hibernate API and committed the transaction? Unless that's done, the index will remain empty.

Note that if you already have some entities saved in DB, then you have to explicitly index those entities using index() method.
But for entities newly created and saved, hibernate will take care of indexing them automatically.
Christian Bastian
Greenhorn

Joined: Aug 26, 2013
Posts: 12
Hi Karthik, thanks again for your post

That means the index is currently empty, because no entities have been saved.

that means that my code doesnt work correctly

After the change to "Index.YES", have you saved some entities using hibernate API and committed the transaction? Unless that's done, the index will remain empty.

I have chande "Index.YES" in my entity class, but the I still get an empty result. What should I do exactly? I have an entity class and some data in my database and I want to search over these information in DB with my entity class and hibernate. I have no transaction, and I dont commit it (sorry I am new in this world, and I dont know exactly, what should I do) . you can see again my DAO:



I have indexed my entity with @indexed in my entity class, should I do more?
Karthik Shiraly
Ranch Hand

Joined: Apr 04, 2009
Posts: 501
    
    5
From your replies, it looks to me like there is already some data in your DB , and you are expecting that the lucene search query will automatically search this data that already exists in DB.

But Hibernate Search doesn't work that way. It does not automatically index data that is already in the DB.

For such existing data, your application should have logic to explicitly have them indexed. How to do this explained along with code examples here.
This bulk indexing is usually a one time operation. It's run the very first time an application integrates hibernate search for existing data. After that, if any new data is created/updated/deleted, hibernate will automatically
update the index.

I have indexed my entity with @indexed in my entity class, should I do more?

That does not index existing data. It just marks the entity as something to be indexed *from that point on*. But data that is already existing has to be explicitly indexed by you as described above.

On a broader note, does your application require text search at all? From the code snippet, it looks like only the numeric ID is being searched. If your application has only numeric ID searches, then plain hibernate queries are good enough and hibernate search does not provide any benefits.

Christian Bastian
Greenhorn

Joined: Aug 26, 2013
Posts: 12
Hi Karthik
From your replies, it looks to me like there is already some data in your DB , and you are expecting that the lucene search query will automatically search this data that already exists in DB.


yes, exactly

On a broader note, does your application require text search at all?

I must not do that. I want to know, how doese Fulltextsearch work. I am using MongoDB and I want to implement a very simple query with Lucene. (SELECT City WHERE UserID=***) . May be the fulltext search is not the best Idea, but...

I dont know plain hibernate query is able to retrieve data from MongoDB.
Karthik Shiraly
Ranch Hand

Joined: Apr 04, 2009
Posts: 501
    
    5
I am using MongoDB and I want to implement a very simple query with Lucene. (SELECT City WHERE UserID=***) . May be the fulltext search is not the best Idea, but...I dont know plain hibernate query is able to retrieve data from MongoDB.

I realized you're using mongodb and hibernate *OGM*, not ORM only now. It's the first time I have come across this OGM framework, but it seems OGM is similar enough to ORM - in fact that seems to be their goal, to provide familiar ORM APIs for nosql DBs.

If your query is a simple "select city from users where userid=n" then possibly standard hibernate query is good enough, as described here.
Try something like

or


However, if you anticipate more complex queries or if you want text search support, then continue with hibernate search based querying. They too recommend the same:
We do realize these are strong limitations, so while it might be interesting to try it out, for real usage we suggest for now to use either Hibernate Search FullTextQueries or the native query technology of the NoSQL storage you are using.

You just have to do the one-time indexing of existing data as described in my previous post.

I want to know, how does Fulltextsearch work.

Full text search is useful for querying textual data in the same way a google search queries textual content of websites.
For example, if there is a table with many textual fields (like "description", "notes", "comments", ...) and application should find all database rows where the word "electricity" or related words (like "electrical", "electricity", "electric", "electrician"...) occur, then a full text search capability is most convenient. In plain SQL, this would be problematic for multiple reasons:
1) SQL does not support find in all fields. So query would have to be complex one like


2) Most databases don't support the "words like this" concept (termed "stemming"). For example, words like "electricity" are "electric", "electrical", "electrician", all of them stemming from the base word "electric". Most databases don't do this well.

In the context of hibernate search, how full text search works is:
1) Whenever an entity is created/updated using hibernate APIs, all the contents of that entity are "indexed".
2) So if the entity with ID=5000 has a "description" with the word "electrical", and an entity with ID=6000 has a description with the word "electrician", then the index is updated with (reverse) index entries like "electric -> 5000" and "electic -> 6000".
3) Later when we query for "electrical" using the lucene query DSL, it'll first stem the query to get "electric". It then searches the index for all occurrences of "electric" and gets back 5000 and 6000.
4) Hibernate then loads the entities with these two IDs from the database and returns back the objects.
5) For maintaining the index and for querying, hibernate search delegates to the Lucene framework. Lucene does all the heavy hitting; hibernate search is simply an integrator between hibernate ORM/OGM and lucene.
Christian Bastian
Greenhorn

Joined: Aug 26, 2013
Posts: 12
You are the best....
Karthik Shiraly
Ranch Hand

Joined: Apr 04, 2009
Posts: 501
    
    5
LOL, unfortunately still a long way to go, but thank you for the kind compliment!
Christian Bastian
Greenhorn

Joined: Aug 26, 2013
Posts: 12
Hi karthik,

If I run this Code, I get an exception:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

my code:



Do have any idea, why I get this error?
Karthik Shiraly
Ranch Hand

Joined: Apr 04, 2009
Posts: 501
    
    5
From the code, I can see 2 issues, but I'm not sure whether those are the cause of the NPE:
1) The query should be since member name in entity is "IdU". In hibernate queries, always use class/member names, not table/column names.
2) It's possible that user is null, if the user ID is not present in DB . There should be a null check before user.getCity() to handle this possibility.

On which line do you get the null pointer exception? The stack trace will tell that.
Christian Bastian
Greenhorn

Joined: Aug 26, 2013
Posts: 12
I have changed my code a little bit ;)

nowUser is the name of the class andUserID is a field in it. I have the UserID=75191 in my DB. I posted my Stacktrace, I didnt see the line number, but I think it is in this line:



if I comment it the code runs without any problem

Karthik Shiraly
Ranch Hand

Joined: Apr 04, 2009
Posts: 501
    
    5

at org.hibernate.internal.AbstractQueryImpl.uniqueResult(AbstractQueryImpl.java:905)
at hogm.hibernateogm_mongodb.FilterCities.jButton1MouseClicked(FilterCities.java:145)

Yes, it looks like hibernate is having trouble processing that query. I'm not sure whether it's the query itself, or something to do with the configuration. All the stack entries for "org.hibernate.engine.jdbc" is a bit odd considering that it's a nosql store, but I'm not familiar enough with OGM internals.

You can try a simpler query like

It should return all users in the DB. If this one too throws the same exception, then perhaps it's a configuration issue.
What does hibernate.cfg.xml contain? Have you followed these settings suitable for mongodb?
Christian Bastian
Greenhorn

Joined: Aug 26, 2013
Posts: 12
Thank you so much

I have changed my code, but I have exactly the same problem. :/

My configuration looks like this:

Karthik Shiraly
Ranch Hand

Joined: Apr 04, 2009
Posts: 501
    
    5
Just some guessing at this point:
1) "hibernate.transaction.factory_class" - I don't think this is relevant to mongodb, or atleast the value should not be JDBCTransactionFactory (but I'm not sure what it should be). Try with removing that.
2) "hibernate.ogm.datastore.provider" - that documentation says it should be "mongodb" (all lowercase)
Christian Bastian
Greenhorn

Joined: Aug 26, 2013
Posts: 12
Hi Karthik,

thanks again

with this configuration I am able to insert some Data in my MongoDB, I think the confiuration is correct
this part is for hibernate search and lucene:



I deleted this part, but the execption remains as before :/
Karthik Shiraly
Ranch Hand

Joined: Apr 04, 2009
Posts: 501
    
    5
with this configuration I am able to insert some Data in my MongoDB, I think the confiuration is correct

Do you mean insertion was already working even with the original configuration, or did it start working after you changed the settings I mentioned in my previous post?
Also, is it User entities that you inserted or some other entities?

the execption remains as before

Not sure why. They mention in their documentation that the hibernate query support in OGM is still incomplete, and recommend using hibernate search or native mongodb queries for production use. Perhaps
this exception is due to some shortcoming in the OGM QL implementation (though something like "from User" seems to me much simpler than the example queries in their own documentation).
Christian Bastian
Greenhorn

Joined: Aug 26, 2013
Posts: 12
Hi Karthik,

thank you soooo much for your posts
You have gave me a very helpful hint

I cant query saved data with lucene, becuase they are not indexed. The meaning of index in lucene ist different from index concept in Database, then I have decided to index data when I want to save them in MongoDB. Now my Index directory looks like total different as before.

now instead of only two file (segment.gen and segment_1) I have many files:



and if I open the index file with Luke It is not only 0:



but I think it is not correct, because for userID I have integere value in Database, but I see here another values (some meaningless values) and for each UserID I have a city name, but here I see only the name of one city. How do you find? It should be so, or I did something wrong?

hier you can fine my entity class:
Karthik Shiraly
Ranch Hand

Joined: Apr 04, 2009
Posts: 501
    
    5
for userID I have integere value in Database, but I see here another values (some meaningless values) and for each UserID I have a city name, but here I see only the name of one city. How do you find? It should be so, or I did something wrong?

I wouldn't pay much importance to the field values shown in luke, because your field settings are set to "Store.NO". That means the raw field values are not stored in lucene index, just an internal representation.
Also there's only 1 document added, so 1 city makes sense.
If the hibernate search query in your original post works (with correct userID supplied), then you're good to proceed.
If you do want to see actual field values in luke, then change the fields to Store.YES. You can then browse the values of each entity from the "Documents" tab.

The term count of 8 for UserID is odd, considering that number of documents is only one and each document has only one UserID. I don't know exactly why that is - it's possible it's because the same index has been used even after code changes related to index.yes were made. I don't think it'll cause any problem but if you want to be sure then shutdown your application, delete the complete index directory and restart your application. That'll create a fresh index. Then add entities and check.
Christian Bastian
Greenhorn

Joined: Aug 26, 2013
Posts: 12
Hi Karthik,
Thank you very much for your nice explanation

Somehow I have solved my problem but not complete, If I use a legall UserID and then run this command:



I see 1 as an output, when I use an illegal UserID I see 0, that means, that works correctly

but I cant show the name of the city, If I use this code:

I see again this exception:



also this code returns the same same exception:


my complete stack trace is:
Karthik Shiraly
Ranch Hand

Joined: Apr 04, 2009
Posts: 501
    
    5
It looks like the exception is occurring inside hibernate search's FullTextQueryImpl.list() method in both cases. Unfortunately, I'm not able to spot from the code snippets what could be the problem - it looks ok to me.
Perhaps you can try posting in hibernate OGM forums for some clue.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: hibernate search returns no result