django-haystack SOLR 配置中的动态字段

发布于 2024-12-16 18:31:18 字数 84 浏览 3 评论 0原文

如何配置 search_indexes.py 以在 django-haystack 中索引动态字段。我使用 SOLR 作为 haystack 的搜索引擎。

How to configure search_indexes.py to index dynamicFields in django-haystack. I'm using SOLR as the search engine for haystack.

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

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

发布评论

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

评论(3

鸠书 2024-12-23 18:31:18

如果您使用 Haystack 的 build_solr_schema 管理命令来创建 schema.xml,请注意,它会自动包含流行字段类型的各种动态字段。例如,查看Haystack v2.1 的架构模板。 (这看起来从 Haystack v1 起就已经存在了。)

这允许您在搜索索引的准备方法中创建动态命名的字段。例如,如果您正在为不断变化的合作伙伴组提供一个 id 字符串的注释进行索引,那么您可以这样做:

def prepare(self, obj):
    self.prepared_data = super(NoteIndex, self).prepare(obj)
    for (partner_name, partner_id) in get_partners():
        self.prepared_data['%s_s' % partner_name] = partner_id
    return self.prepared_data

这里的关键是字段名称以“_s”结尾,根据架构,它是一个字符串类型的动态名称。

不幸的是,这些动态合作伙伴字段没有在 SearchIndex 类的顶部明确定义。您可能想在评论中提及这一点。

If you are using Haystack's build_solr_schema management command to create your schema.xml, notice that it automatically includes various dynamicFields for popular field types. For example, check out the schema template for Haystack v2.1. (This looks like it's been there since Haystack v1.)

This allows you to create dynamically-named fields in your search index's prepare method. For example, if you were indexing notes that could have an id string for your ever-changing group of partners, you could do this:

def prepare(self, obj):
    self.prepared_data = super(NoteIndex, self).prepare(obj)
    for (partner_name, partner_id) in get_partners():
        self.prepared_data['%s_s' % partner_name] = partner_id
    return self.prepared_data

The key thing here is that the field name ends with "_s", which according to the schema is a dynamic name for string types.

Unfortunately, these dynamic partner fields are not explicitly defined at the top of your SearchIndex class. You may want to mention this in a comment.

遥远的绿洲 2024-12-23 18:31:18

据我在 django-haystack 1.2.* 的源代码中看到的,你不能这样做。您可以编写自己的模式,而不是使用管理命令生成它并使用它。

As far as I can see in the source code of django-haystack 1.2.* you can't do this. You can write own schema instead of generating it using management commands and use it.

伤痕我心 2024-12-23 18:31:18

正如 @nofinator 所说,您可以在 SearchIndex< 的 .prepare 方法中执行此操作/code> 类,通过将字段名称与 SOLR Schema.xml 中的前缀连接起来。

默认情况下,Haystack(当前版本是 2.1.1)附带一些默认的 DynamicField,例如 *_s。但如果您愿意,您可以创建自己的 DynamicField。

在我的项目中,我将创建 attr_* 字段并使其工作正常。

您需要做的就是添加此字段,并在 Schema.xml 中使用相同的语法
您可以手动或覆盖标准 Haystack build_solr_schema 管理命令。(顺便说一句,它使用标准 django 渲染模板 fnc。所以它非常简单。

As @nofinator say, you can do this in .prepare method of SearchIndex class by concatinating field name with prefix from SOLR Schema.xml.

By default Haystack(current ver. is 2.1.1) ships with some default DynamicField like a *_s. But if you want, you can make your own DynamicField.

In my project ill make attr_* field and its work fine.

All you need to do, is add this field, with same syntax in Schema.xml
You can do in manualy or overriding standart Haystack build_solr_schema management command.(Btw, its uses standart django render template fnc. so its pretty easy.

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