From d8f498226a2ea6a7b9e98d4503c42dabd9a79efd Mon Sep 17 00:00:00 2001 From: Martijn van Groningen Date: Wed, 12 Feb 2014 19:28:56 -0500 Subject: [PATCH] Fixed issue where highlighting in percolate existing doc api doesn't work (no highlight snippets) --- .../percolator/PercolatorService.java | 12 +++++- .../percolator/PercolatorTests.java | 37 +++++++++++++++++++ 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/elasticsearch/percolator/PercolatorService.java b/src/main/java/org/elasticsearch/percolator/PercolatorService.java index 5bb6851752a..c932070f8ef 100644 --- a/src/main/java/org/elasticsearch/percolator/PercolatorService.java +++ b/src/main/java/org/elasticsearch/percolator/PercolatorService.java @@ -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 { diff --git a/src/test/java/org/elasticsearch/percolator/PercolatorTests.java b/src/test/java/org/elasticsearch/percolator/PercolatorTests.java index 5b568b7771f..a0469b2e863 100644 --- a/src/test/java/org/elasticsearch/percolator/PercolatorTests.java +++ b/src/test/java/org/elasticsearch/percolator/PercolatorTests.java @@ -1519,6 +1519,43 @@ public class PercolatorTests extends ElasticsearchIntegrationTest { assertThat(matches[3].getHighlightFields().get("field1").fragments()[0].string(), equalTo("The quick brown fox jumps 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 jumps 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() { + @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 brown fox 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 lazy dog")); + assertThat(matches[2].getScore(), equalTo(5.5f)); + assertThat(matches[2].getHighlightFields().get("field1").fragments()[0].string(), equalTo("The quick brown fox jumps 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 dog")); + assertThat(matches[4].getScore(), equalTo(5.5f)); + assertThat(matches[4].getHighlightFields().get("field1").fragments()[0].string(), equalTo("The quick brown fox jumps over the lazy dog")); } @Test