Skip to content

Instantly share code, notes, and snippets.

@dj-nitehawk

dj-nitehawk/Program.cs

Last active Apr 21, 2021
Embed
What would you like to do?
fuzzy text search with sorted results
using MongoDB.Entities;
using System.Linq;
using System.Threading.Tasks;
namespace TestApplication
{
public static class Program
{
public class Item : Entity
{
public FuzzyString Text { get; set; }
}
private static async Task Main()
{
await DB.InitAsync("test");
await DB.Index<Item>()
.Key(i => i.Text, KeyType.Text)
.CreateAsync();
await new[] {
new Item { Text = "goggle"},
new Item { Text = "google"},
new Item { Text = "gogle"},
new Item { Text = "altavista"}
}.SaveAsync();
var result = await DB
.Find<Item>()
.Match(Search.Fuzzy, "google")
.ExecuteAsync();
var sortedItems = result
.SortByRelevance("google", i => i.Text)
.Select(x => x.Text.Value)
.ToArray();
//result is: [google,goggle,gogle]
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment