.NET Nest client with highlighting feature

I recently started using Elastic Search with his. NET client NEST. Lots of questions to ask.

I am currently blocked trying to highlight search results in an attachment box using elasticsearch-mapper-attachments plugin. Indexing works well, display seems to be correct, encoding and decoding works well too,

Once I tried searching for the keyword, ES seems to be able to find the documents it wants that contain the keyword, but as a result of highlighting, instead of showing decoded text, it shows nothing or is not encoded.

Read from another post handling the same functionality, the solution is to set store = yes and TermVector = TermVectorOption.WithPositionsOffsets.

So I tried to set it up in my C # class file with

[ElasticProperty(Name = "attach", Type = FieldType.Attachment, Store=true, TermVector = TermVectorOption.WithPositionsOffsets)] 
public string attach { get; set; } 


and the request is reversed (however, no highlighting result is returned)

"fields" : ["name","attach"], 
  "query" : { 
    "query_string" : { 
      "query" : "settings" 
  "highlight" : { 
    "fields" : { 
      "attach" : {} 


It seems that when creating a mapping for a type from a class, the attachment attribute was not set correctly: since when checking the local: 9200 / myindex / MyType / _mapping quite the anchor attribute does not have Store = true, TermVector = TermVectorOption.WithPositionsOffsets for it.

Do you have any idea? thank


source to share

1 answer

I have not been able to get this to work exclusively with the GitHub issue answer, although it did put me in the right direction. After some trial and error, here's what I came up with:

Doc class

public class Doc
    public string File { get; set; }
    // As an example for including additional fields:
    public string Title { get; set; } 


The attachment will be automatically created with all internal fields, so you don't need to create another class for the attachment. I think it would be possible to do something similar to the accepted answer, but you will have to manually add all the properties when you create the index.

Create and store pdf pointers

var index = "my-application";
var node = new Uri("http://localhost:9200");
var settings = new ConnectionSettings(node, defaultIndex: index);
var client = new ElasticClient(settings);

// Create the index, indicating that the contents of the internal "file" field 
// and the internal "title" field should be stored with position offsets to 
// allow highlighting.
client.CreateIndex(_index, c => c
    .AddMapping<Doc>(m => 
        m.Properties(ps => 
            ps.Attachment(a =>
                a.Name(o => o.File)
                    .FileField(t => t.Name("file")
                ).TitleField(t => t                  
        ).Properties(ps =>
            ps.String(s => 
                s.Name(o => o.Title)

string path = @"path\to\sample1.pdf";

var doc = new Doc()
    Title = "Anything you want",
    File = Convert.ToBase64String(System.IO.File.ReadAllBytes(path))




var queryString = "something in your pdf";
var searchResults = _client.Search<Doc>(s => 
           s.Fields("file", "title")
            .Query(quer => quer.QueryString(x => x.Query(queryString)))
            .Highlight(x => 
                    x.OnFields(y => 
                        y.OnField(f => f.File)




All Articles