Search in sources :

Example 1 with ScriptField

use of org.elasticsearch.search.builder.SearchSourceBuilder.ScriptField in project elasticsearch by elastic.

the class InnerHitBuilder method setupInnerHitsContext.

private void setupInnerHitsContext(QueryShardContext context, InnerHitsContext.BaseInnerHits innerHitsContext) throws IOException {
    innerHitsContext.from(from);
    innerHitsContext.size(size);
    innerHitsContext.explain(explain);
    innerHitsContext.version(version);
    innerHitsContext.trackScores(trackScores);
    if (storedFieldsContext != null) {
        innerHitsContext.storedFieldsContext(storedFieldsContext);
    }
    if (docValueFields != null) {
        innerHitsContext.docValueFieldsContext(new DocValueFieldsContext(docValueFields));
    }
    if (scriptFields != null) {
        for (ScriptField field : scriptFields) {
            SearchScript searchScript = innerHitsContext.getQueryShardContext().getSearchScript(field.script(), ScriptContext.Standard.SEARCH);
            innerHitsContext.scriptFields().add(new org.elasticsearch.search.fetch.subphase.ScriptFieldsContext.ScriptField(field.fieldName(), searchScript, field.ignoreFailure()));
        }
    }
    if (fetchSourceContext != null) {
        innerHitsContext.fetchSourceContext(fetchSourceContext);
    }
    if (sorts != null) {
        Optional<SortAndFormats> optionalSort = SortBuilder.buildSort(sorts, context);
        if (optionalSort.isPresent()) {
            innerHitsContext.sort(optionalSort.get());
        }
    }
    if (highlightBuilder != null) {
        innerHitsContext.highlight(highlightBuilder.build(context));
    }
    ParsedQuery parsedQuery = new ParsedQuery(query.toQuery(context), context.copyNamedQueries());
    innerHitsContext.parsedQuery(parsedQuery);
}
Also used : SearchScript(org.elasticsearch.script.SearchScript) ScriptField(org.elasticsearch.search.builder.SearchSourceBuilder.ScriptField) DocValueFieldsContext(org.elasticsearch.search.fetch.subphase.DocValueFieldsContext) SortAndFormats(org.elasticsearch.search.sort.SortAndFormats)

Example 2 with ScriptField

use of org.elasticsearch.search.builder.SearchSourceBuilder.ScriptField in project elasticsearch by elastic.

the class InnerHitBuilder method toXContent.

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
    builder.startObject();
    if (name != null) {
        builder.field(NAME_FIELD.getPreferredName(), name);
    }
    builder.field(IGNORE_UNMAPPED.getPreferredName(), ignoreUnmapped);
    builder.field(SearchSourceBuilder.FROM_FIELD.getPreferredName(), from);
    builder.field(SearchSourceBuilder.SIZE_FIELD.getPreferredName(), size);
    builder.field(SearchSourceBuilder.VERSION_FIELD.getPreferredName(), version);
    builder.field(SearchSourceBuilder.EXPLAIN_FIELD.getPreferredName(), explain);
    builder.field(SearchSourceBuilder.TRACK_SCORES_FIELD.getPreferredName(), trackScores);
    if (fetchSourceContext != null) {
        builder.field(SearchSourceBuilder._SOURCE_FIELD.getPreferredName(), fetchSourceContext, params);
    }
    if (storedFieldsContext != null) {
        storedFieldsContext.toXContent(SearchSourceBuilder.STORED_FIELDS_FIELD.getPreferredName(), builder);
    }
    if (docValueFields != null) {
        builder.startArray(SearchSourceBuilder.DOCVALUE_FIELDS_FIELD.getPreferredName());
        for (String fieldDataField : docValueFields) {
            builder.value(fieldDataField);
        }
        builder.endArray();
    }
    if (scriptFields != null) {
        builder.startObject(SearchSourceBuilder.SCRIPT_FIELDS_FIELD.getPreferredName());
        for (ScriptField scriptField : scriptFields) {
            scriptField.toXContent(builder, params);
        }
        builder.endObject();
    }
    if (sorts != null) {
        builder.startArray(SearchSourceBuilder.SORT_FIELD.getPreferredName());
        for (SortBuilder<?> sort : sorts) {
            sort.toXContent(builder, params);
        }
        builder.endArray();
    }
    if (highlightBuilder != null) {
        builder.field(SearchSourceBuilder.HIGHLIGHT_FIELD.getPreferredName(), highlightBuilder, params);
    }
    if (childInnerHits != null) {
        builder.startObject(INNER_HITS_FIELD.getPreferredName());
        for (Map.Entry<String, InnerHitBuilder> entry : childInnerHits.entrySet()) {
            builder.field(entry.getKey(), entry.getValue(), params);
        }
        builder.endObject();
    }
    builder.endObject();
    return builder;
}
Also used : ScriptField(org.elasticsearch.search.builder.SearchSourceBuilder.ScriptField) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with ScriptField

use of org.elasticsearch.search.builder.SearchSourceBuilder.ScriptField in project elasticsearch by elastic.

the class TopHitsAggregationBuilder method doBuild.

@Override
protected TopHitsAggregatorFactory doBuild(SearchContext context, AggregatorFactory<?> parent, Builder subfactoriesBuilder) throws IOException {
    List<ScriptFieldsContext.ScriptField> fields = new ArrayList<>();
    if (scriptFields != null) {
        for (ScriptField field : scriptFields) {
            SearchScript searchScript = context.getQueryShardContext().getSearchScript(field.script(), ScriptContext.Standard.SEARCH);
            fields.add(new org.elasticsearch.search.fetch.subphase.ScriptFieldsContext.ScriptField(field.fieldName(), searchScript, field.ignoreFailure()));
        }
    }
    final Optional<SortAndFormats> optionalSort;
    if (sorts == null) {
        optionalSort = Optional.empty();
    } else {
        optionalSort = SortBuilder.buildSort(sorts, context.getQueryShardContext());
    }
    return new TopHitsAggregatorFactory(name, from, size, explain, version, trackScores, optionalSort, highlightBuilder, storedFieldsContext, fieldDataFields, fields, fetchSourceContext, context, parent, subfactoriesBuilder, metaData);
}
Also used : ScriptFieldsContext(org.elasticsearch.search.fetch.subphase.ScriptFieldsContext) SearchScript(org.elasticsearch.script.SearchScript) ScriptField(org.elasticsearch.search.builder.SearchSourceBuilder.ScriptField) ArrayList(java.util.ArrayList) SortAndFormats(org.elasticsearch.search.sort.SortAndFormats)

Example 4 with ScriptField

use of org.elasticsearch.search.builder.SearchSourceBuilder.ScriptField in project elasticsearch by elastic.

the class TopHitsAggregationBuilder method internalXContent.

@Override
protected XContentBuilder internalXContent(XContentBuilder builder, Params params) throws IOException {
    builder.startObject();
    builder.field(SearchSourceBuilder.FROM_FIELD.getPreferredName(), from);
    builder.field(SearchSourceBuilder.SIZE_FIELD.getPreferredName(), size);
    builder.field(SearchSourceBuilder.VERSION_FIELD.getPreferredName(), version);
    builder.field(SearchSourceBuilder.EXPLAIN_FIELD.getPreferredName(), explain);
    if (fetchSourceContext != null) {
        builder.field(SearchSourceBuilder._SOURCE_FIELD.getPreferredName(), fetchSourceContext);
    }
    if (storedFieldsContext != null) {
        storedFieldsContext.toXContent(SearchSourceBuilder.STORED_FIELDS_FIELD.getPreferredName(), builder);
    }
    if (fieldDataFields != null) {
        builder.startArray(SearchSourceBuilder.DOCVALUE_FIELDS_FIELD.getPreferredName());
        for (String fieldDataField : fieldDataFields) {
            builder.value(fieldDataField);
        }
        builder.endArray();
    }
    if (scriptFields != null) {
        builder.startObject(SearchSourceBuilder.SCRIPT_FIELDS_FIELD.getPreferredName());
        for (ScriptField scriptField : scriptFields) {
            scriptField.toXContent(builder, params);
        }
        builder.endObject();
    }
    if (sorts != null) {
        builder.startArray(SearchSourceBuilder.SORT_FIELD.getPreferredName());
        for (SortBuilder<?> sort : sorts) {
            sort.toXContent(builder, params);
        }
        builder.endArray();
    }
    if (trackScores) {
        builder.field(SearchSourceBuilder.TRACK_SCORES_FIELD.getPreferredName(), true);
    }
    if (highlightBuilder != null) {
        builder.field(SearchSourceBuilder.HIGHLIGHT_FIELD.getPreferredName(), highlightBuilder);
    }
    builder.endObject();
    return builder;
}
Also used : ScriptField(org.elasticsearch.search.builder.SearchSourceBuilder.ScriptField)

Example 5 with ScriptField

use of org.elasticsearch.search.builder.SearchSourceBuilder.ScriptField in project elasticsearch by elastic.

the class TopHitsAggregationBuilder method parse.

