Skip to content

Commit

Permalink
Fixed per-field forceSource highlighting option
Browse files Browse the repository at this point in the history
Closes #5220
  • Loading branch information
javanna committed Feb 21, 2014
1 parent 2ddcdc4 commit 1f9889b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 23 deletions.
Expand Up @@ -353,7 +353,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
builder.field("options", field.options);
}
if (field.forceSource != null) {
builder.field("force_source", forceSource);
builder.field("force_source", field.forceSource);
}

builder.endObject();
Expand Down
Expand Up @@ -24,6 +24,7 @@
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.search.*;
import org.elasticsearch.client.Requests;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.ImmutableSettings.Builder;
import org.elasticsearch.common.xcontent.XContentBuilder;
Expand Down Expand Up @@ -509,51 +510,60 @@ public void testHighlightingOnWildcardFields() throws Exception {
}

@Test
public void testPlainHighlighterForceSource() throws Exception {
prepareCreate("test")
.addMapping("type1", "field1", "type=string,store=yes,term_vector=with_positions_offsets,index_options=offsets")
.get();
public void testForceSourceWithSourceDisabled() throws Exception {

assertAcked(client().admin().indices().prepareCreate("test")
.addMapping("type1", jsonBuilder().startObject().startObject("type1")
//just to make sure that we hit the stored fields rather than the _source
.startObject("_source").field("enabled", false).endObject()
.startObject("properties")
.startObject("field1").field("type", "string").field("store", "yes").field("index_options", "offsets")
.field("term_vector", "with_positions_offsets").endObject()
.endObject().endObject().endObject()));

ensureGreen();

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

//works using stored field
SearchResponse searchResponse = client().prepareSearch("test")
.setQuery(termQuery("field1", "quick"))
.addHighlightedField(new Field("field1").preTags("<xxx>").postTags("</xxx>").highlighterType("fvh").forceSource(true))
.addHighlightedField(new Field("field1").preTags("<xxx>").postTags("</xxx>"))
.get();
assertHighlight(searchResponse, 0, "field1", 0, 1, equalTo("The <xxx>quick</xxx> brown fox jumps over the lazy dog"));

searchResponse = client().prepareSearch("test")
.setQuery(termQuery("field1", "quick"))
.addHighlightedField(new Field("field1").preTags("<xxx>").postTags("</xxx>").highlighterType("plain").forceSource(true))
.get();
assertHighlight(searchResponse, 0, "field1", 0, 1, equalTo("The <xxx>quick</xxx> brown fox jumps over the lazy dog"));

searchResponse = client().prepareSearch("test")
.setQuery(termQuery("field1", "quick"))
.addHighlightedField(new Field("field1").preTags("<xxx>").postTags("</xxx>").highlighterType("postings").forceSource(true))
.get();
assertHighlight(searchResponse, 0, "field1", 0, 1, equalTo("The <xxx>quick</xxx> brown fox jumps over the lazy dog"));
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"));

searchResponse = client().prepareSearch("test")
.setQuery(termQuery("field1", "quick"))
.addHighlightedField(new Field("field1").preTags("<xxx>").postTags("</xxx>").highlighterType("fvh").forceSource(false))
.get();
assertHighlight(searchResponse, 0, "field1", 0, 1, equalTo("The <xxx>quick</xxx> brown fox jumps over the lazy dog"));

searchResponse = client().prepareSearch("test")
.setQuery(termQuery("field1", "quick"))
.addHighlightedField(new Field("field1").preTags("<xxx>").postTags("</xxx>").highlighterType("plain").forceSource(false))
.addHighlightedField(new Field("field1").preTags("<xxx>").postTags("</xxx>").highlighterType("fvh").forceSource(true))
.get();
assertHighlight(searchResponse, 0, "field1", 0, 1, equalTo("The <xxx>quick</xxx> brown fox jumps over the lazy dog"));
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"));

searchResponse = client().prepareSearch("test")
.setQuery(termQuery("field1", "quick"))
.addHighlightedField(new Field("field1").preTags("<xxx>").postTags("</xxx>").highlighterType("postings").forceSource(false))
.addHighlightedField(new Field("field1").preTags("<xxx>").postTags("</xxx>").highlighterType("postings").forceSource(true))
.get();
assertHighlight(searchResponse, 0, "field1", 0, 1, equalTo("The <xxx>quick</xxx> brown fox jumps over the lazy dog"));
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"));

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"));
}

@Test
Expand Down

0 comments on commit 1f9889b

Please sign in to comment.