Skip to content

Commit 30fd0a1

Browse files
DYN-5795 Lucene Search Weights (#14062)
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 *
1 parent 89a1ae2 commit 30fd0a1

File tree

3 files changed

+85
-39
lines changed

3 files changed

+85
-39
lines changed

src/DynamoCore/Configuration/Configurations.cs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,47 @@ public class Configurations
5656
/// </summary>
5757
public static LuceneVersion LuceneNetVersion = LuceneVersion.LUCENE_48;
5858

59+
60+
/// <summary>
61+
/// This represent the fields that will be indexed when initializing Lucene Search
62+
/// </summary>
63+
public enum IndexFieldsEnum
64+
{
65+
/// <summary>
66+
/// Name - The name of the node
67+
/// </summary>
68+
Name,
69+
/// <summary>
70+
/// FullCategoryName - The category of the node
71+
/// </summary>
72+
FullCategoryName,
73+
/// <summary>
74+
/// Description - The description of the node
75+
/// </summary>
76+
Description,
77+
/// <summary>
78+
/// 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)
79+
/// </summary>
80+
SearchKeywords,
81+
/// <summary>
82+
/// DocName - Name of the Document
83+
/// </summary>
84+
DocName,
85+
/// <summary>
86+
/// Documentation - Documentation of the node
87+
/// </summary>
88+
Documentation
89+
}
90+
5991
/// <summary>
6092
/// Fields to be indexed by Lucene Search
6193
/// </summary>
62-
public static string[] IndexFields = { "Name", "FullCategoryName", "Description", "SearchKeywords", "InputParameters", "OutputParameters", "DocName", "Documentation", "PackageName", "PackageVersion" };
94+
public static string[] IndexFields = { nameof(IndexFieldsEnum.Name),
95+
nameof(IndexFieldsEnum.FullCategoryName),
96+
nameof(IndexFieldsEnum.Description),
97+
nameof(IndexFieldsEnum.SearchKeywords),
98+
nameof(IndexFieldsEnum.DocName),
99+
nameof(IndexFieldsEnum.Documentation)};
63100

64101
#endregion
65102

src/DynamoCore/Models/DynamoModel.cs

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3273,25 +3273,22 @@ private NodeModelSearchElement AddNodeTypeToSearch(TypeLoadData typeLoadData)
32733273
private Document InitializeIndexDocument()
32743274
{
32753275
if (IsTestMode) return null;
3276-
//TODO: all this harcoded string values should be moved to a different class.
3277-
var fullCategory = new TextField("FullCategoryName", "", Field.Store.YES);
3278-
var name = new TextField("Name", "", Field.Store.YES);
3279-
var description = new TextField("Description", "", Field.Store.YES);
3280-
var keywords = new TextField("SearchKeywords", "", Field.Store.YES);
3281-
var inp = new TextField("InputParameters", "", Field.Store.YES);
3282-
var outp = new TextField("OutputParameters", "", Field.Store.YES);
32833276

3284-
var docName = new StringField("DocName", "", Field.Store.YES);
3285-
var fullDoc = new TextField("Documentation", "", Field.Store.YES);
3286-
3287-
var pkgName = new TextField("PackageName", "", Field.Store.YES);
3288-
var pkgVer = new TextField("PackageVersion", "", Field.Store.YES);
3277+
var name = new TextField(nameof(Configurations.IndexFieldsEnum.Name), string.Empty, Field.Store.YES);
3278+
var fullCategory = new TextField(nameof(Configurations.IndexFieldsEnum.FullCategoryName), string.Empty, Field.Store.YES);
3279+
var description = new TextField(nameof(Configurations.IndexFieldsEnum.Description), string.Empty, Field.Store.YES);
3280+
var keywords = new TextField(nameof(Configurations.IndexFieldsEnum.SearchKeywords), string.Empty, Field.Store.YES);
3281+
var docName = new StringField(nameof(Configurations.IndexFieldsEnum.DocName), string.Empty, Field.Store.YES);
3282+
var fullDoc = new TextField(nameof(Configurations.IndexFieldsEnum.Documentation), string.Empty, Field.Store.YES);
32893283

32903284
var d = new Document()
32913285
{
3292-
fullCategory, name, description, keywords, inp,outp,
3293-
fullDoc, docName,
3294-
pkgName, pkgVer
3286+
fullCategory,
3287+
name,
3288+
description,
3289+
keywords,
3290+
fullDoc,
3291+
docName
32953292
};
32963293
return d;
32973294
}
@@ -3306,12 +3303,10 @@ private void AddNodeTypeToSearchIndex(NodeSearchElement node, Document doc)
33063303
if (IsTestMode) return;
33073304
if (addedFields == null) return;
33083305

3309-
SetDocumentFieldValue(doc, "FullCategoryName", node.FullCategoryName);
3310-
SetDocumentFieldValue(doc, "Name", node.Name);
3311-
SetDocumentFieldValue(doc, "Description", node.Description);
3312-
SetDocumentFieldValue(doc, "InputParameters", string.Join(" ", node.InputParameters.Select(t => $"{t.Item1}:{t.Item2}")));
3313-
SetDocumentFieldValue(doc, "OutputParameters", node.OutputParameters.Aggregate((x, y) => x + " " + y));
3314-
if (node.SearchKeywords.Count > 0) SetDocumentFieldValue(doc, "SearchKeywords", node.SearchKeywords.Aggregate((x, y) => x + " " + y), true, true);
3306+
SetDocumentFieldValue(doc, nameof(Configurations.IndexFieldsEnum.FullCategoryName), node.FullCategoryName);
3307+
SetDocumentFieldValue(doc, nameof(Configurations.IndexFieldsEnum.Name), node.Name);
3308+
SetDocumentFieldValue(doc, nameof(Configurations.IndexFieldsEnum.Description), node.Description);
3309+
if (node.SearchKeywords.Count > 0) SetDocumentFieldValue(doc, nameof(Configurations.IndexFieldsEnum.SearchKeywords), node.SearchKeywords.Aggregate((x, y) => x + " " + y), true, true);
33153310

33163311
writer?.AddDocument(doc);
33173312
}

