fuzzy text search with sorted results
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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