org.elasticsearch.script.Script Java Examples

The following examples show how to use org.elasticsearch.script.Script. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ElasticsearchTransactionManager.java    From jstarcraft-core with Apache License 2.0 7 votes vote down vote up
@Override
protected void unlock(TransactionDefinition definition) {
    // 尝试解锁
    String key = definition.getName();
    Long value = definition.getMost().toEpochMilli();
    Map<String, Object> document = new HashMap<>();
    document.put(NAME, key);
    document.put(MOST, value);
    document.put(NOW, Instant.now().toEpochMilli());
    UpdateRequest request = new UpdateRequest().index(index).type(type)

            .id(key)

            .script(new Script(ScriptType.INLINE, SCRIPT, UNLOCK_SCRIPT, document));
    try {
        UpdateResponse response = elastic.update(request, RequestOptions.DEFAULT);
        if (response.getResult() == DocWriteResponse.Result.NOOP) {
            throw new TransactionUnlockException();
        }
    } catch (Exception exception) {
        throw new TransactionUnlockException(exception);
    }
}
 
Example #2
Source File: ScriptHeuristic.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
public ScriptHeuristic(ExecutableScript searchScript, Script script) {
    subsetSizeHolder = new LongAccessor();
    supersetSizeHolder = new LongAccessor();
    subsetDfHolder = new LongAccessor();
    supersetDfHolder = new LongAccessor();
    this.searchScript = searchScript;
    if (searchScript != null) {
        searchScript.setNextVar("_subset_freq", subsetDfHolder);
        searchScript.setNextVar("_subset_size", subsetSizeHolder);
        searchScript.setNextVar("_superset_freq", supersetDfHolder);
        searchScript.setNextVar("_superset_size", supersetSizeHolder);
    }
    this.script = script;


}
 
Example #3
Source File: UpdateQueryParser.java    From elasticsearch-sql with MIT License 6 votes vote down vote up
private void parseUpdateByQuery(ElasticDslContext dslContext) {
    dslContext.getParseResult().setSqlOperation(SqlOperation.UPDATE_BY_QUERY);
    ElasticsearchParser.UpdateOperationContext updateOperationContext = dslContext.getSqlContext().updateOperation();
    String indexName = updateOperationContext.tableRef().indexName.getText();
    UpdateByQueryRequest updateByQueryRequest = new UpdateByQueryRequest(indexName);
    int size = updateOperationContext.ID().size();
    StringBuilder builder = new StringBuilder();
    Map<String, Object> params = new HashMap<>(0);
    for (int i = 0; i < size; i++) {
        builder.append(UPDATE_PREFIX).append(updateOperationContext.ID(i).getText()).append('=');
        if (updateOperationContext.identity(i).identityList() != null) {
            builder.append("params.").append(PARAM_PREFIX).append(params.size());
            params.put(PARAM_PREFIX + params.size(), parseIdentityList(updateOperationContext.identity(i).identityList().identity()));
        } else if (updateOperationContext.identity(i).STRING() != null) {
            builder.append('\'').append(StringManager.removeStringSymbol(updateOperationContext.identity(i).STRING().getText())).append('\'');
        } else {
            builder.append(updateOperationContext.identity(i).number.getText());
        }
        builder.append(';');
    }
    updateByQueryRequest.setScript(new Script(ScriptType.INLINE, "painless", builder.toString(), params));
    if (updateOperationContext.routingClause() != null) {
        updateByQueryRequest.setRouting(StringManager.removeStringSymbol(updateOperationContext.routingClause().STRING(0).getText()));
    }
    if (updateOperationContext.whereClause() != null) {
        BoolExpressionParser boolExpressionParser = new BoolExpressionParser();
        updateByQueryRequest.setQuery(boolExpressionParser.parseBoolQueryExpr(updateOperationContext.whereClause().expression()));
    } else {
        updateByQueryRequest.setQuery(QueryBuilders.matchAllQuery());
    }
    if (updateOperationContext.batchClause() != null) {
        updateByQueryRequest.setBatchSize(Integer.parseInt(updateOperationContext.batchClause().size.getText()));
    }
    if (updateOperationContext.limitClause() != null) {
        updateByQueryRequest.setMaxDocs(Integer.parseInt(updateOperationContext.limitClause().size.getText()));
    }
    dslContext.getParseResult().setUpdateByQueryRequest(updateByQueryRequest);
}
 
Example #4
Source File: DefaultQueryAction.java    From elasticsearch-sql with Apache License 2.0 6 votes vote down vote up
/**
 * Add sorts to the elasticsearch query based on the 'ORDER BY' clause.
 * 
 * @param orderBys
 *            list of Order object
 */
private void setSorts(List<Order> orderBys) {
	for (Order order : orderBys) {
           if (order.getNestedPath() != null) {
               request.addSort(SortBuilders.fieldSort(order.getName()).order(SortOrder.valueOf(order.getType())).setNestedSort(new NestedSortBuilder(order.getNestedPath())));
           } else if (order.getName().contains("script(")) { //zhongshu-comment 该分支是我后来加的,用于兼容order by case when那种情况

			String scriptStr = order.getName().substring("script(".length(), order.getName().length() - 1);
			Script script = new Script(scriptStr);
			ScriptSortBuilder scriptSortBuilder = SortBuilders.scriptSort(script, order.getScriptSortType());

			scriptSortBuilder = scriptSortBuilder.order(SortOrder.valueOf(order.getType()));
			request.addSort(scriptSortBuilder);
		} else {
               request.addSort(
               		order.getName(),
					SortOrder.valueOf(order.getType()));
           }
	}
}
 
Example #5
Source File: ElasticSearchIntegrationTest.java    From core-ng-project with Apache License 2.0 6 votes vote down vote up
@Test
void search() {
    TestDocument document = document("1", "1st Test's Product", 1, 0, null, LocalTime.NOON);
    documentType.index(document.id, document);
    elasticSearch.refreshIndex("document");

    // test synonyms
    SearchRequest request = new SearchRequest();
    request.query = boolQuery()
        .must(matchQuery("string_field", "first"))
        .filter(termQuery("enum_field", JSON.toEnumValue(TestDocument.TestEnum.VALUE1)));
    request.sorts.add(SortBuilders.scriptSort(new Script("doc['int_field'].value * 3"), ScriptSortBuilder.ScriptSortType.NUMBER));
    SearchResponse<TestDocument> response = documentType.search(request);

    assertThat(response.totalHits).isEqualTo(1);
    assertThat(response.hits.get(0)).isEqualToIgnoringGivenFields(document, "zonedDateTimeField");

    // test stemmer
    request = new SearchRequest();
    request.query = matchQuery("string_field", "test");
    response = documentType.search(request);

    assertThat(response.totalHits).isEqualTo(1);
    assertThat(response.hits.get(0)).isEqualToIgnoringGivenFields(document, "zonedDateTimeField");
}
 
Example #6
Source File: ScriptFeature.java    From elasticsearch-learning-to-rank with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public ScriptFeature(String name, Script script, Collection<String> queryParams) {
    this.name = Objects.requireNonNull(name);
    this.script = Objects.requireNonNull(script);
    this.queryParams = queryParams;
    Map<String, Object> ltrScriptParams = new HashMap<>();
    Map<String, String> ltrExtraScriptParams = new HashMap<>();
    for (Map.Entry<String, Object> entry : script.getParams().entrySet()) {
        if (!entry.getKey().equals(EXTRA_SCRIPT_PARAMS)) {
            ltrScriptParams.put(String.valueOf(entry.getKey()), entry.getValue());
        } else {
            ltrExtraScriptParams = (Map<String, String>) entry.getValue();
        }
    }
    this.baseScriptParams = ltrScriptParams;
    this.extraScriptParams = ltrExtraScriptParams;
}
 
Example #7
Source File: ValuesSourceAggregationBuilder.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
protected final XContentBuilder internalXContent(XContentBuilder builder, Params builderParams) throws IOException {
    builder.startObject();
    if (field != null) {
        builder.field("field", field);
    }

    if (script == null) {
        if (scriptString != null) {
            builder.field("script", new Script(scriptString, ScriptType.INLINE, lang, params));
        }
    } else {
        builder.field("script", script);
    }
    if (missing != null) {
        builder.field("missing", missing);
    }

    doInternalXContent(builder, builderParams);
    return builder.endObject();
}
 
