elastic / elasticsearch

Free and Open, Distributed, RESTful Search Engine
https://www.elastic.co/products/elasticsearch
Other
69.53k stars 24.61k forks source link

Highlight does not work properly with ScriptScore, version 7.8 #59704

Open Jeet0027 opened 4 years ago

Jeet0027 commented 4 years ago

Elasticsearch version (7.8):

OS version (Windows 10):

Along with ScriptScore function, Highlight function does not work properly. If we search two words only first word gets higlighted or last word doesn't display highlighted:

Eg. Doc content- this is a boy On searching - this is Output content shows only 'this' as highlighted

` var searchResponse = await elasticClient.SearchAsync(body => body

   .Index(Settings.Index + "-*")
         .Query(qx =>

                 qx.ScriptScore(fx => fx
           .Query(qt =>

            qt.Bool
          (bb => bb.Must(q =>
             q.MatchPhrasePrefix(m => m.Field(f => f.Content).Query(req.SearchText)))   

           ) 

) .Script(w => w.Source("String gmtString = params.searchdate;ZonedDateTime zdt = ZonedDateTime.parse(gmtString,DateTimeFormatter.RFC_1123_DATE_TIME);ZonedDateTime zdt2 =ZonedDateTime.of(doc['date'].value.year,doc['date'].value.monthOfYear,doc['date'].value.dayOfMonth,doc['date'].value.hourOfDay,doc['date'].value.minuteOfHour,doc['date'].value.secondOfMinute, 0, ZoneId.of('Z')); if (zdt2.toInstant().toEpochMilli()==zdt.toInstant().toEpochMilli()){return 100;}else{return _score;} ").Params(par)))

   ).Highlight(h => h.Fields(
         fs => fs
.Field(p => p.Content)
   .NumberOfFragments(0) ).NumberOfFragments(0).Type(HighlighterType.Unified)
).PreTags("<b>").PostTags("</b>")));

`

Please suggest any solution

Jeet0027 commented 4 years ago

@mayya-sharipova, @jimczi could you please suggest

Jeet0027 commented 4 years ago

I have fixed this issue using HighlightQuery method, but this is an alternate solution

elasticmachine commented 4 years ago

Pinging @elastic/es-search (:Search/Highlighting)

jimczi commented 4 years ago

Can you share the mapping of your fields ? We have an open issue regarding the highlighting of prefix queries when using the index_prefixes option or search_as_you_type.

Jeet0027 commented 4 years ago

ding the highlighting of prefix querie

Sorry for the late reply! var indexMappingResponse = await elasticClient.Indices.CreateAsync(Settings.Index + "-text", c => c .Settings(s => s.NumberOfShards(2) .Analysis(a => a.Analyzers(an => an.Custom("rebuilt_whitespace", ca => ca.Tokenizer("whitespace") .Filters("lowercase")) ))) .Map<Document>(mp => mp .AutoMap() .Properties(ps => ps .Text(t => t .Name(n => n.Content).TermVector(TermVectorOption.WithPositionsOffsets)) .Text(t => t.Name(n => n.Tags).Analyzer("rebuilt_whitespace")) .Date(x => x.Name(n => n.date).Format("yyyy-MM-dd HH:mm:ss")) ))); Field 'Content' contains the text input

elasticsearchmachine commented 2 months ago

Pinging @elastic/es-search-relevance (Team:Search Relevance)