private async void buttonCreateIndex_ClickAsync(object sender, RoutedEventArgs e) { textBox.Text = string.Empty; _index = await Lunr.Index.Build(async builder => { builder .AddField("id") .AddField("body") .AddField("title") .AddField("author"); var doc = new Lunr.Document { { "id", "1" }, { "title", "Twelfth-Night" }, { "body", "If music be the food of love, play on: Give me excess of it…" }, { "author", "William Shakespeare" }, }; await builder.Add(doc); await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { textBox.Text += "id: 1" + Environment.NewLine; textBox.Text += "title: Twelfth-Night" + Environment.NewLine; textBox.Text += "body: If music be the food of love, play on: Give me excess of it…" + Environment.NewLine; textBox.Text += "author: William Shakespeare" + Environment.NewLine; textBox.Text += "Created index." + Environment.NewLine; }); }); }
protected override async Task <IEnumerable <IDocument> > ExecuteContextAsync(IExecutionContext context) { if (context.Inputs.Length == 0) { throw new InvalidOperationException($"No input documents available. Cannot build a search index for pipeline {context.PipelineName}"); } var documents = new Dictionary <string, object>(); Index idx = await Index.Build(async builder => { builder.ReferenceField = "id"; builder .AddField("title") .AddField("content") .AddField("tags") .AddField("description"); foreach (IDocument doc in context.Inputs) { var searchItem = doc[SearchIndex.SearchItemKey] as ILunrIndexItem; if (searchItem == null) { continue; } var id = Guid.NewGuid().ToString("D"); var content = StripHtmlAndSpecialChars.Replace(searchItem.Content, " ").Trim(); documents.Add(id, new { url = searchItem.GetLink(context, false), title = searchItem.Title, body = searchItem.Description }); var lunrDoc = new global::Lunr.Document { { "id", id }, { "title", searchItem.Title }, { "content", content }, { "tags", searchItem.Tags }, { "description", searchItem.Description } }; await builder.Add(lunrDoc); } }); return(new[] { (await context.CreateDocumentAsync(destination: "index.js", $@"var documents={System.Text.Json.JsonSerializer.Serialize(documents)};{Environment.NewLine}var data='{idx.ToJson()}';{Environment.NewLine}")) }); }