如何在 Zend search lucene 关键字中保存多个整数值?

发布于 2024-12-05 21:06:31 字数 789 浏览 2 评论 0原文

我想在我的数据库模型上添加一个 lucene 层。一切工作正常,我在主要内容旁边使用了几个关键字,这些关键字由一些 HTML 数据填充。

例如,

$doc = Zend_Search_Lucene_Document_Html::loadHTML($html);
$doc->addField(Zend_Search_Lucene_Field::keyword('author_id', 1));

我有一个 1:N 表,其中包含一些我想用来过滤搜索结果的数据。假设该文档是一篇可以在不同类别中发布的文章。我现在有一个类别 ID 数组,我想以某种方式将其添加到此文档中。我的第一个方法是仅内爆数组并将其保存为字符串:

$doc->addField(Zend_Search_Lucene_Field::keyword('categories', '12, 15, 22'));

但事实证明搜索有点复杂(搜索类别 1 也返回 12 和 15)。然后我创建了一些“神奇”的字符串,例如 CAT_12_A 而不是 12,以使它们独一无二:

$doc->addField(Zend_Search_Lucene_Field::keyword('categories', 'CAT_12_A, CAT_15_A, CAT_22_A'));

这工作得很好,但我对这个解决方案并不完全满意,因为它显然是一个 hack。是否有机会在没有这种奇怪的 hack 的情况下向 Zend Lucene 文档添加多值“关键字”?

谢谢你的帮助。

I would like to add a lucene layer on my database model. Everything works fine and I use several keywords beside the main content which is filled by some HTML data.

e.g.

$doc = Zend_Search_Lucene_Document_Html::loadHTML($html);
$doc->addField(Zend_Search_Lucene_Field::keyword('author_id', 1));

I have an 1:N-table with some data I would like to use aswell for filtering the search result. Let's assume that the document is an article which can be published in different categories. I now have an array of category IDs which I somehow want to add to this document. My first approach was to just implode the array and save it as a string:

$doc->addField(Zend_Search_Lucene_Field::keyword('categories', '12, 15, 22'));

But it turned out to be a little complicated for search (searching for category 1 also returned 12 and 15). Then I created some "magical" strings such as CAT_12_A instead of 12 to make them unique:

$doc->addField(Zend_Search_Lucene_Field::keyword('categories', 'CAT_12_A, CAT_15_A, CAT_22_A'));

This works pretty fine but I am not totally happy with this solution because it is obviously a hack. Is there any chance to add a multi-value "keyword" to a Zend Lucene document without such a strange hack?

Thanks for helping.

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

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

发布评论

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

评论(1

深海蓝天 2024-12-12 21:06:31

找到你的问题后,我做了很多谷歌搜索:看起来没有办法在不修改 zend 框架核心代码的情况下直接添加多值字段。

你尝试了吗

$doc->addField(Zend_Search_Lucene_Field::keyword('category_1', '12' ));
$doc->addField(Zend_Search_Lucene_Field::keyword('category_2', '15' ));
$doc->addField(Zend_Search_Lucene_Field::keyword('category_3', '22' ));

?看起来 query() 将搜索所有字段,所以如果你不关心哪个字段匹配你应该没问题?

I did a lot of googling after finding your question: looks like there is no way to directly add multi-valued fields without modifying zend framework core code.

Did you try

$doc->addField(Zend_Search_Lucene_Field::keyword('category_1', '12' ));
$doc->addField(Zend_Search_Lucene_Field::keyword('category_2', '15' ));
$doc->addField(Zend_Search_Lucene_Field::keyword('category_3', '22' ));

? Looks like query() will search across all fields, so if you don't care which field matches you should be fine?

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