mattercontrol/MatterControlLib/HelpIndex.cs

204 lines
6.3 KiB
C#
Raw Normal View History

2019-06-21 15:33:55 -07:00
/*
Copyright (c) 2019, Lars Brubaker, John Lewin
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
The views and conclusions contained in the software and documentation are those
of the authors and should not be interpreted as representing official policies,
either expressed or implied, of the FreeBSD Project.
*/
using System;
2019-06-17 18:12:34 -07:00
using System.Collections.Generic;
using System.IO.Compression;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
2019-06-17 18:12:34 -07:00
using Lucene.Net.Analysis.Standard;
using Lucene.Net.Documents;
using Lucene.Net.Index;
using Lucene.Net.QueryParsers.Classic;
using Lucene.Net.Search;
using Lucene.Net.Store;
using Lucene.Net.Util;
using MatterHackers.Agg;
2019-06-17 18:12:34 -07:00
using MatterHackers.Agg.Platform;
using MatterHackers.Localizations;
2019-06-17 18:12:34 -07:00
using MatterHackers.MatterControl;
using MatterHackers.MatterControl.DataStorage;
using MatterHackers.MatterControl.PartPreviewWindow;
using Newtonsoft.Json;
namespace MatterControlLib
{
2019-06-21 15:33:55 -07:00
public static class HelpIndex
2019-06-17 18:12:34 -07:00
{
private static IndexWriter writer;
private static StandardAnalyzer analyzer;
2019-06-21 15:33:55 -07:00
static HelpIndex()
2019-06-17 18:12:34 -07:00
{
// Ensures index backwards compatibility
var AppLuceneVersion = LuceneVersion.LUCENE_48;
var indexLocation = System.IO.Path.Combine(ApplicationDataStorage.Instance.ApplicationTempDataPath, "LuceneIndex");
System.IO.Directory.CreateDirectory(indexLocation);
// create an analyzer to process the text
analyzer = new StandardAnalyzer(AppLuceneVersion);
// create an index writer
2019-06-21 15:33:55 -07:00
writer = new IndexWriter(
FSDirectory.Open(indexLocation),
new IndexWriterConfig(AppLuceneVersion, analyzer));
}
2019-06-17 18:12:34 -07:00
2019-06-21 15:33:55 -07:00
public static bool IndexExists => writer.MaxDoc > 0;
2019-06-17 18:12:34 -07:00
2019-06-21 15:33:55 -07:00
public static Task RebuildIndex()
{
// If the index lacks a reasonable number of documents, rebuild it from the zip file
2019-06-21 15:33:55 -07:00
return ApplicationController.Instance.Tasks.Execute(
"Preparing help index".Localize(),
null,
(progress, cancellationToken) =>
{
string relativePath = System.IO.Path.Combine("OemSettings", "help-docs.zip");
2019-06-21 15:33:55 -07:00
IndexZipFile(relativePath, progress, cancellationToken);
2019-06-17 18:12:34 -07:00
2019-06-21 15:33:55 -07:00
return Task.CompletedTask;
});
2019-06-17 18:12:34 -07:00
}
private static void ProcessHelpTree(HelpArticle context, Dictionary<string, HelpArticle> helpArticles)
{
helpArticles[context.Path] = context;
foreach (var child in context.Children)
{
ProcessHelpTree(child, helpArticles);
}
}
private static void IndexZipFile(string filePath, IProgress<ProgressStatus> progress, CancellationToken cancellationToken)
2019-06-17 18:12:34 -07:00
{
Dictionary<string, HelpArticle> helpArticles;
// Clear existing
writer.DeleteAll();
// Build index from help-docs.zip
using (var file = StaticData.Instance.OpenStream(filePath))
2019-06-17 18:12:34 -07:00
using (var zip = new ZipArchive(file, ZipArchiveMode.Read))
{
var tocEntry = zip.Entries.FirstOrDefault(e => e.FullName == "toc.json");
using (var docStream = tocEntry.Open())
{
var reader = new System.IO.StreamReader(docStream);
var tocText = reader.ReadToEnd();
var rootHelpArticle = JsonConvert.DeserializeObject<HelpArticle>(tocText);
helpArticles = new Dictionary<string, HelpArticle>();
// Walk the documents tree building up a dictionary of article paths to articles
ProcessHelpTree(rootHelpArticle, helpArticles);
}
var progressStatus = new ProgressStatus()
{
//Status = "",
Progress0To1 = 0
};
var count = zip.Entries.Count;
double i = 0;
2019-06-17 18:12:34 -07:00
foreach (var entry in zip.Entries)
{
progressStatus.Progress0To1 = i++ / count;
progress.Report(progressStatus);
// Observe and abort on cancellationToken signal
if (cancellationToken.IsCancellationRequested)
{
writer.DeleteAll();
writer.Commit();
return;
}
2019-06-17 18:12:34 -07:00
if (entry.FullName.ToLower().EndsWith(".md"))
{
using (var docStream = entry.Open())
{
var reader = new System.IO.StreamReader(docStream);
string text = reader.ReadToEnd();
var doc = new Document();
// StringField indexes but doesn't tokenise
2019-06-19 17:59:26 -07:00
doc.Add(new TextField("name", helpArticles[entry.FullName].Name, Field.Store.YES));
2019-06-17 18:12:34 -07:00
doc.Add(new StringField("path", entry.FullName, Field.Store.YES));
doc.Add(new TextField("body", text, Field.Store.NO));
writer.AddDocument(doc);
writer.Flush(triggerMerge: false, applyAllDeletes: false);
}
}
}
}
writer.Commit();
}
2019-06-21 15:33:55 -07:00
public static IEnumerable<HelpSearchResult> Search(string text)
2019-06-17 18:12:34 -07:00
{
2019-06-21 15:33:55 -07:00
// If the index lacks a reasonable number of documents, rebuild it from the zip file
if (writer.MaxDoc < 10)
{
RebuildIndex();
}
2019-06-19 17:59:26 -07:00
var parser = new MultiFieldQueryParser(LuceneVersion.LUCENE_48, new[] { "body", "name" }, analyzer);
2019-06-17 18:12:34 -07:00
var query = parser.Parse(text);
// re-use the writer to get real-time updates
var searcher = new IndexSearcher(writer.GetReader(applyAllDeletes: true));
2019-06-20 17:32:02 -07:00
var hits = searcher.Search(query, 40 /* top 20 */).ScoreDocs;
2019-06-17 18:12:34 -07:00
return hits.Select(hit =>
{
var foundDoc = searcher.Doc(hit.Doc);
return new HelpSearchResult()
{
Name = foundDoc.Get("name"),
Path = foundDoc.Get("path")
};
});
}
}
}