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-5795 Lucene Search Weights #14062

Merged
merged 1 commit into from
Jun 13, 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
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is fine place for now, I may move these to dedicated Lucene config file later in my PR

{
/// <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 + "*"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This probably could benefit with some comments


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