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 Fix 2 #14917

Merged
merged 5 commits into from
Feb 6, 2024
Merged
Changes from 1 commit
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
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();
QilongTang marked this conversation as resolved.
Show resolved Hide resolved
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
Loading