我刚开始在C#项目中使用elasticsearch。我想在结果页面中显示搜索的术语作为亮点,但不知道我如何处理它们的显示。
我的查询如下
result = client.Search<MyContentClass>(s => s
.Query(a =>
a.MatchPhrase(m => m.OnField("_all").Query(m_strSearchQuery))
.From(intFrom)
.Size(intSize)
.Highlight(h => h
.PreTags("<b style='color:orange'>")
.PostTags("</b>")
.OnFields(f => f
.OnField(e => e.Title)
.OnField(e => e.Content)
)
)
);
然后我将结果设置为变量,该变量是我的转发器的数据
var documents = result.Hits.Select(h => h.Source);
this.rptSearch.DataSource = documents;
this.rptSearch.DataBind();
this.rptSearch.Visible = true;
我没有看到我的结果中突出显示的任何术语,也没有看到高亮标签中包含的术语...
我做得怎么样?
亮点存储在Hightlights
对象的Hit
属性中。
这是您可能想要访问它们的方式:
result.Hits.Select(h => h.Highlights.Values.Select(v => string.Join(", ", v.Highlights)))
希望能帮助到你。