Skip to content

Instantly share code, notes, and snippets.

@BartDM
Created January 13, 2014 16:28
Show Gist options
  • Save BartDM/8403197 to your computer and use it in GitHub Desktop.
Save BartDM/8403197 to your computer and use it in GitHub Desktop.
/// <summary>
/// Parse the givven query string to a Lucene Query object
/// </summary>
/// <param name="searchQuery">The query string</param>
/// <param name="parser">The Lucense QueryParser</param>
/// <returns>A Lucene Query object</returns>
private Query ParseQuery(string searchQuery, QueryParser parser)
{
parser.AllowLeadingWildcard = true;
Query q;
try
{
q = parser.Parse(searchQuery);
}
catch (ParseException e)
{
Log.Error("Query parser exception", e);
q = null;
}
if (q == null || string.IsNullOrEmpty(q.ToString()))
{
string cooked = Regex.Replace(searchQuery, @"[^\w\.@-]", " ");
q = parser.Parse(cooked);
}
Log.DebugFormat("Parsed query for Lucene: \"{0}\"", q);
return q;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment