Skip to content

Commit

Permalink
DYN-6037 lucene overloaded nodes (#14136)
Browse files Browse the repository at this point in the history
* DYN-6037 Overloaded Nodes

Some nodes were missing for the search due that were overloaded so the same node was added several times in the search results without considering the parameters.
The fix was indexing also the parameters so when executing the search we need to also match the parameters.

* DYN-6037 Overloaded Nodes

Fixing comment

* DYN-6037 Overloaded Nodes Code Review

Adding comments for explaining a piece of code.

* DYN-6037 Overloaded Nodes Code Review

Renaming IndexFieldsEnum to NodeFieldsEnum.
  • Loading branch information
RobertGlobant20 authored Jul 11, 2023
1 parent 37fa16d commit c91c6f8
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 41 deletions.
30 changes: 18 additions & 12 deletions src/DynamoCore/Configuration/LuceneConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ internal class LuceneConfig
/// <summary>
/// This represent the fields that will be indexed when initializing Lucene Search
/// </summary>
public enum IndexFieldsEnum
public enum NodeFieldsEnum
{
/// <summary>
/// Name - The name of the node
Expand Down Expand Up @@ -127,26 +127,32 @@ public enum IndexFieldsEnum
/// <summary>
/// Hosts - Package hosts
/// </summary>
Hosts
Hosts,

/// <summary>
/// Node Input Parameters as string (there are nodes with same name and category but different parameters)
/// </summary>
Parameters
}

/// <summary>
/// Nodes Fields to be indexed by Lucene Search
/// </summary>
public static string[] NodeIndexFields = { nameof(IndexFieldsEnum.Name),
nameof(IndexFieldsEnum.FullCategoryName),
nameof(IndexFieldsEnum.Description),
nameof(IndexFieldsEnum.SearchKeywords),
nameof(IndexFieldsEnum.DocName),
nameof(IndexFieldsEnum.Documentation)};
public static string[] NodeIndexFields = { nameof(NodeFieldsEnum.Name),
nameof(NodeFieldsEnum.FullCategoryName),
nameof(NodeFieldsEnum.Description),
nameof(NodeFieldsEnum.SearchKeywords),
nameof(NodeFieldsEnum.DocName),
nameof(NodeFieldsEnum.Documentation),
nameof(NodeFieldsEnum.Parameters)};


/// <summary>
/// Package Fields to be indexed by Lucene Search
/// </summary>
public static string[] PackageIndexFields = { nameof(IndexFieldsEnum.Name),
nameof(IndexFieldsEnum.Description),
nameof(IndexFieldsEnum.SearchKeywords),
nameof(IndexFieldsEnum.Hosts)};
public static string[] PackageIndexFields = { nameof(NodeFieldsEnum.Name),
nameof(NodeFieldsEnum.Description),
nameof(NodeFieldsEnum.SearchKeywords),
nameof(NodeFieldsEnum.Hosts)};
}
}
9 changes: 5 additions & 4 deletions src/DynamoCore/Models/DynamoModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3253,10 +3253,11 @@ private void AddNodeTypeToSearchIndex(NodeSearchElement node, Document doc)
if (IsTestMode) return;
if (LuceneSearchUtility.addedFields == null) return;

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

