Skip to content

Commit

Permalink
Fixed field names returned when using wildcard expression to specify …
Browse files Browse the repository at this point in the history
…fields to highlight

Closes #5221
  • Loading branch information
javanna committed Feb 21, 2014
1 parent 6ab17d8 commit 1b47507
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
Expand Up @@ -65,7 +65,7 @@ public HighlightField highlight(HighlighterContext highlighterContext) {
FieldMapper<?> mapper = highlighterContext.mapper;

if (!(mapper.fieldType().storeTermVectors() && mapper.fieldType().storeTermVectorOffsets() && mapper.fieldType().storeTermVectorPositions())) {
throw new ElasticSearchIllegalArgumentException("the field [" + field.field() + "] should be indexed with term vector with position offsets to be used with fast vector highlighter");
throw new ElasticSearchIllegalArgumentException("the field [" + highlighterContext.fieldName + "] should be indexed with term vector with position offsets to be used with fast vector highlighter");
}

Encoder encoder = field.encoder().equals("html") ? HighlightUtils.Encoders.HTML : HighlightUtils.Encoders.DEFAULT;
Expand Down Expand Up @@ -156,7 +156,7 @@ public HighlightField highlight(HighlighterContext highlighterContext) {
}

if (fragments != null && fragments.length > 0) {
return new HighlightField(field.field(), StringText.convertFromStringArray(fragments));
return new HighlightField(highlighterContext.fieldName, StringText.convertFromStringArray(fragments));
}

int noMatchSize = highlighterContext.field.noMatchSize();
Expand All @@ -167,7 +167,7 @@ public HighlightField highlight(HighlighterContext highlighterContext) {
fragments = entry.fragmentsBuilder.createFragments(hitContext.reader(), hitContext.docId(), mapper.names().indexName(),
fieldFragList, 1, field.preTags(), field.postTags(), encoder);
if (fragments != null && fragments.length > 0) {
return new HighlightField(field.field(), StringText.convertFromStringArray(fragments));
return new HighlightField(highlighterContext.fieldName, StringText.convertFromStringArray(fragments));
}
}

Expand Down
Expand Up @@ -58,7 +58,7 @@ public HighlightField highlight(HighlighterContext highlighterContext) {
FieldMapper<?> fieldMapper = highlighterContext.mapper;
SearchContextHighlight.Field field = highlighterContext.field;
if (fieldMapper.fieldType().indexOptions() != FieldInfo.IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS) {
throw new ElasticSearchIllegalArgumentException("the field [" + field.field() + "] should be indexed with positions and offsets in the postings list to be used with postings highlighter");
throw new ElasticSearchIllegalArgumentException("the field [" + highlighterContext.fieldName + "] should be indexed with positions and offsets in the postings list to be used with postings highlighter");
}

SearchContext context = highlighterContext.context;
Expand Down
Expand Up @@ -52,6 +52,7 @@
import static org.elasticsearch.search.builder.SearchSourceBuilder.highlight;
import static org.elasticsearch.search.builder.SearchSourceBuilder.searchSource;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.*;
import static org.elasticsearch.test.hamcrest.RegexMatcher.matches;
import static org.hamcrest.Matchers.*;

/**
Expand Down Expand Up @@ -1122,6 +1123,19 @@ public void testFastVectorHighlighterShouldFailIfNoTermVectors() throws Exceptio
.setHighlighterType("fast-vector-highlighter")
.execute().actionGet();
assertThat(search.getFailedShards(), equalTo(2));
for (ShardSearchFailure shardSearchFailure : search.getShardFailures()) {
assertThat(shardSearchFailure.reason(), containsString("the field [title] should be indexed with term vector with position offsets to be used with fast vector highlighter"));
}

search = client().prepareSearch()
.setQuery(matchPhraseQuery("title", "this is a test"))
.addHighlightedField("tit*", 50, 1, 10)
.setHighlighterType("fast-vector-highlighter")
.execute().actionGet();
assertThat(search.getFailedShards(), equalTo(2));
for (ShardSearchFailure shardSearchFailure : search.getShardFailures()) {
assertThat(shardSearchFailure.reason(), containsString("the field [title] should be indexed with term vector with position offsets to be used with fast vector highlighter"));
}
}

@Test
Expand Down Expand Up @@ -2170,6 +2184,17 @@ public void testPostingsHighlighterShouldFailIfNoOffsets() throws Exception {
for (ShardSearchFailure shardSearchFailure : search.getShardFailures()) {
assertThat(shardSearchFailure.reason(), containsString("the field [title] should be indexed with positions and offsets in the postings list to be used with postings highlighter"));
}

search = client().prepareSearch()
.setQuery(matchQuery("title", "this is a test"))
.addHighlightedField("tit*")
.setHighlighterType("postings")
.get();

assertThat(search.getFailedShards(), equalTo(2));
for (ShardSearchFailure shardSearchFailure : search.getShardFailures()) {
assertThat(shardSearchFailure.reason(), containsString("the field [title] should be indexed with positions and offsets in the postings list to be used with postings highlighter"));
}
}

@Test
Expand Down

0 comments on commit 1b47507

Please sign in to comment.