Example #8
Source File: PredicateAnalyzer.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
private static QueryBuilder genScriptFilter(RexNode expression, StoragePluginId pluginId, boolean variationDetected, Throwable cause)
    throws ExpressionNotAnalyzableException {
  try {
    final boolean supportsV5Features = pluginId.getCapabilities().getCapability(ElasticsearchStoragePlugin.ENABLE_V5_FEATURES);
    final ElasticsearchConf config = ElasticsearchConf.createElasticsearchConf(pluginId.getConnectionConf());
    final Script script = ProjectAnalyzer.getScript(
        expression,
        config.isUsePainless(),
        supportsV5Features,
        config.isScriptsEnabled(),
        false, /* _source is not available in filter context */
        config.isAllowPushdownOnNormalizedOrAnalyzedFields(),
        variationDetected);
    QueryBuilder builder = scriptQuery(script);
    return builder;
  } catch (Throwable t) {
    cause.addSuppressed(t);
    throw new ExpressionNotAnalyzableException(format(
        "Failed to fully convert predicate: [%s] into an elasticsearch filter", expression), cause);
  }
}
 
Example #9
Source File: ElasticsearchHammingDistanceScoringStrategy.java    From vertexium with Apache License 2.0 6 votes vote down vote up
@Override
public QueryBuilder updateElasticsearchQuery(
    Graph graph,
    Elasticsearch7SearchIndex searchIndex,
    QueryBuilder query,
    QueryParameters queryParameters
) {
    List<String> fieldNames = getFieldNames(graph, searchIndex, queryParameters, getField());
    if (fieldNames == null) {
        return query;
    }

    HashMap<String, Object> scriptParams = new HashMap<>();
    scriptParams.put("hash", getHash());
    scriptParams.put("fieldNames", fieldNames);
    Script script = new Script(ScriptType.INLINE, "painless", scriptSrc, scriptParams);
    return QueryBuilders.functionScoreQuery(query, new ScriptScoreFunctionBuilder(script));
}
 
Example #10
Source File: ElasticsearchFieldValueScoringStrategy.java    From vertexium with Apache License 2.0 6 votes vote down vote up
@Override
public QueryBuilder updateElasticsearchQuery(
    Graph graph,
    Elasticsearch7SearchIndex searchIndex,
    QueryBuilder query,
    QueryParameters queryParameters
) {
    List<String> fieldNames = getFieldNames(graph, searchIndex, queryParameters, getField());
    if (fieldNames == null) {
        return query;
    }

    HashMap<String, Object> scriptParams = new HashMap<>();
    scriptParams.put("fieldNames", fieldNames);
    Script script = new Script(ScriptType.INLINE, "painless", scriptSrc, scriptParams);
    return QueryBuilders.functionScoreQuery(query, new ScriptScoreFunctionBuilder(script));
}
 
Example #11
Source File: ScriptSortBuilder.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params builderParams) throws IOException {
    builder.startObject("_script");
    if (script == null) {

        builder.field("script", new Script(scriptString, ScriptType.INLINE, lang, params));
    } else {
        builder.field("script", script);
    }
    builder.field("type", type);
    if (order == SortOrder.DESC) {
        builder.field("order", "desc");
    }
    if (sortMode != null) {
        builder.field("mode", sortMode);
    }
    if (nestedPath != null) {
        builder.field("nested_path", nestedPath);
    }
    if (nestedFilter != null) {
        builder.field("nested_filter", nestedFilter, builderParams);
    }
    builder.endObject();
    return builder;
}
 
Example #12
Source File: ScriptedMetricAggregator.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
protected ScriptedMetricAggregator(String name, Script initScript, Script mapScript, Script combineScript, Script reduceScript,
        Map<String, Object> params, AggregationContext context, Aggregator parent, List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData)
        throws IOException {
    super(name, context, parent, pipelineAggregators, metaData);
    this.params = params;
    ScriptService scriptService = context.searchContext().scriptService();
    if (initScript != null) {
        scriptService.executable(initScript, ScriptContext.Standard.AGGS, context.searchContext(), Collections.<String, String>emptyMap()).run();
    }
    this.mapScript = scriptService.search(context.searchContext().lookup(), mapScript, ScriptContext.Standard.AGGS, Collections.<String, String>emptyMap());
    if (combineScript != null) {
        this.combineScript = scriptService.executable(combineScript, ScriptContext.Standard.AGGS, context.searchContext(), Collections.<String, String>emptyMap());
    } else {
        this.combineScript = null;
    }
    this.reduceScript = reduceScript;
}
 
