Skip to content

Commit

Permalink
DYN-5795 Lucene Search Weights (#14062)
Browse files Browse the repository at this point in the history
The hard-coded values for the field names were moved to the Configurations class and all the places in which this names were used were replaced.
Also in the SearchViewModel.Search() method I've done minor changes to consider the wildcard expression * keyword *
  • Loading branch information
RobertGlobant20 authored Jun 13, 2023
1 parent 89a1ae2 commit 30fd0a1
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 39 deletions.
39 changes: 38 additions & 1 deletion src/DynamoCore/Configuration/Configurations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,47 @@ public class Configurations
/// </summary>
public static LuceneVersion LuceneNetVersion = LuceneVersion.LUCENE_48;


/// <summary>
/// This represent the fields that will be indexed when initializing Lucene Search
/// </summary>
public enum IndexFieldsEnum
{
/// <summary>
/// Name - The name of the node
/// </summary>
Name,
/// <summary>
/// FullCategoryName - The category of the node
/// </summary>
FullCategoryName,
/// <summary>
/// Description - The description of the node
/// </summary>
Description,
/// <summary>
/// SearchKeywords - Several keywords that will be used when searching any word (this values are coming from xml files like BuiltIn.xml, DesignScriptBuiltin.xml or ProtoGeometry.xml)
/// </summary>
SearchKeywords,
/// <summary>
/// DocName - Name of the Document
/// </summary>
DocName,
/// <summary>
/// Documentation - Documentation of the node
/// </summary>
Documentation
}

/// <summary>
/// Fields to be indexed by Lucene Search
/// </summary>
public static string[] IndexFields = { "Name", "FullCategoryName", "Description", "SearchKeywords", "InputParameters", "OutputParameters", "DocName", "Documentation", "PackageName", "PackageVersion" };
public static string[] IndexFields = { nameof(IndexFieldsEnum.Name),
nameof(IndexFieldsEnum.FullCategoryName),
nameof(IndexFieldsEnum.Description),
nameof(IndexFieldsEnum.SearchKeywords),
nameof(IndexFieldsEnum.DocName),
nameof(IndexFieldsEnum.Documentation)};

#endregion

Expand Down
37 changes: 16 additions & 21 deletions src/DynamoCore/Models/DynamoModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3273,25 +3273,22 @@ private NodeModelSearchElement AddNodeTypeToSearch(TypeLoadData typeLoadData)
private Document InitializeIndexDocument()
{
if (IsTestMode) return null;
//TODO: all this harcoded string values should be moved to a different class.
var fullCategory = new TextField("FullCategoryName", "", Field.Store.YES);
var name = new TextField("Name", "", Field.Store.YES);
var description = new TextField("Description", "", Field.Store.YES);
var keywords = new TextField("SearchKeywords", "", Field.Store.YES);
var inp = new TextField("InputParameters", "", Field.Store.YES);
var outp = new TextField("OutputParameters", "", Field.Store.YES);

var docName = new StringField("DocName", "", Field.Store.YES);
var fullDoc = new TextField("Documentation", "", Field.Store.YES);

var pkgName = new TextField("PackageName", "", Field.Store.YES);
var pkgVer = new TextField("PackageVersion", "", Field.Store.YES);
var name = new TextField(nameof(Configurations.IndexFieldsEnum.Name), string.Empty, Field.Store.YES);
var fullCategory = new TextField(nameof(Configurations.IndexFieldsEnum.FullCategoryName), string.Empty, Field.Store.YES);
var description = new TextField(nameof(Configurations.IndexFieldsEnum.Description), string.Empty, Field.Store.YES);
var keywords = new TextField(nameof(Configurations.IndexFieldsEnum.SearchKeywords), string.Empty, Field.Store.YES);
var docName = new StringField(nameof(Configurations.IndexFieldsEnum.DocName), string.Empty, Field.Store.YES);
var fullDoc = new TextField(nameof(Configurations.IndexFieldsEnum.Documentation), string.Empty, Field.Store.YES);

var d = new Document()
{
fullCategory, name, description, keywords, inp,outp,
fullDoc, docName,
pkgName, pkgVer
fullCategory,
name,
description,
keywords,
fullDoc,
docName
};
return d;
}
Expand All @@ -3306,12 +3303,10 @@ private void AddNodeTypeToSearchIndex(NodeSearchElement node, Document doc)
if (IsTestMode) return;
if (addedFields == null) return;

SetDocumentFieldValue(doc, "FullCategoryName", node.FullCategoryName);
SetDocumentFieldValue(doc, "Name", node.Name);
SetDocumentFieldValue(doc, "Description", node.Description);
SetDocumentFieldValue(doc, "InputParameters", string.Join(" ", node.InputParameters.Select(t => $"{t.Item1}:{t.Item2}")));
SetDocumentFieldValue(doc, "OutputParameters", node.OutputParameters.Aggregate((x, y) => x + " " + y));
if (node.SearchKeywords.Count > 0) SetDocumentFieldValue(doc, "SearchKeywords", node.SearchKeywords.Aggregate((x, y) => x + " " + y), true, true);
SetDocumentFieldValue(doc, nameof(Configurations.IndexFieldsEnum.FullCategoryName), node.FullCategoryName);
SetDocumentFieldValue(doc, nameof(Configurations.IndexFieldsEnum.Name), node.Name);
SetDocumentFieldValue(doc, nameof(Configurations.IndexFieldsEnum.Description), node.Description);
if (node.SearchKeywords.Count > 0) SetDocumentFieldValue(doc, nameof(Configurations.IndexFieldsEnum.SearchKeywords), node.SearchKeywords.Aggregate((x, y) => x + " " + y), true, true);

writer?.AddDocument(doc);
}
Expand Down
48 changes: 31 additions & 17 deletions src/DynamoCoreWpf/ViewModels/Search/SearchViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -959,21 +959,16 @@ internal IEnumerable<NodeSearchElementViewModel> Search(string search, bool useL
Document resultDoc = Model.Searcher.Doc(topDocs.ScoreDocs[i].Doc);

// TODO: use consts in static class for the Lucene field names
string name = resultDoc.Get("Name");
string name = resultDoc.Get(nameof(Configurations.IndexFieldsEnum.Name));

string docName = resultDoc.Get("DocName");
string cat = resultDoc.Get("FullCategoryName");
string fulldesc = resultDoc.Get("Documentation");
string pkgName = resultDoc.Get("PackageName");
string docName = resultDoc.Get(nameof(Configurations.IndexFieldsEnum.DocName));
string cat = resultDoc.Get(nameof(Configurations.IndexFieldsEnum.FullCategoryName));
string fulldesc = resultDoc.Get(nameof(Configurations.IndexFieldsEnum.Description));

if (!string.IsNullOrEmpty(docName))
{
//code for setting up documentation info
}
else if (!string.IsNullOrEmpty(pkgName))
{
//code for setting up package info
}
else
{
var foundNode = FindViewModelForNodeNameAndCategory(name, cat);
Expand Down Expand Up @@ -1018,13 +1013,25 @@ private string CreateSearchQuery(string[] fields, string searchKey)
}

var wildcardQuery = new WildcardQuery(new Term(f, searchTerm));
if (f.Equals("Name")) { wildcardQuery.Boost = 10; }
else { wildcardQuery.Boost = 6; }
if (f.Equals(nameof(Configurations.IndexFieldsEnum.Name)))
{
wildcardQuery.Boost = 10;
}
else
{
wildcardQuery.Boost = 6;
}
booleanQuery.Add(wildcardQuery, Occur.SHOULD);

wildcardQuery = new WildcardQuery(new Term(f, searchTerm + "*"));
if (f.Equals("Name")) { wildcardQuery.Boost = 7; }
else { wildcardQuery.Boost = 4; }
wildcardQuery = new WildcardQuery(new Term(f, "*" + searchTerm + "*"));
if (f.Equals(nameof(Configurations.IndexFieldsEnum.Name)))
{
wildcardQuery.Boost = 7;
}
else
{
wildcardQuery.Boost = 4;
}
booleanQuery.Add(wildcardQuery, Occur.SHOULD);

if (searchTerm.Contains(' ') || searchTerm.Contains('.'))
Expand All @@ -1036,9 +1043,16 @@ private string CreateSearchQuery(string[] fields, string searchKey)
fuzzyQuery = new FuzzyQuery(new Term(f, s), fuzzyLogicRange);
booleanQuery.Add(fuzzyQuery, Occur.SHOULD);
}
wildcardQuery = new WildcardQuery(new Term(f, s + "*"));
if (f.Equals("Name")) { wildcardQuery.Boost = 5; }
else { wildcardQuery.Boost = 2; }
wildcardQuery = new WildcardQuery(new Term(f, "*" + s + "*"));

if (f.Equals(nameof(Configurations.IndexFieldsEnum.Name)))
{
wildcardQuery.Boost = 5;
}
else
{
wildcardQuery.Boost = 2;
}
booleanQuery.Add(wildcardQuery, Occur.SHOULD);
}
}
Expand Down

0 comments on commit 30fd0a1

Please sign in to comment.