diff --git a/MatterControlLib/LuceneHelpSearch.cs b/MatterControlLib/HelpIndex.cs similarity index 64% rename from MatterControlLib/LuceneHelpSearch.cs rename to MatterControlLib/HelpIndex.cs index c5d1fe94a..a4ce01129 100644 --- a/MatterControlLib/LuceneHelpSearch.cs +++ b/MatterControlLib/HelpIndex.cs @@ -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 helpArticles) { @@ -150,9 +173,14 @@ namespace MatterControlLib writer.Commit(); } - public IEnumerable Search(string text) + public static IEnumerable 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); diff --git a/MatterControlLib/PartPreviewWindow/SearchPanel.cs b/MatterControlLib/PartPreviewWindow/SearchPanel.cs index 334ffc17b..8f8cb1b94 100644 --- a/MatterControlLib/PartPreviewWindow/SearchPanel.cs +++ b/MatterControlLib/PartPreviewWindow/SearchPanel.cs @@ -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(); diff --git a/MatterControlLib/SetupWizard/HelpTreePanel.cs b/MatterControlLib/SetupWizard/HelpTreePanel.cs index 35e1d869a..8c75409ab 100644 --- a/MatterControlLib/SetupWizard/HelpTreePanel.cs +++ b/MatterControlLib/SetupWizard/HelpTreePanel.cs @@ -73,13 +73,11 @@ namespace MatterHackers.MatterControl AddGuides(); CreateMousePage(); CreateKeyBindingsPage(); - - searcher = new LuceneHelpSearch(); } protected override void PerformSearch(string filter) { - searchHits = new HashSet(searcher.Search(filter).Select(d => d.Path)); + searchHits = new HashSet(HelpIndex.Search(filter).Select(d => d.Path)); base.PerformSearch(filter); } @@ -355,7 +353,6 @@ namespace MatterHackers.MatterControl private TreeNode rootNode; private Dictionary nodesByPath = new Dictionary(); - private LuceneHelpSearch searcher; private IEnumerable searchResults; private HashSet searchHits;