Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Splitting escaped search terms causes QueryParser.Parse to throw at

escape boundaries. Therefore we need to escape individual search terms
separately
  • Loading branch information...
commit 27232992cfcf17b703896698a7c4addaa22bdee9 1 parent 2789792
Pranav K pranavkm authored
Showing with 12 additions and 5 deletions.
  1. +12 −5 Website/Infrastructure/Lucene/LuceneSearchService.cs
17 Website/Infrastructure/Lucene/LuceneSearchService.cs
View
@@ -90,8 +90,6 @@ private static Query ParseQuery(string searchTerm)
var fields = new Dictionary<string, float> { { "Id", 1.2f }, { "Title", 1.0f }, { "Tags", 0.8f }, { "Description", 0.1f },
{ "Author", 1.0f } };
var analyzer = new StandardAnalyzer(LuceneCommon.LuceneVersion);
- searchTerm = QueryParser.Escape(searchTerm).ToLowerInvariant();
-
var queryParser = new MultiFieldQueryParser(LuceneCommon.LuceneVersion, fields.Keys.ToArray(), analyzer, fields);
var conjuctionQuery = new BooleanQuery();
@@ -100,9 +98,12 @@ private static Query ParseQuery(string searchTerm)
disjunctionQuery.SetBoost(0.1f);
var wildCardQuery = new BooleanQuery();
wildCardQuery.SetBoost(0.5f);
- var exactIdQuery = new TermQuery(new Term("Id-Exact", searchTerm));
+
+ // Escape the entire term we use for exact searches.
+ var escapedSearchTerm = Escape(searchTerm);
+ var exactIdQuery = new TermQuery(new Term("Id-Exact", escapedSearchTerm));
exactIdQuery.SetBoost(2.5f);
- var wildCardIdQuery = new WildcardQuery(new Term("Id-Exact", "*" + searchTerm + "*"));
+ var wildCardIdQuery = new WildcardQuery(new Term("Id-Exact", "*" + escapedSearchTerm + "*"));
foreach(var term in GetSearchTerms(searchTerm))
{
@@ -127,7 +128,13 @@ private static IEnumerable<string> GetSearchTerms(string searchTerm)
{
return searchTerm.Split(new[] { ' ', '.', '-' }, StringSplitOptions.RemoveEmptyEntries)
.Concat(new[] { searchTerm })
- .Distinct(StringComparer.OrdinalIgnoreCase);
+ .Distinct(StringComparer.OrdinalIgnoreCase)
+ .Select(Escape);
+ }
+
+ private static string Escape(string term)
+ {
+ return QueryParser.Escape(term).ToLowerInvariant();
}
}
}

1 comment on commit 2723299

Jeff Handley
Owner

:shipit:

Please sign in to comment.
Something went wrong with that request. Please try again.