模块:Mongoid::SearchIndexable::ClassMethods

定义于:
lib/mongoid/search_indexable.rb

Overview

该功能的类级方法的实现。

实例方法摘要折叠

实例方法详细信息

# create_search_indexesArray<String>

请求创建所有已注册的搜索索引。 请注意,搜索索引是异步创建的,可能需要几分钟才能完全可用。

返回:

  • ( Array<String> )

    搜索索引的名称。



65
66
67
68
69
# File 'lib/mongoid/search_indexable.rb', line 65

def create_search_indexes
  return if search_index_specs.空?

  集合.search_indexes.create_many(search_index_specs)
end

# remove_search_index (name: nil, ID : nil) ⇒对象

删除给定名称或 ID 指定的搜索索引。 必须提供名称或 ID,但不能同时提供两者。

参数:

  • 名称 ( string | nil ) (默认为: nil

    要删除的索引的名称

  • id ( string | nil ) (默认为: nil

    要删除的索引的ID



107
108
109
110
111
112
113
114
# File 'lib/mongoid/search_indexable.rb', line 107

def remove_search_index(名称: nil, ID : nil)
  记录器.信息(
    " 搜索索引: 正在 删除 ID' #{ collection . name } ' 上的 搜索 集合 ' #{ { name || ID } ' " \ " 。 ")集合 search_indexes  drop_one ( name: name , ID : ID ) end

# remove_search_indexes对象

注意:

如果这样可以仅删除搜索索引,那就太好了

请求删除所有已注册的搜索索引。 请注意,搜索索引是异步删除的,可能需要几分钟才能完全删除。

已在模型上声明,但由于模型可能未命名索引,因此我们不能保证知道相应索引的名称或 ID。 不过,可以合理地假设,模型的意图是一对一地声明所有所需的搜索索引,因此删除所有搜索索引就足够了。 如果需要删除特定索引或索引设立,请考虑将 search_indexes.each 与 remove_search_index 一起使用。



128
129
130
131
132
# File 'lib/mongoid/search_indexable.rb', line 128

def remove_search_indexes
  search_indexes. do |spec|
    remove_search_index ID : spec[' ID ']
  end
end

# search_index (name_or_defn, defn = nil) ⇒对象

为提供的单键或复合键添加索引定义。

例子:

创建基本索引。

class Person
  include Mongoid::Document
  field :name, type: String
  search_index({ ... })
  search_index :name_of_index, { ... }
end

参数:

  • name_or_defn (符号 | string | 哈希)

    要定义的索引名称或索引定义。

  • defn 哈希 (默认为: nil

    搜索索引定义。



147
148
149
150
151
152
153
# File 'lib/mongoid/search_indexable.rb', line 147

def search_index(name_or_defn, defn = nil)
  名称 = name_or_defn
  名称, defn = nil, 名称 if 名称.is_a?(哈希)

  spec = { 定义: defn }.点击 { |s| s[:name] = 名称.to_s if 名称 }
  search_index_specs.推动(spec)
end

# search_indexes (options = {}) ⇒对象

查询当前模型集合上可用搜索索引的便捷方法。

参数:

  • 选项 哈希 (默认为: {}

    要传递给搜索索引查询的选项。

选项哈希 ( options ):

  • :id string

    要查询的特定索引的 ID(可选)

  • :name string

    要查询的特定索引的名称(可选)

  • :aggregate 哈希

    要传递给聚合命令的选项哈希(可选)



98
99
100
# File 'lib/mongoid/search_indexable.rb', line 98

def search_indexes(选项 = {})
  集合.search_indexes(选项)
end

# wait_for_search_indexes (names, Interval: 5 ) {|SearchIndexable::Status| ... } ⇒对象

等待创建命名搜索索引。

参数:

  • 名称 ( Array<String> )

    要等待的索引名称列表

  • interval 整数 (默认为: 5

    再次轮询之前等待的秒数(仅在给出进度回调时使用)。

产量:



78
79
80
81
82
83
84
85
86
# File 'lib/mongoid/search_indexable.rb', line 78

def wait_for_search_indexes(名称, interval: 5)
  循环 do
    状态 = 状态.new(get_indexes(名称))
    产量 状态 if block_given?
    中断 if 状态.准备好了吗?

    睡眠 interval
  end
end