@@ -21,6 +21,9 @@ int main(){
21
21
auto collection = db[" movies" ];
22
22
// end-db-coll
23
23
24
+ // start-siv
25
+ auto siv = collection.search_indexes ();
26
+ // end-siv
24
27
{
25
28
// start-index-single
26
29
auto index_specification = make_document (kvp (" title" , 1 ));
@@ -65,5 +68,91 @@ int main(){
65
68
collection.indexes ().drop_one (" *" );
66
69
// end-remove-all-wildcard
67
70
}
71
+ {
72
+ // start-create-static-search-index
73
+ // Create an index model with your index name and definition containing the fields you want to index
74
+ auto name = " myStaticIndex" ;
75
+ auto fields = make_document (kvp (" title" , make_document (kvp (" type" , " string" ), kvp (" analyzer" ," lucene.standard" ))), kvp (" year" , make_document (kvp (" type" ," number" ))));
76
+ auto definition = make_document (kvp (" mappings" , make_document (kvp (" dynamic" , false ), kvp (" fields" , fields))));
77
+ auto model = mongocxx::search_index_model (name, definition.view ());
78
+
79
+ // Create the search index
80
+ auto result = siv.create_one (model);
81
+ std::cout << " New index name: " << result << std::endl;
82
+ // end-create-static-search-index
83
+ }
84
+ {
85
+ // start-create-dynamic-search-index
86
+ // Create an index model with your index name and definition
87
+ auto name = " myDynamicIndex" ;
88
+ auto definition = make_document (kvp (" mappings" , make_document (kvp (" dynamic" , true ))));
89
+ auto model = mongocxx::search_index_model (name, definition.view ());
90
+
91
+ // Create the search index
92
+ auto result = siv.create_one (model);
93
+ std::cout << " New index name: " << result << std::endl;
94
+ // end-create-dynamic-search-index
95
+ }
96
+ {
97
+ // start-create-multiple-search-indexes
98
+ // Create a vector to store Search index models
99
+ std::vector<mongocxx::search_index_model> models;
100
+
101
+ // Add an index model with dynamic mappings to the input vector
102
+ auto name_1 = " myDynamicIndex" ;
103
+ auto definition_1 = make_document (kvp (" mappings" , make_document (kvp (" dynamic" , true ))));
104
+ auto model_1 = mongocxx::search_index_model (name_1, definition_1.view ());
105
+ models.push_back (model_1);
106
+
107
+ // Add an index model with static mappings to the input vector
108
+ auto name_2 = " myStaticIndex" ;
109
+ auto fields = make_document (kvp (" title" , make_document (kvp (" type" , " string" ), kvp (" analyzer" ," lucene.standard" ))), kvp (" year" , make_document (kvp (" type" ," number" ))));
110
+ auto definition_2 = make_document (kvp (" mappings" , make_document (kvp (" dynamic" , false ), kvp (" fields" , fields))));
111
+ auto model_2 = mongocxx::search_index_model (name_2, definition_2.view ());
112
+ models.push_back (model_2);
113
+
114
+ // Create the search indexes
115
+ auto result = siv.create_many (models);
116
+
117
+ // Print the search index names
118
+ std::cout << " New index names:" << std::endl;
119
+ for (const std::string& name : result) {
120
+ std::cout << name << std::endl;
121
+ }
122
+ // end-create-multiple-search-indexes
123
+ }
124
+
125
+ {
126
+ // start-list-search-indexes
127
+ auto cursor = siv.list ();
128
+ for (mongocxx::cursor::iterator it = cursor.begin (); it != cursor.end (); ++it) {
129
+ std::cout << bsoncxx::to_json (*it) << std::endl;
130
+ }
131
+ // end-list-search-indexes
132
+ }
133
+ {
134
+ // start-list-search-index
135
+ auto cursor = siv.list (" myDynamicIndex" );
136
+ for (mongocxx::cursor::iterator it = cursor.begin (); it != cursor.end (); ++it) {
137
+ std::cout << bsoncxx::to_json (*it) << std::endl;
138
+ }
139
+ // end-list-search-index
140
+ // Print list() output using a range-based for loop
141
+ for (const auto &idx : cursor) {
142
+ std::cout << bsoncxx::to_json (idx) << std::endl;
143
+ }
144
+ }
145
+ {
146
+ // start-update-search-index
147
+ auto update_fields = make_document (kvp (" title" , make_document (kvp (" type" , " string" ), kvp (" analyzer" ," lucene.simple" ))), kvp (" year" , make_document (kvp (" type" ," number" ))));
148
+ auto update_definition = make_document (kvp (" mappings" , make_document (kvp (" dynamic" , false ), kvp (" fields" , update_fields))));
149
+ siv.update_one (" myStaticIndex" , update_definition.view ());
150
+ // end-update-search-index
151
+ }
152
+ {
153
+ // start-remove-search-index
154
+ siv.drop_one (" myDynamicIndex" );
155
+ // end-remove-search-index
156
+ }
68
157
69
158
}
0 commit comments