public void innerToXContent(XContentBuilder builder, Params params) throws IOException { if (from != -1) { builder.field(FROM_FIELD.getPreferredName(), from); } if (size != -1) { builder.field(SIZE_FIELD.getPreferredName(), size); } if (timeoutInMillis != -1) { builder.field(TIMEOUT_FIELD.getPreferredName(), timeoutInMillis); } if (terminateAfter != SearchContext.DEFAULT_TERMINATE_AFTER) { builder.field(TERMINATE_AFTER_FIELD.getPreferredName(), terminateAfter); } if (queryBuilder != null) { builder.field(QUERY_FIELD.getPreferredName(), queryBuilder); } if (postQueryBuilder != null) { builder.field(POST_FILTER_FIELD.getPreferredName(), postQueryBuilder); } if (minScore != null) { builder.field(MIN_SCORE_FIELD.getPreferredName(), minScore); } if (version != null) { builder.field(VERSION_FIELD.getPreferredName(), version); } if (explain != null) { builder.field(EXPLAIN_FIELD.getPreferredName(), explain); } if (fetchSourceContext != null) { builder.field(_SOURCE_FIELD.getPreferredName(), fetchSourceContext); } if (fieldNames != null) { if (fieldNames.size() == 1) { builder.field(FIELDS_FIELD.getPreferredName(), fieldNames.get(0)); } else { builder.startArray(FIELDS_FIELD.getPreferredName()); for (String fieldName : fieldNames) { builder.value(fieldName); } builder.endArray(); } } if (fieldDataFields != null) { builder.startArray(FIELDDATA_FIELDS_FIELD.getPreferredName()); for (String fieldDataField : fieldDataFields) { builder.value(fieldDataField); } builder.endArray(); } if (scriptFields != null) { builder.startObject(SCRIPT_FIELDS_FIELD.getPreferredName()); for (ScriptField scriptField : scriptFields) { scriptField.toXContent(builder, params); } builder.endObject(); } if (sorts != null) { builder.startArray(SORT_FIELD.getPreferredName()); for (BytesReference sort : sorts) { XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(sort); parser.nextToken(); builder.copyCurrentStructure(parser); } builder.endArray(); } if (trackScores) { builder.field(TRACK_SCORES_FIELD.getPreferredName(), true); } if (indexBoost != null) { builder.startObject(INDICES_BOOST_FIELD.getPreferredName()); assert !indexBoost.containsKey(null); final Object[] keys = indexBoost.keys; final float[] values = indexBoost.values; for (int i = 0; i < keys.length; i++) { if (keys[i] != null) { builder.field((String) keys[i], values[i]); } } builder.endObject(); } if (aggregations != null) { builder.field(AGGREGATIONS_FIELD.getPreferredName()); builder.startObject(); for (BytesReference aggregation : aggregations) { XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(aggregation); parser.nextToken(); parser.nextToken(); builder.copyCurrentStructure(parser); } builder.endObject(); } if (highlightBuilder != null) { builder.field(HIGHLIGHT_FIELD.getPreferredName()); XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(highlightBuilder); parser.nextToken(); builder.copyCurrentStructure(parser); } if (innerHitsBuilder != null) { builder.field(INNER_HITS_FIELD.getPreferredName()); XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(innerHitsBuilder); parser.nextToken(); builder.copyCurrentStructure(parser); } if (suggestBuilder != null) { builder.field(SUGGEST_FIELD.getPreferredName()); XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(suggestBuilder); parser.nextToken(); builder.copyCurrentStructure(parser); } if (rescoreBuilders != null) { builder.startArray(RESCORE_FIELD.getPreferredName()); for (BytesReference rescoreBuilder : rescoreBuilders) { XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(rescoreBuilder); parser.nextToken(); builder.copyCurrentStructure(parser); } builder.endArray(); } if (stats != null) { builder.field(STATS_FIELD.getPreferredName(), stats); } if (ext != null) { builder.field(EXT_FIELD.getPreferredName()); XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(ext); parser.nextToken(); builder.copyCurrentStructure(parser); } }
@Override public void writeTo(StreamOutput out) throws IOException { boolean hasAggregations = aggregations != null; out.writeBoolean(hasAggregations); if (hasAggregations) { out.writeVInt(aggregations.size()); for (BytesReference aggregation : aggregations) { out.writeBytesReference(aggregation); } } out.writeOptionalBoolean(explain); FetchSourceContext.optionalWriteToStream(fetchSourceContext, out); boolean hasFieldDataFields = fieldDataFields != null; out.writeBoolean(hasFieldDataFields); if (hasFieldDataFields) { out.writeVInt(fieldDataFields.size()); for (String field : fieldDataFields) { out.writeString(field); } } boolean hasFieldNames = fieldNames != null; out.writeBoolean(hasFieldNames); if (hasFieldNames) { out.writeVInt(fieldNames.size()); for (String field : fieldNames) { out.writeString(field); } } out.writeVInt(from); boolean hasHighlightBuilder = highlightBuilder != null; out.writeBoolean(hasHighlightBuilder); if (hasHighlightBuilder) { out.writeBytesReference(highlightBuilder); } boolean hasIndexBoost = indexBoost != null; out.writeBoolean(hasIndexBoost); if (hasIndexBoost) { out.writeVInt(indexBoost.size()); for (ObjectCursor<String> key : indexBoost.keys()) { out.writeString(key.value); out.writeFloat(indexBoost.get(key.value)); } } boolean hasInnerHitsBuilder = innerHitsBuilder != null; out.writeBoolean(hasInnerHitsBuilder); if (hasInnerHitsBuilder) { out.writeBytesReference(innerHitsBuilder); } boolean hasMinScore = minScore != null; out.writeBoolean(hasMinScore); if (hasMinScore) { out.writeFloat(minScore); } boolean hasPostQuery = postQueryBuilder != null; out.writeBoolean(hasPostQuery); if (hasPostQuery) { out.writeQuery(postQueryBuilder); } boolean hasQuery = queryBuilder != null; out.writeBoolean(hasQuery); if (hasQuery) { out.writeQuery(queryBuilder); } boolean hasRescoreBuilders = rescoreBuilders != null; out.writeBoolean(hasRescoreBuilders); if (hasRescoreBuilders) { out.writeVInt(rescoreBuilders.size()); for (BytesReference rescoreBuilder : rescoreBuilders) { out.writeBytesReference(rescoreBuilder); } } boolean hasScriptFields = scriptFields != null; out.writeBoolean(hasScriptFields); if (hasScriptFields) { out.writeVInt(scriptFields.size()); for (ScriptField scriptField : scriptFields) { scriptField.writeTo(out); } } out.writeVInt(size); boolean hasSorts = sorts != null; out.writeBoolean(hasSorts); if (hasSorts) { out.writeVInt(sorts.size()); for (BytesReference sort : sorts) { out.writeBytesReference(sort); } } boolean hasStats = stats != null; out.writeBoolean(hasStats); if (hasStats) { out.writeVInt(stats.size()); for (String stat : stats) { out.writeString(stat); } } boolean hasSuggestBuilder = suggestBuilder != null; out.writeBoolean(hasSuggestBuilder); if (hasSuggestBuilder) { out.writeBytesReference(suggestBuilder); } out.writeVInt(terminateAfter); out.writeLong(timeoutInMillis); out.writeBoolean(trackScores); out.writeOptionalBoolean(version); boolean hasExt = ext != null; out.writeBoolean(hasExt); if (hasExt) { out.writeBytesReference(ext); } }
public SearchSourceBuilder fromXContent(XContentParser parser, QueryParseContext context) throws IOException { SearchSourceBuilder builder = new SearchSourceBuilder(); XContentParser.Token token = parser.currentToken(); String currentFieldName = null; if (token != XContentParser.Token.START_OBJECT && (token = parser.nextToken()) != XContentParser.Token.START_OBJECT) { throw new ParsingException( parser.getTokenLocation(), "Expected [" + XContentParser.Token.START_OBJECT + "] but found [" + token + "]", parser.getTokenLocation()); } while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); } else if (token.isValue()) { if (context.parseFieldMatcher().match(currentFieldName, FROM_FIELD)) { builder.from = parser.intValue(); } else if (context.parseFieldMatcher().match(currentFieldName, SIZE_FIELD)) { builder.size = parser.intValue(); } else if (context.parseFieldMatcher().match(currentFieldName, TIMEOUT_FIELD)) { builder.timeoutInMillis = parser.longValue(); } else if (context.parseFieldMatcher().match(currentFieldName, TERMINATE_AFTER_FIELD)) { builder.terminateAfter = parser.intValue(); } else if (context.parseFieldMatcher().match(currentFieldName, MIN_SCORE_FIELD)) { builder.minScore = parser.floatValue(); } else if (context.parseFieldMatcher().match(currentFieldName, VERSION_FIELD)) { builder.version = parser.booleanValue(); } else if (context.parseFieldMatcher().match(currentFieldName, EXPLAIN_FIELD)) { builder.explain = parser.booleanValue(); } else if (context.parseFieldMatcher().match(currentFieldName, TRACK_SCORES_FIELD)) { builder.trackScores = parser.booleanValue(); } else if (context.parseFieldMatcher().match(currentFieldName, _SOURCE_FIELD)) { builder.fetchSourceContext = FetchSourceContext.parse(parser, context); } else if (context.parseFieldMatcher().match(currentFieldName, FIELDS_FIELD)) { List<String> fieldNames = new ArrayList<>(); fieldNames.add(parser.text()); builder.fieldNames = fieldNames; } else if (context.parseFieldMatcher().match(currentFieldName, SORT_FIELD)) { builder.sort(parser.text()); } else { throw new ParsingException( parser.getTokenLocation(), "Unknown key for a " + token + " in [" + currentFieldName + "].", parser.getTokenLocation()); } } else if (token == XContentParser.Token.START_OBJECT) { if (context.parseFieldMatcher().match(currentFieldName, QUERY_FIELD)) { builder.queryBuilder = context.parseInnerQueryBuilder(); } else if (context.parseFieldMatcher().match(currentFieldName, POST_FILTER_FIELD)) { builder.postQueryBuilder = context.parseInnerQueryBuilder(); } else if (context.parseFieldMatcher().match(currentFieldName, _SOURCE_FIELD)) { builder.fetchSourceContext = FetchSourceContext.parse(parser, context); } else if (context.parseFieldMatcher().match(currentFieldName, SCRIPT_FIELDS_FIELD)) { List<ScriptField> scriptFields = new ArrayList<>(); while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { String scriptFieldName = parser.currentName(); token = parser.nextToken(); if (token == XContentParser.Token.START_OBJECT) { Script script = null; boolean ignoreFailure = false; while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); } else if (token.isValue()) { if (context.parseFieldMatcher().match(currentFieldName, SCRIPT_FIELD)) { script = Script.parse(parser, context.parseFieldMatcher()); } else if (context .parseFieldMatcher() .match(currentFieldName, IGNORE_FAILURE_FIELD)) { ignoreFailure = parser.booleanValue(); } else { throw new ParsingException( parser.getTokenLocation(), "Unknown key for a " + token + " in [" + currentFieldName + "].", parser.getTokenLocation()); } } else if (token == XContentParser.Token.START_OBJECT) { if (context.parseFieldMatcher().match(currentFieldName, SCRIPT_FIELD)) { script = Script.parse(parser, context.parseFieldMatcher()); } else { throw new ParsingException( parser.getTokenLocation(), "Unknown key for a " + token + " in [" + currentFieldName + "].", parser.getTokenLocation()); } } else { throw new ParsingException( parser.getTokenLocation(), "Unknown key for a " + token + " in [" + currentFieldName + "].", parser.getTokenLocation()); } } scriptFields.add(new ScriptField(scriptFieldName, script, ignoreFailure)); } else { throw new ParsingException( parser.getTokenLocation(), "Expected [" + XContentParser.Token.START_OBJECT + "] in [" + currentFieldName + "] but found [" + token + "]", parser.getTokenLocation()); } } builder.scriptFields = scriptFields; } else if (context.parseFieldMatcher().match(currentFieldName, INDICES_BOOST_FIELD)) { ObjectFloatHashMap<String> indexBoost = new ObjectFloatHashMap<String>(); while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); } else if (token.isValue()) { indexBoost.put(currentFieldName, parser.floatValue()); } else { throw new ParsingException( parser.getTokenLocation(), "Unknown key for a " + token + " in [" + currentFieldName + "].", parser.getTokenLocation()); } } builder.indexBoost = indexBoost; } else if (context.parseFieldMatcher().match(currentFieldName, AGGREGATIONS_FIELD)) { List<BytesReference> aggregations = new ArrayList<>(); while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { currentFieldName = parser.currentName(); token = parser.nextToken(); if (token == XContentParser.Token.START_OBJECT) { XContentBuilder xContentBuilder = XContentFactory.contentBuilder(parser.contentType()); xContentBuilder.startObject(); xContentBuilder.field(currentFieldName); xContentBuilder.copyCurrentStructure(parser); xContentBuilder.endObject(); aggregations.add(xContentBuilder.bytes()); } else { throw new ParsingException( parser.getTokenLocation(), "Unknown key for a " + token + " in [" + currentFieldName + "].", parser.getTokenLocation()); } } builder.aggregations = aggregations; } else if (context.parseFieldMatcher().match(currentFieldName, HIGHLIGHT_FIELD)) { XContentBuilder xContentBuilder = XContentFactory.contentBuilder(parser.contentType()).copyCurrentStructure(parser); builder.highlightBuilder = xContentBuilder.bytes(); } else if (context.parseFieldMatcher().match(currentFieldName, INNER_HITS_FIELD)) { XContentBuilder xContentBuilder = XContentFactory.contentBuilder(parser.contentType()).copyCurrentStructure(parser); builder.innerHitsBuilder = xContentBuilder.bytes(); } else if (context.parseFieldMatcher().match(currentFieldName, SUGGEST_FIELD)) { XContentBuilder xContentBuilder = XContentFactory.contentBuilder(parser.contentType()); xContentBuilder.copyCurrentStructure(parser); builder.suggestBuilder = xContentBuilder.bytes(); } else if (context.parseFieldMatcher().match(currentFieldName, SORT_FIELD)) { List<BytesReference> sorts = new ArrayList<>(); XContentBuilder xContentBuilder = XContentFactory.contentBuilder(parser.contentType()).copyCurrentStructure(parser); sorts.add(xContentBuilder.bytes()); builder.sorts = sorts; } else if (context.parseFieldMatcher().match(currentFieldName, EXT_FIELD)) { XContentBuilder xContentBuilder = XContentFactory.contentBuilder(parser.contentType()).copyCurrentStructure(parser); builder.ext = xContentBuilder.bytes(); } else { throw new ParsingException( parser.getTokenLocation(), "Unknown key for a " + token + " in [" + currentFieldName + "].", parser.getTokenLocation()); } } else if (token == XContentParser.Token.START_ARRAY) { if (context.parseFieldMatcher().match(currentFieldName, FIELDS_FIELD)) { List<String> fieldNames = new ArrayList<>(); while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { if (token == XContentParser.Token.VALUE_STRING) { fieldNames.add(parser.text()); } else { throw new ParsingException( parser.getTokenLocation(), "Expected [" + XContentParser.Token.VALUE_STRING + "] in [" + currentFieldName + "] but found [" + token + "]", parser.getTokenLocation()); } } builder.fieldNames = fieldNames; } else if (context.parseFieldMatcher().match(currentFieldName, FIELDDATA_FIELDS_FIELD)) { List<String> fieldDataFields = new ArrayList<>(); while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { if (token == XContentParser.Token.VALUE_STRING) { fieldDataFields.add(parser.text()); } else { throw new ParsingException( parser.getTokenLocation(), "Expected [" + XContentParser.Token.VALUE_STRING + "] in [" + currentFieldName + "] but found [" + token + "]", parser.getTokenLocation()); } } builder.fieldDataFields = fieldDataFields; } else if (context.parseFieldMatcher().match(currentFieldName, SORT_FIELD)) { List<BytesReference> sorts = new ArrayList<>(); while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { XContentBuilder xContentBuilder = XContentFactory.contentBuilder(parser.contentType()).copyCurrentStructure(parser); sorts.add(xContentBuilder.bytes()); } builder.sorts = sorts; } else if (context.parseFieldMatcher().match(currentFieldName, RESCORE_FIELD)) { List<BytesReference> rescoreBuilders = new ArrayList<>(); while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { XContentBuilder xContentBuilder = XContentFactory.contentBuilder(parser.contentType()).copyCurrentStructure(parser); rescoreBuilders.add(xContentBuilder.bytes()); } builder.rescoreBuilders = rescoreBuilders; } else if (context.parseFieldMatcher().match(currentFieldName, STATS_FIELD)) { List<String> stats = new ArrayList<>(); while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { if (token == XContentParser.Token.VALUE_STRING) { stats.add(parser.text()); } else { throw new ParsingException( parser.getTokenLocation(), "Expected [" + XContentParser.Token.VALUE_STRING + "] in [" + currentFieldName + "] but found [" + token + "]", parser.getTokenLocation()); } } builder.stats = stats; } else if (context.parseFieldMatcher().match(currentFieldName, _SOURCE_FIELD)) { builder.fetchSourceContext = FetchSourceContext.parse(parser, context); } else { throw new ParsingException( parser.getTokenLocation(), "Unknown key for a " + token + " in [" + currentFieldName + "].", parser.getTokenLocation()); } } else { throw new ParsingException( parser.getTokenLocation(), "Unknown key for a " + token + " in [" + currentFieldName + "].", parser.getTokenLocation()); } } return builder; }
@Override public SearchSourceBuilder readFrom(StreamInput in) throws IOException { SearchSourceBuilder builder = new SearchSourceBuilder(); if (in.readBoolean()) { int size = in.readVInt(); List<BytesReference> aggregations = new ArrayList<>(size); for (int i = 0; i < size; i++) { aggregations.add(in.readBytesReference()); } builder.aggregations = aggregations; } builder.explain = in.readOptionalBoolean(); builder.fetchSourceContext = FetchSourceContext.optionalReadFromStream(in); boolean hasFieldDataFields = in.readBoolean(); if (hasFieldDataFields) { int size = in.readVInt(); List<String> fieldDataFields = new ArrayList<>(size); for (int i = 0; i < size; i++) { fieldDataFields.add(in.readString()); } builder.fieldDataFields = fieldDataFields; } boolean hasFieldNames = in.readBoolean(); if (hasFieldNames) { int size = in.readVInt(); List<String> fieldNames = new ArrayList<>(size); for (int i = 0; i < size; i++) { fieldNames.add(in.readString()); } builder.fieldNames = fieldNames; } builder.from = in.readVInt(); if (in.readBoolean()) { builder.highlightBuilder = in.readBytesReference(); } boolean hasIndexBoost = in.readBoolean(); if (hasIndexBoost) { int size = in.readVInt(); ObjectFloatHashMap<String> indexBoost = new ObjectFloatHashMap<String>(size); for (int i = 0; i < size; i++) { indexBoost.put(in.readString(), in.readFloat()); } builder.indexBoost = indexBoost; } if (in.readBoolean()) { builder.innerHitsBuilder = in.readBytesReference(); } if (in.readBoolean()) { builder.minScore = in.readFloat(); } if (in.readBoolean()) { builder.postQueryBuilder = in.readQuery(); } if (in.readBoolean()) { builder.queryBuilder = in.readQuery(); } if (in.readBoolean()) { int size = in.readVInt(); List<BytesReference> rescoreBuilders = new ArrayList<>(); for (int i = 0; i < size; i++) { rescoreBuilders.add(in.readBytesReference()); } builder.rescoreBuilders = rescoreBuilders; } if (in.readBoolean()) { int size = in.readVInt(); List<ScriptField> scriptFields = new ArrayList<>(size); for (int i = 0; i < size; i++) { scriptFields.add(ScriptField.PROTOTYPE.readFrom(in)); } builder.scriptFields = scriptFields; } builder.size = in.readVInt(); if (in.readBoolean()) { int size = in.readVInt(); List<BytesReference> sorts = new ArrayList<>(); for (int i = 0; i < size; i++) { sorts.add(in.readBytesReference()); } builder.sorts = sorts; } if (in.readBoolean()) { int size = in.readVInt(); List<String> stats = new ArrayList<>(); for (int i = 0; i < size; i++) { stats.add(in.readString()); } builder.stats = stats; } if (in.readBoolean()) { builder.suggestBuilder = in.readBytesReference(); } builder.terminateAfter = in.readVInt(); builder.timeoutInMillis = in.readLong(); builder.trackScores = in.readBoolean(); builder.version = in.readOptionalBoolean(); if (in.readBoolean()) { builder.ext = in.readBytesReference(); } return builder; }