Package org.elasticsearch.script

Examples of org.elasticsearch.script.SearchScript


        FiltersFunctionScoreQuery.FilterFunction[] filterFunctions = new FiltersFunctionScoreQuery.FilterFunction[filters.size()];
        for (int i = 0; i < filterFunctions.length; i++) {
            ScoreFunction scoreFunction;
            String script = scripts.get(i);
            if (script != null) {
                SearchScript searchScript = context.scriptService().search(context.lookup(), scriptLang, script, vars);
                scoreFunction = new CustomScoreQueryParser.ScriptScoreFunction(searchScript);
            } else {
                scoreFunction = new BoostScoreFunction(boosts.get(i));
            }
            filterFunctions[i] = new FiltersFunctionScoreQuery.FilterFunction(filters.get(i), scoreFunction);
View Full Code Here


        SearchContext context = SearchContext.current();
        if (context == null) {
            throw new ElasticSearchIllegalStateException("No search context on going...");
        }
        SearchScript searchScript = context.scriptService().search(context.lookup(), scriptLang, script, vars);
        FunctionScoreQuery functionScoreQuery = new FunctionScoreQuery(query, new ScriptScoreFunction(searchScript));
        functionScoreQuery.setBoost(boost);
        return functionScoreQuery;
    }
View Full Code Here

                        }
                        if (script != null) {
                            if (searchLookup == null) {
                                searchLookup = new SearchLookup(indexService.mapperService(), indexService.cache().fieldData());
                            }
                            SearchScript searchScript = scriptService.search(searchLookup, "mvel", script, null);
                            searchScript.setNextReader(docIdAndVersion.reader);
                            searchScript.setNextDocId(docIdAndVersion.docId);

                            try {
                                Object value = searchScript.run();
                                if (fields == null) {
                                    fields = newHashMapWithExpectedSize(2);
                                }
                                GetField getField = fields.get(field);
                                if (getField == null) {
                                    getField = new GetField(field, new ArrayList<Object>(2));
                                    fields.put(field, getField);
                                }
                                getField.values().add(value);
                            } catch (RuntimeException e) {
                                if (logger.isTraceEnabled()) {
                                    logger.trace("failed to execute get request script field [{}]", e, script);
                                }
                                // ignore
                            }
                        }
                    }
                }

                return new GetResponse(index, type, id, get.version(), get.exists(), source == null ? null : new BytesHolder(source), fields);
            } else {
                BytesHolder source = get.source();

                Map<String, GetField> fields = null;
                boolean sourceRequested = false;

                // we can only load scripts that can run against the source
                if (gFields == null) {
                    sourceRequested = true;
                } else if (gFields.length == 0) {
                    // no fields, and no source
                    sourceRequested = false;
                } else {
                    Map<String, Object> sourceAsMap = SourceLookup.sourceAsMap(source.bytes(), source.offset(), source.length());
                    SearchLookup searchLookup = null;
                    for (String field : gFields) {
                        if (field.equals("_source")) {
                            sourceRequested = true;
                            continue;
                        }
                        String script = null;
                        if (field.contains("_source.")) {
                            script = field;
                        } else {
                            FieldMappers x = docMapper.mappers().smartName(field);
                            if (x != null) {
                                script = "_source." + x.mapper().names().fullName();
                            }
                        }
                        if (script != null) {
                            if (searchLookup == null) {
                                searchLookup = new SearchLookup(indexService.mapperService(), indexService.cache().fieldData());
                            }
                            SearchScript searchScript = scriptService.search(searchLookup, "mvel", script, null);
                            // we can't do this, only allow to run scripts against the source
                            //searchScript.setNextReader(docIdAndVersion.reader);
                            //searchScript.setNextDocId(docIdAndVersion.docId);

                            // but, we need to inject the parsed source into the script, so it will be used...
                            searchScript.setNextSource(sourceAsMap);

                            try {
                                Object value = searchScript.run();
                                if (fields == null) {
                                    fields = newHashMapWithExpectedSize(2);
                                }
                                GetField getField = fields.get(field);
                                if (getField == null) {
View Full Code Here

                        } else if ("ignore_failure".equals(currentFieldName)) {
                            ignoreException = parser.booleanValue();
                        }
                    }
                }
                SearchScript searchScript = context.scriptService().search(context.lookup(), scriptLang, script, params);
                context.scriptFields().add(new ScriptFieldsContext.ScriptField(fieldName, searchScript, ignoreException));
            }
        }
    }
