Hi,
I created a custom class called CustomRes and have implemented a SearchIndex for it.
Problem is when performing a search on some words, zero results are returned even when they're in the CustomRes item.
E.g, 'unique', 'something' - first I thought it was the number of characters but it's hit and miss.
fieldsInfoProvider.xml
public IList<ResultItem> Search(string searchQuery, string indexName, int startIndex, int max, string mode, bool escapeChars, out int totalItems)
{
IList<ResultItem> result = new List<ResultItem>();
string[] fields = GetFields(indexName);
string indexDirectory = PathUtil.GetIndexPhysicalPath(indexName);
IndexSearcher searcher = new IndexSearcher(indexDirectory);
QueryParser.Operator searchMode = QueryParser.AND_OPERATOR;
MultiFieldQueryParser parser = new MultiFieldQueryParser(fields, new StandardAnalyzer());
if (!String.IsNullOrEmpty(mode)
&& mode == "AnyWord")
{
searchMode = QueryParser.OR_OPERATOR;
}
parser.SetDefaultOperator(searchMode);
if (escapeChars)
{
searchQuery = QueryParser.Escape(searchQuery);
}
Query query = searcher.Rewrite(parser.Parse(searchQuery));
Highlighter highlighter = new Highlighter(new QueryScorer(query));
Hits hits = searcher.Search(query, Sort.INDEXORDER);
totalItems = hits.Length();
if (max == 0)
max = totalItems;
else if (startIndex + max > totalItems)
max = totalItems;
else
max = startIndex + max;
for (int i = startIndex; i < max; i++)
{
Document doc = hits.Doc(i);
ResultItem item = new ResultItem();
item.Title = doc.Get("title");
item.Url = ResolveUrl(doc.Get("path"));
item.Score = hits.Score(i);
StringBuilder snippet = new StringBuilder();
bool lastAppendedIsSeparator = false;
for (int k = 0; k < fields.Length; k++)
{
string field = fields[k];
switch (field.ToLower())
{
case "title":
case "path":
case "keywords":
case "description":
continue;
}
string[] values = doc.GetValues(field);
if (values != null && values.Length > 0)
{
for (int j = 0; j < values.Length; j++)
{
if (values[j].Length > 0)
{
snippet.Append(values[j]);
lastAppendedIsSeparator = false;
if (j < values.Length - 1)
{
snippet.AppendLine(SearchManager.SEARCH_RESULTS_SEPARATOR);
lastAppendedIsSeparator = true;
}
}
}
}
if (k < fields.Length - 1 && !lastAppendedIsSeparator)
{
snippet.AppendLine(SearchManager.SEARCH_RESULTS_SEPARATOR);
lastAppendedIsSeparator = true;
}
}
string[] bestFragments = highlighter.GetBestFragments(new StandardAnalyzer(), fields[0], snippet.ToString(), 5);
//item.Snippet = String.Join(SearchManager.SEARCH_RESULTS_SEPARATOR, bestFragments);
item.Snippet = snippet.ToString();
item.Snippet.TrimEnd(' ', '.');
item.Snippet += SearchManager.SEARCH_RESULTS_SEPARATOR;
result.Add(item);
}
searcher.Close();
return result;
}
part of CustomResIndexerInfo.cs