LuceneSearchUtility.writer?.AddDocument(doc);
}
Expand Down
30 changes: 16 additions & 14 deletions src/DynamoCore/Utilities/LuceneSearchUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,13 @@ internal Document InitializeIndexDocumentForNodes()
{
if (DynamoModel.IsTestMode) return null;

var name = new TextField(nameof(LuceneConfig.IndexFieldsEnum.Name), string.Empty, Field.Store.YES);
var fullCategory = new TextField(nameof(LuceneConfig.IndexFieldsEnum.FullCategoryName), string.Empty, Field.Store.YES);
var description = new TextField(nameof(LuceneConfig.IndexFieldsEnum.Description), string.Empty, Field.Store.YES);
var keywords = new TextField(nameof(LuceneConfig.IndexFieldsEnum.SearchKeywords), string.Empty, Field.Store.YES);
var docName = new StringField(nameof(LuceneConfig.IndexFieldsEnum.DocName), string.Empty, Field.Store.YES);
var fullDoc = new TextField(nameof(LuceneConfig.IndexFieldsEnum.Documentation), string.Empty, Field.Store.YES);
var name = new TextField(nameof(LuceneConfig.NodeFieldsEnum.Name), string.Empty, Field.Store.YES);
var fullCategory = new TextField(nameof(LuceneConfig.NodeFieldsEnum.FullCategoryName), string.Empty, Field.Store.YES);
var description = new TextField(nameof(LuceneConfig.NodeFieldsEnum.Description), string.Empty, Field.Store.YES);
var keywords = new TextField(nameof(LuceneConfig.NodeFieldsEnum.SearchKeywords), string.Empty, Field.Store.YES);
var docName = new StringField(nameof(LuceneConfig.NodeFieldsEnum.DocName), string.Empty, Field.Store.YES);
var fullDoc = new TextField(nameof(LuceneConfig.NodeFieldsEnum.Documentation), string.Empty, Field.Store.YES);
var parameters = new TextField(nameof(LuceneConfig.NodeFieldsEnum.Parameters), string.Empty, Field.Store.YES);

var d = new Document()
{
Expand All @@ -93,7 +94,8 @@ internal Document InitializeIndexDocumentForNodes()
description,
keywords,
fullDoc,
docName
docName,
parameters
};
return d;
}
Expand All @@ -106,10 +108,10 @@ internal Document InitializeIndexDocumentForPackages()
{
if (DynamoModel.IsTestMode) return null;

var name = new TextField(nameof(LuceneConfig.IndexFieldsEnum.Name), string.Empty, Field.Store.YES);
var description = new TextField(nameof(LuceneConfig.IndexFieldsEnum.Description), string.Empty, Field.Store.YES);
var keywords = new TextField(nameof(LuceneConfig.IndexFieldsEnum.SearchKeywords), string.Empty, Field.Store.YES);
var hosts = new TextField(nameof(LuceneConfig.IndexFieldsEnum.Hosts), string.Empty, Field.Store.YES);
var name = new TextField(nameof(LuceneConfig.NodeFieldsEnum.Name), string.Empty, Field.Store.YES);
var description = new TextField(nameof(LuceneConfig.NodeFieldsEnum.Description), string.Empty, Field.Store.YES);
var keywords = new TextField(nameof(LuceneConfig.NodeFieldsEnum.SearchKeywords), string.Empty, Field.Store.YES);
var hosts = new TextField(nameof(LuceneConfig.NodeFieldsEnum.Hosts), string.Empty, Field.Store.YES);

var d = new Document()
{
Expand Down Expand Up @@ -196,7 +198,7 @@ internal string CreateSearchQuery(string[] fields, string SearchTerm)
}

var wildcardQuery = new WildcardQuery(new Term(f, searchTerm));
if (f.Equals(nameof(LuceneConfig.IndexFieldsEnum.Name)))
if (f.Equals(nameof(LuceneConfig.NodeFieldsEnum.Name)))
{
wildcardQuery.Boost = LuceneConfig.SearchNameWeight;
}
Expand All @@ -207,7 +209,7 @@ internal string CreateSearchQuery(string[] fields, string SearchTerm)
booleanQuery.Add(wildcardQuery, Occur.SHOULD);

wildcardQuery = new WildcardQuery(new Term(f, "*" + searchTerm + "*"));
if (f.Equals(nameof(LuceneConfig.IndexFieldsEnum.Name)))
if (f.Equals(nameof(LuceneConfig.NodeFieldsEnum.Name)))
{
wildcardQuery.Boost = LuceneConfig.WildcardsSearchNameWeight;
}
Expand All @@ -228,7 +230,7 @@ internal string CreateSearchQuery(string[] fields, string SearchTerm)
}
wildcardQuery = new WildcardQuery(new Term(f, "*" + s + "*"));

