Java API for require_field_match, closes #1795.

This commit is contained in:
Shay Banon 2012-03-20 11:42:12 +02:00
parent 6ab1c9e442
commit a5af2519ed
2 changed files with 49 additions and 24 deletions

View File

@ -559,6 +559,14 @@ public class SearchRequestBuilder extends BaseRequestBuilder<SearchRequest, Sear
return this;
}
/**
* Adds a highlighted field.
*/
public SearchRequestBuilder addHighlightedField(HighlightBuilder.Field field) {
highlightBuilder().field(field);
return this;
}
/**
* Set a tag scheme that encapsulates a built in pre and post tags. The allows schemes
* are <tt>styled</tt> and <tt>default</tt>.
@ -605,6 +613,11 @@ public class SearchRequestBuilder extends BaseRequestBuilder<SearchRequest, Sear
return this;
}
public SearchRequestBuilder setHighlighterRequireFieldMatch(boolean requireFieldMatch) {
highlightBuilder().requireFieldMatch(requireFieldMatch);
return this;
}
/**
* Sets the source of the request as a json string. Note, settings anything other
* than the search type will cause this source to be overridden, consider using

View File

@ -30,7 +30,6 @@ import static com.google.common.collect.Lists.newArrayList;
/**
* A builder for search highlighting.
*
*
* @see org.elasticsearch.search.builder.SearchSourceBuilder#highlight()
*/
public class HighlightBuilder implements ToXContent {
@ -47,6 +46,8 @@ public class HighlightBuilder implements ToXContent {
private String encoder;
private Boolean requireFieldMatch;
/**
* Adds a field to be highlighted with default fragment size of 100 characters, and
* default number of fragments of 5 using the default encoder
@ -113,6 +114,13 @@ public class HighlightBuilder implements ToXContent {
return this;
}
public HighlightBuilder field(Field field) {
if (fields == null) {
fields = newArrayList();
}
fields.add(field);
return this;
}
/**
* Set a tag scheme that encapsulates a built in pre and post tags. The allows schemes
@ -163,6 +171,11 @@ public class HighlightBuilder implements ToXContent {
return this;
}
public HighlightBuilder requireFieldMatch(boolean requireFieldMatch) {
this.requireFieldMatch = requireFieldMatch;
return this;
}
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject("highlight");
@ -181,18 +194,24 @@ public class HighlightBuilder implements ToXContent {
if (encoder != null) {
builder.field("encoder", encoder);
}
if (requireFieldMatch != null) {
builder.field("require_field_match", requireFieldMatch);
}
if (fields != null) {
builder.startObject("fields");
for (Field field : fields) {
builder.startObject(field.name());
if (field.fragmentSize() != -1) {
builder.field("fragment_size", field.fragmentSize());
if (field.fragmentSize != -1) {
builder.field("fragment_size", field.fragmentSize);
}
if (field.numOfFragments() != -1) {
builder.field("number_of_fragments", field.numOfFragments());
if (field.numOfFragments != -1) {
builder.field("number_of_fragments", field.numOfFragments);
}
if (field.fragmentOffset() != -1) {
builder.field("fragment_offset", field.fragmentOffset());
if (field.fragmentOffset != -1) {
builder.field("fragment_offset", field.fragmentOffset);
}
if (field.requireFieldMatch != null) {
builder.field("require_field_match", field.requireFieldMatch);
}
builder.endObject();
@ -204,11 +223,12 @@ public class HighlightBuilder implements ToXContent {
return builder;
}
private static class Field {
private final String name;
private int fragmentSize = -1;
private int fragmentOffset = -1;
private int numOfFragments = -1;
public static class Field {
final String name;
int fragmentSize = -1;
int fragmentOffset = -1;
int numOfFragments = -1;
Boolean requireFieldMatch;
private Field(String name) {
this.name = name;
@ -218,32 +238,24 @@ public class HighlightBuilder implements ToXContent {
return name;
}
public int fragmentSize() {
return fragmentSize;
}
public Field fragmentSize(int fragmentSize) {
this.fragmentSize = fragmentSize;
return this;
}
public int fragmentOffset() {
return fragmentOffset;
}
public Field fragmentOffset(int fragmentOffset) {
this.fragmentOffset = fragmentOffset;
return this;
}
public int numOfFragments() {
return numOfFragments;
}
public Field numOfFragments(int numOfFragments) {
this.numOfFragments = numOfFragments;
return this;
}
public Field requireFieldMatch(boolean requireFieldMatch) {
this.requireFieldMatch = requireFieldMatch;
return this;
}
}
}