我有一个问题需要突出显示工作。我使用带有WCF服务的ASP.Net MVC应用程序。 WCF服务将结果返回到视图。我的代码是:
public IEnumerable<Dictionary<string, string>> SolrQuery(string searchString)
{
SolrInitialSetup();
var solr = ServiceLocator.Current.GetInstance<ISolrOperations<SolrPDFDocument>>();
var results = solr.Query(new SolrQueryByField("author", searchString),
new QueryOptions
{
Highlight = new HighlightingParameters
{
Fields = new[] { "search_snippet" },
}
});
var returnedResults = new List<Dictionary<string, string>>();
if (results.Count > 0)
{
foreach (var pdfDoc in results)
{
var docBuffer = new Dictionary<string, string>();
docBuffer.Add("Id", ""+pdfDoc.Id.GetHashCode());
docBuffer.Add("Content", "" + pdfDoc.Content.ElementAt(0));
docBuffer.Add("Version", "" + pdfDoc.Version);
foreach (var h in results.Highlights[results[0].Id])
{
docBuffer.Add("search_snippet", String.Format("{0}", string.Join(", ", h.Value.ToArray())));
Debug.WriteLine("search_snippet", String.Format("{0}", string.Join(", ", h.Value.ToArray())));
}
returnedResults.Add(docBuffer);
}
}
return returnedResults;
}
@ Line foreach (var h in results.Highlights[results[0].Id])
我在浏览器中遇到错误The given key was not present in the dictionary.
我不知道什么是错的。
我的schema.xml:
<field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" />
<field name="content" type="text_general" indexed="true" stored="true" multiValued="true" termVectors="true" termPositions="true" termOffsets="true"/>
<field name="author" type="text_general" indexed="true" stored="true" termVectors="true" termPositions="true" termOffsets="true"/>
<field name="_version_" type="long" indexed="true" stored="true"/>
<field name="search_snippet" type="text_general" indexed="true" stored="true" termVectors="true" termPositions="true" termOffsets="true" />
我的SolrPDFDocument.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SolrNet.Attributes;
namespace WcfSearchService
{
class SolrPDFDocument
{
[SolrUniqueKey("id")]
public string Id { get; set; }
[SolrField("author")]
public string Author { get; set; }
[SolrField("content")]
public ICollection<string> Content { get; set; }
[SolrField("search_snippet")]
public ICollection<string> SearchSnippet { get; set; }
[SolrField("_version_")]
public long Version { get; set; }
}
}
我的solrconfig.xml
<requestHandler name="/select" class="solr.SearchHandler">
<!-- default values for query parameters can be specified, these
will be overridden by parameters in the request
-->
<lst name="defaults">
<str name="echoParams">explicit</str>
<int name="rows">10</int>
<str name="df">text</str>
<!-- Highlighting defaults -->
<str name="hl">on</str>
<str name="hl.fl">search_snippet author content</str>
</lst>
</requestHandler>
答案 0 :(得分:2)
好的,我已经解决了我的问题。
而不是像那样使用示例代码:
var results = solr.Query(new SolrQueryByField("author", searchString),
new QueryOptions
{
Highlight = new HighlightingParameters
{
Fields = new[] { "author" },
}
});
我像这样设置新字段的参数:
var results = solr.Query(new SolrQueryByField("text", searchString),
new QueryOptions
{
Highlight = new HighlightingParameters
{
Fields = new[] { "*" },
}
});
所以我得到了所有结果的所有片段。谢谢你的帮助!!!!
答案 1 :(得分:0)
您可能根本没有获得任何突出显示结果。您可以调试代码并验证是否填充了results.Highlights
。同时检查突出显示的默认值设置,因为我认为你需要设置hl = true。
<str name="hl">true</str>
答案 2 :(得分:0)
这很有趣。对我来说,这不起作用(我没有得到任何结果):
var results = solr.Query(new SolrQueryByField("text", searchString),
new QueryOptions
{
Highlight = new HighlightingParameters
{
Fields = new[] { "*" },
}
});
但是,这对我有用:
var results = solr.Query(string.Format("{0}:{1}", "text", searchString),
new QueryOptions
{
Highlight = new HighlightingParameters
{
Fields = new[] { "*" },
}
});
我正在使用solr 4.6.0和SolrNet build#173(10月13日00:32)