src/DynamoCoreWpf/ViewModels/Search/SearchViewModel.cs

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -959,21 +959,16 @@ internal IEnumerable<NodeSearchElementViewModel> Search(string search, bool useL
959959
Document resultDoc = Model.Searcher.Doc(topDocs.ScoreDocs[i].Doc);
960960

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

964-
string docName = resultDoc.Get("DocName");
965-
string cat = resultDoc.Get("FullCategoryName");
966-
string fulldesc = resultDoc.Get("Documentation");
967-
string pkgName = resultDoc.Get("PackageName");
964+
string docName = resultDoc.Get(nameof(Configurations.IndexFieldsEnum.DocName));
965+
string cat = resultDoc.Get(nameof(Configurations.IndexFieldsEnum.FullCategoryName));
966+
string fulldesc = resultDoc.Get(nameof(Configurations.IndexFieldsEnum.Description));
968967

969968
if (!string.IsNullOrEmpty(docName))
970969
{
971970
//code for setting up documentation info
972971
}
973-
else if (!string.IsNullOrEmpty(pkgName))
974-
{
975-
//code for setting up package info
976-
}
977972
else
978973
{
979974
var foundNode = FindViewModelForNodeNameAndCategory(name, cat);
@@ -1018,13 +1013,25 @@ private string CreateSearchQuery(string[] fields, string searchKey)
10181013
}
10191014

10201015
var wildcardQuery = new WildcardQuery(new Term(f, searchTerm));
1021-
if (f.Equals("Name")) { wildcardQuery.Boost = 10; }
1022-
else { wildcardQuery.Boost = 6; }
1016+
if (f.Equals(nameof(Configurations.IndexFieldsEnum.Name)))
1017+
{
1018+
wildcardQuery.Boost = 10;
1019+
}
1020+
else
1021+
{
1022+
wildcardQuery.Boost = 6;
1023+
}
10231024
booleanQuery.Add(wildcardQuery, Occur.SHOULD);
10241025

1025-
wildcardQuery = new WildcardQuery(new Term(f, searchTerm + "*"));
1026-
if (f.Equals("Name")) { wildcardQuery.Boost = 7; }
1027-
else { wildcardQuery.Boost = 4; }
1026+
wildcardQuery = new WildcardQuery(new Term(f, "*" + searchTerm + "*"));
1027+
if (f.Equals(nameof(Configurations.IndexFieldsEnum.Name)))
1028+
{
1029+
wildcardQuery.Boost = 7;
1030+
}
1031+
else
1032+
{
1033+
wildcardQuery.Boost = 4;
1034+
}
10281035
booleanQuery.Add(wildcardQuery, Occur.SHOULD);
10291036

10301037
if (searchTerm.Contains(' ') || searchTerm.Contains('.'))
@@ -1036,9 +1043,16 @@ private string CreateSearchQuery(string[] fields, string searchKey)
10361043
fuzzyQuery = new FuzzyQuery(new Term(f, s), fuzzyLogicRange);
10371044
booleanQuery.Add(fuzzyQuery, Occur.SHOULD);
10381045
}
1039-
wildcardQuery = new WildcardQuery(new Term(f, s + "*"));
1040-
if (f.Equals("Name")) { wildcardQuery.Boost = 5; }
1041-
else { wildcardQuery.Boost = 2; }
1046+
wildcardQuery = new WildcardQuery(new Term(f, "*" + s + "*"));
1047+
1048+
if (f.Equals(nameof(Configurations.IndexFieldsEnum.Name)))
1049+
{
1050+
wildcardQuery.Boost = 5;
1051+
}
1052+
else
1053+
{
1054+
wildcardQuery.Boost = 2;
1055+
}
10421056
booleanQuery.Add(wildcardQuery, Occur.SHOULD);
10431057
}
10441058
}

0 commit comments

Comments
 (0)