您好我使用Nest作为Elastisearch的接口。 一切正常,只有一件我无法做到的事情。 这就是突出显示。
我有以下'型号'
[ElasticType(Name = "WebResource", SearchAnalyzer = "full_name", IndexAnalyzer = "partial_name", DateDetection = true, NumericDetection = true)]
public class WebResource
{
public string _id;
[ElasticProperty(Type = FieldType.integer_type, Index = FieldIndexOption.not_analyzed)]
public string Id
{
get
{
if (_id == null || _id == Guid.Empty.ToString())
{
_id = Guid.NewGuid().ToString();
}
return _id;
}
set
{
_id = value;
}
}
[ElasticProperty(Type = FieldType.string_type, Index = FieldIndexOption.analyzed)]
public string Keywords { get; set; }
[ElasticProperty(Type = FieldType.string_type, Index = FieldIndexOption.analyzed)]
public string Content { get; set; }
}
我有索引和搜索返回文档,但突出显示始终为零
Client.Search<WebResource>(g => g.Query(k => k.Term(l => l.Content, searchText) || k.Term(l => l.Keywords, searchText)).Highlight(k => k.OnFields(p => p.OnField("Keywords"), p => p.OnField("Content")).FragmentSize(200)));
其中searchText是searchtext。 任何帮助表示赞赏。
亲切的问候JR
答案 0 :(得分:1)
原来这个问题与此有关 NEST (elasticsearch) Highlighting in multiple fields
我的解决方案是将其分解为更易读的形式
Action<HighlightFieldDescriptor<WebResource>> actWeb = (t) => t.OnField(g => g.Content);
Action<HighlightFieldDescriptor<WebResource>> actKey = (t) => t.OnField(g => g.Keywords);
Action<HighlightDescriptor<WebResource>> higDesc = t => t.OnFields(actWeb,actKey);
SearchDescriptor<WebResource> searchdesc = new SearchDescriptor<WebResource>();
searchdesc.Query( t => t.Term( k => k.Content,searchText) || t.Term( l =>l.Keywords,searchText));
searchdesc.Highlight(higDesc);
var resp = Client.Search(searchdesc);
结合这些字段的方式证明了这一点。