Skip to content

Instantly share code, notes, and snippets.

@dj-nitehawk
Last active April 21, 2021 05:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dj-nitehawk/14ae67d8169c7185876a87503d8b6007 to your computer and use it in GitHub Desktop.
Save dj-nitehawk/14ae67d8169c7185876a87503d8b6007 to your computer and use it in GitHub Desktop.
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