问题描述
在阅读 Tire 文档时,我的印象是您应该使用 mapping
或 to_indexed_json
方法,因为(我的理解是..)mapping
用于提供 to_indexed_json
代码>.
While reading the Tire doc, I was under the impression that you should use either mapping
or to_indexed_json
methods, since (my understanding was..) the mapping
is used to feed the to_indexed_json
.
问题是,我发现了一些同时使用了两者的教程.为什么?
The problem is, that I found some tutorials where both are used. WHY?
基本上,我的应用程序现在可以使用 to_indexed_json
但我不知道如何设置某些属性的 boost 值(因此我开始研究映射的原因),我想知道使用两者是否会产生一些冲突.
Basically, my app works right now with the to_indexed_json
but I can't figure out how to set the boost value of some of the attributes (hence the reason I started looking at mapping) and I was wondering if using both would create some conflicts.
推荐答案
虽然 mapping
和 to_indexed_json
方法是相关的,但实际上它们有两个不同的目的.
While the mapping
and to_indexed_json
methods are related, they serve two different purposes, in fact.
mapping
方法的目的是为索引中的文档属性定义映射.您可能希望将某些属性定义为not_analyzed",这样它就不会被分解成tokens,或者为该属性设置特定的分析器,或者(如您所提到的)索引时间boost 因素.您还可以定义 multifield 属性、日期 类型的自定义格式等.
The purpose of the mapping
method is to define mapping for the document properties within an index. You may want to define certain property as "not_analyzed", so it is not broken into tokens, or set a specific analyzer for the property, or (as you mention) indexing time boost factor. You may also define multifield property, custom formats for date types, etc.
这个映射然后被使用,例如.当Tire自动创建模型的索引.
This mapping is then used eg. when Tire automatically creates an index for your model.
to_indexed_json
方法的目的是为您的文档/模型定义 JSON 序列化.
默认to_indexed_json
方法 确实使用您的映射定义,仅使用映射中定义的属性——如果您足够关心定义映射,默认情况下轮胎 仅索引具有定义映射的属性.
The default to_indexed_json
method does use your mapping definition, to use only properties defined in the mapping — on a basis that if you care enough to define the mapping, by default Tire indexes only properties with defined mapping.
现在,当您想要严格控制您的模型实际上是如何序列化为 elasticsearch 的 JSON 时,您只需定义自己的 to_indexed_json
方法(如 README 指示).
Now, when you want a tight grip on how your model is in fact serialized into JSON for elasticsearch, you just define your own to_indexed_json
methods (as the README instructs).
这个自定义的MyModel#to_indexed_method
通常不关心mapping的定义,而是从头开始构建JSON序列化(通过利用ActiveRecord的to_json
,使用 JSON 构建器,例如 jbuilder,或者只是构建一个普通的 Hash并调用 Hash#to_json
).
This custom MyModel#to_indexed_method
usually does not care about mapping definition, and builds the JSON serialization from scratch (by leveraging ActiveRecord's to_json
, using a JSON builder such as jbuilder, or just building a plain old Hash and calling Hash#to_json
).
因此,要回答您问题的最后一部分,同时使用 mapping
和 to_indexed_json
绝对不会产生任何冲突,并且在在 elasticsearch 中使用高级功能所需的事实.
So, to answer the last part of your question, using both mapping
and to_indexed_json
will absolutely not create any conflicts, and is in fact required to use advanced features in elasticsearch.
总结:
- 您使用
mapping
方法为搜索引擎定义模型的映射 - 您使用自定义
to_indexed_json
方法来定义搜索引擎如何查看您的文档/模型.
- You use the
mapping
method to define the mapping for your models for the search engine - You use a custom
to_indexed_json
method to define how the search engine sees your documents/models.
这篇关于ElasticSearch &轮胎:使用映射和 to_indexed_json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!