use of org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder.Field in project elasticsearch by elastic.
the class HighlighterSearchIT method testForceSourceWithSourceDisabled.
public void testForceSourceWithSourceDisabled() throws Exception {
assertAcked(prepareCreate("test").addMapping("type1", jsonBuilder().startObject().startObject("type1").startObject("_source").field("enabled", false).endObject().startObject("properties").startObject("field1").field("type", "text").field("store", true).field("index_options", "offsets").field("term_vector", "with_positions_offsets").endObject().startObject("field2").field("type", "text").endObject().endObject().endObject().endObject()));
ensureGreen();
client().prepareIndex("test", "type1").setSource("field1", "The quick brown fox jumps over the lazy dog", "field2", "second field content").get();
refresh();
for (String type : UNIFIED_AND_NULL) {
//works using stored field
SearchResponse searchResponse = client().prepareSearch("test").setQuery(termQuery("field1", "quick")).highlighter(new HighlightBuilder().field(new Field("field1").preTags("<xxx>").postTags("</xxx>").highlighterType(type))).get();
assertHighlight(searchResponse, 0, "field1", 0, 1, equalTo("The <xxx>quick</xxx> brown fox jumps over the lazy dog"));
assertFailures(client().prepareSearch("test").setQuery(termQuery("field1", "quick")).highlighter(new HighlightBuilder().field(new Field("field1").preTags("<xxx>").postTags("</xxx>").highlighterType(type).forceSource(true))), RestStatus.BAD_REQUEST, containsString("source is forced for fields [field1] but type [type1] has disabled _source"));
SearchSourceBuilder searchSource = SearchSourceBuilder.searchSource().query(termQuery("field1", "quick")).highlighter(highlight().forceSource(true).field("field1").highlighterType(type));
assertFailures(client().prepareSearch("test").setSource(searchSource), RestStatus.BAD_REQUEST, containsString("source is forced for fields [field1] but type [type1] has disabled _source"));
searchSource = SearchSourceBuilder.searchSource().query(termQuery("field1", "quick")).highlighter(highlight().forceSource(true).field("field*").highlighterType(type));
assertFailures(client().prepareSearch("test").setSource(searchSource), RestStatus.BAD_REQUEST, matches("source is forced for fields \\[field\\d, field\\d\\] but type \\[type1\\] has disabled _source"));
}
}
use of org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder.Field in project elasticsearch by elastic.
the class HighlightBuilderTests method testParsingEmptyStructure.
/**
* test parsing empty highlight or empty fields blocks
*/
public void testParsingEmptyStructure() throws IOException {
String highlightElement = "{ }";
XContentParser parser = createParser(JsonXContent.jsonXContent, highlightElement);
QueryParseContext context = new QueryParseContext(parser);
HighlightBuilder highlightBuilder = HighlightBuilder.fromXContent(context);
assertEquals("expected plain HighlightBuilder", new HighlightBuilder(), highlightBuilder);
highlightElement = "{ \"fields\" : { } }";
parser = createParser(JsonXContent.jsonXContent, highlightElement);
context = new QueryParseContext(parser);
highlightBuilder = HighlightBuilder.fromXContent(context);
assertEquals("defining no field should return plain HighlightBuilder", new HighlightBuilder(), highlightBuilder);
highlightElement = "{ \"fields\" : { \"foo\" : { } } }";
parser = createParser(JsonXContent.jsonXContent, highlightElement);
context = new QueryParseContext(parser);
highlightBuilder = HighlightBuilder.fromXContent(context);
assertEquals("expected HighlightBuilder with field", new HighlightBuilder().field(new Field("foo")), highlightBuilder);
}
use of org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder.Field in project elasticsearch by elastic.
the class HighlightBuilderTests method mutate.
/**
* mutate the given highlighter builder so the returned one is different in one aspect
*/
private static HighlightBuilder mutate(HighlightBuilder original) throws IOException {
HighlightBuilder mutation = serializedCopy(original);
if (randomBoolean()) {
mutateCommonOptions(mutation);
} else {
switch(randomIntBetween(0, 2)) {
// change settings that only exists on top level
case 0:
mutation.useExplicitFieldOrder(!original.useExplicitFieldOrder());
break;
case 1:
mutation.encoder(original.encoder() + randomAsciiOfLength(2));
break;
case 2:
if (randomBoolean()) {
// add another field
mutation.field(new Field(randomAsciiOfLength(10)));
} else {
// change existing fields
List<Field> originalFields = original.fields();
Field fieldToChange = originalFields.get(randomInt(originalFields.size() - 1));
if (randomBoolean()) {
fieldToChange.fragmentOffset(randomIntBetween(101, 200));
} else {
fieldToChange.matchedFields(randomStringArray(5, 10));
}
}
break;
}
}
return mutation;
}
Aggregations