|
51 | 51 | import org.opensearch.core.xcontent.XContentBuilder;
|
52 | 52 | import org.opensearch.core.xcontent.XContentHelper;
|
53 | 53 | import org.opensearch.core.xcontent.XContentParser;
|
| 54 | +import org.opensearch.index.mapper.DerivedFieldMapper; |
54 | 55 | import org.opensearch.index.query.QueryBuilder;
|
55 | 56 | import org.opensearch.index.query.QueryRewriteContext;
|
56 | 57 | import org.opensearch.index.query.Rewriteable;
|
@@ -113,6 +114,7 @@ public final class SearchSourceBuilder implements Writeable, ToXContentObject, R
|
113 | 114 | public static final ParseField DOCVALUE_FIELDS_FIELD = new ParseField("docvalue_fields");
|
114 | 115 | public static final ParseField FETCH_FIELDS_FIELD = new ParseField("fields");
|
115 | 116 | public static final ParseField SCRIPT_FIELDS_FIELD = new ParseField("script_fields");
|
| 117 | + public static final ParseField DERIVED_FIELDS_FIELD = new ParseField(DerivedFieldMapper.CONTENT_TYPE); |
116 | 118 | public static final ParseField SCRIPT_FIELD = new ParseField("script");
|
117 | 119 | public static final ParseField IGNORE_FAILURE_FIELD = new ParseField("ignore_failure");
|
118 | 120 | public static final ParseField SORT_FIELD = new ParseField("sort");
|
@@ -192,6 +194,7 @@ public static HighlightBuilder highlight() {
|
192 | 194 | private StoredFieldsContext storedFieldsContext;
|
193 | 195 | private List<FieldAndFormat> docValueFields;
|
194 | 196 | private List<ScriptField> scriptFields;
|
| 197 | + private Map<String, Object> derivedFields; |
195 | 198 | private FetchSourceContext fetchSourceContext;
|
196 | 199 | private List<FieldAndFormat> fetchFields;
|
197 | 200 |
|
@@ -247,6 +250,9 @@ public SearchSourceBuilder(StreamInput in) throws IOException {
|
247 | 250 | if (in.readBoolean()) {
|
248 | 251 | scriptFields = in.readList(ScriptField::new);
|
249 | 252 | }
|
| 253 | + if (in.readBoolean()) { |
| 254 | + derivedFields = in.readMap(); |
| 255 | + } |
250 | 256 | size = in.readVInt();
|
251 | 257 | if (in.readBoolean()) {
|
252 | 258 | int size = in.readVInt();
|
@@ -310,6 +316,11 @@ public void writeTo(StreamOutput out) throws IOException {
|
310 | 316 | if (hasScriptFields) {
|
311 | 317 | out.writeList(scriptFields);
|
312 | 318 | }
|
| 319 | + boolean hasDerivedFields = derivedFields != null; |
| 320 | + out.writeBoolean(hasDerivedFields); |
| 321 | + if (hasDerivedFields) { |
| 322 | + out.writeMap(derivedFields); |
| 323 | + } |
313 | 324 | out.writeVInt(size);
|
314 | 325 | boolean hasSorts = sorts != null;
|
315 | 326 | out.writeBoolean(hasSorts);
|
@@ -955,6 +966,10 @@ public List<ScriptField> scriptFields() {
|
955 | 966 | return scriptFields;
|
956 | 967 | }
|
957 | 968 |
|
| 969 | + public Map<String, Object> derivedFields() { |
| 970 | + return derivedFields; |
| 971 | + } |
| 972 | + |
958 | 973 | /**
|
959 | 974 | * Sets the boost a specific index or alias will receive when the query is executed
|
960 | 975 | * against it.
|
@@ -1119,6 +1134,7 @@ private SearchSourceBuilder shallowCopy(
|
1119 | 1134 | rewrittenBuilder.queryBuilder = queryBuilder;
|
1120 | 1135 | rewrittenBuilder.rescoreBuilders = rescoreBuilders;
|
1121 | 1136 | rewrittenBuilder.scriptFields = scriptFields;
|
| 1137 | + rewrittenBuilder.derivedFields = derivedFields; |
1122 | 1138 | rewrittenBuilder.searchAfterBuilder = searchAfterBuilder;
|
1123 | 1139 | rewrittenBuilder.sliceBuilder = slice;
|
1124 | 1140 | rewrittenBuilder.size = size;
|
@@ -1220,6 +1236,8 @@ public void parseXContent(XContentParser parser, boolean checkTrailingTokens) th
|
1220 | 1236 | while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
|
1221 | 1237 | scriptFields.add(new ScriptField(parser));
|
1222 | 1238 | }
|
| 1239 | + } else if (DERIVED_FIELDS_FIELD.match(currentFieldName, parser.getDeprecationHandler())) { |
| 1240 | + derivedFields = parser.map(); |
1223 | 1241 | } else if (INDICES_BOOST_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
|
1224 | 1242 | deprecationLogger.deprecate(
|
1225 | 1243 | "indices_boost_object_format",
|
@@ -1434,6 +1452,10 @@ public XContentBuilder innerToXContent(XContentBuilder builder, Params params) t
|
1434 | 1452 | builder.endObject();
|
1435 | 1453 | }
|
1436 | 1454 |
|
| 1455 | + if (derivedFields != null) { |
| 1456 | + builder.field(DERIVED_FIELDS_FIELD.getPreferredName(), derivedFields); |
| 1457 | + } |
| 1458 | + |
1437 | 1459 | if (sorts != null) {
|
1438 | 1460 | builder.startArray(SORT_FIELD.getPreferredName());
|
1439 | 1461 | for (SortBuilder<?> sort : sorts) {
|
@@ -1772,6 +1794,7 @@ public int hashCode() {
|
1772 | 1794 | queryBuilder,
|
1773 | 1795 | rescoreBuilders,
|
1774 | 1796 | scriptFields,
|
| 1797 | + derivedFields, |
1775 | 1798 | size,
|
1776 | 1799 | sorts,
|
1777 | 1800 | searchAfterBuilder,
|
@@ -1815,6 +1838,7 @@ public boolean equals(Object obj) {
|
1815 | 1838 | && Objects.equals(queryBuilder, other.queryBuilder)
|
1816 | 1839 | && Objects.equals(rescoreBuilders, other.rescoreBuilders)
|
1817 | 1840 | && Objects.equals(scriptFields, other.scriptFields)
|
| 1841 | + && Objects.equals(derivedFields, other.derivedFields) |
1818 | 1842 | && Objects.equals(size, other.size)
|
1819 | 1843 | && Objects.equals(sorts, other.sorts)
|
1820 | 1844 | && Objects.equals(searchAfterBuilder, other.searchAfterBuilder)
|
|
0 commit comments