2

I am using Hibernate Search built on top of Lucene indexing. If indexes are created against database table the performance will be good in returning the results.

My question is, once indexes are created, if we query for the results does Hibernate Search fetch results from the original database table using the created indexes? or does it not need to hit the database to fetch the results?

Thanks!

Sanne
  • 6,027
  • 19
  • 34
user1016403
  • 12,151
  • 35
  • 108
  • 137

2 Answers2

5

Unless you use Projections the indexes are used only to identify the set of primary keys matching the query, these are then used to load the entities from the Database.

There are many good reasons for this:

  • As you pointed out, we don't store all data in the index: a larger index is a slower index
  • Adding all needed metadata to the index would make indexing a very expensive operation
  • Value extraction from the index is not efficient at all: it's good at queries, no more
  • Relational databases are very good at loading data by primary key
  • If you DB isn't good enough, second level cache is excellent to load by primary key
  • By loading from the DB we guarantee consistency especially with async indexing
  • By loading from the DB you have entities participate in Transactions and isolation

That said, if you don't need fully managed entities you can use Projections to load the fields you annotated as Stored.YES. A common pattern is to provide preview of matches using projections, and then when the user clicks for details to load the full entity matching that result.

Sanne
  • 6,027
  • 19
  • 34
1

By default, every time an object is inserted, updated or deleted through Hibernate, Hibernate Search updates the according Lucene index as per documentation

Hence, the further searches will yeild the data through lucene indexes only.

Another Question explaining how Indexes work

Community
  • 1
  • 1
TheWhiteRabbit
  • 15,480
  • 4
  • 33
  • 57
  • Thanks for ur reply. do u mean to say, if we have the indexes it would fetch everything from the indexes itself and not from database? – user1016403 Jan 28 '13 at 11:27
  • yes and indexes are updated along with DB interactions (CRUD ops) – TheWhiteRabbit Jan 28 '13 at 11:28
  • PLz consider an example..i have a domain object with 5 properties and i have placed @Field on only one property then if we query, how does lucene gets data for the other fields which are not indexed? – user1016403 Jan 28 '13 at 11:30
  • Indexes doesn't contain the data, they help to locate the data your looking for faster. hence making the search faster !!! – TheWhiteRabbit Jan 28 '13 at 11:33
  • So, finally it has to fetch the results from database using the indexes right? – user1016403 Jan 28 '13 at 11:41
  • obviously , yes . this link is [useful](http://stackoverflow.com/questions/1108/how-does-database-indexing-work) – TheWhiteRabbit Jan 28 '13 at 11:42