简体   繁体   English

Hibernate Search(Lucene索引)如何工作?

[英]How does Hibernate Search (Lucene indexing) work?

I am using Hibernate Search built on top of Lucene indexing. 我正在使用基于Lucene索引构建的Hibernate Search。 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? 我的问题是,一旦创建索引,如果我们查询结果,Hibernate Search会使用创建的索引从原始数据库表中获取结果吗? or does it not need to hit the database to fetch the results? 还是不需要访问数据库即可获取结果?

Thanks! 谢谢!

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. 除非使用Projections ,否则索引仅用于标识与查询匹配的主键集,然后将这些索引用于从数据库中加载实体。

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. 就是说,如果不需要完全托管的实体,则可以使用Projections加载标注为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. 一种常见的模式是使用投影提供匹配的预览,然后在用户单击以获取详细信息以加载与该结果匹配的完整实体时提供预览。

By default, every time an object is inserted, updated or deleted through Hibernate, Hibernate Search updates the according Lucene index as per documentation 默认情况下,每次通过Hibernate插入,更新或删除对象时,Hibernate Search都会根据文档更新相应的Lucene索引

Hence, the further searches will yeild the data through lucene indexes only. 因此,进一步的搜索将仅通过Lucene索引获得数据。

Another Question explaining how Indexes work 另一个解释索引如何工作的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM