Merge pull request #426 from arknu/searchimprovements

Fix U4-5194 and U4-5167: Handle multiple spaces and quoted searches in the backoffice correctly
This commit is contained in:
Shannon Deminick
2014-07-22 15:26:38 -07:00

View File

@@ -24,6 +24,7 @@ using Examine.LuceneEngine.SearchCriteria;
using Examine.SearchCriteria;
using Umbraco.Web.Dynamics;
using umbraco;
using System.Text.RegularExpressions;
namespace Umbraco.Web.Editors
{
@@ -288,30 +289,78 @@ namespace Umbraco.Web.Editors
// then __nodeName will be matched normally with wildcards
// the rest will be normal without wildcards
var sb = new StringBuilder();
//node name exactly boost x 10
sb.Append("+(__nodeName:");
sb.Append(query.ToLower());
sb.Append("^10.0 ");
//node name normally with wildcards
sb.Append(" __nodeName:");
sb.Append(query.ToLower());
sb.Append("* ");
foreach (var f in fields)
bool hasSingleQuotes = Regex.IsMatch(query, "\"[^\"]*");
if (hasSingleQuotes)
{
//additional fields normally
sb.Append(f);
sb.Append(":");
sb.Append(query);
sb.Append(" ");
//we cannot search for single qoutes, so we ignore them
query = query.Replace("\"", "");
}
var querywords = query.Split(' ');
bool hasDoubleQuotes = Regex.IsMatch(query, "\"[^\"]*\"");
if (string.IsNullOrWhiteSpace(query))
{
return new List<EntityBasic>();
}
if (hasDoubleQuotes)
{
//node name exactly boost x 10
sb.Append("+(__nodeName: (");
sb.Append(query.ToLower());
sb.Append(")^10.0 ");
foreach (var f in fields)
{
//additional fields normally
sb.Append(f);
sb.Append(": (");
sb.Append(query);
sb.Append(") ");
}
}
else
{
//node name exactly boost x 10
sb.Append("+(__nodeName:");
sb.Append("\"");
sb.Append(query.ToLower());
sb.Append("\"");
sb.Append("^10.0 ");
//node name normally with wildcards
sb.Append(" __nodeName:");
sb.Append("(");
foreach (var w in querywords)
{
sb.Append(w.ToLower());
sb.Append("* ");
}
sb.Append(") ");
foreach (var f in fields)
{
//additional fields normally
sb.Append(f);
sb.Append(":");
sb.Append("(");
foreach (var w in querywords)
{
sb.Append(w.ToLower());
sb.Append("* ");
}
sb.Append(")");
sb.Append(" ");
}
}
//must match index type
sb.Append(") +__IndexType:");
sb.Append(type);
var raw = internalSearcher.CreateSearchCriteria().RawQuery(sb.ToString());
var result = internalSearcher.Search(raw);