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-6535 DynamoRevit Improve Search #14878

Merged
merged 5 commits into from
Jan 25, 2024
Merged
Changes from 4 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: 43 additions & 13 deletions src/DynamoCore/Utilities/LuceneSearchUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ internal class LuceneSearchUtility
/// </summary>
internal static readonly LuceneStartConfig DefaultPkgIndexStartConfig = new LuceneStartConfig(LuceneSearchUtility.LuceneStorage.FILE_SYSTEM, LuceneConfig.PackagesIndexingDirectory);

private bool hasEmptySpaces { get; set; }

public enum LuceneStorage
{
//Lucene Storage will be located in RAM and all the info indexed will be lost when Dynamo app is closed
Expand All @@ -82,6 +84,15 @@ public enum LuceneStorage
FILE_SYSTEM
}

public enum SearchType
Copy link
Contributor

Choose a reason for hiding this comment

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

comments for public fields please

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've added extra comments: 584f794

{
//Normal search using just one word matching a specific node name
Normal,

//Search by category using the "." character for example "list.re"
ByCategory
}

// Used for creating the StandardAnalyzer
internal Analyzer Analyzer;

Expand Down Expand Up @@ -264,7 +275,14 @@ internal void SetDocumentFieldValue(Document doc, string field, string value, bo
/// <returns></returns>
internal string CreateSearchQuery(string[] fields, string SearchTerm)
{
//By Default the search will be normal
SearchType searchType = SearchType.Normal;
int fuzzyLogicMaxEdits = LuceneConfig.FuzzySearchMinEdits;
hasEmptySpaces = false;

//Max number of nodes allowed in the search when is a ByEmptySpace search
const int MaxNodeNamesRepeated = 20;

// Use a larger max edit value - more tolerant with typo when search term is longer than threshold
if (SearchTerm.Length > LuceneConfig.FuzzySearchMaxEditsThreshold)
{
Expand All @@ -273,13 +291,21 @@ internal string CreateSearchQuery(string[] fields, string SearchTerm)

var booleanQuery = new BooleanQuery();
string searchTerm = QueryParser.Escape(SearchTerm);
var bCategoryBasedSearch = searchTerm.Contains('.') ? true : false;

if (searchTerm.Contains('.'))
searchType = SearchType.ByCategory;
else if (searchTerm.Contains(' '))
hasEmptySpaces = true;
else
searchType = SearchType.Normal;

var trimmedSearchTerm = hasEmptySpaces == true ? searchTerm.Replace(" ", "") : searchTerm;

foreach (string f in fields)
{
//Needs to be again due that now a query can contain different values per field (e.g. CategorySplitted:list, Name:tr)
searchTerm = QueryParser.Escape(SearchTerm);
if (bCategoryBasedSearch == true)
if (searchType == SearchType.ByCategory)
{
//This code section should be only executed if the search criteria is CategoryBased like "category.nodename"
if (f != nameof(LuceneConfig.NodeFieldsEnum.NameSplitted) &&
Expand All @@ -297,26 +323,26 @@ internal string CreateSearchQuery(string[] fields, string SearchTerm)
}
}

FuzzyQuery fuzzyQuery;
if (searchTerm.Length > LuceneConfig.FuzzySearchMinimalTermLength)
{
fuzzyQuery = new FuzzyQuery(new Term(f, searchTerm), fuzzyLogicMaxEdits);
booleanQuery.Add(fuzzyQuery, Occur.SHOULD);
}

//For normal search we don't consider the fields NameSplitted and CategorySplitted
if ((f == nameof(LuceneConfig.NodeFieldsEnum.NameSplitted) ||
f == nameof(LuceneConfig.NodeFieldsEnum.CategorySplitted)) && bCategoryBasedSearch == false)
f == nameof(LuceneConfig.NodeFieldsEnum.CategorySplitted)) && searchType != SearchType.ByCategory)
continue;

//This case is for when the user type something like "list.", I mean, not specifying the node name or part of it
if (string.IsNullOrEmpty(searchTerm))
continue;

var fieldQuery = CalculateFieldWeight(f, searchTerm);
var wildcardQuery = CalculateFieldWeight(f, searchTerm, true);
FuzzyQuery fuzzyQuery;
if (searchTerm.Length > LuceneConfig.FuzzySearchMinimalTermLength)
{
fuzzyQuery = new FuzzyQuery(new Term(f, hasEmptySpaces == true ? trimmedSearchTerm : searchTerm), fuzzyLogicMaxEdits);
booleanQuery.Add(fuzzyQuery, Occur.SHOULD);
}

if (bCategoryBasedSearch && f == nameof(LuceneConfig.NodeFieldsEnum.CategorySplitted))
var fieldQuery = CalculateFieldWeight(f, hasEmptySpaces == true ? trimmedSearchTerm : searchTerm);
var wildcardQuery = CalculateFieldWeight(f, hasEmptySpaces == true ? trimmedSearchTerm : searchTerm, true);

if (searchType == SearchType.ByCategory && f == nameof(LuceneConfig.NodeFieldsEnum.CategorySplitted))
{
booleanQuery.Add(fieldQuery, Occur.MUST);
booleanQuery.Add(wildcardQuery, Occur.MUST);
Expand All @@ -331,6 +357,10 @@ internal string CreateSearchQuery(string[] fields, string SearchTerm)
{
foreach (string s in searchTerm.Split(' ', '.'))
{
//If is a ByEmptySpace search and the splitted words match with more than MaxNodeNamesRepeated nodes then the word is skipped
int nodesFrequency = dynamoModel.SearchModel.Entries.Where(entry => entry.Name.ToLower().Contains(s) && !string.IsNullOrEmpty(s)).Count();
if (nodesFrequency > MaxNodeNamesRepeated) continue;

if (string.IsNullOrEmpty(s)) continue;

if (s.Length > LuceneConfig.FuzzySearchMinimalTermLength)
Expand Down
Loading