模块:Mongoid::Fields::ClassMethods
- 定义于:
- lib/mongoid/fields.rb ,
lib/mongoid/fields.rb
实例方法摘要折叠
-
#attribute_names ⇒ Array<String>
返回此对象可用属性的名称大量。
-
#auto_embed_field(name, model: 'voyage-4', num_dimensions: nil, quantization: nil, 相似度: nil, 索引: nil) ⇒ 对象
声明一个文本字段,并使用自动嵌入 (autoEmbed) 类型为其注册相应的Atlas Vector Search索引。
-
# clean_localized_field_names (name) ⇒ 字段
从给定字段名称中删除 _translations。
-
# database_field_name (name) ⇒ string
获取所提供字段存储在数据库中的名称。
-
#extract_id_field (attributes) ⇒ 对象
private
根据此类中定义的别名,从指定属性哈希中提取ID字段。
-
# 字段 (name, options = {}) ⇒ Field
定义文档上可访问的所有字段。对于定义的每个字段,都会将 getter 和 setter 作为实例方法添加到文档中。
-
# id_fields ⇒ Array<Symbol | string >
private
以字符串和符号形式返回此模型类的ID字段列表。
-
# replace_field (name, type) ="Serializable"
将字段替换为新类型。
-
# traverse_association_tree (key) {|The, The,是否| ... } ⇒ 字段
private
向下遍历关联树并搜索给定键的字段。
-
# using_object_id? ⇒ true | false
确定是否使用
BSON::ObjectIds作为 ID 的便捷方法。 -
#vector_field(name, dimensions:, similarity: 'cosine', 索引: nil) ⇒ 对象
声明一个vector embeddings字段,并一步为其注册相应的Atlas Vector Search索引。
实例方法详细信息
#attribute_names ⇒ Array<String>
返回此对象可用属性的名称大量。
以与 ORM 无关的方式提供字段名称。 Rails v 3.1 + 使用此方法自动包装JSON请求中的参数。
449 450 451 |
# File 'lib/mongoid/fields.rb', line 449 def attribute_names 字段.密钥 end |
# auto_embed_field (name, model: 'voyage-4', num_dimensions: nil, quantization: nil, 相似度: nil, 索引: nil) ⇒ 对象
声明一个文本字段,并使用自动嵌入 (autoEmbed) 类型为其注册相应的Atlas Vector Search索引。Atlas在索引和查询时自动生成嵌入;无需预先计算向量。
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 |
# File 'lib/mongoid/fields.rb', line 542 def (名称, 模型: 'voyage-4', num_dimensions: nil, quantization: nil, 相似度: nil, index: nil) 字段(名称, 类型: 字符串) field_spec = { 类型: 'autoEmbed', 模态: 'text', path: 名称.to_s, 模型: 模型 } field_spec[numDimensions] = num_dimensions if num_dimensions field_spec[quantization] = 量化 if 量化 field_spec[:similarity] = 相似度 if 相似度 if 索引(index) vector_search_index(索引(index), 字段: [ field_spec ]) else vector_search_index(字段: [ field_spec ]) end end |
# clean_localized_field_names (name) ⇒字段
从给定字段名称中删除 _translations。仅当尚不存在具有相同名称的字段名称或关系(即带有 _translations 后缀)时,才会执行此操作。对现有字段的检查是以递归方式完成的
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/mongoid/fields.rb', line 98 def clean_localized_field_names(名称) 名称 = database_field_name(名称.to_s) klass = self [].点击 do |res| ar = 名称.拆分(' 。 ') ar.each_with_index do |fn, i| key = fn 除非 klass.字段.键?(fn) || klass.关系.键?(fn) key = if fn.end_with?(TRANSLATIONS_SFX) fn.delete_suffix(TRANSLATIONS_SFX) else fn end end res.推动(key) if klass.字段.键?(fn) res.推动(ar.删除(i + 1).连接 (JOIN)(' 。 ')) 除非 i == ar.长度 - 1 中断 elsif klass.关系.键?(fn) klass = klass.关系[key].klass end end end.连接 (JOIN)(' 。 ') end |
# database_field_name (name) ⇒ string
获取所提供字段存储在数据库中的名称。 用于确定字段是否有别名。
459 460 461 |
# File 'lib/mongoid/fields.rb', line 459 def database_field_name(名称) 字段.database_field_name(名称, 关系, aliased_fields, aliased_associations) end |
#extract_id_field (attributes) ⇒对象
此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。
根据此类中定义的别名,从指定属性哈希中提取ID字段。
81 82 83 84 85 86 87 88 |
# File 'lib/mongoid/fields.rb', line 81 def extract_id_field(属性) id_fields.每 do |k| if v = 属性[k] return v end end nil end |
# 字段 (name, options = {}) ⇒ Field
定义文档上可访问的所有字段。对于定义的每个字段,都会将 getter 和 setter 作为实例方法添加到文档中。
577 578 579 580 581 582 583 584 585 |
# File 'lib/mongoid/fields.rb', line 577 def 字段(名称, = {}) 命名 = 名称.to_s 验证器::宏.验证(self, 名称, ) 添加 = add_field(命名, ) descendants.每 do |子类| 子类.add_field(命名, ) end 添加 end |
# id_fields ⇒ Array<Symbol | string >
此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。
以字符串和符号形式返回此模型类的ID字段列表。
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/mongoid/fields.rb', line 62 def id_fields IDS.dup.点击 do |id_fields| aliased_fields.每 do |k, v| if v == '_id' id_fields << k.to_sym id_fields << k end end end end |
# replace_field (name, type) =" Serializable "
将字段替换为新类型。
596 597 598 599 |
# File 'lib/mongoid/fields.rb', line 596 def replace_field(名称, 类型) remove_defaults(名称) add_field(名称, 字段[名称]..合并(merge)(类型: 类型)) end |
# traverse_association_tree (key) {|The, The,是否| ... } ⇒字段
此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。
向下遍历关联树并搜索给定键的字段。
627 628 629 |
# File 'lib/mongoid/fields.rb', line 627 def traverse_association_tree(key, 和块) 字段.traverse_association_tree(key, 字段, 关系, aliased_associations, 和块) end |
# using_object_id? ⇒ true | false
确定是否使用BSON::ObjectIds作为 ID 的便捷方法。
608 609 610 |
# File 'lib/mongoid/fields.rb', line 608 def using_object_id? 字段['_id'].object_id_field? end |
# vector_field(name,dimensions:,similarity:'cosine', 索引:nil) ⇒ 对象
声明一个vector embeddings字段,并一步为其注册相应的Atlas Vector Search索引。
该字段存储为数组。对于高级索引选项(quantization、索引方法、HNSW 调整),请改用显式 field + vector_search_index 组合。
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 |
# File 'lib/mongoid/fields.rb', line 490 def vector_field(名称, dimensions:, 相似度: 'cosine', index: nil) 字段(名称, 类型: 阵列) field_spec = { 类型: ' vector ', path: 名称.to_s, numDimensions: 维度, 相似度: 相似度 } if 索引(index) vector_search_index(索引(index), 字段: [ field_spec ]) else vector_search_index(字段: [ field_spec ]) end end |