use of org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder.Field in project elasticsearch by elastic.
the class HighlighterSearchIT method testHighlightingWithWildcardName.
public void testHighlightingWithWildcardName() throws IOException {
// test the kibana case with * as fieldname that will try highlight all fields including meta fields
XContentBuilder mappings = jsonBuilder();
mappings.startObject();
mappings.startObject("type").startObject("properties").startObject("text").field("type", "text").field("analyzer", "keyword").field("index_options", "offsets").field("term_vector", "with_positions_offsets").endObject().endObject().endObject();
mappings.endObject();
assertAcked(prepareCreate("test").addMapping("type", mappings));
client().prepareIndex("test", "type", "1").setSource(jsonBuilder().startObject().field("text", "text").endObject()).get();
refresh();
for (String type : ALL_TYPES) {
SearchResponse search = client().prepareSearch().setQuery(constantScoreQuery(matchQuery("text", "text"))).highlighter(new HighlightBuilder().field(new Field("*").highlighterType(type))).get();
assertHighlight(search, 0, "text", 0, equalTo("<em>text</em>"));
}
}
use of org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder.Field in project elasticsearch by elastic.
the class HighlighterSearchIT method testPostingsHighlighterNumberOfFragments.
public void testPostingsHighlighterNumberOfFragments() throws Exception {
assertAcked(prepareCreate("test").addMapping("type1", type1PostingsffsetsMapping()));
ensureGreen();
client().prepareIndex("test", "type1", "1").setSource("field1", "The quick brown fox jumps over the lazy dog. The lazy red fox jumps over the quick dog. " + "The quick brown dog jumps over the lazy fox.", "field2", "The quick brown fox jumps over the lazy dog. The lazy red fox jumps over the quick dog. " + "The quick brown dog jumps over the lazy fox.").get();
refresh();
for (String type : UNIFIED_AND_NULL) {
logger.info("--> highlighting and searching on field1");
SearchSourceBuilder source = searchSource().query(termQuery("field1", "fox")).highlighter(highlight().field(new Field("field1").numOfFragments(5).preTags("<field1>").postTags("</field1>").highlighterType(type)));
SearchResponse searchResponse = client().search(searchRequest("test").source(source)).actionGet();
assertHighlight(searchResponse, 0, "field1", 0, equalTo("The quick brown <field1>fox</field1> jumps over the lazy dog."));
assertHighlight(searchResponse, 0, "field1", 1, equalTo("The lazy red <field1>fox</field1> jumps over the quick dog."));
assertHighlight(searchResponse, 0, "field1", 2, 3, equalTo("The quick brown dog jumps over the lazy <field1>fox</field1>."));
client().prepareIndex("test", "type1", "2").setSource("field1", new String[] { "The quick brown fox jumps over the lazy dog. Second sentence not finished", "The lazy red fox jumps over the quick dog.", "The quick brown dog jumps over the lazy fox." }).get();
refresh();
source = searchSource().query(termQuery("field1", "fox")).highlighter(highlight().field(new Field("field1").numOfFragments(0).preTags("<field1>").postTags("</field1>").highlighterType(type)));
searchResponse = client().search(searchRequest("test").source(source)).actionGet();
assertHitCount(searchResponse, 2L);
for (SearchHit searchHit : searchResponse.getHits()) {
if ("1".equals(searchHit.getId())) {
assertHighlight(searchHit, "field1", 0, 1, equalTo("The quick brown <field1>fox</field1> jumps over the lazy dog. " + "The lazy red <field1>fox</field1> jumps over the quick dog. " + "The quick brown dog jumps over the lazy <field1>fox</field1>."));
} else if ("2".equals(searchHit.getId())) {
assertHighlight(searchHit, "field1", 0, 3, equalTo("The quick brown <field1>fox</field1> jumps over the lazy dog. Second sentence not finished"));
assertHighlight(searchHit, "field1", 1, 3, equalTo("The lazy red <field1>fox</field1> jumps over the quick dog."));
assertHighlight(searchHit, "field1", 2, 3, equalTo("The quick brown dog jumps over the lazy <field1>fox</field1>."));
} else {
fail("Only hits with id 1 and 2 are returned");
}
}
}
}
use of org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder.Field in project elasticsearch by elastic.
the class HighlighterSearchIT method testHighlightNoMatchSize.
public void testHighlightNoMatchSize() throws IOException {
assertAcked(prepareCreate("test").addMapping("type1", "text", "type=text," + randomStoreField() + "term_vector=with_positions_offsets,index_options=offsets"));
ensureGreen();
String text = "I am pretty long so some of me should get cut off. Second sentence";
index("test", "type1", "1", "text", text);
refresh();
// When you don't set noMatchSize you don't get any results if there isn't anything to highlight.
HighlightBuilder.Field field = new HighlightBuilder.Field("text").fragmentSize(21).numOfFragments(1).highlighterType("plain");
SearchResponse response = client().prepareSearch("test").highlighter(new HighlightBuilder().field(field)).get();
assertNotHighlighted(response, 0, "text");
field.highlighterType("fvh");
response = client().prepareSearch("test").highlighter(new HighlightBuilder().field(field)).get();
assertNotHighlighted(response, 0, "text");
field.highlighterType("postings");
response = client().prepareSearch("test").highlighter(new HighlightBuilder().field(field)).get();
assertNotHighlighted(response, 0, "text");
field.highlighterType("unified");
response = client().prepareSearch("test").highlighter(new HighlightBuilder().field(field)).get();
assertNotHighlighted(response, 0, "text");
// When noMatchSize is set to 0 you also shouldn't get any
field.highlighterType("plain").noMatchSize(0);
response = client().prepareSearch("test").highlighter(new HighlightBuilder().field(field)).get();
assertNotHighlighted(response, 0, "text");
field.highlighterType("fvh");
response = client().prepareSearch("test").highlighter(new HighlightBuilder().field(field)).get();
assertNotHighlighted(response, 0, "text");
field.highlighterType("postings");
response = client().prepareSearch("test").highlighter(new HighlightBuilder().field(field)).get();
assertNotHighlighted(response, 0, "text");
field.highlighterType("unified");
response = client().prepareSearch("test").highlighter(new HighlightBuilder().field(field)).get();
assertNotHighlighted(response, 0, "text");
// When noMatchSize is between 0 and the size of the string
field.highlighterType("plain").noMatchSize(21);
response = client().prepareSearch("test").highlighter(new HighlightBuilder().field(field)).get();
assertHighlight(response, 0, "text", 0, 1, equalTo("I am pretty long so"));
// The FVH also works but the fragment is longer than the plain highlighter because of boundary_max_scan
field.highlighterType("fvh");
response = client().prepareSearch("test").highlighter(new HighlightBuilder().field(field)).get();
assertHighlight(response, 0, "text", 0, 1, equalTo("I am pretty long so some"));
// Unified hl also works but the fragment is longer than the plain highlighter because of the boundary is the word
field.highlighterType("unified");
response = client().prepareSearch("test").highlighter(new HighlightBuilder().field(field)).get();
assertHighlight(response, 0, "text", 0, 1, equalTo("I am pretty long so some"));
// Postings hl also works but the fragment is the whole first sentence (size ignored)
field.highlighterType("postings");
response = client().prepareSearch("test").highlighter(new HighlightBuilder().field(field)).get();
assertHighlight(response, 0, "text", 0, 1, equalTo("I am pretty long so some of me should get cut off."));
// We can also ask for a fragment longer than the input string and get the whole string
field.highlighterType("plain").noMatchSize(text.length() * 2);
response = client().prepareSearch("test").highlighter(new HighlightBuilder().field(field)).get();
assertHighlight(response, 0, "text", 0, 1, equalTo(text));
field.highlighterType("fvh");
response = client().prepareSearch("test").highlighter(new HighlightBuilder().field(field)).get();
assertHighlight(response, 0, "text", 0, 1, equalTo(text));
field.highlighterType("unified");
response = client().prepareSearch("test").highlighter(new HighlightBuilder().field(field)).get();
assertHighlight(response, 0, "text", 0, 1, equalTo(text));
//no difference using postings hl as the noMatchSize is ignored (just needs to be greater than 0)
field.highlighterType("postings");
response = client().prepareSearch("test").highlighter(new HighlightBuilder().field(field)).get();
assertHighlight(response, 0, "text", 0, 1, equalTo("I am pretty long so some of me should get cut off."));
// We can also ask for a fragment exactly the size of the input field and get the whole field
field.highlighterType("plain").noMatchSize(text.length());
response = client().prepareSearch("test").highlighter(new HighlightBuilder().field(field)).get();
assertHighlight(response, 0, "text", 0, 1, equalTo(text));
field.highlighterType("fvh");
response = client().prepareSearch("test").highlighter(new HighlightBuilder().field(field)).get();
assertHighlight(response, 0, "text", 0, 1, equalTo(text));
// unified hl returns the first sentence as the noMatchSize does not cross sentence boundary.
field.highlighterType("unified");
response = client().prepareSearch("test").highlighter(new HighlightBuilder().field(field)).get();
assertHighlight(response, 0, "text", 0, 1, equalTo(text));
//no difference using postings hl as the noMatchSize is ignored (just needs to be greater than 0)
field.highlighterType("postings");
response = client().prepareSearch("test").highlighter(new HighlightBuilder().field(field)).get();
assertHighlight(response, 0, "text", 0, 1, equalTo("I am pretty long so some of me should get cut off."));
// You can set noMatchSize globally in the highlighter as well
field.highlighterType("plain").noMatchSize(null);
response = client().prepareSearch("test").highlighter(new HighlightBuilder().field(field).noMatchSize(21)).get();
assertHighlight(response, 0, "text", 0, 1, equalTo("I am pretty long so"));
field.highlighterType("fvh");
response = client().prepareSearch("test").highlighter(new HighlightBuilder().field(field).noMatchSize(21)).get();
assertHighlight(response, 0, "text", 0, 1, equalTo("I am pretty long so some"));
field.highlighterType("unified");
response = client().prepareSearch("test").highlighter(new HighlightBuilder().field(field).noMatchSize(21)).get();
assertHighlight(response, 0, "text", 0, 1, equalTo("I am pretty long so some"));
field.highlighterType("postings");
response = client().prepareSearch("test").highlighter(new HighlightBuilder().field(field).noMatchSize(21)).get();
assertHighlight(response, 0, "text", 0, 1, equalTo("I am pretty long so some of me should get cut off."));
// We don't break if noMatchSize is less than zero though
field.highlighterType("plain").noMatchSize(randomIntBetween(Integer.MIN_VALUE, -1));
response = client().prepareSearch("test").highlighter(new HighlightBuilder().field(field)).get();
assertNotHighlighted(response, 0, "text");
field.highlighterType("fvh");
response = client().prepareSearch("test").highlighter(new HighlightBuilder().field(field)).get();
assertNotHighlighted(response, 0, "text");
field.highlighterType("postings");
response = client().prepareSearch("test").highlighter(new HighlightBuilder().field(field)).get();
assertNotHighlighted(response, 0, "text");
field.highlighterType("unified");
response = client().prepareSearch("test").highlighter(new HighlightBuilder().field(field)).get();
assertNotHighlighted(response, 0, "text");
}
use of org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder.Field in project elasticsearch by elastic.
the class HighlighterSearchIT method testGlobalHighlightingSettingsOverriddenAtFieldLevel.
public void testGlobalHighlightingSettingsOverriddenAtFieldLevel() {
createIndex("test");
ensureGreen();
client().prepareIndex("test", "type1").setSource("field1", new String[] { "this is a test", "this is the second test" }, "field2", new String[] { "this is another test", "yet another test" }).get();
refresh();
logger.info("--> highlighting and searching on field1 and field2 produces different tags");
SearchSourceBuilder source = searchSource().query(termQuery("field1", "test")).highlighter(highlight().order("score").preTags("<global>").postTags("</global>").fragmentSize(1).numOfFragments(1).field(new HighlightBuilder.Field("field1").numOfFragments(2)).field(new HighlightBuilder.Field("field2").preTags("<field2>").postTags("</field2>").fragmentSize(50).requireFieldMatch(false)));
SearchResponse searchResponse = client().prepareSearch("test").setSource(source).get();
assertHighlight(searchResponse, 0, "field1", 0, 2, equalTo(" <global>test</global>"));
assertHighlight(searchResponse, 0, "field1", 1, 2, equalTo(" <global>test</global>"));
assertHighlight(searchResponse, 0, "field2", 0, 1, equalTo("this is another <field2>test</field2>"));
}
use of org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder.Field in project elasticsearch by elastic.
the class HighlightBuilderTests method randomHighlighterBuilder.
/**
* create random highlight builder that is put under test
*/
public static HighlightBuilder randomHighlighterBuilder() {
HighlightBuilder testHighlighter = new HighlightBuilder();
setRandomCommonOptions(testHighlighter);
testHighlighter.useExplicitFieldOrder(randomBoolean());
if (randomBoolean()) {
testHighlighter.encoder(randomFrom(Arrays.asList(new String[] { "default", "html" })));
}
int numberOfFields = randomIntBetween(1, 5);
for (int i = 0; i < numberOfFields; i++) {
Field field = new Field(i + "_" + randomAsciiOfLengthBetween(1, 10));
setRandomCommonOptions(field);
if (randomBoolean()) {
field.fragmentOffset(randomIntBetween(1, 100));
}
if (randomBoolean()) {
field.matchedFields(randomStringArray(0, 4));
}
testHighlighter.field(field);
}
return testHighlighter;
}
Aggregations