Skip to content

Commit d2124a0

Browse files
DOCSP-52181 -- Convert Partial Match and Autocomplete to Composable tutorial (#13562)
* DOCSP-52181 -- Convert to Composable tutorial * DOCSP-52181 -- dropdown selector wip * DOCSP-52181 -- dropdown selector wip * DOCSP-52181 -- dropdown selector wip * DOCSP-52181 -- dropdown selector wip * DOCSP-52181 -- dropdown selector wip * DOCSP-52181 -- dropdown selector wip * DOCSP-52181 -- dropdown selector wip * DOCSP-52181 -- dropdown selector wip * DOCSP-52181 -- dropdown selector wip * DOCSP-52181 -- dropdown selector wip * DOCSP-52181 -- dropdown selector wip * DOCSP-52181 -- dropdown selector wip * DOCSP-52181 -- dropdown selector wip * DOCSP-52181 -- dropdown selector wip * DOCSP-52181 -- dropdown selector wip * DOCSP-52181 -- dropdown selector wip * DOCSP-52181 -- fix includes * DOCSP-52181 -- fix atlas api example * DOCSP-52181 -- actually fix api example * DOCSP-52181 -- remove atlas api query examples * DOCSP-52181 -- add working code examples * DOCSP-52181 -- add tabs to index creation for c driver * DOCSP-52181 -- add tabs to index creation for all drivers * DOCSP-52181 -- fix connection string placeholders * DOCSP-52181 -- fix includes paths * DOCSP-52181 -- change io-code-blocks to literarl includes * DOCSP-52181 -- tabs to UI index creation * DOCSP-52181 -- remove atlas admin api * DOCSP-52181 -- fix java includes * DOCSP-52181 -- rename index creation thingies * DOCSP-52181 -- fix mongosh examples * DOCSP-52181 -- fix java examples * DOCSP-52181 -- fix csharp includes * DOCSP-52181 -- fix csharp includes * DOCSP-52181 -- fix csharp includes * DOCSP-52181 -- replace examples that are included in other files * DOCSP-52181 -- fix build errors * DOCSP-52181 -- add inline replacements * Copy review revisions
1 parent 101ed00 commit d2124a0

File tree

104 files changed

+4507
-2186
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

104 files changed

+4507
-2186
lines changed

content/atlas/source/atlas-search/tutorial/partial-match.txt

Lines changed: 495 additions & 82 deletions
Large diffs are not rendered by default.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using MongoDB.Bson;
2+
using MongoDB.Driver;
3+
using System;
4+
using System.Threading.Tasks;
5+
6+
namespace MongoSearchExamples
7+
{
8+
class CreateAutoCompleteIndex
9+
{
10+
// Connection URI for your Atlas deployment
11+
private static readonly string connectionUri = "<connection-string>";
12+
13+
static async Task Main(string[] args)
14+
{
15+
var client = new MongoClient(connectionUri);
16+
17+
try
18+
{
19+
// Set namespace
20+
var database = client.GetDatabase("sample_mflix");
21+
var collection = database.GetCollection<BsonDocument>("movies");
22+
23+
// Define your Atlas Search index
24+
var indexDefinition = new BsonDocument
25+
{
26+
{ "name", "partial-match-tutorial-autocomplete" },
27+
{
28+
"definition", new BsonDocument
29+
{
30+
{
31+
"mappings", new BsonDocument
32+
{
33+
{ "dynamic", false },
34+
{
35+
"fields", new BsonDocument
36+
{
37+
{
38+
"title", new BsonDocument
39+
{
40+
{ "type", "autocomplete" },
41+
{ "analyzer", "lucene.standard" },
42+
{ "tokenization", "edgeGram" },
43+
{ "minGrams", 3 },
44+
{ "maxGrams", 5 },
45+
{ "foldDiacritics", false }
46+
}
47+
}
48+
}
49+
}
50+
}
51+
}
52+
}
53+
}
54+
};
55+
56+
// Create the search index
57+
var result = await collection.SearchIndexes.CreateOneAsync(indexDefinition);
58+
Console.WriteLine($"New index name: {result}");
59+
}
60+
catch (Exception ex)
61+
{
62+
Console.WriteLine($"Error: {ex.Message}");
63+
}
64+
}
65+
}
66+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import com.mongodb.client.MongoClient;
2+
import com.mongodb.client.MongoClients;
3+
import com.mongodb.client.MongoCollection;
4+
import com.mongodb.client.MongoDatabase;
5+
import org.bson.Document;
6+
7+
public class CreateAutoCompleteIndex {
8+
public static void main(String[] args) {
9+
// connect to your Atlas deployment
10+
String uri = "<connection-string>";
11+
12+
try (MongoClient mongoClient = MongoClients.create(uri)) {
13+
// set namespace
14+
MongoDatabase database = mongoClient.getDatabase("sample_mflix");
15+
MongoCollection<Document> collection = database.getCollection("movies");
16+
17+
// define your Atlas Search index
18+
Document indexDefinition = new Document("mappings",
19+
new Document("dynamic", false)
20+
.append("fields",
21+
new Document("title",
22+
new Document("type", "autocomplete")
23+
.append("analyzer", "lucene.standard")
24+
.append("tokenization", "edgeGram")
25+
.append("minGrams", 3)
26+
.append("maxGrams", 5)
27+
.append("foldDiacritics", false)
28+
)
29+
)
30+
);
31+
32+
// run the helper method
33+
String result = collection.createSearchIndex("partial-match-tutorial-autocomplete", indexDefinition);
34+
System.out.println("New index name: " + result);
35+
} catch (Exception e) {
36+
e.printStackTrace();
37+
}
38+
}
39+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using MongoDB.Bson;
2+
using MongoDB.Driver;
3+
using System;
4+
using System.Threading.Tasks;
5+
6+
namespace MongoSearchExamples
7+
{
8+
class CreateStringIndex
9+
{
10+
// Connection URI for your Atlas deployment
11+
private static readonly string connectionUri = "<connection-string>";
12+
13+
static async Task Main(string[] args)
14+
{
15+
var client = new MongoClient(connectionUri);
16+
17+
try
18+
{
19+
// Set namespace
20+
var database = client.GetDatabase("sample_mflix");
21+
var collection = database.GetCollection<BsonDocument>("movies");
22+
23+
// Define your Atlas Search index
24+
var indexDefinition = new BsonDocument
25+
{
26+
{ "name", "partial-match-tutorial" },
27+
{
28+
"definition", new BsonDocument
29+
{
30+
{
31+
"mappings", new BsonDocument
32+
{
33+
{ "dynamic", false },
34+
{
35+
"fields", new BsonDocument
36+
{
37+
{
38+
"title", new BsonDocument
39+
{
40+
{ "type", "string" }
41+
}
42+
}
43+
}
44+
}
45+
}
46+
}
47+
}
48+
}
49+
};
50+
51+
// Create the search index
52+
var result = await collection.SearchIndexes.CreateOneAsync(indexDefinition);
53+
Console.WriteLine($"New index name: {result}");
54+
}
55+
catch (Exception ex)
56+
{
57+
Console.WriteLine($"Error: {ex.Message}");
58+
}
59+
}
60+
}
61+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import com.mongodb.client.MongoClient;
2+
import com.mongodb.client.MongoClients;
3+
import com.mongodb.client.MongoCollection;
4+
import com.mongodb.client.MongoDatabase;
5+
import org.bson.Document;
6+
7+
public class CreateStringIndex {
8+
public static void main(String[] args) {
9+
// connect to your Atlas deployment
10+
String uri = "<connection-string>";
11+
12+
try (MongoClient mongoClient = MongoClients.create(uri)) {
13+
// set namespace
14+
MongoDatabase database = mongoClient.getDatabase("sample_mflix");
15+
MongoCollection<Document> collection = database.getCollection("movies");
16+
17+
// define your Atlas Search index
18+
Document indexDefinition = new Document("mappings",
19+
new Document("dynamic", false)
20+
.append("fields",
21+
new Document("title",
22+
new Document("type", "string")
23+
)
24+
)
25+
);
26+
27+
// run the helper method
28+
String result = collection.createSearchIndex("partial-match-tutorial", indexDefinition);
29+
System.out.println("New index name: " + result);
30+
} catch (Exception e) {
31+
e.printStackTrace();
32+
}
33+
}
34+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using MongoDB.Bson;
2+
using MongoDB.Driver;
3+
using System;
4+
using System.Threading.Tasks;
5+
6+
namespace MongoSearchExamples
7+
{
8+
class QueryAutoComplete
9+
{
10+
// Connection URI for your Atlas deployment
11+
private static readonly string connectionUri = "<connection-string>";
12+
13+
static async Task Main(string[] args)
14+
{
15+
var client = new MongoClient(connectionUri);
16+
17+
try
18+
{
19+
// Set namespace
20+
var database = client.GetDatabase("sample_mflix");
21+
var collection = database.GetCollection<BsonDocument>("movies");
22+
23+
// Define pipeline
24+
var pipeline = new BsonDocument[]
25+
{
26+
new BsonDocument("$search", new BsonDocument
27+
{
28+
{ "index", "partial-match-tutorial-autocomplete" },
29+
{
30+
"autocomplete", new BsonDocument
31+
{
32+
{ "query", "Great" },
33+
{ "path", "title" }
34+
}
35+
}
36+
}),
37+
new BsonDocument("$limit", 10),
38+
new BsonDocument("$project", new BsonDocument
39+
{
40+
{ "_id", 0 },
41+
{ "title", 1 }
42+
})
43+
};
44+
45+
// Run pipeline
46+
var result = collection.Aggregate<BsonDocument>(pipeline);
47+
48+
// Print results
49+
await result.ForEachAsync(doc => Console.WriteLine(doc.ToJson()));
50+
}
51+
catch (Exception ex)
52+
{
53+
Console.WriteLine($"Error: {ex.Message}");
54+
}
55+
}
56+
}
57+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import com.mongodb.client.MongoClient;
2+
import com.mongodb.client.MongoClients;
3+
import com.mongodb.client.MongoCollection;
4+
import com.mongodb.client.MongoDatabase;
5+
import com.mongodb.client.MongoCursor;
6+
import org.bson.Document;
7+
import java.util.Arrays;
8+
9+
public class QueryAutoComplete {
10+
public static void main(String[] args) {
11+
// connect to your Atlas cluster
12+
String uri = "<connection-string>";
13+
14+
try (MongoClient mongoClient = MongoClients.create(uri)) {
15+
// set namespace
16+
MongoDatabase database = mongoClient.getDatabase("sample_mflix");
17+
MongoCollection<Document> collection = database.getCollection("movies");
18+
19+
// define pipeline
20+
Document searchStage = new Document("$search",
21+
new Document("index", "partial-match-tutorial-autocomplete")
22+
.append("autocomplete",
23+
new Document("query", "Great")
24+
.append("path", "title")
25+
)
26+
);
27+
28+
Document limitStage = new Document("$limit", 10);
29+
30+
Document projectStage = new Document("$project",
31+
new Document("_id", 0)
32+
.append("title", 1)
33+
);
34+
35+
// run pipeline
36+
try (MongoCursor<Document> cursor = collection.aggregate(
37+
Arrays.asList(searchStage, limitStage, projectStage)
38+
).iterator()) {
39+
// print results
40+
while (cursor.hasNext()) {
41+
Document result = cursor.next();
42+
System.out.println(result.toJson());
43+
}
44+
}
45+
} catch (Exception e) {
46+
e.printStackTrace();
47+
}
48+
}
49+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using MongoDB.Bson;
2+
using MongoDB.Driver;
3+
using System;
4+
using System.Threading.Tasks;
5+
6+
namespace MongoSearchExamples
7+
{
8+
class QueryPhrase
9+
{
10+
// Connection URI for your Atlas deployment
11+
private static readonly string connectionUri = "<connection-string>";
12+
13+
static async Task Main(string[] args)
14+
{
15+
var client = new MongoClient(connectionUri);
16+
17+
try
18+
{
19+
// Set namespace
20+
var database = client.GetDatabase("sample_mflix");
21+
var collection = database.GetCollection<BsonDocument>("movies");
22+
23+
// Define pipeline
24+
var pipeline = new BsonDocument[]
25+
{
26+
new BsonDocument("$search", new BsonDocument
27+
{
28+
{ "index", "partial-match-tutorial" },
29+
{
30+
"phrase", new BsonDocument
31+
{
32+
{ "path", "plot" },
33+
{ "query", "new york" }
34+
}
35+
}
36+
}),
37+
new BsonDocument("$project", new BsonDocument
38+
{
39+
{ "_id", 0 },
40+
{ "title", 1 }
41+
})
42+
};
43+
44+
// Run pipeline
45+
var result = collection.Aggregate<BsonDocument>(pipeline);
46+
47+
// Print results
48+
await result.ForEachAsync(doc => Console.WriteLine(doc.ToJson()));
49+
}
50+
catch (Exception ex)
51+
{
52+
Console.WriteLine($"Error: {ex.Message}");
53+
}
54+
}
55+
}
56+
}

0 commit comments

Comments
 (0)