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; 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 * 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>.
@ -605,6 +613,11 @@ public class SearchRequestBuilder extends BaseRequestBuilder<SearchRequest, Sear
return this; 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 * 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 * 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. * A builder for search highlighting.
* *
*
* @see org.elasticsearch.search.builder.SearchSourceBuilder#highlight() * @see org.elasticsearch.search.builder.SearchSourceBuilder#highlight()
*/ */
public class HighlightBuilder implements ToXContent { public class HighlightBuilder implements ToXContent {
@ -47,6 +46,8 @@ public class HighlightBuilder implements ToXContent {
private String encoder; private String encoder;
private Boolean requireFieldMatch;
/** /**
* Adds a field to be highlighted with default fragment size of 100 characters, and * Adds a field to be highlighted with default fragment size of 100 characters, and
* default number of fragments of 5 using the default encoder * default number of fragments of 5 using the default encoder
@ -113,6 +114,13 @@ public class HighlightBuilder implements ToXContent {
return this; 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 * 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; return this;
} }
public HighlightBuilder requireFieldMatch(boolean requireFieldMatch) {
this.requireFieldMatch = requireFieldMatch;
return this;
}
@Override @Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject("highlight"); builder.startObject("highlight");
@ -181,18 +194,24 @@ public class HighlightBuilder implements ToXContent {
if (encoder != null) { if (encoder != null) {
builder.field("encoder", encoder); builder.field("encoder", encoder);
} }
if (requireFieldMatch != null) {
builder.field("require_field_match", requireFieldMatch);
}
if (fields != null) { if (fields != null) {
builder.startObject("fields"); builder.startObject("fields");
for (Field field : fields) { for (Field field : fields) {
builder.startObject(field.name()); builder.startObject(field.name());
if (field.fragmentSize() != -1) { if (field.fragmentSize != -1) {
builder.field("fragment_size", field.fragmentSize()); builder.field("fragment_size", field.fragmentSize);
} }
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) { if (field.fragmentOffset != -1) {
builder.field("fragment_offset", field.fragmentOffset()); builder.field("fragment_offset", field.fragmentOffset);
}
if (field.requireFieldMatch != null) {
builder.field("require_field_match", field.requireFieldMatch);
} }
builder.endObject(); builder.endObject();
@ -204,11 +223,12 @@ public class HighlightBuilder implements ToXContent {
return builder; return builder;
} }
private static class Field { public static class Field {
private final String name; final String name;
private int fragmentSize = -1; int fragmentSize = -1;
private int fragmentOffset = -1; int fragmentOffset = -1;
private int numOfFragments = -1; int numOfFragments = -1;
Boolean requireFieldMatch;
private Field(String name) { private Field(String name) {
this.name = name; this.name = name;
@ -218,32 +238,24 @@ public class HighlightBuilder implements ToXContent {
return name; return name;
} }
public int fragmentSize() {
return fragmentSize;
}
public Field fragmentSize(int fragmentSize) { public Field fragmentSize(int fragmentSize) {
this.fragmentSize = fragmentSize; this.fragmentSize = fragmentSize;
return this; return this;
} }
public int fragmentOffset() {
return fragmentOffset;
}
public Field fragmentOffset(int fragmentOffset) { public Field fragmentOffset(int fragmentOffset) {
this.fragmentOffset = fragmentOffset; this.fragmentOffset = fragmentOffset;
return this; return this;
} }
public int numOfFragments() {
return numOfFragments;
}
public Field numOfFragments(int numOfFragments) { public Field numOfFragments(int numOfFragments) {
this.numOfFragments = numOfFragments; this.numOfFragments = numOfFragments;
return this; return this;
} }
public Field requireFieldMatch(boolean requireFieldMatch) {
this.requireFieldMatch = requireFieldMatch;
return this;
}
} }
} }