Example #13
Source File: ElasticsearchHammingDistanceScoringStrategy.java    From vertexium with Apache License 2.0 6 votes vote down vote up
@Override
public QueryBuilder updateElasticsearchQuery(
    Graph graph,
    Elasticsearch5SearchIndex searchIndex,
    QueryBuilder query,
    QueryParameters queryParameters
) {
    List<String> fieldNames = getFieldNames(graph, searchIndex, queryParameters, getField());
    if (fieldNames == null) {
        return query;
    }

    HashMap<String, Object> scriptParams = new HashMap<>();
    scriptParams.put("hash", getHash());
    scriptParams.put("fieldNames", fieldNames);
    Script script = new Script(ScriptType.INLINE, "painless", scriptSrc, scriptParams);
    return QueryBuilders.functionScoreQuery(query, new ScriptScoreFunctionBuilder(script));
}
 
Example #14
Source File: ElasticsearchLockProvider.java    From ShedLock with Apache License 2.0 6 votes vote down vote up
@Override
public void doUnlock() {
    // Set lockUtil to now or lockAtLeastUntil whichever is later
    try {
        UpdateRequest ur = new UpdateRequest()
            .index(index)
            .type(type)
            .id(lockConfiguration.getName())
            .script(new Script(ScriptType.INLINE,
                "painless",
                "ctx._source.lockUntil = params.unlockTime",
                Collections.singletonMap("unlockTime", lockConfiguration.getUnlockTime().toEpochMilli())));
        highLevelClient.update(ur, RequestOptions.DEFAULT);
    } catch (IOException | ElasticsearchException e) {
        throw new LockException("Unexpected exception occurred", e);
    }
}
 
Example #15
Source File: LtrQueryBuilderTests.java    From elasticsearch-learning-to-rank with Apache License 2.0 5 votes vote down vote up
@Override
protected LtrQueryBuilder doCreateTestQueryBuilder() {
    LtrQueryBuilder builder = new LtrQueryBuilder();
    builder.features(Arrays.asList(
            new MatchQueryBuilder("foo", "bar"),
            new MatchQueryBuilder("baz", "sham")
    ));
    builder.rankerScript(new Script(ScriptType.INLINE, "ranklib",
            // Remove escape sequences
            simpleModel.replace("\\\"", "\"")
                    .replace("\\n", "\n"),
            Collections.emptyMap()));
    return builder;
}
 
Example #16
Source File: ElasticsearchIndexer.java    From datashare with GNU Affero General Public License v3.0 5 votes vote down vote up
private Script createUntagScript(Tag[] tags) {
    return new Script(ScriptType.INLINE, "painless",
            "int updates = 0;" +
                    "for (int i = 0; i < params.tags.length; i++) {" +
                    "  if (ctx._source.tags.contains(params.tags[i])) {" +
                    "    ctx._source.tags.remove(ctx._source.tags.indexOf(params.tags[i]));" +
                    "    updates++;" +
                    "  }" +
                    "}" +
                    "if (updates == 0) ctx.op = 'noop';",
            new HashMap<String, Object>() {{put("tags", stream(tags).map(t -> t.label).collect(toList()));}});
}
 
Example #17
Source File: PredicateAnalyzer.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
public static Script getScript(String script, StoragePluginId pluginId) {
  if (pluginId.getCapabilities().getCapability(ElasticsearchStoragePlugin.ENABLE_V5_FEATURES)) {
    // when returning a painless script, let's make sure we cast to a valid output type.
    return new Script(ScriptType.INLINE, "painless", String.format("(def) (%s)", script), ImmutableMap.of());
  } else {
    // keeping this so plan matching tests will pass
    return new Script(ScriptType.INLINE, "groovy", script, ImmutableMap.of());
  }
}
 
Example #18
Source File: ElasticsearchIndexer.java    From datashare with GNU Affero General Public License v3.0 5 votes vote down vote up
private boolean groupTagUntag(Project prj, List<String> documentIds, Script untagScript) throws IOException {
    UpdateByQueryRequest updateByQuery = new UpdateByQueryRequest(prj.getId());
    updateByQuery.setQuery(termsQuery("_id", documentIds.toArray(new String[0])));
    updateByQuery.setConflicts("proceed");
    updateByQuery.setScript(untagScript);
    updateByQuery.setRefresh(esCfg.refreshPolicy.getValue().equals("true"));
    BulkByScrollResponse updateResponse = client.updateByQuery(updateByQuery, RequestOptions.DEFAULT);
    return updateResponse.getBulkFailures().size() == 0 && updateResponse.getUpdated() > 0 ;
}
 
Example #19
Source File: ElasticsearchSearchQueryBase.java    From vertexium with Apache License 2.0 5 votes vote down vote up
private Collection<? extends AggregationBuilder> getElasticsearchCalendarFieldAggregation(CalendarFieldAggregation agg) {
    List<AggregationBuilder> aggs = new ArrayList<>();
    PropertyDefinition propertyDefinition = getPropertyDefinition(agg.getPropertyName());
    if (propertyDefinition == null) {
        throw new VertexiumException("Could not find mapping for property: " + agg.getPropertyName());
    }
    Class propertyDataType = propertyDefinition.getDataType();
    for (String propertyName : getPropertyNames(agg.getPropertyName())) {
        String visibilityHash = getSearchIndex().getPropertyVisibilityHashFromPropertyName(propertyName);
        String aggName = createAggregationName(agg.getAggregationName(), visibilityHash);
        if (propertyDataType == Date.class) {
            HistogramAggregationBuilder histAgg = AggregationBuilders.histogram(aggName);
            histAgg.interval(1);
            if (agg.getMinDocumentCount() != null) {
                histAgg.minDocCount(agg.getMinDocumentCount());
            } else {
                histAgg.minDocCount(1L);
            }
            Script script = new Script(
                ScriptType.INLINE,
                "painless",
                getCalendarFieldAggregationScript(agg, propertyName),
                ImmutableMap.of(
                    "tzId", agg.getTimeZone().getID(),
                    "fieldName", propertyName,
                    "calendarField", agg.getCalendarField())
            );
            histAgg.script(script);

            for (AggregationBuilder subAgg : getElasticsearchAggregations(agg.getNestedAggregations())) {
                histAgg.subAggregation(subAgg);
            }

            aggs.add(histAgg);
        } else {
            throw new VertexiumException("Only dates are supported for hour of day aggregations");
        }
    }
    return aggs;
}
 