if (f.Equals(nameof(LuceneConfig.IndexFieldsEnum.Name)))
if (f.Equals(nameof(LuceneConfig.NodeFieldsEnum.Name)))
{
wildcardQuery.Boost = 5;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -455,17 +455,17 @@ private void AddPackageToSearchIndex(PackageManagerSearchElement package, Docume
if (DynamoModel.IsTestMode) return;
if (LuceneSearchUtility.addedFields == null) return;

LuceneSearchUtility.SetDocumentFieldValue(doc, nameof(LuceneConfig.IndexFieldsEnum.Name), package.Name);
LuceneSearchUtility.SetDocumentFieldValue(doc, nameof(LuceneConfig.IndexFieldsEnum.Description), package.Description);
LuceneSearchUtility.SetDocumentFieldValue(doc, nameof(LuceneConfig.NodeFieldsEnum.Name), package.Name);
LuceneSearchUtility.SetDocumentFieldValue(doc, nameof(LuceneConfig.NodeFieldsEnum.Description), package.Description);

if (package.Keywords.Count() > 0)
{
LuceneSearchUtility.SetDocumentFieldValue(doc, nameof(LuceneConfig.IndexFieldsEnum.SearchKeywords), package.Keywords);
LuceneSearchUtility.SetDocumentFieldValue(doc, nameof(LuceneConfig.NodeFieldsEnum.SearchKeywords), package.Keywords);
}

if (package.Hosts != null && string.IsNullOrEmpty(package.Hosts.ToString()))
{
LuceneSearchUtility.SetDocumentFieldValue(doc, nameof(LuceneConfig.IndexFieldsEnum.Hosts), package.Hosts.ToString(), true, true);
LuceneSearchUtility.SetDocumentFieldValue(doc, nameof(LuceneConfig.NodeFieldsEnum.Hosts), package.Hosts.ToString(), true, true);
}

LuceneSearchUtility.writer?.AddDocument(doc);
Expand Down Expand Up @@ -1132,7 +1132,7 @@ internal IEnumerable<PackageManagerSearchElementViewModel> Search(string searchT
Document resultDoc = LuceneSearchUtility.Searcher.Doc(topDocs.ScoreDocs[i].Doc);

// Get the view model of the package element and add it to the results.
string name = resultDoc.Get(nameof(LuceneConfig.IndexFieldsEnum.Name));
string name = resultDoc.Get(nameof(LuceneConfig.NodeFieldsEnum.Name));

var foundPackage = GetViewModelForPackageSearchElement(name);
if (foundPackage != null)
Expand Down
20 changes: 14 additions & 6 deletions src/DynamoCoreWpf/ViewModels/Search/SearchViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -962,17 +962,18 @@ internal IEnumerable<NodeSearchElementViewModel> Search(string search, bool useL
// read back a Lucene doc from results
Document resultDoc = LuceneSearchUtility.Searcher.Doc(topDocs.ScoreDocs[i].Doc);

string name = resultDoc.Get(nameof(LuceneConfig.IndexFieldsEnum.Name));
string docName = resultDoc.Get(nameof(LuceneConfig.IndexFieldsEnum.DocName));
string cat = resultDoc.Get(nameof(LuceneConfig.IndexFieldsEnum.FullCategoryName));
string name = resultDoc.Get(nameof(LuceneConfig.NodeFieldsEnum.Name));
string docName = resultDoc.Get(nameof(LuceneConfig.NodeFieldsEnum.DocName));
string cat = resultDoc.Get(nameof(LuceneConfig.NodeFieldsEnum.FullCategoryName));
string parameters = resultDoc.Get(nameof(LuceneConfig.NodeFieldsEnum.Parameters));

if (!string.IsNullOrEmpty(docName))
{
//code for setting up documentation info
}
else
{
var foundNode = FindViewModelForNodeNameAndCategory(name, cat);
var foundNode = FindViewModelForNodeNameAndCategory(name, cat, parameters);
if (foundNode != null)
{
candidates.Add(foundNode);
Expand All @@ -992,13 +993,20 @@ internal IEnumerable<NodeSearchElementViewModel> Search(string search, bool useL
/// </summary>
/// <param name="nodeName">Name of the node</param>
/// <param name="nodeCategory">Full Category of the node</param>
/// <param name="parameters">Node input parameters</param>
/// <returns></returns>
private NodeSearchElementViewModel FindViewModelForNodeNameAndCategory(string nodeName, string nodeCategory)
private NodeSearchElementViewModel FindViewModelForNodeNameAndCategory(string nodeName, string nodeCategory, string parameters)
{
var result = Model.SearchEntries.Where(e => {
if (e.Name.Equals(nodeName) && e.FullCategoryName.Equals(nodeCategory))
{
return true;
//When the node info was indexed if Parameters was null we added an empty space (null cannot be indexed)
//Then in this case when searching if e.Parameters is null we need to check against empty space
if (e.Parameters == null)
return string.IsNullOrEmpty(parameters);
//Parameters contain a value so we need to compare against the value indexed
else
return e.Parameters.Equals(parameters);
}
return false;
});
Expand Down

0 comments on commit c91c6f8

Please sign in to comment.