简体   繁体   中英

Highlight All fields Nest ElasticSearch

everybody
I'm working with Nest driver of elasticsearch for c#. In my project I don't have any document mapping so, if I want to highlight matching fields I should use this Json part in my query which highlights all fields:

"highlight":{
    "fields":{
      "*":{}
    }
  }

but I want to do it with nest. I use this code:

 client.Search<dynamic>(s => s
       .Index('my index name')
       .Type('my doc type name')
       .From(page*PageSize)
       .Size(PageSize)
       .Query(q => q
              .QueryString(qs => qs.Query('my query')))
       .Highlight(h => h
              .OnFields(f => f
                    .OnAll()
                    .PreTags("<b style='color:black'>")
                    .PostTags("</b>")
       )));

and it is not working for me, returned result contains hits, but doesn't contain any highlight :(

I guess you are looking for

client.Search<dynamic>(s => s
   .Index('my index name')
   .Type('my doc type name')
   .From(page*PageSize)
   .Size(PageSize)
   .Query(q => q
          .QueryString(qs => qs.Query('my query')))
   .Highlight(h => h
          .OnFields(f => f
                .OnField("*")
                .PreTags("<b style='color:black'>")
                .PostTags("</b>")
   )));

because .OnAll() means .OnField("_all") .

Take a look

UPDATE: object initializer syntax(NEST 5.x)

var searchRequest = new SearchRequest
{
    Query = ..
    Highlight = new Highlight
    {
        PostTags = new[] {"<a>"},
        PreTags = new[] {"</a>"},
        Fields = new FluentDictionary<Field, IHighlightField>().Add("*", new HighlightField())
    }
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM