Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DYN-6038 Lucene search IMP #14428

Merged
merged 18 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 15 additions & 41 deletions src/DynamoCore/Models/DynamoModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -921,16 +921,7 @@ protected DynamoModel(IStartConfiguration config)

CustomNodeManager = new CustomNodeManager(NodeFactory, MigrationManager, LibraryServices);

LuceneSearch.LuceneUtilityNodeSearch = new LuceneSearchUtility(this);

if (IsTestMode)
{
LuceneUtility.InitializeLuceneConfig(string.Empty, LuceneSearchUtility.LuceneStorage.RAM);
}
else
{
LuceneUtility.InitializeLuceneConfig(LuceneConfig.NodesIndexingDirectory);
}
LuceneSearch.LuceneUtilityNodeSearch = new LuceneSearchUtility(this, LuceneSearchUtility.DefaultNodeIndexStartConfig);

InitializeCustomNodeManager();

Expand Down Expand Up @@ -1013,10 +1004,15 @@ protected DynamoModel(IStartConfiguration config)
TraceReconciliationProcessor = this;

State = DynamoModelState.StartedUIless;
// This event should only be raised at the end of this method.
DynamoReady(new ReadyParams(this));
// Write index to disk only once
LuceneUtility.CommitWriterChanges();
//Disposed writer if it is in file system mode so that the index files can be used by other processes (potentially a second Dynamo session)
if (LuceneUtility.startConfig.StorageType == LuceneSearchUtility.LuceneStorage.FILE_SYSTEM)
{
LuceneUtility.DisposeWriter();
}
// This event should only be raised at the end of this method.
DynamoReady(new ReadyParams(this));
}

private void SearchModel_ItemProduced(NodeModel node)
Expand Down Expand Up @@ -1405,12 +1401,8 @@ public void Dispose()
PreferenceSettings.MessageLogged -= LogMessage;
}

//The writer have to be disposed at DynamoModel level due that we could index package-nodes as new packages are installed
LuceneUtility.DisposeWriter();

// Lucene disposals (just if LuceneNET was initialized)
LuceneUtility.indexDir?.Dispose();
LuceneUtility.dirReader?.Dispose();
LuceneUtility.DisposeAll();

#if DEBUG
CurrentWorkspace.NodeAdded -= CrashOnDemand.CurrentWorkspace_NodeAdded;
Expand Down Expand Up @@ -1474,7 +1466,7 @@ private void InitializeCustomNodeManager()
var iDoc = LuceneUtility.InitializeIndexDocumentForNodes();
if (searchElement != null)
{
AddNodeTypeToSearchIndex(searchElement, iDoc);
LuceneUtility.AddNodeTypeToSearchIndex(searchElement, iDoc);
}

Action<CustomNodeInfo> infoUpdatedHandler = null;
Expand Down Expand Up @@ -1543,7 +1535,7 @@ private void InitializeIncludedNodes()

var cnbNode = new CodeBlockNodeSearchElement(cbnData, LibraryServices);
SearchModel?.Add(cnbNode);
AddNodeTypeToSearchIndex(cnbNode, iDoc);
LuceneUtility.AddNodeTypeToSearchIndex(cnbNode, iDoc);

var symbolSearchElement = new NodeModelSearchElement(symbolData)
{
Expand All @@ -1562,10 +1554,10 @@ private void InitializeIncludedNodes()
};

SearchModel?.Add(symbolSearchElement);
AddNodeTypeToSearchIndex(symbolSearchElement, iDoc);
LuceneUtility.AddNodeTypeToSearchIndex(symbolSearchElement, iDoc);

SearchModel?.Add(outputSearchElement);
AddNodeTypeToSearchIndex(outputSearchElement, iDoc);
LuceneUtility.AddNodeTypeToSearchIndex(outputSearchElement, iDoc);

}

Expand Down Expand Up @@ -1719,7 +1711,7 @@ private void LoadNodeModels(List<TypeLoadData> nodes, bool isPackageMember)
// TODO: get search element some other way
if (ele != null)
{
AddNodeTypeToSearchIndex(ele, iDoc);
LuceneUtility.AddNodeTypeToSearchIndex(ele, iDoc);
}
}
catch (Exception e)
Expand Down Expand Up @@ -3225,24 +3217,6 @@ private NodeModelSearchElement AddNodeTypeToSearch(TypeLoadData typeLoadData)
return node;
}

/// <summary>
/// Add node information to Lucene index
/// </summary>
/// <param name="node">node info that will be indexed</param>
/// <param name="doc">Lucene document in which the node info will be indexed</param>
internal void AddNodeTypeToSearchIndex(NodeSearchElement node, Document doc)
{
if (LuceneUtility.addedFields == null) return;

LuceneUtility.SetDocumentFieldValue(doc, nameof(LuceneConfig.NodeFieldsEnum.FullCategoryName), node.FullCategoryName);
LuceneUtility.SetDocumentFieldValue(doc, nameof(LuceneConfig.NodeFieldsEnum.Name), node.Name);
LuceneUtility.SetDocumentFieldValue(doc, nameof(LuceneConfig.NodeFieldsEnum.Description), node.Description);
if (node.SearchKeywords.Count > 0) LuceneUtility.SetDocumentFieldValue(doc, nameof(LuceneConfig.NodeFieldsEnum.SearchKeywords), node.SearchKeywords.Aggregate((x, y) => x + " " + y), true, true);
LuceneUtility.SetDocumentFieldValue(doc, nameof(LuceneConfig.NodeFieldsEnum.Parameters), node.Parameters ?? string.Empty);

LuceneUtility.writer?.AddDocument(doc);
}

/// <summary>
/// Remove node information from Lucene indexing.
/// </summary>
Expand Down Expand Up @@ -3302,7 +3276,7 @@ private void AddZeroTouchNodeToSearch(FunctionDescriptor functionDescriptor, Doc
{
var ele = new ZeroTouchSearchElement(functionDescriptor);
SearchModel?.Add(ele);
AddNodeTypeToSearchIndex(ele, iDoc);
LuceneUtility.AddNodeTypeToSearchIndex(ele, iDoc);
}
}

Expand Down
12 changes: 9 additions & 3 deletions src/DynamoCore/Search/NodeSearchModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Dynamo.Utilities;
using DynamoUtilities;
using Lucene.Net.Documents;
using Lucene.Net.Index;
using Lucene.Net.QueryParsers.Classic;
using Lucene.Net.Search;

Expand Down Expand Up @@ -236,9 +237,14 @@ internal IEnumerable<NodeSearchElement> Search(string search, LuceneSearchUtilit
if (luceneSearchUtility != null)
{
//The DirectoryReader and IndexSearcher have to be assigned after commiting indexing changes and before executing the Searcher.Search() method, otherwise new indexed info won't be reflected
luceneSearchUtility.dirReader = luceneSearchUtility.writer?.GetReader(applyAllDeletes: true);
if (luceneSearchUtility.dirReader == null) return null;

if (luceneSearchUtility.writer != null)
{
luceneSearchUtility.dirReader = luceneSearchUtility.writer.GetReader(applyAllDeletes: true);
}
else
{
luceneSearchUtility.dirReader = DirectoryReader.Open(luceneSearchUtility.indexDir);
}
luceneSearchUtility.Searcher = new IndexSearcher(luceneSearchUtility.dirReader);

string searchTerm = search.Trim();
Expand Down
Loading