简体   繁体   中英

C# NEST ElasticSearch search and highlight all fields in all types

Im new to elastic-stack and want to implement global search on my indexed models and this is what I came up with at the moment:

        var response = _client.Search<IndexBase>(s => s
                                                     .Type(Types.Type(typeof(A), typeof(B)))
                                                     .Query(qry => qry
                                                                .Bool(b => b
                                                                          .Must(m => m
                                                                                    .QueryString(qs => qs
                                                                                                     .DefaultField("_all")
                                                                                                     .Query(request.Query)))))
                                                     .Highlight(h =>
                                                                    h.Fields(f => f.Field("_all")))

But no highlights is showing up in my hits for documents. What Im doing wrong? How I can retrieve [index, count] pairs for highlighting?

Try replacing "_all" in the highlighter with *

     var response = _client.Search<IndexBase>(s => s
                                                 .Type(Types.Type(typeof(A), typeof(B)))
                                                 .Query(qry => qry
                                                            .Bool(b => b
                                                                      .Must(m => m
                                                                                .QueryString(qs => qs
                                                                                                 .DefaultField("_all")
                                                                                                 .Query(request.Query)))))
                                                 .Highlight(h =>
                                                                h.Fields(f => f.Field("*")))

You are not indexing the fields properly.

In order to perform highlighting, the actual content of the field is required. If the field in question is stored (has store set to true in the mapping) it will be used, otherwise, the actual _source will be loaded and the relevant field will be extracted from it.

The _all field cannot be extracted from _source, so it can only be used for highlighting if it mapped to have store set to true. Source

Also for any bigger set of data this can cause a performance drop so it's always better to set the search fields in the proper way and know what you are trying to highlight.

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