Skip to content

Commit f00836d

Browse files
Fix highlighting for script_score query (#46507)
1 parent cb230e5 commit f00836d

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

modules/lang-painless/src/test/resources/rest-api-spec/test/painless/80_script_score.yml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,3 +496,53 @@
496496
- match: { hits.total: 2 }
497497
- match: { hits.hits.0._id : "2" }
498498
- match: { hits.hits.1._id : "1" }
499+
500+
501+
---
502+
"Script Score With Highlight":
503+
- skip:
504+
version: " - 7.4.0"
505+
reason: "highlight for script_score was introduced in 7.4.1"
506+
507+
- do:
508+
indices.create:
509+
index: test_index
510+
body:
511+
mappings:
512+
"properties":
513+
"company":
514+
"type": "text"
515+
"reputation":
516+
"type": "integer"
517+
- do:
518+
bulk:
519+
refresh: true
520+
body:
521+
- '{"index": {"_index": "test_index", "_id" : "1"}}'
522+
- '{"company": "ABC company", "reputation": 300}'
523+
- '{"index": {"_index": "test_index", "_id" : "2"}}'
524+
- '{"company": "ABC ABCD company", "reputation": 200}'
525+
- '{"index": {"_index": "test_index", "_id" : "3"}}'
526+
- '{"company": "ABCD company", "reputation": 100}'
527+
528+
- do:
529+
search:
530+
body:
531+
query:
532+
script_score:
533+
script:
534+
source: "doc['reputation'].value"
535+
query:
536+
bool:
537+
should:
538+
- match:
539+
company: ABC
540+
- match:
541+
company: ABCD
542+
highlight:
543+
fields:
544+
company: {}
545+
546+
- match: {hits.hits.0.highlight.company.0: "<em>ABC</em> company"}
547+
- match: {hits.hits.1.highlight.company.0: "<em>ABC</em> <em>ABCD</em> company"}
548+
- match: {hits.hits.2.highlight.company.0: "<em>ABCD</em> company"}

server/src/main/java/org/elasticsearch/common/lucene/search/function/ScriptScoreQuery.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,17 @@
2222
import org.apache.lucene.index.IndexReader;
2323
import org.apache.lucene.index.LeafReaderContext;
2424
import org.apache.lucene.index.Term;
25+
import org.apache.lucene.search.BooleanClause;
2526
import org.apache.lucene.search.DocIdSetIterator;
2627
import org.apache.lucene.search.Explanation;
2728
import org.apache.lucene.search.Query;
29+
import org.apache.lucene.search.QueryVisitor;
2830
import org.apache.lucene.search.Weight;
2931
import org.apache.lucene.search.IndexSearcher;
3032
import org.apache.lucene.search.ScoreMode;
3133
import org.apache.lucene.search.Scorer;
3234
import org.elasticsearch.ElasticsearchException;
3335

34-
3536
import java.io.IOException;
3637
import java.util.Objects;
3738
import java.util.Set;
@@ -137,6 +138,11 @@ public boolean isCacheable(LeafReaderContext ctx) {
137138
};
138139
}
139140

141+
@Override
142+
public void visit(QueryVisitor visitor) {
143+
// Highlighters must visit the child query to extract terms
144+
subQuery.visit(visitor.getSubVisitor(BooleanClause.Occur.MUST, this));
145+
}
140146

141147
@Override
142148
public String toString(String field) {

0 commit comments

Comments
 (0)