public static TopHitsAggregationBuilder parse(String aggregationName, QueryParseContext context) throws IOException {
    TopHitsAggregationBuilder factory = new TopHitsAggregationBuilder(aggregationName);
    XContentParser.Token token;
    String currentFieldName = null;
    XContentParser parser = context.parser();
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (token.isValue()) {
            if (SearchSourceBuilder.FROM_FIELD.match(currentFieldName)) {
                factory.from(parser.intValue());
            } else if (SearchSourceBuilder.SIZE_FIELD.match(currentFieldName)) {
                factory.size(parser.intValue());
            } else if (SearchSourceBuilder.VERSION_FIELD.match(currentFieldName)) {
                factory.version(parser.booleanValue());
            } else if (SearchSourceBuilder.EXPLAIN_FIELD.match(currentFieldName)) {
                factory.explain(parser.booleanValue());
            } else if (SearchSourceBuilder.TRACK_SCORES_FIELD.match(currentFieldName)) {
                factory.trackScores(parser.booleanValue());
            } else if (SearchSourceBuilder._SOURCE_FIELD.match(currentFieldName)) {
                factory.fetchSource(FetchSourceContext.fromXContent(context.parser()));
            } else if (SearchSourceBuilder.STORED_FIELDS_FIELD.match(currentFieldName)) {
                factory.storedFieldsContext = StoredFieldsContext.fromXContent(SearchSourceBuilder.STORED_FIELDS_FIELD.getPreferredName(), context);
            } else if (SearchSourceBuilder.SORT_FIELD.match(currentFieldName)) {
                factory.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 (SearchSourceBuilder._SOURCE_FIELD.match(currentFieldName)) {
                factory.fetchSource(FetchSourceContext.fromXContent(context.parser()));
            } else if (SearchSourceBuilder.SCRIPT_FIELDS_FIELD.match(currentFieldName)) {
                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 (SearchSourceBuilder.SCRIPT_FIELD.match(currentFieldName)) {
                                    script = Script.parse(parser);
                                } else if (SearchSourceBuilder.IGNORE_FAILURE_FIELD.match(currentFieldName)) {
                                    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 (SearchSourceBuilder.SCRIPT_FIELD.match(currentFieldName)) {
                                    script = Script.parse(parser);
                                } 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());
                    }
                }
                factory.scriptFields(scriptFields);
            } else if (SearchSourceBuilder.HIGHLIGHT_FIELD.match(currentFieldName)) {
                factory.highlighter(HighlightBuilder.fromXContent(context));
            } else if (SearchSourceBuilder.SORT_FIELD.match(currentFieldName)) {
                List<SortBuilder<?>> sorts = SortBuilder.fromXContent(context);
                factory.sorts(sorts);
            } else {
                throw new ParsingException(parser.getTokenLocation(), "Unknown key for a " + token + " in [" + currentFieldName + "].", parser.getTokenLocation());
            }
        } else if (token == XContentParser.Token.START_ARRAY) {
            if (SearchSourceBuilder.STORED_FIELDS_FIELD.match(currentFieldName)) {
                factory.storedFieldsContext = StoredFieldsContext.fromXContent(SearchSourceBuilder.STORED_FIELDS_FIELD.getPreferredName(), context);
            } else if (SearchSourceBuilder.DOCVALUE_FIELDS_FIELD.match(currentFieldName)) {
                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());
                    }
                }
                factory.fieldDataFields(fieldDataFields);
            } else if (SearchSourceBuilder.SORT_FIELD.match(currentFieldName)) {
                List<SortBuilder<?>> sorts = SortBuilder.fromXContent(context);
                factory.sorts(sorts);
            } else if (SearchSourceBuilder._SOURCE_FIELD.match(currentFieldName)) {
                factory.fetchSource(FetchSourceContext.fromXContent(context.parser()));
            } 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 factory;
}
Also used : SearchScript(org.elasticsearch.script.SearchScript) Script(org.elasticsearch.script.Script) ScriptField(org.elasticsearch.search.builder.SearchSourceBuilder.ScriptField) ParsingException(org.elasticsearch.common.ParsingException) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Aggregations

ScriptField (org.elasticsearch.search.builder.SearchSourceBuilder.ScriptField)6 SearchScript (org.elasticsearch.script.SearchScript)3 ArrayList (java.util.ArrayList)2 SortAndFormats (org.elasticsearch.search.sort.SortAndFormats)2 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 ParsingException (org.elasticsearch.common.ParsingException)1 XContentParser (org.elasticsearch.common.xcontent.XContentParser)1 Script (org.elasticsearch.script.Script)1 DocValueFieldsContext (org.elasticsearch.search.fetch.subphase.DocValueFieldsContext)1 ScriptFieldsContext (org.elasticsearch.search.fetch.subphase.ScriptFieldsContext)1