django-haystack SOLR 配置中的动态字段
如何配置 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您使用 Haystack 的
build_solr_schema
管理命令来创建 schema.xml,请注意,它会自动包含流行字段类型的各种动态字段。例如,查看Haystack v2.1 的架构模板。 (这看起来从 Haystack v1 起就已经存在了。)这允许您在搜索索引的准备方法中创建动态命名的字段。例如,如果您正在为不断变化的合作伙伴组提供一个 id 字符串的注释进行索引,那么您可以这样做:
这里的关键是字段名称以“_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:
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.
据我在 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.
正如 @nofinator 所说,您可以在
SearchIndex< 的
.prepare
方法中执行此操作/code> 类,通过将字段名称与 SOLRSchema.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 ofSearchIndex
class by concatinating field name with prefix from SOLRSchema.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.