Hibernate 搜索中的反向搜索

发布于 2024-08-29 06:54:22 字数 876 浏览 1 评论 0原文

我正在使用 Hibernate Search(它使用 Lucene)来搜索我在目录中索引的一些数据。它工作正常,但我需要进行反向搜索。通过反向搜索,我的意思是我的数据库中存储了一个查询列表,每次创建数据对象时,我需要检查其中哪个查询与数据对象匹配。当数据对象与用户创建的查询匹配时,我需要它来提醒用户。因此,我需要为刚刚创建的这个单个数据对象建立索引,并查看列表中的哪些查询具有该对象。

我已经看到 Lucene MemoryIndex 类在内存中创建索引,因此我可以为列表中的每个查询执行类似此示例的操作(尽管在 Java 查询列表中迭代不会非常有效):

//Iterating over my list<Query>
MemoryIndex index = new MemoryIndex();
//Add all fields
index.addField("myField", "myFieldData", analyzer);
...
QueryParser parser = new QueryParser("myField", analyzer);
float score = index.search(query);
if (score > 0.0f) {
    System.out.println("it's a match");
} else {
    System.out.println("no match found");
}

这里的问题是这个 Data类有几个 Hibernate 搜索注释 @Field、@IndexedEmbedded...,它们指示应如何对字段进行索引,因此当我在 FullTextEntityManager 实例上调用 index() 方法时,它使用此信息来索引目录中的对象。是否有类似的方法使用这些信息在内存中对其进行索引?

有没有更有效的方法来进行反向搜索?

I'm using Hibernate Search (which uses Lucene) for searching some Data I have indexed in a directory. It works fine but I need to do a reverse search. By reverse search I mean that I have a list of queries stored in my database I need to check which one of these queries match with a Data object each time Data Object is created. I need it to alert the user when a Data Object matches with a Query he has created. So I need to index this single Data Object which has just been created and see which queries of my list has this object as a result.

I've seen Lucene MemoryIndex Class to create an index in memory so I can do something like this example for every query in a list (though iterating in a Java list of queries would not be very efficient):

//Iterating over my list<Query>
MemoryIndex index = new MemoryIndex();
//Add all fields
index.addField("myField", "myFieldData", analyzer);
...
QueryParser parser = new QueryParser("myField", analyzer);
float score = index.search(query);
if (score > 0.0f) {
    System.out.println("it's a match");
} else {
    System.out.println("no match found");
}

The problem here is that this Data Class has several Hibernate Search Annotations @Field,@IndexedEmbedded,... which indicated how fields should be indexed, so when I invoke index() method on the FullTextEntityManager instance it uses this information to index the object in the directory. Is there a similar way to index it in memory using this information?

Is there a more efficient way of doing this reverse search?

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

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

发布评论

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

评论(2

天气好吗我好吗 2024-09-05 06:54:22

只需索引新对象(如果使用自动索引,除了提交当前事务之外,您无需执行任何操作),然后检索要运行的查询并在布尔查询中运行所有查询,将存储的查询与新对象的 id。像这样:

...
BooleanQuery query = new BooleanQuery();
query.add(storedQuery, BooleanClause.Occur.MUST);
query.add(new TermQuery(ProjectionConstants.ID, id), BooleanClause.Occur.MUST);
... 

如果你得到结果,你就知道查询匹配。

Just index the new object (if you use automatic indexing you don't have to do anything besides committing the current transaction), then retrieve the queries you want to run and run all of them in a boolean query, combining the stored query with the id of the new object. Something like this:

...
BooleanQuery query = new BooleanQuery();
query.add(storedQuery, BooleanClause.Occur.MUST);
query.add(new TermQuery(ProjectionConstants.ID, id), BooleanClause.Occur.MUST);
... 

If you get a result you know the query matched.

折戟 2024-09-05 06:54:22

由于 MemoryIndex 是一个完全独立的组件,它不扩展或实现 Lucene 的 Directory 或 IndexReader,因此我认为没有办法将其插入 Hibernate Search Annotations 中。我猜想如果您选择使用 MemoryIndex,您将需要编写 addField() 调用,这基本上反映了您在注释中所做的事情。

我们在这里讨论了多少查询?根据有多少个索引,您可能只需在 Hibernate 维护的主索引上运行查询即可,确保将搜索限制为您刚刚添加的文档 ID。或者,对于添加的每个文档,使用 RAMDirectory 创建一个单文档内存中索引,并通过该索引运行查询。

Since MemoryIndex is a completely separate component that doesn't extend or implement Lucene's Directory or IndexReader, I don't think there's a way you can plug this into Hibernate Search Annotations. I'm guessing that if you choose to use MemoryIndex, you'll need to write your addField() calls which basically mirrors what you're doing in the annotations.

How many queries are we talking about here? Depending on how many there are you might be able to get away with just running the queries on the main index that Hibernate maintains, ensuring to constrain the search to the document ID you just added. Or for every document that's added, create a one-document in-memory index using RAMDirectory and run the queries through that.

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