Fix highlighting for script_score query (#46507)

This commit is contained in:
Mayya Sharipova 2019-09-10 08:26:20 -04:00
parent 690164d0be
commit 2c5f9b558b
2 changed files with 57 additions and 1 deletions

View File

@ -501,3 +501,53 @@ setup:
- match: { hits.total: 2 }
- match: { hits.hits.0._id : "2" }
- match: { hits.hits.1._id : "1" }
---
"Script Score With Highlight":
- skip:
version: " - 7.4.0"
reason: "highlight for script_score was introduced in 7.4.1"
- do:
indices.create:
index: test_index
body:
mappings:
"properties":
"company":
"type": "text"
"reputation":
"type": "integer"
- do:
bulk:
refresh: true
body:
- '{"index": {"_index": "test_index", "_id" : "1"}}'
- '{"company": "ABC company", "reputation": 300}'
- '{"index": {"_index": "test_index", "_id" : "2"}}'
- '{"company": "ABC ABCD company", "reputation": 200}'
- '{"index": {"_index": "test_index", "_id" : "3"}}'
- '{"company": "ABCD company", "reputation": 100}'
- do:
search:
body:
query:
script_score:
script:
source: "doc['reputation'].value"
query:
bool:
should:
- match:
company: ABC
- match:
company: ABCD
highlight:
fields:
company: {}
- match: {hits.hits.0.highlight.company.0: "<em>ABC</em> company"}
- match: {hits.hits.1.highlight.company.0: "<em>ABC</em> <em>ABCD</em> company"}
- match: {hits.hits.2.highlight.company.0: "<em>ABCD</em> company"}

View File

@ -22,16 +22,17 @@ package org.elasticsearch.common.lucene.search.function;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.DocIdSetIterator;
import org.apache.lucene.search.Explanation;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.QueryVisitor;
import org.apache.lucene.search.Weight;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.ScoreMode;
import org.apache.lucene.search.Scorer;
import org.elasticsearch.ElasticsearchException;
import java.io.IOException;
import java.util.Objects;
import java.util.Set;
@ -137,6 +138,11 @@ public class ScriptScoreQuery extends Query {
};
}
@Override
public void visit(QueryVisitor visitor) {
// Highlighters must visit the child query to extract terms
subQuery.visit(visitor.getSubVisitor(BooleanClause.Occur.MUST, this));
}
@Override
public String toString(String field) {