Change The solution below is tested only on ElasticSearch 2.x, and not on ElasticSearch 5.x / 6.x
Highlights can be accessed in searchResults.Highlights (for all highlights) or in IHit<T>.Highlights for this hit.
Is this what you are trying to achieve?
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));
Edit Comments: In this example, there is no real reason for if(!hit.Highlights.Any()) continue; , except that it is safe, but if you made the following request, you could get hits without glare:
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; } }
In this case, you can get hit on the request range, but he would not have any glare.
For number 2, for me, I just looked at the object that I got from the search, both in Quick Watch, and in the object browser and through IntelliSense in VS.
source share