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 d94b252 commit e913b66
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 10 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 @@ -89,7 +89,7 @@ public void hitExecute(SearchContext context, HitContext hitContext) throws Elas
if (field.forceSource()) {
SourceFieldMapper sourceFieldMapper = context.mapperService().documentMapper(hitContext.hit().type()).sourceMapper();
if (!sourceFieldMapper.enabled()) {
throw new ElasticsearchIllegalArgumentException("source is forced for field [" + field.field() + "] but type [" + hitContext.hit().type() + "] has disabled _source");
throw new ElasticsearchIllegalArgumentException("source is forced for fields " + fieldNamesToHighlight + " but type [" + hitContext.hit().type() + "] has disabled _source");
}
}

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 @@ -524,7 +525,7 @@ public void testForceSourceWithSourceDisabled() throws Exception {
ensureGreen();

client().prepareIndex("test", "type1")
.setSource("field1", "The quick brown fox jumps over the lazy dog").get();
.setSource("field1", "The quick brown fox jumps over the lazy dog", "field2", "second field content").get();
refresh();

//works using stored field
Expand All @@ -540,30 +541,37 @@ public void testForceSourceWithSourceDisabled() throws Exception {
.get();
assertThat(searchResponse.getFailedShards(), equalTo(1));
assertThat(searchResponse.getShardFailures().length, equalTo(1));
assertThat(searchResponse.getShardFailures()[0].reason(), containsString("source is forced for field [field1] but type [type1] has disabled _source"));
assertThat(searchResponse.getShardFailures()[0].reason(), containsString("source is forced for fields [field1] but type [type1] has disabled _source"));

searchResponse = client().prepareSearch("test")
.setQuery(termQuery("field1", "quick"))
.addHighlightedField(new Field("field1").preTags("<xxx>").postTags("</xxx>").highlighterType("fvh").forceSource(true))
.get();
assertThat(searchResponse.getFailedShards(), equalTo(1));
assertThat(searchResponse.getShardFailures().length, equalTo(1));
assertThat(searchResponse.getShardFailures()[0].reason(), containsString("source is forced for field [field1] but type [type1] has disabled _source"));
assertThat(searchResponse.getShardFailures()[0].reason(), containsString("source is forced for fields [field1] but type [type1] has disabled _source"));

searchResponse = client().prepareSearch("test")
.setQuery(termQuery("field1", "quick"))
.addHighlightedField(new Field("field1").preTags("<xxx>").postTags("</xxx>").highlighterType("postings").forceSource(true))
.get();
assertThat(searchResponse.getFailedShards(), equalTo(1));
assertThat(searchResponse.getShardFailures().length, equalTo(1));
assertThat(searchResponse.getShardFailures()[0].reason(), containsString("source is forced for field [field1] but type [type1] has disabled _source"));
assertThat(searchResponse.getShardFailures()[0].reason(), containsString("source is forced for fields [field1] but type [type1] has disabled _source"));

SearchSourceBuilder searchSource = SearchSourceBuilder.searchSource().query(termQuery("field1", "quick"))
.highlight(highlight().forceSource(true).field("field1"));
searchResponse = client().search(Requests.searchRequest("test").source(searchSource)).get();
assertThat(searchResponse.getFailedShards(), equalTo(1));
assertThat(searchResponse.getShardFailures().length, equalTo(1));
assertThat(searchResponse.getShardFailures()[0].reason(), containsString("source is forced for field [field1] but type [type1] has disabled _source"));
assertThat(searchResponse.getShardFailures()[0].reason(), containsString("source is forced for fields [field1] but type [type1] has disabled _source"));

searchSource = SearchSourceBuilder.searchSource().query(termQuery("field1", "quick"))
.highlight(highlight().forceSource(true).field("field*"));
searchResponse = client().search(Requests.searchRequest("test").source(searchSource)).get();
assertThat(searchResponse.getFailedShards(), equalTo(1));
assertThat(searchResponse.getShardFailures().length, equalTo(1));
assertThat(searchResponse.getShardFailures()[0].reason(), matches("source is forced for fields \\[field\\d, field\\d\\] but type \\[type1\\] has disabled _source"));
}

@Test
Expand Down Expand Up @@ -1179,6 +1187,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 @@ -2227,6 +2248,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 e913b66

Please sign in to comment.