Skip to content
This repository has been archived by the owner on Jul 30, 2024. It is now read-only.
/ NuGet.Jobs Public archive

Commit

Permalink
Handle empty quoted terms in Azure Search query (#598)
Browse files Browse the repository at this point in the history
  • Loading branch information
joelverhagen committed Jul 17, 2019
1 parent c983a74 commit 0e1b221
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/NuGet.Indexing/NuGetQueryParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,16 @@ public class NuGetQueryParser
{ QueryField.Any, new [] { "*" } }
};

public Dictionary<QueryField, HashSet<string>> ParseQuery(string query)
public Dictionary<QueryField, HashSet<string>> ParseQuery(string query, bool skipWhiteSpace = false)
{
var grouping = new Dictionary<QueryField, HashSet<string>>();
foreach (Clause clause in MakeClauses(Tokenize(query)))
{
if (skipWhiteSpace && string.IsNullOrWhiteSpace(clause.Text))
{
continue;
}

HashSet<string> text;
var queryField = GetQueryField(clause.Field);
if (!grouping.TryGetValue(queryField, out text))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ private string GetLuceneQuery(string query)
}

// Parse the NuGet query.
var grouping = _parser.ParseQuery(query.Trim());
var grouping = _parser.ParseQuery(query.Trim(), skipWhiteSpace: true);
if (!grouping.Any())
{
return MatchAllDocumentsQuery;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,22 @@ public static IEnumerable<object[]> CommonAzureSearchQueryData()
{ @"""foo bar"" baz", @"""foo bar"" baz" },
{ @"title:""foo bar""", @"title:""foo bar""" },
{ @"title:""a b"" c title:d f", @"+title:(""a b"" d) c f" },
{ @"title:"" a b c """, @"title:""a b c""" },

// Dangling quotes are handled with best effort
{ @"Tags:""windows", "tags:windows" },
{ @"json Tags:""net"" Tags:""windows sdk", @"+tags:(net windows sdk) json" },
{ @"json Tags:""net Tags:""windows sdk""", @"+tags:(net Tags\:) json windows sdk" },
{ @"sdk Tags:""windows", "+tags:windows sdk" },
{ @"Tags:""windows sdk", "tags:(windows sdk)" },
{ @"Tags:""""windows""", "windows" },

// Empty quotes are ignored
{ @"Tags:""""", @"*" },
{ @"Tags:"" """, @"*" },
{ @"Tags:"" """, @"*" },
{ @"windows Tags:"" """, @"windows" },
{ @"windows Tags:"" "" Tags:sdk", @"+tags:sdk windows" },

// Duplicate search terms on the same query field are folded
{ "a a", "a" },
Expand Down

0 comments on commit 0e1b221

Please sign in to comment.