如何使用 Hibernate Search 从索引中省略一些对象?

发布于 2024-12-06 21:28:45 字数 265 浏览 2 评论 0原文

我们希望在 Hibernate 数据库上仅针对特定实体的某些对象启用全文搜索。有没有办法阻止休眠搜索索引该实体的某些实例?我们不想过滤搜索结果,我们只是希望某些实例根本不被索引。

举个例子:我们有一个包含在职和退休员工的数据库。我们不需要能够搜寻退休员工。我们是一家非常古老的IT公司,成立于1695年,因此我们有大约200万退休员工,我们非常喜欢,但不想索引,只索引10个活跃的员工。有没有办法让 Hibernate Search 只索引退休 = false 的员工?

问候, 约亨

We want to enable fulltext search on a Hibernate database for only some objectes of a specific entity. Is there a way to prevent hibernate search from indexing some instances of this entity? We do not want to filter the search results, we just want some instances to not be indexed at all.

An example: We have a database with employees, both active and retired. We don't need to be able to search retired employees. We're a very old IT company, founded in 1695, therefor we have about 2 million retired employees that we are very fond of, but don't want to index, only the 10 active ones. Is there a way we can tell Hibernate Search to only index employees where retired = false?

Regards,
Jochen

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

往昔成烟 2024-12-13 21:28:45

我认为您不应该直接在事件侦听器中使用IndexReader。您应该扩展(或编写新版本)现有的FullTextIndexEventListener 并在回调方法中检查您的实体,并根据退休字段调用或不调用处理工作

如果您想使用 Hibernate Search 4(连同 Hibernate Core 4),您还需要一个自定义的 HibernateSearchIntegrator

此解决方案可行,但应被视为临时解决方案,直到实施 HSEARCH-471 为止。

I don't think that you should work with the IndexReader directly in your event listener. You should instead extend (or write a new version) of the existing FullTextIndexEventListener and inspect your entity in the callback method and depending on the retired field call or not call processWork.

If you want to use Hibernate Search 4 (together with Hibernate Core 4) you will also need a custom HibernateSearchIntegrator.

This solution will work, but should be considered an interim solution until HSEARCH-471 is implemented.

离去的眼神 2024-12-13 21:28:45

您将需要一个 PreUpdateEventListener,在此侦听器中检查实体并确定是否要将其添加到 lucene 索引中。

这段代码不能保证有效,但希望您能明白。

public class LuceneUpdateListener implements PreUpdateEventListener {

    protected FSDirectory directory; // = path to lucene index

    public boolean onPreUpdate(PreUpdateEvent event)  {
        if (event.getEntity() instanceof Employee ) {

                try {       

                    Employee employee = (Employee) event.getEntity();

                    //Remove on update
                    remove((Employee) event.getEntity(), (Long) event.getId(), directory);

                    //Add it back if this instance should be indexed
                    try { 
                        if (employee.shouldBeIndexed()) {
                            add((Employee) event.getEntity(), (Long) event.getId(), directory);                         
                        }
                    } 
                    catch (Exception e) {

                    }
                } 
                catch (Exception e) {
                    throw new CallbackException(e.getMessage());
                }
            }
        }
        return false;
    }


    protected synchronized void add(Employee employee, Id employeeId, FSDirectory directory) {
          try{
            IndexWriter writer = new IndexWriter(directory, new StandardAnalyzer(), false);
            Document d = LuceneDocumentFactory.makeDocument(employee);
            writer.addDocument(d);
            writer.close();
            directory.close();
            }
            catch(Exception e) { 

            }
    }

    protected synchronized void remove(Long id, FSDirectory directory) throws IOException {
            try {
                IndexReader ir = IndexReader.open(directory); 
                ir.deleteDocuments(new Term("id", id.toString()));
                ir.close();
            }        
            catch(Exception e) {                
            }   
    }

    public FSDirectory getDirectory() {
            return directory;
    }

    public void setDirectory(FSDirectory directory) {
        this.directory = directory;
    }

}

为了在休眠事件之外索引这些对象,您可以从此类中提取逻辑,并批量处理您的员工。

另外不要忘记注册您的监听器。

You'll need a PreUpdateEventListener, in this listener inspect the entity and determine if you want to add it to the lucene index.

This code not guaranteed to work, but hopefully you'll get the idea.

public class LuceneUpdateListener implements PreUpdateEventListener {

    protected FSDirectory directory; // = path to lucene index

    public boolean onPreUpdate(PreUpdateEvent event)  {
        if (event.getEntity() instanceof Employee ) {

                try {       

                    Employee employee = (Employee) event.getEntity();

                    //Remove on update
                    remove((Employee) event.getEntity(), (Long) event.getId(), directory);

                    //Add it back if this instance should be indexed
                    try { 
                        if (employee.shouldBeIndexed()) {
                            add((Employee) event.getEntity(), (Long) event.getId(), directory);                         
                        }
                    } 
                    catch (Exception e) {

                    }
                } 
                catch (Exception e) {
                    throw new CallbackException(e.getMessage());
                }
            }
        }
        return false;
    }


    protected synchronized void add(Employee employee, Id employeeId, FSDirectory directory) {
          try{
            IndexWriter writer = new IndexWriter(directory, new StandardAnalyzer(), false);
            Document d = LuceneDocumentFactory.makeDocument(employee);
            writer.addDocument(d);
            writer.close();
            directory.close();
            }
            catch(Exception e) { 

            }
    }

    protected synchronized void remove(Long id, FSDirectory directory) throws IOException {
            try {
                IndexReader ir = IndexReader.open(directory); 
                ir.deleteDocuments(new Term("id", id.toString()));
                ir.close();
            }        
            catch(Exception e) {                
            }   
    }

    public FSDirectory getDirectory() {
            return directory;
    }

    public void setDirectory(FSDirectory directory) {
        this.directory = directory;
    }

}

In order to index these objects outside of a hibernate event you can extract the logic out of this class, and process your employees in batch.

Also don't forget to register your listener.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文