Example #20
Source File: ScriptFeature.java    From elasticsearch-learning-to-rank with Apache License 2.0 5 votes vote down vote up
public static ScriptFeature compile(StoredFeature feature) {
    try {
        XContentParser xContentParser = XContentType.JSON.xContent().createParser(NamedXContentRegistry.EMPTY,
                LoggingDeprecationHandler.INSTANCE, feature.template());
        return new ScriptFeature(feature.name(), Script.parse(xContentParser, "native"), feature.queryParams());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example #21
Source File: ElasticsearchIndexer.java    From datashare with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public boolean bulkAdd(final String indexName, Pipeline.Type nerType, List<NamedEntity> namedEntities, Document parent) throws IOException {
    BulkRequest bulkRequest = new BulkRequest();

    String routing = ofNullable(parent.getRootDocument()).orElse(parent.getId());
    bulkRequest.add(new UpdateRequest(indexName, esCfg.indexType, parent.getId()).doc(
            jsonBuilder().startObject()
                    .field("status", Document.Status.DONE)
                    .endObject()).routing(routing));
    bulkRequest.add(new UpdateRequest(indexName, esCfg.indexType, parent.getId())
            .script(new Script(ScriptType.INLINE, "painless",
                    "if (!ctx._source.nerTags.contains(params.nerTag)) ctx._source.nerTags.add(params.nerTag);",
                    new HashMap<String, Object>() {{put("nerTag", nerType.toString());}})).routing(routing));

    for (Entity child : namedEntities) {
        bulkRequest.add(createIndexRequest(indexName, JsonObjectMapper.getType(child), child.getId(),
                getJson(child), parent.getId(), routing));
    }
    bulkRequest.setRefreshPolicy(esCfg.refreshPolicy);

    BulkResponse bulkResponse = client.bulk(bulkRequest);
    if (bulkResponse.hasFailures()) {
        for (BulkItemResponse resp : bulkResponse.getItems()) {
            if (resp.isFailed()) {
                LOGGER.error("bulk add failed : {}", resp.getFailureMessage());
            }
        }
        return false;
    }
    return true;
}
 
Example #22
Source File: ElasticsearchIndexer.java    From datashare with GNU Affero General Public License v3.0 5 votes vote down vote up
private Script createTagScript(Tag[] tags) {
    return new Script(ScriptType.INLINE, "painless",
            "int updates = 0;" +
                    "if (ctx._source.tags == null) ctx._source.tags = [];" +
                    "for (int i = 0; i < params.tags.length; i++) {" +
                    "  if (!ctx._source.tags.contains(params.tags[i])) {" +
                    "   ctx._source.tags.add(params.tags[i]);" +
                    "   updates++;" +
                    "  }" +
                    "}" +
                    "if (updates == 0) ctx.op = 'noop';",
            new HashMap<String, Object>() {{put("tags", stream(tags).map(t -> t.label).collect(toList()));}});
}
 
Example #23
Source File: UpdateRequest.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public void readFrom(StreamInput in) throws IOException {
    super.readFrom(in);
    consistencyLevel = WriteConsistencyLevel.fromId(in.readByte());
    type = in.readString();
    id = in.readString();
    routing = in.readOptionalString();
    parent = in.readOptionalString();
    if (in.readBoolean()) {
        script = Script.readScript(in);
    }
    retryOnConflict = in.readVInt();
    refresh = in.readBoolean();
    if (in.readBoolean()) {
        doc = new IndexRequest();
        doc.readFrom(in);
    }
    int size = in.readInt();
    if (size >= 0) {
        fields = new String[size];
        for (int i = 0; i < size; i++) {
            fields[i] = in.readString();
        }
    }
    if (in.readBoolean()) {
        upsertRequest = new IndexRequest();
        upsertRequest.readFrom(in);
    }
    docAsUpsert = in.readBoolean();
    version = in.readLong();
    versionType = VersionType.fromValue(in.readByte());
    detectNoop = in.readBoolean();
    scriptedUpsert = in.readBoolean();
}
 
Example #24
Source File: AggMaker.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
public MovFnPipelineAggregationBuilder makeMovingFieldAgg(MethodField field, AggregationBuilder parent) throws SqlParseException {
    //question 加到groupMap里是为了什么
    groupMap.put(field.getAlias(), new KVValue("FIELD", parent));

    String bucketPath = field.getParams().get(0).value.toString();
    int window = Integer.parseInt(field.getParams().get(1).value.toString());

    ValuesSourceAggregationBuilder builder;
    field.setAlias(fixAlias(field.getAlias()));
    switch (field.getName().toUpperCase()) {
        //added by xzb 增加 movingavg和rollingstd
        case "MOVINGAVG":
            MovFnPipelineAggregationBuilder mvAvg =
                    //PipelineAggregatorBuilders.movingFunction("movingAvgIncome", new Script("MovingFunctions.unweightedAvg(values)"), "incomeSum", 2);
                    PipelineAggregatorBuilders.movingFunction(field.getAlias(),  new Script("MovingFunctions.unweightedAvg(values)"), bucketPath, window);

            return mvAvg;

        case "ROLLINGSTD":
            MovFnPipelineAggregationBuilder stdDev =
                    //PipelineAggregatorBuilders.movingFunction("stdDevIncome", new Script("MovingFunctions.stdDev(values, MovingFunctions.unweightedAvg(values))"), "incomeSum", 2);
                    PipelineAggregatorBuilders.movingFunction(field.getAlias() , new Script("MovingFunctions.stdDev(values, MovingFunctions.unweightedAvg(values))"), bucketPath, window);

            return stdDev;
    }
    return  null;
}
 
Example #25
Source File: DocumentMapper.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
/**
 * @deprecated Use {@link #transform(ScriptService, Script)} instead.
 */
@Deprecated
public Builder transform(ScriptService scriptService, String script, ScriptType scriptType, String language,
        Map<String, Object> parameters) {
    sourceTransforms.add(new ScriptTransform(scriptService, new Script(script, scriptType, language, parameters)));
    return this;
}
 
Example #26
Source File: DocumentMapperParser.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private void parseTransform(DocumentMapper.Builder docBuilder, Map<String, Object> transformConfig, Version indexVersionCreated) {
    Script script = Script.parse(transformConfig, true, parseFieldMatcher);
    if (script != null) {
        docBuilder.transform(scriptService, script);
    }
    checkNoRemainingFields(transformConfig, indexVersionCreated, "Transform config has unsupported parameters: ");
}
 
Example #27
Source File: GroupParser.java    From sql4es with Apache License 2.0 5 votes vote down vote up
/**
 * Adds a set of 'leaf aggregations' to the provided parent metric (i.e. count, sum, max etc)
 * @param parentAgg
 * @param heading
 * @param addCount
 */
@SuppressWarnings("rawtypes")
private void addMetrics(AggregationBuilder parentAgg, Heading heading, boolean addCount){
	for(Column metric : heading.columns()){
		AggregationBuilder agg = null;
	    if(metric.getOp() == Operation.AVG) {
               agg = AggregationBuilders.avg(metric.getAggName());
           } else if(addCount && metric.getOp() == Operation.COUNT) {
               agg = AggregationBuilders.count(metric.getAggName());
           } else if(metric.getOp() == Operation.MAX) {
               agg = AggregationBuilders.max(metric.getAggName());
           } else if(metric.getOp() == Operation.MIN) {
               agg = AggregationBuilders.min(metric.getAggName());
           } else if(metric.getOp() == Operation.SUM) {
               agg = AggregationBuilders.sum(metric.getAggName());
           }
		if (agg != null) {
	        String col = metric.getColumn();
	        // * or number
	        if ("*".equals(col) || col.matches("-?\\d+")) {
                   agg = ((ValuesSourceAggregationBuilder.LeafOnly)agg).script(new Script("1"));
               } else if (col.startsWith("script:")) {
                   agg = ((ValuesSourceAggregationBuilder.LeafOnly)agg).script(new Script(col.replaceAll("script:","")));
               } else {
                   agg = ((ValuesSourceAggregationBuilder.LeafOnly) agg).field(metric.getColumn());
               }
               parentAgg.subAggregation(agg);
           }
	}
}
 
Example #28
Source File: NestedUpdateGroovyScritpBuilder.java    From search-spring-boot-starter with Apache License 2.0 5 votes vote down vote up
public Script build(NestedESObject nestedESObject, OperateTypeEnum operateType, Map<Object, Object> dataMap) {
    switch (operateType) {
        case ADD:
            return addTag(nestedESObject, dataMap);
        case UPDATE:
            return updateTag(nestedESObject, dataMap);
        case DELETE:
            return deleteTag(nestedESObject, dataMap);
        default:
            return null;
    }
}
 
Example #29
Source File: ElasticsearchLengthOfStringSortingStrategy.java    From vertexium with Apache License 2.0 5 votes vote down vote up
@Override
public void updateElasticsearchQuery(
    Graph graph,
    Elasticsearch7SearchIndex searchIndex,
    SearchRequestBuilder q,
    QueryParameters parameters,
    SortDirection direction
) {
    PropertyDefinition propertyDefinition = graph.getPropertyDefinition(getPropertyName());

    SortOrder esOrder = direction == SortDirection.ASCENDING ? SortOrder.ASC : SortOrder.DESC;
    Map<String, Object> scriptParams = new HashMap<>();
    String[] propertyNames = searchIndex.getPropertyNames(graph, getPropertyName(), parameters.getAuthorizations());
    List<String> fieldNames = Arrays.stream(propertyNames)
        .map(propertyName -> {
            String suffix = propertyDefinition.getDataType() == String.class
                ? Elasticsearch7SearchIndex.EXACT_MATCH_PROPERTY_NAME_SUFFIX
                : "";
            return propertyName + suffix;
        })
        .collect(Collectors.toList());
    scriptParams.put("fieldNames", fieldNames);
    scriptParams.put("direction", esOrder.name());
    Script script = new Script(ScriptType.INLINE, "painless", scriptSource, scriptParams);
    ScriptSortBuilder.ScriptSortType sortType = ScriptSortBuilder.ScriptSortType.NUMBER;
    q.addSort(SortBuilders.scriptSort(script, sortType).order(SortOrder.ASC));
}
 
Example #30
Source File: UpdatByQueryDemo.java    From elasticsearch-full with Apache License 2.0 5 votes vote down vote up
@Test
public void test() throws Exception {
    UpdateByQueryRequestBuilder updateByQueryRequestBuilder = UpdateByQueryAction.INSTANCE.newRequestBuilder(client);
    updateByQueryRequestBuilder
            .script(new Script(ScriptType.INLINE,"painless","ctx_source.likes++",null))
            .source()
            .setQuery(QueryBuilders.termQuery("user","kimchy"))
            .setIndices("twitter")
            .get();
}