Make (HighlightBuilder|SearchContextHighlight).Field consistent
Update HighlightBuilder.Field API, it should allow for the same API as SearchConstextHighlight.Field. In other words, what is possible to setup using DSL in highlighting at the field level is also possible via the Java API. Closes #3435
This commit is contained in:
parent
5cf429d144
commit
f2168d32c1
|
@ -202,7 +202,7 @@ public class HighlightBuilder implements ToXContent {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Allows to set custom options for custom highlighters
|
* Allows to set custom options for custom highlighters.
|
||||||
*/
|
*/
|
||||||
public HighlightBuilder options(Map<String, Object> options) {
|
public HighlightBuilder options(Map<String, Object> options) {
|
||||||
this.options = options;
|
this.options = options;
|
||||||
|
@ -243,6 +243,12 @@ public class HighlightBuilder implements ToXContent {
|
||||||
builder.startObject("fields");
|
builder.startObject("fields");
|
||||||
for (Field field : fields) {
|
for (Field field : fields) {
|
||||||
builder.startObject(field.name());
|
builder.startObject(field.name());
|
||||||
|
if (field.preTags != null) {
|
||||||
|
builder.field("pre_tags", field.preTags);
|
||||||
|
}
|
||||||
|
if (field.postTags != null) {
|
||||||
|
builder.field("post_tags", field.postTags);
|
||||||
|
}
|
||||||
if (field.fragmentSize != -1) {
|
if (field.fragmentSize != -1) {
|
||||||
builder.field("fragment_size", field.fragmentSize);
|
builder.field("fragment_size", field.fragmentSize);
|
||||||
}
|
}
|
||||||
|
@ -252,9 +258,21 @@ public class HighlightBuilder implements ToXContent {
|
||||||
if (field.fragmentOffset != -1) {
|
if (field.fragmentOffset != -1) {
|
||||||
builder.field("fragment_offset", field.fragmentOffset);
|
builder.field("fragment_offset", field.fragmentOffset);
|
||||||
}
|
}
|
||||||
|
if (field.highlightFilter != null) {
|
||||||
|
builder.field("highlight_filter", field.highlightFilter);
|
||||||
|
}
|
||||||
|
if (field.order != null) {
|
||||||
|
builder.field("order", field.order);
|
||||||
|
}
|
||||||
if (field.requireFieldMatch != null) {
|
if (field.requireFieldMatch != null) {
|
||||||
builder.field("require_field_match", field.requireFieldMatch);
|
builder.field("require_field_match", field.requireFieldMatch);
|
||||||
}
|
}
|
||||||
|
if (field.boundaryMaxScan != -1) {
|
||||||
|
builder.field("boundary_max_scan", field.boundaryMaxScan);
|
||||||
|
}
|
||||||
|
if (field.boundaryChars != null) {
|
||||||
|
builder.field("boundary_chars", field.boundaryChars);
|
||||||
|
}
|
||||||
if (field.highlighterType != null) {
|
if (field.highlighterType != null) {
|
||||||
builder.field("type", field.highlighterType);
|
builder.field("type", field.highlighterType);
|
||||||
}
|
}
|
||||||
|
@ -276,10 +294,16 @@ public class HighlightBuilder implements ToXContent {
|
||||||
|
|
||||||
public static class Field {
|
public static class Field {
|
||||||
final String name;
|
final String name;
|
||||||
|
String[] preTags;
|
||||||
|
String[] postTags;
|
||||||
int fragmentSize = -1;
|
int fragmentSize = -1;
|
||||||
int fragmentOffset = -1;
|
int fragmentOffset = -1;
|
||||||
int numOfFragments = -1;
|
int numOfFragments = -1;
|
||||||
|
Boolean highlightFilter;
|
||||||
|
String order;
|
||||||
Boolean requireFieldMatch;
|
Boolean requireFieldMatch;
|
||||||
|
int boundaryMaxScan = -1;
|
||||||
|
char[] boundaryChars;
|
||||||
String highlighterType;
|
String highlighterType;
|
||||||
String fragmenter;
|
String fragmenter;
|
||||||
Map<String, Object> options;
|
Map<String, Object> options;
|
||||||
|
@ -292,6 +316,24 @@ public class HighlightBuilder implements ToXContent {
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Explicitly set the pre tags for this field that will be used for highlighting.
|
||||||
|
* This overrides global settings set by {@link HighlightBuilder#preTags(String...)}.
|
||||||
|
*/
|
||||||
|
public Field preTags(String... preTags) {
|
||||||
|
this.preTags = preTags;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Explicitly set the post tags for this field that will be used for highlighting.
|
||||||
|
* This overrides global settings set by {@link HighlightBuilder#postTags(String...)}.
|
||||||
|
*/
|
||||||
|
public Field postTags(String... postTags) {
|
||||||
|
this.postTags = postTags;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public Field fragmentSize(int fragmentSize) {
|
public Field fragmentSize(int fragmentSize) {
|
||||||
this.fragmentSize = fragmentSize;
|
this.fragmentSize = fragmentSize;
|
||||||
return this;
|
return this;
|
||||||
|
@ -307,21 +349,61 @@ public class HighlightBuilder implements ToXContent {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Field highlightFilter(boolean highlightFilter) {
|
||||||
|
this.highlightFilter = highlightFilter;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The order of fragments per field. By default, ordered by the order in the
|
||||||
|
* highlighted text. Can be <tt>score</tt>, which then it will be ordered
|
||||||
|
* by score of the fragments.
|
||||||
|
* This overrides global settings set by {@link HighlightBuilder#order(String)}.
|
||||||
|
*/
|
||||||
|
public Field order(String order) {
|
||||||
|
this.order = order;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public Field requireFieldMatch(boolean requireFieldMatch) {
|
public Field requireFieldMatch(boolean requireFieldMatch) {
|
||||||
this.requireFieldMatch = requireFieldMatch;
|
this.requireFieldMatch = requireFieldMatch;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Field boundaryMaxScan(int boundaryMaxScan) {
|
||||||
|
this.boundaryMaxScan = boundaryMaxScan;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Field boundaryChars(char[] boundaryChars) {
|
||||||
|
this.boundaryChars = boundaryChars;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set type of highlighter to use. Supported types
|
||||||
|
* are <tt>highlighter</tt> and <tt>fast-vector-highlighter</tt>.
|
||||||
|
* This overrides global settings set by {@link HighlightBuilder#highlighterType(String)}.
|
||||||
|
*/
|
||||||
public Field highlighterType(String highlighterType) {
|
public Field highlighterType(String highlighterType) {
|
||||||
this.highlighterType = highlighterType;
|
this.highlighterType = highlighterType;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets what fragmenter to use to break up text that is eligible for highlighting.
|
||||||
|
* This option is only applicable when using plain / normal highlighter.
|
||||||
|
* This overrides global settings set by {@link HighlightBuilder#fragmenter(String)}.
|
||||||
|
*/
|
||||||
public Field fragmenter(String fragmenter) {
|
public Field fragmenter(String fragmenter) {
|
||||||
this.fragmenter = fragmenter;
|
this.fragmenter = fragmenter;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Allows to set custom options for custom highlighters.
|
||||||
|
* This overrides global settings set by {@link HighlightBuilder#options(Map<String, Object>)}.
|
||||||
|
*/
|
||||||
public Field options(Map<String, Object> options) {
|
public Field options(Map<String, Object> options) {
|
||||||
this.options = options;
|
this.options = options;
|
||||||
return this;
|
return this;
|
||||||
|
|
|
@ -540,9 +540,33 @@ public class HighlighterSearchTests extends AbstractSharedClusterTest {
|
||||||
assertThat(search.getHits().hits()[0].highlightFields().get("titleTV").fragments()[1].string(), equalTo("<em>highlight</em> other text"));
|
assertThat(search.getHits().hits()[0].highlightFields().get("titleTV").fragments()[1].string(), equalTo("<em>highlight</em> other text"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGlobalHighlightingSettingsOverriddenAtFieldLevel() {
|
||||||
|
client().admin().indices().prepareCreate("test").execute().actionGet();
|
||||||
|
client().admin().cluster().prepareHealth("test").setWaitForGreenStatus().execute().actionGet();
|
||||||
|
|
||||||
|
client().prepareIndex("test", "type1")
|
||||||
|
.setSource("field1", "this is a test", "field2", "this is another test")
|
||||||
|
.setRefresh(true).execute().actionGet();
|
||||||
|
|
||||||
|
logger.info("--> highlighting and searching on field1 and field2 produces different tags");
|
||||||
|
SearchSourceBuilder source = searchSource()
|
||||||
|
.query(termQuery("field1", "test"))
|
||||||
|
.from(0).size(60).explain(true)
|
||||||
|
.highlight(highlight().order("score").preTags("<global>").postTags("</global>")
|
||||||
|
.field(new HighlightBuilder.Field("field1"))
|
||||||
|
.field(new HighlightBuilder.Field("field2").preTags("<field2>").postTags("</field2>")));
|
||||||
|
|
||||||
|
SearchResponse searchResponse = client().search(searchRequest("test").source(source).searchType(QUERY_THEN_FETCH).scroll(timeValueMinutes(10))).actionGet();
|
||||||
|
assertThat("Failures " + Arrays.toString(searchResponse.getShardFailures()), searchResponse.getShardFailures().length, equalTo(0));
|
||||||
|
assertThat(searchResponse.getHits().totalHits(), equalTo(1l));
|
||||||
|
|
||||||
|
assertThat(searchResponse.getHits().getAt(0).highlightFields().get("field1").fragments()[0].string(), equalTo("this is a <global>test</global>"));
|
||||||
|
assertThat(searchResponse.getHits().getAt(0).highlightFields().get("field2").fragments()[0].string(), equalTo("this is another <field2>test</field2>"));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testHighlightingOnWildcardFields() throws Exception {
|
public void testHighlightingOnWildcardFields() throws Exception {
|
||||||
client().admin().indices().prepareDelete().execute().actionGet();
|
|
||||||
client().admin().indices().prepareCreate("test").execute().actionGet();
|
client().admin().indices().prepareCreate("test").execute().actionGet();
|
||||||
client().admin().cluster().prepareHealth("test").setWaitForGreenStatus().execute().actionGet();
|
client().admin().cluster().prepareHealth("test").setWaitForGreenStatus().execute().actionGet();
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue