forked from mongodb/docs-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatlas-search-index.rb
96 lines (80 loc) · 2.38 KB
/
atlas-search-index.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
require 'mongo'
# Replace the placeholders with your credentials
uri = "<connection string>"
# Sets the server_api field of the options object to Stable API version 1
options = { server_api: { version: "1" }}
# Creates a new client and connect to the server
client = Mongo::Client.new(uri, options)
database = client.use('sample_mflix')
collection = database[:movies]
# start-create-search-index
# Creates indexes on all dynamically indexable fields with a default index name
collection.search_indexes.create_one(
{ mappings: { dynamic: true } }
)
# Creates an index on the specified field with the specified index name
index-definition = {
mappings: {
dynamic: false,
fields: {
<field name>: {type: '<field type>'}
}
}
}
collection.search_indexes.create_one(index_definition, name: '<index name>')
# end-create-search-index
# start-create-multiple-search-indexes
index_spec_1 = {
name: '<index 1 name>',
definition: {
mappings: {
dynamic: false,
fields: {
<field name>: {type: '<field type>'}
}
}
}
}
index_spec_2 = {
name: '<index 2 name>',
definition: {
mappings: {
dynamic: false,
fields: {
<field name>: {type: '<field type>'}
}
}
}
}
collection.search_indexes.create_many([index_spec_1, index_spec_2])
# end-create-multiple-search-indexes
# start-update-search-indexes
updated_definition = {
mappings: {
dynamic: false,
fields: { <updated field name>: { type: '<updated field type>' } }
}
}
# Specifies the index to update by using the index name
collection.search_indexes.update_one(updated_definition, name: '<index name>')
# Specifies the index to update by using the index id
collection.search_indexes.update_one(updated_definition, id: <index id>)
# end-update-search-indexes
# start-drop-search-index
# Specifies the index to delete by using the index name
collection.search_indexes.drop_one(name: '<index name>')
# Specifies the index to delete by using the index id
collection.search_indexes.drop_one(id: <index id>)
# end-drop-search-index
# start-list-entire-spec
puts collection.search_indexes.collect(&:to_json)
# end-list-entire-spec
# start-list-certain-elements
collection.search_indexes.each do |index_spec|
p index_spec['id']
p index_spec['name']
p index_spec['status']
p index_spec['queryable']
p index_spec['latestDefinition']
end
# end-list-certain-elements