Fixed field names returned when using wildcard expression to specify fields to highlight

Closes #5221
This commit is contained in:
javanna 2014-02-21 18:56:35 +01:00 committed by Luca Cavanna
parent d94b25278a
commit e913b6626f
4 changed files with 42 additions and 10 deletions

View File

@ -65,7 +65,7 @@ public class FastVectorHighlighter implements Highlighter {
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;
@ -156,7 +156,7 @@ public class FastVectorHighlighter implements Highlighter {
}
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();
@ -167,7 +167,7 @@ public class FastVectorHighlighter implements Highlighter {
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));
}
}

View File

@ -89,7 +89,7 @@ public class HighlightPhase extends AbstractComponent implements FetchSubPhase {
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");
}
}

View File

@ -58,7 +58,7 @@ public class PostingsHighlighter implements Highlighter {
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;

View File

@ -52,6 +52,7 @@ import static org.elasticsearch.index.query.QueryBuilders.*;
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.*;
/**
@ -524,7 +525,7 @@ public class HighlighterSearchTests extends ElasticsearchIntegrationTest {
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
@ -540,7 +541,7 @@ public class HighlighterSearchTests extends ElasticsearchIntegrationTest {
.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"))
@ -548,7 +549,7 @@ public class HighlighterSearchTests extends ElasticsearchIntegrationTest {
.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"))
@ -556,14 +557,21 @@ public class HighlighterSearchTests extends ElasticsearchIntegrationTest {
.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
@ -1179,6 +1187,19 @@ public class HighlighterSearchTests extends ElasticsearchIntegrationTest {
.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
@ -2227,6 +2248,17 @@ public class HighlighterSearchTests extends ElasticsearchIntegrationTest {
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