编辑:下面的解决方案仅在 ElasticSearch 2.x 上测试,而不是 ElasticSearch 5.x/6.x
可以在searchResults.Highlights(所有精彩片段)或IHit<T>.Highlights 中访问精彩片段。
这是否符合您想要实现的目标?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using Elasticsearch.Net.ConnectionPool;
using Nest;
namespace ESTester
{
internal class Program
{
private static void Main(string[] args)
{
const string indexName = "testindex";
var connectionSettings = new ConnectionSettings(new SingleNodeConnectionPool(new Uri("http://127.0.0.1:9200")));
var client = new ElasticClient(connectionSettings);
var existResponse = client.IndexExists(descriptor => descriptor.Index(indexName));
if (existResponse.Exists)
client.DeleteIndex(descriptor => descriptor.Index(indexName));
// Making sure the refresh interval is low, since it's boring to have to wait for things to catch up
client.PutTemplate("", descriptor => descriptor.Name("testindex").Template("testindex").Settings(objects => objects.Add("index.refresh_interval", "1s")));
client.CreateIndex(descriptor => descriptor.Index(indexName));
var docs = new List<Document>
{
new Document{Text = "This is the first document" },
new Document{Text = "This is the second document" },
new Document{Text = "This is the third document" }
};
var bulkDecsriptor = new BulkDescriptor().IndexMany(docs, (descriptor, document) => descriptor.Index(indexName));
client.Bulk(bulkDecsriptor);
// Making sure ES has indexed the documents
Thread.Sleep(TimeSpan.FromSeconds(2));
var searchDescriptor = new SearchDescriptor<Document>()
.Index(indexName)
.Query(q => q
.Match(m => m
.OnField(d => d.Text)
.Query("the second")))
.Highlight(h => h
.OnFields(f => f
.OnField(d => d.Text)
.PreTags("<em>")
.PostTags("</em>")));
var result = client.Search<Document>(searchDescriptor);
if (result.Hits.Any())
{
foreach (var hit in result.Hits)
{
Console.WriteLine("Found match: {0}", hit.Source.Text);
if (!hit.Highlights.Any()) continue;
foreach (var highlight in hit.Highlights.SelectMany(highlight => highlight.Value.Highlights))
{
Console.WriteLine("Found highlight: {0}", highlight);
}
}
}
Console.WriteLine("Press any key to exit!");
Console.ReadLine();
}
}
internal class Document
{
public string Text { get; set; }
}
}
为 cmets 编辑:
在此示例中,if(!hit.Highlights.Any()) continue; 没有真正的原因,除了安全之外,但如果您改为执行以下查询,则最终可能会得到没有高亮显示的命中:
var docs = new List<Document>
{
new Document{Text = "This is the first document", Number = 1 },
new Document{Text = "This is the second document", Number =500 },
new Document{Text = "This is the third document", Number = 1000 }
};
var searchDescriptor = new SearchDescriptor<Document>()
.Index(indexName)
.Query(q => q
.Bool(b => b
.Should(s1 => s1
.Match(m => m
.Query("second")
.OnField(f => f.Text)),
s2 => s2
.Range(r =>r
.OnField(f => f.Number)
.Greater(750)))
.MinimumShouldMatch(1)))
.Highlight(h => h
.OnFields(f => f
.OnField(d => d.Text)
.PreTags("<em>")
.PostTags("</em>")));
internal class Document
{
public string Text { get; set; }
public int Number { get; set; }
}
在这种情况下,您可能会在范围查询中获得成功,但不会有任何亮点。
对于第 2 点,对我来说,我只是在快速观察、对象浏览器和 VS 中的 IntelliSense 中探索了从搜索中返回的对象。