diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/client/action/search/SearchRequestBuilder.java b/modules/elasticsearch/src/main/java/org/elasticsearch/client/action/search/SearchRequestBuilder.java index e186ab3732b..50412d6a6b9 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/client/action/search/SearchRequestBuilder.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/client/action/search/SearchRequestBuilder.java @@ -440,6 +440,20 @@ public class SearchRequestBuilder extends BaseRequestBuilderstyled and default. diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/search/highlight/HighlightBuilder.java b/modules/elasticsearch/src/main/java/org/elasticsearch/search/highlight/HighlightBuilder.java index 90adb07f49b..bc13e068f08 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/search/highlight/HighlightBuilder.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/search/highlight/HighlightBuilder.java @@ -90,6 +90,23 @@ public class HighlightBuilder implements ToXContent { return this; } + /** + * Adds a field to be highlighted with a provided fragment size (in characters), and + * a provided (maximum) number of fragments. + * + * @param name The field to highlight + * @param fragmentSize The size of a fragment in characters + * @param numberOfFragments The (maximum) number of fragments + * @param fragmentOffset The offset from the start of the fragment to the start of the highlight + */ + public HighlightBuilder field(String name, int fragmentSize, int numberOfFragments, int fragmentOffset) { + if (fields == null) { + fields = newArrayList(); + } + fields.add(new Field(name).fragmentSize(fragmentSize).numOfFragments(numberOfFragments).fragmentOffset(fragmentOffset)); + return this; + } + /** * Set a tag scheme that encapsulates a built in pre and post tags. The allows schemes * are styled and default. @@ -151,6 +168,9 @@ public class HighlightBuilder implements ToXContent { if (field.numOfFragments() != -1) { builder.field("number_of_fragments", field.numOfFragments()); } + if (field.fragmentOffset() != -1) { + builder.field("fragment_offset", field.fragmentOffset()); + } builder.endObject(); } builder.endObject(); @@ -162,8 +182,10 @@ public class HighlightBuilder implements ToXContent { private static class Field { private final String name; private int fragmentSize = -1; + private int fragmentOffset = -1; private int numOfFragments = -1; + private Field(String name) { this.name = name; } @@ -181,6 +203,15 @@ public class HighlightBuilder implements ToXContent { return this; } + public int fragmentOffset() { + return fragmentOffset; + } + + public Field fragmentOffset(int fragmentOffset) { + this.fragmentOffset = fragmentOffset; + return this; + } + public int numOfFragments() { return numOfFragments; } diff --git a/modules/test/integration/src/test/java/org/elasticsearch/test/integration/search/highlight/HighlighterSearchTests.java b/modules/test/integration/src/test/java/org/elasticsearch/test/integration/search/highlight/HighlighterSearchTests.java index 296cc763ff1..4b85fe5565e 100644 --- a/modules/test/integration/src/test/java/org/elasticsearch/test/integration/search/highlight/HighlighterSearchTests.java +++ b/modules/test/integration/src/test/java/org/elasticsearch/test/integration/search/highlight/HighlighterSearchTests.java @@ -404,4 +404,37 @@ public class HighlighterSearchTests extends AbstractNodesTests { assertThat(hit.highlightFields().get("title").fragments()[0], equalTo("This is a test on the highlighting bug present in elasticsearch ")); } } + + @Test public void testFastVectorHighlighterOffsetParameter() throws Exception { + try { + client.admin().indices().prepareDelete("test").execute().actionGet(); + } catch (Exception e) { + // ignore + } + + client.admin().indices().prepareCreate("test").setSettings(ImmutableSettings.settingsBuilder().put("number_of_shards", 2)) + .addMapping("type1", jsonBuilder().startObject().startObject("type1").startObject("properties") + .startObject("title").field("type", "string").field("store", "yes").field("term_vector", "with_positions_offsets").endObject() + .endObject().endObject().endObject()) + .execute().actionGet(); + + for (int i = 0; i < 5; i++) { + client.prepareIndex("test", "type1", Integer.toString(i)) + .setSource("title", "This is a test on the highlighting bug present in elasticsearch").setRefresh(true).execute().actionGet(); + } + + SearchResponse search = client.prepareSearch() + .setQuery(fieldQuery("title", "bug")) + .addHighlightedField("title", 50, 1, 10) + .execute().actionGet(); + + assertThat(search.hits().totalHits(), equalTo(5l)); + assertThat(search.hits().hits().length, equalTo(5)); + assertThat(search.getFailedShards(), equalTo(0)); + + for (SearchHit hit : search.hits()) { + // LUCENE 3.1 UPGRADE: Caused adding the space at the end... + assertThat(hit.highlightFields().get("title").fragments()[0], equalTo("hlighting bug present in elasticsearch ")); + } + } }