Fixed issue where highlighting in percolate existing doc api doesn't work (no highlight snippets)
This commit is contained in:
parent
b591d7fef7
commit
d8f498226a
|
@ -177,7 +177,7 @@ public class PercolatorService extends AbstractComponent {
|
|||
}
|
||||
|
||||
if (request.docSource() != null && request.docSource().length() != 0) {
|
||||
parsedDocument = parseFetchedDoc(request.docSource(), percolateIndexService, request.documentType());
|
||||
parsedDocument = parseFetchedDoc(context, request.docSource(), percolateIndexService, request.documentType());
|
||||
} else if (parsedDocument == null) {
|
||||
throw new ElasticsearchIllegalArgumentException("Nothing to percolate");
|
||||
}
|
||||
|
@ -381,7 +381,7 @@ public class PercolatorService extends AbstractComponent {
|
|||
}
|
||||
}
|
||||
|
||||
private ParsedDocument parseFetchedDoc(BytesReference fetchedDoc, IndexService documentIndexService, String type) {
|
||||
private ParsedDocument parseFetchedDoc(PercolateContext context, BytesReference fetchedDoc, IndexService documentIndexService, String type) {
|
||||
ParsedDocument doc = null;
|
||||
XContentParser parser = null;
|
||||
try {
|
||||
|
@ -389,6 +389,14 @@ public class PercolatorService extends AbstractComponent {
|
|||
MapperService mapperService = documentIndexService.mapperService();
|
||||
DocumentMapper docMapper = mapperService.documentMapperWithAutoCreate(type);
|
||||
doc = docMapper.parse(source(parser).type(type).flyweight(true));
|
||||
|
||||
if (context.highlight() != null) {
|
||||
// Enforce highlighting by source, because MemoryIndex doesn't support stored fields.
|
||||
for (SearchContextHighlight.Field field : context.highlight().fields()) {
|
||||
field.forceSource(true);
|
||||
}
|
||||
doc.setSource(fetchedDoc);
|
||||
}
|
||||
} catch (Throwable e) {
|
||||
throw new ElasticsearchParseException("failed to parse request", e);
|
||||
} finally {
|
||||
|
|
|
@ -1519,6 +1519,43 @@ public class PercolatorTests extends ElasticsearchIntegrationTest {
|
|||
assertThat(matches[3].getHighlightFields().get("field1").fragments()[0].string(), equalTo("The quick brown fox <em>jumps</em> over the lazy dog"));
|
||||
assertThat(matches[4].getScore(), equalTo(5.5f));
|
||||
assertThat(matches[4].getHighlightFields().get("field1").fragments()[0].string(), equalTo("The quick brown fox <em>jumps</em> over the lazy dog"));
|
||||
|
||||
// Highlighting an existing doc
|
||||
client.prepareIndex("test", "type", "1")
|
||||
.setSource(jsonBuilder().startObject().field("field1", "The quick brown fox jumps over the lazy dog").endObject())
|
||||
.get();
|
||||
|
||||
logger.info("--> Top percolate for doc with field1=The quick brown fox jumps over the lazy dog");
|
||||
response = client.preparePercolate()
|
||||
.setIndices("test").setDocumentType("type")
|
||||
.setSize(5)
|
||||
.setGetRequest(Requests.getRequest("test").type("type").id("1"))
|
||||
.setHighlightBuilder(new HighlightBuilder().field("field1"))
|
||||
.setPercolateQuery(functionScoreQuery(matchAllQuery()).add(new FactorBuilder().boostFactor(5.5f)))
|
||||
.setSortByScore(true)
|
||||
.execute().actionGet();
|
||||
assertMatchCount(response, 5l);
|
||||
assertThat(response.getMatches(), arrayWithSize(5));
|
||||
assertThat(convertFromTextArray(response.getMatches(), "test"), arrayContainingInAnyOrder("1", "2", "3", "4", "5"));
|
||||
|
||||
matches = response.getMatches();
|
||||
Arrays.sort(matches, new Comparator<PercolateResponse.Match>() {
|
||||
@Override
|
||||
public int compare(PercolateResponse.Match a, PercolateResponse.Match b) {
|
||||
return a.getId().compareTo(b.getId());
|
||||
}
|
||||
});
|
||||
|
||||
assertThat(matches[0].getScore(), equalTo(5.5f));
|
||||
assertThat(matches[0].getHighlightFields().get("field1").fragments()[0].string(), equalTo("The quick <em>brown</em> <em>fox</em> jumps over the lazy dog"));
|
||||
assertThat(matches[1].getScore(), equalTo(5.5f));
|
||||
assertThat(matches[1].getHighlightFields().get("field1").fragments()[0].string(), equalTo("The quick brown fox jumps over the <em>lazy</em> <em>dog</em>"));
|
||||
assertThat(matches[2].getScore(), equalTo(5.5f));
|
||||
assertThat(matches[2].getHighlightFields().get("field1").fragments()[0].string(), equalTo("The quick brown fox <em>jumps</em> over the lazy dog"));
|
||||
assertThat(matches[3].getScore(), equalTo(5.5f));
|
||||
assertThat(matches[3].getHighlightFields().get("field1").fragments()[0].string(), equalTo("The quick brown fox jumps over the lazy <em>dog</em>"));
|
||||
assertThat(matches[4].getScore(), equalTo(5.5f));
|
||||
assertThat(matches[4].getHighlightFields().get("field1").fragments()[0].string(), equalTo("The quick brown <em>fox</em> jumps over the lazy dog"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
Loading…
Reference in New Issue