public AutoCompletionResult Autocomplete(string text, bool includeExplanation = false)
{
if (string.IsNullOrWhiteSpace(text)) return AutoCompletionResult.NoResult(text);
var searchers = _directoryFactory.GetAllDirectories().Select(d =>
{
try
{
return new IndexSearcher(d, true);
}
catch (Exception e)
{
_log.Error(e, "While searching directory {0}", d);
return null;
}
})
.Where(s => s != null)
.ToArray();
using (var searcher = new MultiSearcher(searchers))
{
try
{
BooleanQuery query = GetQueryForText(text);
var results = searcher.Search(query, 10);
var commands = results.ScoreDocs
.Select(d =>
{
var document = searcher.Doc(d.Doc);
try
{
Explanation explanation = null;
if (includeExplanation)
{
explanation = searcher.Explain(query, d.Doc);
}
var coreDoc = CoreDocument.Rehydrate(document);
var command = _converterRepository.FromDocumentToItem(coreDoc);
return new AutoCompletionResult.CommandResult(command, coreDoc.GetDocumentId(), explanation);
}
catch (Exception e)
{
_log.Error(e, "Error getting command result for document {0}:{1}",
document.GetField(SpecialFields.ConverterId).StringValue,
document.GetField(SpecialFields.Id).StringValue);
return null;
}
})
.Where(r => r != null);
return AutoCompletionResult.OrderedResult(text, commands);
}
catch (ParseException e)
{
_log.Error(e, "Error parsing '{0}'", text);
return AutoCompletionResult.NoResult(text);
}
}
}