View Full Code Here

            throw new SearchParseException(context, "_script sorting requires setting the script to sort by");
        }
        if (type == null) {
            throw new SearchParseException(context, "_script sorting requires setting the type of the script");
        }
        SearchScript searchScript = context.scriptService().search(context.lookup(), scriptLang, script, params);
        FieldComparatorSource fieldComparatorSource;
        if ("string".equals(type)) {
            fieldComparatorSource = StringFieldsFunctionDataComparator.comparatorSource(searchScript);
        } else if ("number".equals(type)) {
            fieldComparatorSource = DoubleFieldsFunctionDataComparator.comparatorSource(searchScript);
View Full Code Here

            boolean added = false;
            while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
                String name = parser.text();
                if (name.contains("_source.") || name.contains("doc[")) {
                    // script field to load from source
                    SearchScript searchScript = context.scriptService().search(context.lookup(), "mvel", name, null);
                    context.scriptFields().add(new ScriptFieldsContext.ScriptField(name, searchScript, true));
                } else {
                    if ("*".equals(name)) {
                        added = true;
                        context.fieldNames().add("*");
                    } else {
                        FieldMapper fieldMapper = context.mapperService().smartNameFieldMapper(name);
                        if (fieldMapper != null) {
                            if (fieldMapper.stored()) {
                                added = true;
                                context.fieldNames().add(name);
                            } else {
                                SearchScript searchScript = context.scriptService().search(context.lookup(), "mvel", "_source." + fieldMapper.names().fullName(), null);
                                context.scriptFields().add(new ScriptFieldsContext.ScriptField(name, searchScript, true));
                            }
                        }
                    }
                }
            }
            if (!added) {
                context.emptyFieldNames();
            }
        } else if (token == XContentParser.Token.VALUE_STRING) {
            String name = parser.text();
            if (name.contains("_source.") || name.contains("doc[")) {
                // script field to load from source
                SearchScript searchScript = context.scriptService().search(context.lookup(), "mvel", name, null);
                context.scriptFields().add(new ScriptFieldsContext.ScriptField(name, searchScript, true));
            } else {
                if ("*".equals(name)) {
                    context.fieldNames().add("*");
                } else {
                    FieldMapper fieldMapper = context.mapperService().smartNameFieldMapper(name);
                    if (fieldMapper != null) {
                        if (fieldMapper.stored()) {
                            context.fieldNames().add(name);
                        } else {
                            SearchScript searchScript = context.scriptService().search(context.lookup(), "mvel", "_source." + fieldMapper.names().fullName(), null);
                            context.scriptFields().add(new ScriptFieldsContext.ScriptField(name, searchScript, true));
                        }
                    } else {
                        context.emptyFieldNames(); // don't load anything if we can't find mapping
                    }
View Full Code Here

                                        currentFieldName)) {
                                    ignoreException = parser.booleanValue();
                                }
                            }
                        }
                        SearchScript searchScript = context.scriptService()
                                .search(
                                context.lookup(), scriptLang, script, params);
                        context.scriptFields().add(
                                new ScriptFieldsContext.ScriptField(scriptName,
                                        searchScript, ignoreException));
                    } else {
                        source = parser.text();
                        sourceValue = getLiteral(source);
                    }
                    token = parser.nextToken();
                    assert (token == XContentParser.Token.END_ARRAY);
                } else {
                    source = target = parser.text();
                }
                if (!source.startsWith(
                        SCRIPT_FIELD_PREFIX) && sourceValue == null) {
                    if (source.contains("_source.") || source.contains(
                            "doc[")) {
                        // script field to load from source
                        SearchScript searchScript = context.scriptService()
                                .search(
                                context.lookup(), "mvel", source, null);
                        context.scriptFields().add(
                                new ScriptFieldsContext.ScriptField(source,
                                        searchScript, true));
                    } else {
                        added = true;
                        context.fieldNames().add(source);
                    }
                }
                ((SearchIntoContext) context).outputNames().put(source,
                        target);
            }

            if (!added) {
                context.emptyFieldNames();
            }
        } else if (token == XContentParser.Token.VALUE_STRING) {
            String name = parser.text();
            if (name.contains("_source.") || name.contains("doc[")) {
                // script field to load from source
                SearchScript searchScript = context.scriptService().search(
                        context.lookup(), "mvel", name, null);
                context.scriptFields().add(new ScriptFieldsContext.ScriptField(
                        name, searchScript, true));
            } else {
                context.fieldNames().add(name);
View Full Code Here

        if (script == null) {
            throw new QueryParsingException(parseContext.index(), NAMES[0] + " requires 'script' field");
        }

        SearchScript searchScript;
        try {
            searchScript = parseContext.scriptService().search(parseContext.lookup(), scriptLang, script, scriptType, vars);
            return new ScriptScoreFunction(script, vars, searchScript);
        } catch (Exception e) {
            throw new QueryParsingException(parseContext.index(), NAMES[0] + " the script could not be loaded", e);
View Full Code Here

            throw new SearchParseException(context, "_script sorting requires setting the script to sort by");
        }
        if (type == null) {
            throw new SearchParseException(context, "_script sorting requires setting the type of the script");
        }
        final SearchScript searchScript = context.scriptService().search(context.lookup(), scriptLang, script, scriptType, params);

        if (STRING_SORT_TYPE.equals(type) && (sortMode == MultiValueMode.SUM || sortMode == MultiValueMode.AVG)) {
            throw new SearchParseException(context, "type [string] doesn't support mode [" + sortMode + "]");
        }

        if (sortMode == null) {
            sortMode = reverse ? MultiValueMode.MAX : MultiValueMode.MIN;
        }

        // If nested_path is specified, then wrap the `fieldComparatorSource` in a `NestedFieldComparatorSource`
        ObjectMapper objectMapper;
        final Nested nested;
        if (nestedPath != null) {
            ObjectMappers objectMappers = context.mapperService().objectMapper(nestedPath);
            if (objectMappers == null) {
                throw new ElasticsearchIllegalArgumentException("failed to find nested object mapping for explicit nested path [" + nestedPath + "]");
            }
            objectMapper = objectMappers.mapper();
            if (!objectMapper.nested().isNested()) {
                throw new ElasticsearchIllegalArgumentException("mapping for explicit nested path is not mapped as nested: [" + nestedPath + "]");
            }

            BitDocIdSetFilter rootDocumentsFilter = context.bitsetFilterCache().getBitDocIdSetFilter(NonNestedDocsFilter.INSTANCE);
            BitDocIdSetFilter innerDocumentsFilter;
            if (nestedFilter != null) {
                innerDocumentsFilter = context.bitsetFilterCache().getBitDocIdSetFilter(nestedFilter);
            } else {
                innerDocumentsFilter = context.bitsetFilterCache().getBitDocIdSetFilter(objectMapper.nestedTypeFilter());
            }
            nested = new Nested(rootDocumentsFilter, innerDocumentsFilter);
        } else {
            nested = null;
        }

        final IndexFieldData.XFieldComparatorSource fieldComparatorSource;
        switch (type) {
            case STRING_SORT_TYPE:
                fieldComparatorSource = new BytesRefFieldComparatorSource(null, null, sortMode, nested) {
                    @Override
                    protected SortedBinaryDocValues getValues(LeafReaderContext context) {
                        searchScript.setNextReader(context);
                        final BinaryDocValues values = new BinaryDocValues() {
                            final BytesRefBuilder spare = new BytesRefBuilder();
                            @Override
                            public BytesRef get(int docID) {
                                searchScript.setNextDocId(docID);
                                spare.copyChars(searchScript.run().toString());
                                return spare.get();
                            }
                        };
                        return FieldData.singleton(values, null);
                    }
                    @Override
                    protected void setScorer(Scorer scorer) {
                        searchScript.setScorer(scorer);
                    }
                };
                break;
            case NUMBER_SORT_TYPE:
                // TODO: should we rather sort missing values last?
                fieldComparatorSource = new DoubleValuesComparatorSource(null, Double.MAX_VALUE, sortMode, nested) {
                    @Override
                    protected SortedNumericDoubleValues getValues(LeafReaderContext context) {
                        searchScript.setNextReader(context);
                        final NumericDoubleValues values = new NumericDoubleValues() {
                            @Override
                            public double get(int docID) {
                                searchScript.setNextDocId(docID);
                                return searchScript.runAsDouble();
                            }
                        };
                        return FieldData.singleton(values, null);
                    }
                    @Override
                    protected void setScorer(Scorer scorer) {
                        searchScript.setScorer(scorer);
                    }
                };
                break;
            default:
                throw new SearchParseException(context, "custom script sort type [" + type + "] not supported");
View Full Code Here

                    script = scriptValue.script();
                    scriptType = scriptValue.scriptType();
                }
                scriptLang = scriptParameterParser.lang();
               
                SearchScript searchScript = context.scriptService().search(context.lookup(), scriptLang, script, scriptType, params);
                context.scriptFields().add(new ScriptFieldsContext.ScriptField(fieldName, searchScript, ignoreException));
            }
        }
    }
View Full Code Here

TOP

Related Classes of org.elasticsearch.script.SearchScript

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.