Added access to highlight offset through java client, previously just through transport
This commit is contained in:
parent
cee29dedb2
commit
77f873fdf1
|
@ -440,6 +440,20 @@ public class SearchRequestBuilder extends BaseRequestBuilder<SearchRequest, Sear
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a field to be highlighted with a provided fragment size (in characters),
|
||||||
|
* a provided (maximum) number of fragments and an offset for the highlight.
|
||||||
|
*
|
||||||
|
* @param name The field to highlight
|
||||||
|
* @param fragmentSize The size of a fragment in characters
|
||||||
|
* @param numberOfFragments The (maximum) number of fragments
|
||||||
|
*/
|
||||||
|
public SearchRequestBuilder addHighlightedField(String name, int fragmentSize, int numberOfFragments,
|
||||||
|
int fragmentOffset) {
|
||||||
|
highlightBuilder().field(name, fragmentSize, numberOfFragments, fragmentOffset);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set a tag scheme that encapsulates a built in pre and post tags. The allows schemes
|
* Set a tag scheme that encapsulates a built in pre and post tags. The allows schemes
|
||||||
* are <tt>styled</tt> and <tt>default</tt>.
|
* are <tt>styled</tt> and <tt>default</tt>.
|
||||||
|
|
|
@ -90,6 +90,23 @@ public class HighlightBuilder implements ToXContent {
|
||||||
return this;
|
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
|
* Set a tag scheme that encapsulates a built in pre and post tags. The allows schemes
|
||||||
* are <tt>styled</tt> and <tt>default</tt>.
|
* are <tt>styled</tt> and <tt>default</tt>.
|
||||||
|
@ -151,6 +168,9 @@ public class HighlightBuilder implements ToXContent {
|
||||||
if (field.numOfFragments() != -1) {
|
if (field.numOfFragments() != -1) {
|
||||||
builder.field("number_of_fragments", field.numOfFragments());
|
builder.field("number_of_fragments", field.numOfFragments());
|
||||||
}
|
}
|
||||||
|
if (field.fragmentOffset() != -1) {
|
||||||
|
builder.field("fragment_offset", field.fragmentOffset());
|
||||||
|
}
|
||||||
builder.endObject();
|
builder.endObject();
|
||||||
}
|
}
|
||||||
builder.endObject();
|
builder.endObject();
|
||||||
|
@ -162,8 +182,10 @@ public class HighlightBuilder implements ToXContent {
|
||||||
private static class Field {
|
private static class Field {
|
||||||
private final String name;
|
private final String name;
|
||||||
private int fragmentSize = -1;
|
private int fragmentSize = -1;
|
||||||
|
private int fragmentOffset = -1;
|
||||||
private int numOfFragments = -1;
|
private int numOfFragments = -1;
|
||||||
|
|
||||||
|
|
||||||
private Field(String name) {
|
private Field(String name) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
@ -181,6 +203,15 @@ public class HighlightBuilder implements ToXContent {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int fragmentOffset() {
|
||||||
|
return fragmentOffset;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Field fragmentOffset(int fragmentOffset) {
|
||||||
|
this.fragmentOffset = fragmentOffset;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public int numOfFragments() {
|
public int numOfFragments() {
|
||||||
return numOfFragments;
|
return numOfFragments;
|
||||||
}
|
}
|
||||||
|
|
|
@ -404,4 +404,37 @@ public class HighlighterSearchTests extends AbstractNodesTests {
|
||||||
assertThat(hit.highlightFields().get("title").fragments()[0], equalTo("This is a test on the highlighting <em>bug</em> present in elasticsearch "));
|
assertThat(hit.highlightFields().get("title").fragments()[0], equalTo("This is a test on the highlighting <em>bug</em> 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 <em>bug</em> present in elasticsearch "));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue