org.elasticsearch.script.ScriptType Java Examples

The following examples show how to use org.elasticsearch.script.ScriptType. 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: 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 #3
Source File: IndexSearchDaoImpl.java    From herd with Apache License 2.0 6 votes vote down vote up
/**
 * Processes the scripts and score function
 *
 * @param queryBuilder the query builder
 *
 * @return the function score query builder
 */
private FunctionScoreQueryBuilder getFunctionScoreQueryBuilder(QueryBuilder queryBuilder, String bdefActiveIndex)
{
    // Script for tag search score multiplier. If bdef set to tag search score multiplier else set to a default value.
    String inlineScript = "_score * (doc['_index'].value == '" + bdefActiveIndex + "' ? doc['" + BDEF_TAGS_SEARCH_SCORE_MULTIPLIER + "'].value : 1)";

    // Set the lang to painless
    Script script = new Script(ScriptType.INLINE, "painless", inlineScript, Collections.emptyMap());

    // Set the script
    ScriptScoreFunctionBuilder scoreFunction = ScoreFunctionBuilders.scriptFunction(script);

    // Create function score query builder
    return new FunctionScoreQueryBuilder(queryBuilder, scoreFunction);
}
 
Example #4
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 #5
Source File: ElasticsearchFieldValueScoringStrategy.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("fieldNames", fieldNames);
    Script script = new Script(ScriptType.INLINE, "painless", scriptSrc, scriptParams);
    return QueryBuilders.functionScoreQuery(query, new ScriptScoreFunctionBuilder(script));
}
 
Example #6
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 #7
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 #8
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 #9
Source File: ElasticsearchJavaAPIScriptTest.java    From syncer with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
   * https://discuss.elastic.co/t/java-api-plainless-script-indexof-give-wrong-answer/139016/7
   */
  public static void scriptIndexOf() throws Exception {
    AbstractClient client = ElasticTestUtil.getDevClient();

    HashMap<String, Object> params = new HashMap<>();
    params.put("users", LONG_ID);
    Script add = new Script(ScriptType.INLINE, "painless",
        "ctx._source.users.add(params.users);",
        params);
    Script meta = new Script(ScriptType.INLINE, "painless",
        "ctx._source.t1 = params.users; ctx._source.i11= ctx._source.users.indexOf(params.users); ctx._source.i21= ctx._source.users.lastIndexOf(params.users);",
        params);
    Script remove = new Script(ScriptType.INLINE, "painless",
        "ctx._source.users.remove(ctx._source.users.indexOf(params.users));",
        params);

    BulkRequestBuilder bulkRequest = client.prepareBulk();
    bulkRequest.add(indexRequest(client, getSource()));
    bulkRequest.add(updateRequest(client, add));
    bulkRequest.add(updateRequest(client, meta));
    bulkRequest.add(updateRequest(client, remove));
    bulkRequest.add(updateRequest(client, meta));
    BulkResponse bulkItemResponses = bulkRequest.execute().actionGet();
    if (bulkItemResponses.hasFailures()) {
      for (BulkItemResponse itemResponse : bulkItemResponses.getItems()) {
        System.out.println(itemResponse.getFailure());
      }
//      Assert.fail();
    }
  }
 
Example #10
Source File: ScriptFilter.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
private void parseAndUpdateScriptType(String scriptType) {
    String scriptTypeUpper = scriptType.toUpperCase();
    switch(scriptTypeUpper){
        case "INLINE":
            this.scriptType = ScriptType.INLINE;
            break;
        case "INDEXED":
        case "STORED":
            this.scriptType = ScriptType.STORED;
            break;
    }
}
 
Example #11
Source File: DynamicRanker.java    From elasticsearch-dynarank with Apache License 2.0 5 votes vote down vote up
ScriptInfo(final String script, final String lang, final String scriptType, final Settings settings, final int reorderSize) {
    this.script = script;
    this.lang = lang;
    this.reorderSize = reorderSize;
    this.settings = new HashMap<>();
    for (final String name : settings.keySet()) {
        final List<String> list = settings.getAsList(name);
        this.settings.put(name, list.toArray(new String[list.size()]));
    }
    if ("STORED".equalsIgnoreCase(scriptType)) {
        this.scriptType = ScriptType.STORED;
    } else {
        this.scriptType = ScriptType.INLINE;
    }
}
 
Example #12
Source File: RestHighLevelClientCase.java    From skywalking with Apache License 2.0 5 votes vote down vote up
private void update(RestHighLevelClient client, String indexName) throws IOException {
    UpdateRequest request = new UpdateRequest(indexName, "_doc", "1");
    Map<String, Object> parameters = singletonMap("title", "c++ programing.");
    Script inline = new Script(ScriptType.INLINE, "painless", "ctx._source.title = params.title", parameters);
    request.script(inline);

    UpdateResponse updateResponse = client.update(request, RequestOptions.DEFAULT);
    if (updateResponse.getVersion() != 2) {
        String message = "elasticsearch update data fail.";
        logger.error(message);
        throw new RuntimeException(message);
    }
}
 
Example #13
Source File: CaseController.java    From skywalking with Apache License 2.0 5 votes vote down vote up
private void update(String indexName) throws IOException {
    UpdateRequest request = new UpdateRequest(indexName, "1");
    Map<String, Object> parameters = singletonMap("title", "c++ programing.");
    Script inline = new Script(ScriptType.INLINE, "painless", "ctx._source.title = params.title", parameters);
    request.script(inline);

    UpdateResponse updateResponse = client.update(request, RequestOptions.DEFAULT);
    if (updateResponse.getVersion() != 2) {
        String message = "elasticsearch update data fail.";
        logger.error(message);
        throw new RuntimeException(message);
    }
}
 
Example #14
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 #15
Source File: ReindexQueryParser.java    From elasticsearch-sql with MIT License 5 votes vote down vote up
private void buildScript(ElasticsearchParser.ReindexOperationContext reindexOperationContext, ReindexRequest reindexRequest) {
    StringBuilder script = new StringBuilder();
    for (ElasticsearchParser.NameOperandContext nameOperandContext : reindexOperationContext.fieldList().nameOperand()) {
        if (nameOperandContext.exclude != null) {
            script.append("ctx._source.remove('").append(nameOperandContext.fieldName.getText()).append("')';");
        }
    }
    if (StringUtils.isNotBlank(script.toString())) {
        reindexRequest.setScript(new Script(ScriptType.INLINE, "painless", script.toString(), Collections.emptyMap()));
    }
}
 
Example #16
Source File: AbstractEs6_4ClientInstrumentationTest.java    From apm-agent-java with Apache License 2.0 5 votes vote down vote up
private SearchTemplateRequest prepareSearchTemplateRequest() {
    SearchTemplateRequest searchTemplateRequest = new SearchTemplateRequest();
    searchTemplateRequest.setRequest(new SearchRequest(INDEX));
    searchTemplateRequest.setScriptType(ScriptType.INLINE);
    searchTemplateRequest.setScript(
        "{" +
            "  \"query\": { \"term\" : { \"{{field}}\" : \"{{value}}\" } }," +
            "  \"size\" : \"{{size}}\"" +
            "}");
    Map<String, Object> scriptParams = new HashMap<>();
    scriptParams.put("field", FOO);
    scriptParams.put("value", BAR);
    scriptParams.put("size", 5);
    searchTemplateRequest.setScriptParams(scriptParams);
    return searchTemplateRequest;
}
 
Example #17
Source File: BulkUpdateItem.java    From vertexium with Apache License 2.0 5 votes vote down vote up
@Override
public void addToBulkRequest(Client client, BulkRequestBuilder bulkRequestBuilder) {
    UpdateRequestBuilder updateRequestBuilder = client
        .prepareUpdate(getIndexName(), getType(), getDocumentId());
    if (!updateOnly) {
        updateRequestBuilder = updateRequestBuilder
            .setScriptedUpsert(true)
            .setUpsert(source);
    }
    UpdateRequest updateRequest = updateRequestBuilder
        .setScript(new Script(
            ScriptType.STORED,
            "painless",
            "updateFieldsOnDocumentScript",
            ImmutableMap.of(
                "fieldsToSet", fieldsToSet.entrySet().stream()
                    .collect(Collectors.toMap(
                        Map.Entry::getKey,
                        entry -> new ArrayList<>(entry.getValue())
                    )),
                "fieldsToRemove", new ArrayList<>(fieldsToRemove),
                "fieldsToRename", fieldsToRename,
                "additionalVisibilities", new ArrayList<>(additionalVisibilities),
                "additionalVisibilitiesToDelete", new ArrayList<>(additionalVisibilitiesToDelete)
            )
        ))
        .setRetryOnConflict(Elasticsearch5SearchIndex.MAX_RETRIES)
        .request();
    bulkRequestBuilder.add(updateRequest);
}
 
Example #18
Source File: ElasticsearchLengthOfStringSortingStrategy.java    From vertexium with Apache License 2.0 5 votes vote down vote up
@Override
public void updateElasticsearchQuery(
    Graph graph,
    Elasticsearch5SearchIndex 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
                ? Elasticsearch5SearchIndex.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 #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: ElasticSearchService.java    From Mahuta with Apache License 2.0 5 votes vote down vote up
@Override
public void updateField(String indexName, String indexDocId, String key, Object value) {
    log.debug("Update field on document in ElasticSearch [indexName: {}, indexDocId: {}, key: {}, value: {}]", indexName, indexDocId, key, value);

    // Validation
    ValidatorUtils.rejectIfEmpty("indexName", indexName);
    ValidatorUtils.rejectIfEmpty("indexDocId", indexDocId);
    ValidatorUtils.rejectIfEmpty("key", key);

    // Format index
    indexName = indexName.toLowerCase();
    
    try {
        Map<String, Object> params = new HashMap<>();
        params.put("value", transformValue(value));
        
        client.prepareUpdate(indexName, DEFAULT_TYPE, indexDocId)
            .setRetryOnConflict(RETRY_ON_CONFLICT)
            .setScript(new Script(
                    ScriptType.INLINE,
                    "painless",
                    "ctx._source."+key+" = params.value",
                    params
            )).execute().actionGet();

        log.debug("Field updated on document in ElasticSearch [indexName: {}, indexDocId: {}, key: {}, value: {}]", indexName, indexDocId, key, value);

        this.refreshIndex(indexName);

    } catch (Exception ex) {
        log.error("Error while updating field [indexName: {}, indexDocId: {}, key: {}, value: {}]", indexName, indexDocId, key, value, ex);
        throw new TechnicalException("Error while updating key " + key + " of doc indexDocId: " + indexDocId, ex);
    }
}
 
Example #21
Source File: ElasticsearchTransactionManager.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
@Override
protected void lock(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, LOCK_SCRIPT, document))

            .upsert(document)

            .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
    try {
        UpdateResponse response = elastic.update(request, RequestOptions.DEFAULT);
        if (response.getResult() == DocWriteResponse.Result.NOOP) {
            throw new TransactionLockException();
        }
    } catch (Exception exception) {
        throw new TransactionLockException(exception);
    }

}
 
Example #22
Source File: BulkUpdateItem.java    From vertexium with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({"unchecked", "rawtypes"})
@Override
public void addToBulkRequest(Client client, BulkRequestBuilder bulkRequestBuilder) {
    UpdateRequestBuilder updateRequestBuilder = client
        .prepareUpdate(getIndexName(), getType(), getDocumentId());
    if (!updateOnly) {
        updateRequestBuilder = updateRequestBuilder
            .setScriptedUpsert(true)
            .setUpsert((Map<String, Object>) (Map) source);
    }
    UpdateRequest updateRequest = updateRequestBuilder
        .setScript(new Script(
            ScriptType.STORED,
            null,
            "updateFieldsOnDocumentScript",
            ImmutableMap.of(
                "fieldsToSet", fieldsToSet.entrySet().stream()
                    .collect(Collectors.toMap(
                        Map.Entry::getKey,
                        entry -> new ArrayList<>(entry.getValue())
                    )),
                "fieldsToRemove", new ArrayList<>(fieldsToRemove),
                "fieldsToRename", fieldsToRename,
                "additionalVisibilities", new ArrayList<>(additionalVisibilities),
                "additionalVisibilitiesToDelete", new ArrayList<>(additionalVisibilitiesToDelete)
            )
        ))
        .setRetryOnConflict(Elasticsearch7SearchIndex.MAX_RETRIES)
        .request();
    bulkRequestBuilder.add(updateRequest);
}
 
Example #23
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 #24
Source File: ElasticsearchLockProvider.java    From ShedLock with Apache License 2.0 5 votes vote down vote up
@Override
@NonNull
public Optional<SimpleLock> lock(@NonNull LockConfiguration lockConfiguration) {
    try {
        Map<String, Object> lockObject = lockObject(lockConfiguration.getName(),
            lockConfiguration.getLockAtMostUntil(),
            now());
        UpdateRequest ur = new UpdateRequest()
            .index(index)
            .type(type)
            .id(lockConfiguration.getName())
            .script(new Script(ScriptType.INLINE,
                "painless",
                UPDATE_SCRIPT,
                lockObject)
            )
            .upsert(lockObject)
            .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
        UpdateResponse res = highLevelClient.update(ur, RequestOptions.DEFAULT);
        if (res.getResult() != DocWriteResponse.Result.NOOP) {
            return Optional.of(new ElasticsearchSimpleLock(lockConfiguration));
        } else {
            return Optional.empty();
        }
    } catch (IOException | ElasticsearchException e) {
        if (e instanceof ElasticsearchException && ((ElasticsearchException) e).status() == RestStatus.CONFLICT) {
            return Optional.empty();
        } else {
            throw new LockException("Unexpected exception occurred", e);
        }
    }
}
 
