Skip to content

Commit

Permalink
DYN-6535 DynamoRevit Improve Search Fix 2
Browse files Browse the repository at this point in the history
The specific case "element set parameter" was failing on Revit 2025 (not finding anything), then I've modified the code so that check if the first term is a category if that is the case it will create a specific query using the first term as category and the next terms as node name.
  • Loading branch information
RobertGlobant20 committed Jan 31, 2024
1 parent 062b393 commit 5ef4493
Showing 1 changed file with 45 additions and 7 deletions.
52 changes: 45 additions & 7 deletions src/DynamoCore/Utilities/LuceneSearchUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,18 @@ internal void SetDocumentFieldValue(Document doc, string field, string value, bo
}
}

/// <summary>
/// Check if the term passed as parameter is found inside the FullCategoryName
/// </summary>
/// <param name="term">Splitted search term e.g. if the search term is "set parameter" the parameter will be "set" or "parameter"</param>
/// <param name="FullCategoryName">The complete category used for a specific node, like Core.File.FileSystem</param>
/// <returns></returns>
private bool IsMatchingCategory(string term, string FullCategoryName)
{
var categoryTerms = FullCategoryName.Split(".").Select(x => x.ToLower());
return categoryTerms.Contains(term);
}

/// <summary>
/// Creates a search query with adjusted priority, fuzzy logic and wildcards.
/// Complete Search term appearing in Name of the package will be given highest priority.
Expand Down Expand Up @@ -306,6 +318,8 @@ internal string CreateSearchQuery(string[] fields, string SearchTerm)

foreach (string f in fields)
{
Occur occurQuery = Occur.SHOULD;

//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 (searchType == SearchType.ByCategory)
Expand Down Expand Up @@ -335,6 +349,33 @@ internal string CreateSearchQuery(string[] fields, string SearchTerm)
if (string.IsNullOrEmpty(searchTerm))
continue;

bool firstTermIsCategory = false;

//This will only valid when the search term has empty spaces
if (hasEmptySpaces)
{
//Check if the first term of the search criteria match any category
var possibleCategory = searchTerm.Split(' ')[0];
firstTermIsCategory = dynamoModel.SearchModel.Entries.Where(entry => IsMatchingCategory(possibleCategory, entry.FullCategoryName) == true && !string.IsNullOrEmpty(possibleCategory)).Any();

//Get the node matching the Category provided in the search term
var matchingCategory = dynamoModel.SearchModel.Entries.Where(entry => IsMatchingCategory(possibleCategory, entry.FullCategoryName) == true && !string.IsNullOrEmpty(possibleCategory)).FirstOrDefault();
if (matchingCategory == null && firstTermIsCategory == true) continue;

if (f == nameof(LuceneConfig.NodeFieldsEnum.FullCategoryName) && firstTermIsCategory)
{
//Means that the first term is a category when we will be using the FullCategoryName for making a specific search based in the category
trimmedSearchTerm = matchingCategory?.FullCategoryName;
occurQuery = Occur.MUST;
}
else if (f == nameof(LuceneConfig.NodeFieldsEnum.Name) && firstTermIsCategory)
{
//If the field being iterated is Name and we are sure that the first term is a Category when we remove the term from the string so we can search for the specific node Name
trimmedSearchTerm = trimmedSearchTerm?.Replace(possibleCategory, string.Empty);
if (string.IsNullOrEmpty(trimmedSearchTerm)) continue;
}
}

FuzzyQuery fuzzyQuery;
if (searchTerm.Length > LuceneConfig.FuzzySearchMinimalTermLength)
{
Expand All @@ -347,14 +388,11 @@ internal string CreateSearchQuery(string[] fields, string SearchTerm)

if (searchType == SearchType.ByCategory && f == nameof(LuceneConfig.NodeFieldsEnum.CategorySplitted))
{
booleanQuery.Add(fieldQuery, Occur.MUST);
booleanQuery.Add(wildcardQuery, Occur.MUST);
}
else
{
booleanQuery.Add(fieldQuery, Occur.SHOULD);
booleanQuery.Add(wildcardQuery, Occur.SHOULD);
occurQuery = Occur.MUST;
}

booleanQuery.Add(fieldQuery, occurQuery);
booleanQuery.Add(wildcardQuery, occurQuery);

if (searchTerm.Contains(' '))
{
Expand Down

0 comments on commit 5ef4493

Please sign in to comment.