Revise naming, make HelpIndex static

This commit is contained in:
jlewin 2019-06-21 15:33:55 -07:00
parent 12d8291a20
commit 7608489e09
3 changed files with 58 additions and 34 deletions

View file

@ -1,4 +1,32 @@

/*
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;
using System.Collections.Generic;
using System.IO.Compression;
@ -22,12 +50,12 @@ using Newtonsoft.Json;
namespace MatterControlLib
{
public class LuceneHelpSearch
public static class HelpIndex
{
private static IndexWriter writer;
private static StandardAnalyzer analyzer;
static LuceneHelpSearch()
static HelpIndex()
{
// Ensures index backwards compatibility
var AppLuceneVersion = LuceneVersion.LUCENE_48;
@ -35,37 +63,32 @@ namespace MatterControlLib
var indexLocation = System.IO.Path.Combine(ApplicationDataStorage.Instance.ApplicationTempDataPath, "LuceneIndex");
System.IO.Directory.CreateDirectory(indexLocation);
var dir = FSDirectory.Open(indexLocation);
// create an analyzer to process the text
analyzer = new StandardAnalyzer(AppLuceneVersion);
// create an index writer
var indexConfig = new IndexWriterConfig(AppLuceneVersion, analyzer);
writer = new IndexWriter(dir, indexConfig);
// If the index lacks a reasonable number of documents, rebuild it from the zip file
if (writer.MaxDoc < 10)
{
ApplicationController.Instance.Tasks.Execute(
"Preparing help index".Localize(),
null,
(progress, cancellationToken) =>
{
string relativePath = System.IO.Path.Combine("OemSettings", "help-docs.zip");
IndexZipFile(relativePath, progress, cancellationToken);
return Task.CompletedTask;
});
}
writer = new IndexWriter(
FSDirectory.Open(indexLocation),
new IndexWriterConfig(AppLuceneVersion, analyzer));
}
public LuceneHelpSearch()
public static bool IndexExists => writer.MaxDoc > 0;
public static Task RebuildIndex()
{
}
// If the index lacks a reasonable number of documents, rebuild it from the zip file
return ApplicationController.Instance.Tasks.Execute(
"Preparing help index".Localize(),
null,
(progress, cancellationToken) =>
{
string relativePath = System.IO.Path.Combine("OemSettings", "help-docs.zip");
IndexZipFile(relativePath, progress, cancellationToken);
return Task.CompletedTask;
});
}
private static void ProcessHelpTree(HelpArticle context, Dictionary<string, HelpArticle> helpArticles)
{
@ -150,9 +173,14 @@ namespace MatterControlLib
writer.Commit();
}
public IEnumerable<HelpSearchResult> Search(string text)
public static IEnumerable<HelpSearchResult> Search(string text)
{
//var parser = new QueryParser(LuceneVersion.LUCENE_48, "body", analyzer);
// If the index lacks a reasonable number of documents, rebuild it from the zip file
if (writer.MaxDoc < 10)
{
RebuildIndex();
}
var parser = new MultiFieldQueryParser(LuceneVersion.LUCENE_48, new[] { "body", "name" }, analyzer);
var query = parser.Parse(text);

View file

@ -87,8 +87,7 @@ namespace MatterHackers.MatterControl.PartPreviewWindow
var searchHits = await Task.Run(() =>
{
var searcher = new LuceneHelpSearch();
return searcher.Search(searchBox.searchInput.Text);
return HelpIndex.Search(searchBox.searchInput.Text);
});
searchResults.CloseAllChildren();

View file

@ -73,13 +73,11 @@ namespace MatterHackers.MatterControl
AddGuides();
CreateMousePage();
CreateKeyBindingsPage();
searcher = new LuceneHelpSearch();
}
protected override void PerformSearch(string filter)
{
searchHits = new HashSet<string>(searcher.Search(filter).Select(d => d.Path));
searchHits = new HashSet<string>(HelpIndex.Search(filter).Select(d => d.Path));
base.PerformSearch(filter);
}
@ -355,7 +353,6 @@ namespace MatterHackers.MatterControl
private TreeNode rootNode;
private Dictionary<string, HelpArticleTreeNode> nodesByPath = new Dictionary<string, HelpArticleTreeNode>();
private LuceneHelpSearch searcher;
private IEnumerable<HelpSearchResult> searchResults;
private HashSet<string> searchHits;