Example #25
Source File: ElasticsearchJavaAPIScriptTest.java    From syncer with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static void scriptEqual() throws Exception {
    AbstractClient client = ElasticTestUtil.getDevClient();

    HashMap<String, Object> params = new HashMap<>();
    HashMap<String, Object> user0 = new HashMap<>();
    user0.put("id", LONG_ID);
    params.put("user0", user0);
    Script add = new Script(ScriptType.INLINE, "painless",
        "ctx._source.nested.add(params.user0);",
        params);

    params = new HashMap<>();
//    params.put("userId0", LONG_ID);
    params.put("userId0", EsTypeUtil.scriptConvert(LONG_ID));
    params.put("user0", user0);
    Script meta = new Script(ScriptType.INLINE, "painless",
        "if (ctx._source.nested == null) {ctx._source.nested = [];}if (ctx._source.nested.find(e -> e.id.equals(params.userId0)) == null) {\n" +
            "ctx._source.nested.add(params.user0);\n" +
            "}",
        params);

    BulkRequestBuilder bulkRequest = client.prepareBulk();
    bulkRequest.add(indexRequest(client, getSource()));
    bulkRequest.add(updateRequest(client, add));
    bulkRequest.add(updateRequest(client, meta));
    BulkResponse bulkItemResponses = bulkRequest.execute().actionGet();
    if (bulkItemResponses.hasFailures()) {
      for (BulkItemResponse itemResponse : bulkItemResponses.getItems()) {
        System.out.println(itemResponse.getFailure());
      }
//      Assert.fail();
    }
  }
 
Example #26
Source File: PainlessScript.java    From vind with Apache License 2.0 5 votes vote down vote up
public Script build(Map<String, Object> parameters) {
    return new Script(
            ScriptType.INLINE,
            "painless",
            painlessScript.toString(),
            parameters);
}
 
Example #27
Source File: BaseDemo.java    From Elasticsearch-Tutorial-zh-CN with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 通过模板文件进行查询
 *
 * @param transportClient
 * @throws IOException
 */
private static void queryByTemplate(TransportClient transportClient) throws IOException {
	//模板文件需要放在:Elasticsearch 安装目录下的:config/scripts 目录下,比如我有一个:query_template_1.mustache
	//传递传输到模板中,参数是按顺序的,模板内容如下。
	/*
	{
		"from": {{from}},
		"size": {{size}},
		"query": {
			"match": {
				"product_name": "{{product_name}}"
			}
		}
	}
	*/

	Map<String, Object> scriptParams = new HashMap<String, Object>();
	scriptParams.put("from", 0);
	scriptParams.put("size", 10);
	scriptParams.put("product_name", "飞利浦");

	SearchResponse searchResponse = new SearchTemplateRequestBuilder(transportClient)
			.setScript("query_template_1")
			.setScriptType(ScriptType.FILE)
			.setScriptParams(scriptParams)
			.setRequest(new SearchRequest("product_index").types("product"))
			.get()
			.getResponse();

	for (SearchHit searchHit : searchResponse.getHits().getHits()) {
		logger.info("--------------------------------:" + searchHit.getSourceAsString());
	}
}
 
Example #28
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();
}
 
Example #29
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 #30
Source File: ProjectAnalyzer.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
public static Script getScript(RexNode node,
    boolean painlessAllowed,
    boolean supportsV5Features,
    boolean scriptsEnabled,
    boolean isAggregationContext,
    boolean allowPushdownAnalyzedNormalizedFields,
    boolean variationDetected){
  ProjectAnalyzer analyzer = new ProjectAnalyzer(isAggregationContext, allowPushdownAnalyzedNormalizedFields);
  RenderMode mode = painlessAllowed && supportsV5Features ? RenderMode.PAINLESS : RenderMode.GROOVY;
  FunctionRenderer r = new FunctionRenderer(supportsV5Features, scriptsEnabled, mode, analyzer);
  analyzer.renderer = r;
  FunctionRender render = node.accept(analyzer);
  if(analyzer.isNotAllowed()){
    throw new RuntimeException(String.format("Failed to convert expression %s to script.", node));
  }

  String nullGuardedScript;
  if (isAggregationContext) {
    nullGuardedScript = render.getNullGuardedScript(variationDetected);
  } else {
    nullGuardedScript = render.getNullGuardedScript("false", variationDetected);
  }

  if (mode == RenderMode.PAINLESS) {
    // 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)", nullGuardedScript), ImmutableMap.of());
  } else {
    // keeping this so plan matching tests will pass
    return new Script(ScriptType.INLINE, "groovy", nullGuardedScript, ImmutableMap.of());
  }
}