Skip to content
Permalink
Browse files

QNTM-5870: When searching weights should be taken into account before…

… string matching (#9312)

* Update changes from librarie.js to fix QNTM-3710

* Add initial logging to the search dictionary

* Update output message

* Add returned count to the logging after the Search method is called

* Fix merge of tagDictionary changes

* Only return the first 20 search results (will be placed under a debug mode after running CI)

* Put search result truncation behind a debug mode

* Add commentds to the constructors

* Remove unused code
  • Loading branch information...
ColinDayOrg committed Dec 14, 2018
1 parent 7ab5632 commit 6925ebbfa3324126ebba8e9c5c56669b3f19af16
@@ -654,7 +654,7 @@ protected DynamoModel(IStartConfiguration config)

pathManager.Preferences = PreferenceSettings;

SearchModel = new NodeSearchModel();
SearchModel = new NodeSearchModel(Logger);
SearchModel.ItemProduced +=
node => ExecuteCommand(new CreateNodeCommand(node, 0, 0, true, true));

@@ -6,6 +6,7 @@
using Dynamo.Graph.Nodes;
using Dynamo.Search.SearchElements;
using DynamoUtilities;
using Dynamo.Logging;

namespace Dynamo.Search
{
@@ -14,6 +15,14 @@ namespace Dynamo.Search
/// </summary>
public class NodeSearchModel : SearchLibrary<NodeSearchElement, NodeModel>
{
/// <summary>
/// Construct a NodeSearchModel object
/// </summary>
/// <param name="logger"> (Optional) A logger to pass through to SearchLibrary for logging search data</param>
internal NodeSearchModel(ILogger logger = null) : base(logger)
{
}

internal override void Add(NodeSearchElement entry)
{
SearchElementGroup group = SearchElementGroup.None;
@@ -1,9 +1,9 @@
using System.Diagnostics;
using Dynamo.Utilities;
using Dynamo.Utilities;
using Dynamo.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Diagnostics;

namespace Dynamo.Search
{
@@ -12,11 +12,27 @@ namespace Dynamo.Search
/// </summary>
public class SearchDictionary<V>
{
private ILogger logger;

/// <summary>
/// Construct a SearchDictionary object
/// </summary>
/// <param name="logger"> (Optional) A logger to use to log search data</param>
internal SearchDictionary(ILogger logger = null)
{
this.logger = logger;
}

protected readonly Dictionary<V, Dictionary<string, double>> entryDictionary =
new Dictionary<V, Dictionary<string, double>>();

private List<IGrouping<string, Tuple<V, double>>> tagDictionary;

/// <summary>
/// Indicates whether to truncate the search results (currently to 20 items).
/// </summary>
internal bool TruncateSearchResults { get; set; } = false;

/// <summary>
/// All the current entries in search.
/// </summary>
@@ -307,14 +323,23 @@ internal void RebuildTagDictionary()
tagWeightAndEntry =>
Tuple.Create(tagWeightAndEntry.Entry, tagWeightAndEntry.Weight)).ToList();
}

/// <summary>
/// Search for elements in the dictionary based on the query
/// </summary>
/// <param name="query"> The query </param>
/// <param name="minResultsForTolerantSearch">Minimum number of results in the original search strategy to justify doing more tolerant search</param>
internal IEnumerable<V> Search(string query, int minResultsForTolerantSearch = 0)
{
#if DEBUG
Stopwatch stopwatch = null;
if (this.logger != null)
{
stopwatch = new Stopwatch();
stopwatch.Start();
}
#endif

var searchDict = new Dictionary<V, double>();

if (tagDictionary == null)
@@ -330,13 +355,38 @@ internal IEnumerable<V> Search(string query, int minResultsForTolerantSearch = 0
ComputeWeightAndAddToDictionary(query, pair, searchDict);
}

return searchDict
.OrderByDescending(x => x.Value)
.Select(x => x.Key);
var orderedSearchDict = searchDict.OrderByDescending(x => x.Value);

var searchResults = orderedSearchDict.Select(x => x.Key);
if (TruncateSearchResults)
{
searchResults = searchResults.Take(20);
}

#if DEBUG
if (this.logger != null)
{
stopwatch.Stop();

var message =
string.Format(
"Searching for: \"{0}\", [Entries:{1}, Tags:{2}] : {3}ms -> {4}",
query,
entryDictionary.Count,
tagDictionary.Count,
stopwatch.ElapsedMilliseconds,
searchResults.Count());
this.logger.Log(message);
}
#endif

return searchResults;
}

private static void ComputeWeightAndAddToDictionary(string query,
IGrouping<string, Tuple<V, double>> pair, Dictionary<V, double> searchDict)
private static void ComputeWeightAndAddToDictionary(
string query,
IGrouping<string, Tuple<V, double>> pair,
Dictionary<V, double> searchDict)
{
// it has a match, how close is it to matching the entire string?
double matchCloseness = ((double)query.Length) / pair.Key.Length;
@@ -1,4 +1,5 @@
using Dynamo.Utilities;
using Dynamo.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
@@ -13,6 +14,14 @@ namespace Dynamo.Search
public class SearchLibrary<TEntry, TItem> : SearchDictionary<TEntry>, ISource<TItem>
where TEntry : ISearchEntry, ISource<TItem>
{
/// <summary>
/// Construct a SearchLibrary object
/// </summary>
/// <param name="logger"> (Optional) A logger to pass through to SearchDictionary for logging search data</param>
internal SearchLibrary(ILogger logger = null) : base(logger)
{
}

/// <summary>
/// Adds an entry to search.
/// </summary>

Some generated files are not rendered by default. Learn more.

@@ -2184,4 +2184,8 @@ Do you want to install the latest Dynamo update?</value>
<data name="RerunButtonToolTip" xml:space="preserve">
<value>Rerun the graph.</value>
</data>
<data name="DynamoViewDebugMenuTruncateSearchResults" xml:space="preserve">
<value>Truncate Search Results</value>
<comment>Debug Menu | Truncate Search Results</comment>
</data>
</root>
@@ -2184,4 +2184,8 @@ Want to publish a different package?</value>
<value>Custom Nodes will be placed in the Add-Ons section of the library.</value>
<comment>Note regarding Custom Node library location</comment>
</data>
<data name="DynamoViewDebugMenuTruncateSearchResults" xml:space="preserve">
<value>Truncate Search Results</value>
<comment>Debug menu | Truncate Search Results</comment>
</data>
</root>
@@ -291,6 +291,18 @@ public bool EnableTSpline
}
}

/// <summary>
/// Indicates whether search results should be truncated (currently to 20 items).
/// </summary>
public bool TruncateSearchResults
{
get { return model.SearchModel.TruncateSearchResults; }
set
{
model.SearchModel.TruncateSearchResults = value;
}
}

public int LibraryWidth
{
get
@@ -784,6 +784,11 @@
IsCheckable="True"
Header="{x:Static p:Resources.DynamoViewDebugMenuShowDebugAST}"
IsChecked="{Binding ShowDebugASTs}"></MenuItem>
<MenuItem Focusable="False"
Name="ExperimentalSearch"
IsCheckable="True"
IsChecked="{Binding TruncateSearchResults}"
Header="{x:Static p:Resources.DynamoViewDebugMenuTruncateSearchResults}" />
<MenuItem Focusable="False"
Name="ForceReexec"
Header="{x:Static p:Resources.DynamoViewDebugMenuForceReExecute}"

0 comments on commit 6925ebb

Please sign in to comment.
You can’t perform that action at this time.