Last active
August 2, 2020 14:38
-
-
Save surgicalcoder/802fed94e01098640656ec97f9f0b792 to your computer and use it in GitHub Desktop.
Lunr Code Example
This file contains hidden or 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 System; | |
| using System.Collections.Generic; | |
| using System.Threading.Tasks; | |
| using Lunr; | |
| using Index = Lunr.Index; | |
| namespace LunrCodeJsonExample | |
| { | |
| class Program | |
| { | |
| public static List<Airport> airports = new List<Airport> | |
| { | |
| new Airport("a","LHR","EGLL","London Heathrow"), | |
| new Airport("b","BQH","EGKB","London Biggin Hill"), | |
| new Airport("c","GON","KGON","Groton-New London"), | |
| new Airport("d", null,"92TE","Chaney San Francisco Ranch"), | |
| new Airport("e", "SPSF",null,"San Francisco (Peru)"), | |
| }; | |
| static async Task Main(string[] args) | |
| { | |
| var builtIndex = await Lunr.Index.Build(async delegate(Builder builder) | |
| { | |
| builder.AddField("IATA", boost: 5); | |
| builder.AddField("ICAO", boost: 10); | |
| builder.AddField("Name"); | |
| foreach (var airport in airports) | |
| { | |
| await builder.Add(new Document() | |
| { | |
| {"IATA", airport.IATA?.ToLowerInvariant() ?? "(none)"}, | |
| {"ICAO", airport.ICAO?.ToLowerInvariant() ?? "(none)"}, | |
| {"Name", airport.Name}, | |
| {"id", airport.Id} | |
| }); | |
| } | |
| }); | |
| var builtResult = await (builtIndex.Search("egll")).ToList(); | |
| var builtCount = builtResult.Count; | |
| var JSON = builtIndex.ToJson(); | |
| var jsonIndex = Index.LoadFromJson(JSON, registry:new PipelineFunctionRegistry() | |
| { | |
| {"trimmer", new Trimmer().FilterFunction}, | |
| {"stopWordFilter", new EnglishStopWordFilter().FilterFunction} | |
| }); | |
| var jsonResult = await (jsonIndex.Search("egll")).ToList(); | |
| var jsonCount = jsonResult.Count; | |
| Console.WriteLine($"Built Index Search result count = {builtCount} "); | |
| Console.WriteLine($"JSON Index Search result count = {jsonCount} "); | |
| } | |
| } | |
| public class Airport | |
| { | |
| public Airport() { } | |
| public Airport(string id, string icao, string iata, string name) | |
| { | |
| Id = id; | |
| ICAO = icao; | |
| IATA = iata; | |
| Name = name; | |
| } | |
| public string Id { get; set; } | |
| public string ICAO { get; set; } | |
| public string IATA { get; set; } | |
| public string Name { get; set; } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment