org.elasticsearch.common.collect.Tuple Java Examples

The following examples show how to use org.elasticsearch.common.collect.Tuple. 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: ExecutionPhasesTask.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public void start() {
    FluentIterable<NodeOperation> nodeOperations = FluentIterable.from(nodeOperationTrees)
        .transformAndConcat(new Function<NodeOperationTree, Iterable<? extends NodeOperation>>() {
            @Nullable
            @Override
            public Iterable<? extends NodeOperation> apply(NodeOperationTree input) {
                return input.nodeOperations();
            }
        });

    Map<String, Collection<NodeOperation>> operationByServer = NodeOperationGrouper.groupByServer(nodeOperations);
    InitializationTracker initializationTracker = new InitializationTracker(operationByServer.size());
    List<Tuple<ExecutionPhase, RowReceiver>> handlerPhases = createHandlerPhases(initializationTracker);
    try {
        setupContext(operationByServer, handlerPhases, initializationTracker);
    } catch (Throwable throwable) {
        for (SettableFuture<TaskResult> result : results) {
            result.setException(throwable);
        }
    }
}
 
Example #2
Source File: TransportListStoresAction.java    From elasticsearch-learning-to-rank with Apache License 2.0 6 votes vote down vote up
@Override
protected void masterOperation(ListStoresActionRequest request, ClusterState state,
                               ActionListener<ListStoresActionResponse> listener) throws Exception {
    String[] names = indexNameExpressionResolver.concreteIndexNames(state,
            new ClusterStateRequest().indices(IndexFeatureStore.DEFAULT_STORE, IndexFeatureStore.STORE_PREFIX + "*"));
    final MultiSearchRequestBuilder req = client.prepareMultiSearch();
    final List<Tuple<String, Integer>> versions = new ArrayList<>();
    Stream.of(names)
            .filter(IndexFeatureStore::isIndexStore)
            .map((s) -> clusterService.state().metaData().getIndices().get(s))
            .filter(Objects::nonNull)
            .filter((im) -> STORE_VERSION_PROP.exists(im.getSettings()))
            .forEach((m) -> {
                req.add(countSearchRequest(m));
                versions.add(tuple(m.getIndex().getName(),STORE_VERSION_PROP.get(m.getSettings())));
            });
    if (versions.isEmpty()) {
        listener.onResponse(new ListStoresActionResponse(Collections.emptyList()));
    } else {
        req.execute(wrap((r) -> listener.onResponse(toResponse(r, versions)), listener::onFailure));
    }
}
 
Example #3
Source File: TransportPutChunkAction.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
protected Tuple<PutChunkResponse, PutChunkReplicaRequest> shardOperationOnPrimary(MetaData metaData,
                                                                                  PutChunkRequest request) throws Throwable {
    PutChunkResponse response = newResponseInstance();
    transferTarget.continueTransfer(request, response);

    final PutChunkReplicaRequest replicaRequest = new PutChunkReplicaRequest();
    replicaRequest.setShardId(request.shardId());
    replicaRequest.transferId = request.transferId();
    replicaRequest.sourceNodeId = clusterService.localNode().id();
    replicaRequest.currentPos = request.currentPos();
    replicaRequest.content = request.content();
    replicaRequest.isLast = request.isLast();
    replicaRequest.index(request.index());
    return new Tuple<>(response, replicaRequest);
}
 
Example #4
Source File: PartitionInfos.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Nullable
private static Map<String, Object> buildValuesMap(PartitionName partitionName, MappingMetaData mappingMetaData) throws Exception{
    int i = 0;
    Map<String, Object> valuesMap = new HashMap<>();
    Iterable<Tuple<ColumnIdent, DataType>> partitionColumnInfoIterable = PartitionedByMappingExtractor.extractPartitionedByColumns(mappingMetaData.sourceAsMap());
    for (Tuple<ColumnIdent, DataType> columnInfo : partitionColumnInfoIterable) {
        String columnName = columnInfo.v1().sqlFqn();
        // produce string type values as string, not bytesref
        Object value = BytesRefs.toString(partitionName.values().get(i));
        if (!columnInfo.v2().equals(DataTypes.STRING)) {
            value = columnInfo.v2().value(value);
        }
        valuesMap.put(columnName, value);
        i++;
    }
    return valuesMap;
}
 
Example #5
Source File: SQLFunctions.java    From elasticsearch-sql with Apache License 2.0 6 votes vote down vote up
private static Tuple<String, String> coalesceTemplate(String fieldName, List<KVValue> paramer) {
    //if((doc['age2'].value != null)){doc['age2'].value} else if((doc['age1'].value != null)){doc['age1'].value}
    String name = fieldName + "_" + random();
    StringBuffer sb = new StringBuffer();
    int i = 0;
    //sb.append("def " + name + " = ");
    for (KVValue kv : paramer) {
        String field = kv.value.toString();
        if (i > 0) {
            sb.append(" else ");
        }
        sb.append("if(doc['" + field + "'].value != null){doc['" + field + "'].value}");
        i++;
    }
    return new Tuple<>(name, sb.toString());
}
 
Example #6
Source File: LoggingFetchSubPhase.java    From elasticsearch-learning-to-rank with Apache License 2.0 6 votes vote down vote up
private Tuple<RankerQuery, HitLogConsumer> extractRescore(LoggingSearchExtBuilder.LogSpec logSpec,
                                                          List<RescoreContext> contexts) {
    if (logSpec.getRescoreIndex() >= contexts.size()) {
        throw new IllegalArgumentException("rescore index [" + logSpec.getRescoreIndex() + "] is out of bounds, only " +
                "[" + contexts.size() + "] rescore context(s) are available");
    }
    RescoreContext context = contexts.get(logSpec.getRescoreIndex());
    if (!(context instanceof QueryRescorer.QueryRescoreContext)) {
        throw new IllegalArgumentException("Expected a [QueryRescoreContext] but found a " +
                "[" + context.getClass().getSimpleName() + "] " +
                "at index [" + logSpec.getRescoreIndex() + "]");
    }
    QueryRescorer.QueryRescoreContext qrescore = (QueryRescorer.QueryRescoreContext) context;
    return toLogger(logSpec, inspectQuery(qrescore.query())
            .orElseThrow(() -> new IllegalArgumentException("Expected a [sltr] query but found a " +
                    "[" + qrescore.query().getClass().getSimpleName() + "] " +
                    "at index [" + logSpec.getRescoreIndex() + "]")));
}
 
Example #7
Source File: BaseTransportCoordinateSearchAction.java    From siren-join with GNU Affero General Public License v3.0 6 votes vote down vote up
protected Tuple<XContentType, Map<String, Object>> parseSource(BytesReference source) {
  // nothing to parse...
  if (source == null || source.length() == 0) {
    return null;
  }

  try {
    Tuple<XContentType, Map<String, Object>> parsedSource = XContentHelper.convertToMap(source, false);
    logger.debug("{}: Parsed source: {}", Thread.currentThread().getName(), parsedSource);
    return parsedSource;
  }
  catch (Throwable e) {
      String sSource = "_na_";
      try {
          sSource = XContentHelper.convertToJson(source, false);
      }
      catch (Throwable e1) { /* ignore  */ }
      throw new ElasticsearchParseException("Failed to parse source [" + sSource + "]", e);
  }
}
 
Example #8
Source File: ExecutionPhasesTask.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
private void sendJobRequests(String localNodeId,
                             Map<String, Collection<NodeOperation>> operationByServer,
                             List<PageDownstreamContext> pageDownstreamContexts,
                             List<Tuple<ExecutionPhase, RowReceiver>> handlerPhases,
                             int bucketIdx,
                             InitializationTracker initializationTracker) {
    for (Map.Entry<String, Collection<NodeOperation>> entry : operationByServer.entrySet()) {
        String serverNodeId = entry.getKey();
        JobRequest request = new JobRequest(jobId(), localNodeId, entry.getValue());
        if (hasDirectResponse) {
            transportJobAction.execute(serverNodeId, request,
                new SetBucketAction(pageDownstreamContexts, bucketIdx, initializationTracker));
        } else {
            transportJobAction.execute(serverNodeId, request, new FailureOnlyResponseListener(handlerPhases, initializationTracker));
        }
        bucketIdx++;
    }
}
 
Example #9
Source File: DocIndexMetaData.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
public DocIndexMetaData build() {
    partitionedBy = getPartitionedBy();
    columnPolicy = getColumnPolicy();
    createColumnDefinitions();
    indices = createIndexDefinitions();
    columns = ImmutableList.copyOf(columnsBuilder.build());
    partitionedByColumns = partitionedByColumnsBuilder.build();

    for (Tuple<ColumnIdent, ReferenceInfo> sysColumn : DocSysColumns.forTable(ident)) {
        referencesBuilder.put(sysColumn.v1(), sysColumn.v2());
    }
    references = referencesBuilder.build();
    generatedColumnReferences = generatedColumnReferencesBuilder.build();
    primaryKey = getPrimaryKey();
    routingCol = getRoutingCol();

    initializeGeneratedExpressions();
    return this;
}
 
Example #10
Source File: LuceneQueryBuilder.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public Query apply(Function input, Context context) throws IOException {
    Tuple<Reference, Literal> prepare = prepare(input);
    if (prepare == null) { return null; }
    String fieldName = prepare.v1().info().ident().columnIdent().fqn();
    Object value = prepare.v2().value();

    if (value instanceof BytesRef) {
        BytesRef pattern = ((BytesRef) value);
        if (isPcrePattern(pattern.utf8ToString())) {
            return new RegexQuery(new Term(fieldName, pattern));
        } else {
            return toLuceneRegexpQuery(fieldName, pattern, context);
        }
    }
    throw new IllegalArgumentException("Can only use ~ with patterns of type string");
}
 
Example #11
Source File: ACLDocumentManager.java    From openshift-elasticsearch-plugin with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("rawtypes")
private Collection<SearchGuardACLDocument> loadAcls() throws Exception {
    LOGGER.debug("Loading SearchGuard ACL...waiting up to 30s");
    Map<String, Tuple<Settings, Long>> loadedDocs = configLoader.load(CONFIG_DOCS, 30, TimeUnit.SECONDS);
    Collection<SearchGuardACLDocument> docs = new ArrayList<>(loadedDocs.size());
    for (Entry<String, Tuple<Settings, Long>> item : loadedDocs.entrySet()) {
        Settings settings = item.getValue().v1();
        Long version = item.getValue().v2();
        Map<String, Object> original = settings.getAsStructuredMap();
        if(LOGGER.isDebugEnabled()){
            logContent("Read in {}: {}", item.getKey(), settings);
        }
        switch (item.getKey()) {
        case SEARCHGUARD_ROLE_TYPE:
            docs.add(new SearchGuardRoles(version).load(original));
            break;
        case SEARCHGUARD_MAPPING_TYPE:
            docs.add(new SearchGuardRolesMapping(version).load(original));
            break;
        }
    }
    return docs;
}
 
Example #12
Source File: LuceneQueryBuilder.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public Query apply(Function input, Context context) throws IOException {
    Tuple<Reference, Literal> prepare = prepare(input);
    if (prepare == null) { return null; }
    String fieldName = prepare.v1().info().ident().columnIdent().fqn();
    Object value = prepare.v2().value();

    if (value instanceof BytesRef) {
        RegexQuery query = new RegexQuery(new Term(fieldName, BytesRefs.toBytesRef(value)));
        query.setRegexImplementation(new JavaUtilRegexCapabilities(
                JavaUtilRegexCapabilities.FLAG_CASE_INSENSITIVE |
                JavaUtilRegexCapabilities.FLAG_UNICODE_CASE));
        return query;
    }
    throw new IllegalArgumentException("Can only use ~* with patterns of type string");
}
 
Example #13
Source File: TransportListStoresAction.java    From elasticsearch-learning-to-rank with Apache License 2.0 6 votes vote down vote up
private ListStoresActionResponse toResponse(MultiSearchResponse response, List<Tuple<String, Integer>> versions) {
    assert versions.size() == response.getResponses().length;
    Iterator<Tuple<String, Integer>> vs = versions.iterator();
    Iterator<MultiSearchResponse.Item> rs = response.iterator();
    List<ListStoresAction.IndexStoreInfo> infos = new ArrayList<>(versions.size());
    while (vs.hasNext() && rs.hasNext()) {
        MultiSearchResponse.Item it = rs.next();
        Tuple<String, Integer> idxAndVersion = vs.next();
        Map<String, Integer> counts = Collections.emptyMap();
        if (!it.isFailure()) {
            Terms aggs = it.getResponse()
                    .getAggregations()
                    .get("type");
            counts = aggs
                    .getBuckets()
                    .stream()
                    .collect(toMap(MultiBucketsAggregation.Bucket::getKeyAsString,
                            (b) -> (int) b.getDocCount()));
        }
        infos.add(new ListStoresAction.IndexStoreInfo(idxAndVersion.v1(), idxAndVersion.v2(), counts));
    }
    return new ListStoresActionResponse(infos);
}
 
Example #14
Source File: TransportIndexAction.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
protected Tuple<IndexResponse, IndexRequest> shardOperationOnPrimary(MetaData metaData, IndexRequest request) throws Throwable {

    // validate, if routing is required, that we got routing
    IndexMetaData indexMetaData = metaData.index(request.shardId().getIndex());
    MappingMetaData mappingMd = indexMetaData.mappingOrDefault(request.type());
    if (mappingMd != null && mappingMd.routing().required()) {
        if (request.routing() == null) {
            throw new RoutingMissingException(request.shardId().getIndex(), request.type(), request.id());
        }
    }

    IndexService indexService = indicesService.indexServiceSafe(request.shardId().getIndex());
    IndexShard indexShard = indexService.shardSafe(request.shardId().id());
    indexShard.checkDiskSpace(fsService);
    final WriteResult<IndexResponse> result = executeIndexRequestOnPrimary(null, request, indexShard, mappingUpdatedAction);
    final IndexResponse response = result.response;
    final Translog.Location location = result.location;
    processAfterWrite(request.refresh(), indexShard, location);
    return new Tuple<>(response, request);
}
 
Example #15
Source File: TransportDeleteAction.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
protected Tuple<DeleteResponse, DeleteRequest> shardOperationOnPrimary(MetaData metaData, DeleteRequest request) {
    IndexShard indexShard = indicesService.indexServiceSafe(request.shardId().getIndex()).shardSafe(request.shardId().id());
    final WriteResult<DeleteResponse> result = executeDeleteRequestOnPrimary(request, indexShard);
    processAfterWrite(request.refresh(), indexShard, result.location);
    return new Tuple<>(result.response, request);
}
 
Example #16
Source File: TransportShardFlushAction.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
protected Tuple<ActionWriteResponse, ShardFlushRequest> shardOperationOnPrimary(MetaData metaData, ShardFlushRequest shardRequest) throws Throwable {
    IndexShard indexShard = indicesService.indexServiceSafe(shardRequest.shardId().getIndex()).shardSafe(shardRequest.shardId().id());
    indexShard.flush(shardRequest.getRequest());
    logger.trace("{} flush request executed on primary", indexShard.shardId());
    return new Tuple<>(new ActionWriteResponse(), shardRequest);
}
 
Example #17
Source File: SQLFunctions.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
public static Tuple<String, String> strSingleValueTemplate(String methodName, String strColumn, String valueName) {
    String name = methodName + "_" + random();
    if (valueName == null) {
        return new Tuple(name, "def " + name + " = doc['" + strColumn + "'].value." + methodName + "()" );
    } else {
        return new Tuple(name, strColumn + "; def " + name + " = " + valueName + "." + methodName + "()");
    }

}
 
Example #18
Source File: UpgradeResponse.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);
    int size = in.readVInt();
    versions = newHashMap();
    for (int i=0; i<size; i++) {
        String index = in.readString();
        Version upgradeVersion = Version.readVersion(in);
        String oldestLuceneSegment = in.readString();
        versions.put(index, new Tuple<>(upgradeVersion, oldestLuceneSegment));
    }
}
 
Example #19
Source File: UpgradeSettingsRequest.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public void writeTo(StreamOutput out) throws IOException {
    super.writeTo(out);
    out.writeVInt(versions.size());
    for(Map.Entry<String, Tuple<Version, String>> entry : versions.entrySet()) {
        out.writeString(entry.getKey());
        Version.writeVersion(entry.getValue().v1(), out);
        out.writeString(entry.getValue().v2());
    }
    writeTimeout(out);
}
 
Example #20
Source File: PluginsService.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public Collection<Class<? extends Closeable>> indexServices() {
    List<Class<? extends Closeable>> services = new ArrayList<>();
    for (Tuple<PluginInfo, Plugin> plugin : plugins) {
        services.addAll(plugin.v2().indexServices());
    }
    return services;
}
 
Example #21
Source File: DocumentMapperParser.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private Tuple<String, Map<String, Object>> extractMapping(String type, String source) throws MapperParsingException {
    Map<String, Object> root;
    try (XContentParser parser = XContentFactory.xContent(source).createParser(source)) {
        root = parser.mapOrdered();
    } catch (Exception e) {
        throw new MapperParsingException("failed to parse mapping definition", e);
    }
    return extractMapping(type, root);
}
 
Example #22
Source File: SQLFunctions.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
public static Tuple<String, String> split(String strColumn, String pattern, int index, String valueName) {
    String name = "split_" + random();
    String script = "";
    if (valueName == null) {
        script = "def " + name + " = doc['" + strColumn + "'].value.split('" + pattern + "')[" + index + "]";

    } else {
        script = "; def " + name + " = " + valueName + ".split('" + pattern + "')[" + index + "]";
    }
    return new Tuple<>(name, script);
}
 
Example #23
Source File: SQLFunctions.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
public static Tuple<String, String> substring(String strColumn, int pos, int len, String valueName) {
    String name = "substring_" + random();
    if (valueName == null) {
        return new Tuple(name, "def " + name + " = doc['" + strColumn + "'].value.substring(" + pos + "," + len + ")");
    } else {
        return new Tuple(name, strColumn + ";def " + name + " = " + valueName + ".substring(" + pos + "," + len + ")");
    }

}
 
Example #24
Source File: SnapshotsService.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
/**
 * Returns list of indices with missing shards, and list of indices that are closed
 *
 * @param shards list of shard statuses
 * @return list of failed and closed indices
 */
private Tuple<Set<String>, Set<String>> indicesWithMissingShards(ImmutableMap<ShardId, SnapshotsInProgress.ShardSnapshotStatus> shards, MetaData metaData) {
    Set<String> missing = newHashSet();
    Set<String> closed = newHashSet();
    for (ImmutableMap.Entry<ShardId, SnapshotsInProgress.ShardSnapshotStatus> entry : shards.entrySet()) {
        if (entry.getValue().state() == State.MISSING) {
            if (metaData.hasIndex(entry.getKey().getIndex()) && metaData.index(entry.getKey().getIndex()).getState() == IndexMetaData.State.CLOSE) {
                closed.add(entry.getKey().getIndex());
            } else {
                missing.add(entry.getKey().getIndex());
            }
        }
    }
    return new Tuple<>(missing, closed);
}
 
Example #25
Source File: ScriptService.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private Tuple<String, String> scriptNameExt(Path file) {
    Path scriptPath = scriptsDirectory.relativize(file);
    int extIndex = scriptPath.toString().lastIndexOf('.');
    if (extIndex != -1) {
        String ext = scriptPath.toString().substring(extIndex + 1);
        String scriptName = scriptPath.toString().substring(0, extIndex).replace(scriptPath.getFileSystem().getSeparator(), "_");
        return new Tuple<>(scriptName, ext);
    } else {
        return null;
    }
}
 
Example #26
Source File: DocumentMapperParser.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public DocumentMapper parse(@Nullable String type, CompressedXContent source, String defaultSource) throws MapperParsingException {
    Map<String, Object> mapping = null;
    if (source != null) {
        Map<String, Object> root = XContentHelper.convertToMap(source.compressedReference(), true).v2();
        Tuple<String, Map<String, Object>> t = extractMapping(type, root);
        type = t.v1();
        mapping = t.v2();
    }
    if (mapping == null) {
        mapping = Maps.newHashMap();
    }
    return parse(type, mapping, defaultSource);
}
 
Example #27
Source File: ExecutionPhasesTask.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private List<PageDownstreamContext> getHandlerPageDownstreamContexts(Tuple<List<ExecutionSubContext>, List<ListenableFuture<Bucket>>> onHandler) {
    final List<PageDownstreamContext> pageDownstreamContexts = new ArrayList<>(onHandler.v1().size());
    for (ExecutionSubContext handlerExecutionSubContext : onHandler.v1()) {
        if (handlerExecutionSubContext instanceof DownstreamExecutionSubContext) {
            PageDownstreamContext pageDownstreamContext = ((DownstreamExecutionSubContext) handlerExecutionSubContext).pageDownstreamContext((byte) 0);
            pageDownstreamContexts.add(pageDownstreamContext);
        }
    }
    return pageDownstreamContexts;
}
 
Example #28
Source File: AuditMessage.java    From deprecated-security-advanced-modules with Apache License 2.0 5 votes vote down vote up
public void addTupleToRequestBody(Tuple<XContentType, BytesReference> xContentTuple) {
    if (xContentTuple != null) {
        try {
            auditInfo.put(REQUEST_BODY, XContentHelper.convertToJson(xContentTuple.v2(), false, xContentTuple.v1()));
        } catch (Exception e) {
            auditInfo.put(REQUEST_BODY, "ERROR: Unable to convert to json because of "+e.toString());
        }
    }
}
 
Example #29
Source File: SQLFunctions.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
public static Tuple<String, String> split(String strColumn, String pattern, String valueName) {
    String name = "split_" + random();
    if (valueName == null) {
        return new Tuple(name, "def " + name + " = doc['" + strColumn + "'].value.split('" + pattern + "')" );
    } else {
        return new Tuple(name, strColumn + "; def " + name + " = " + valueName + ".split('" + pattern + "')");
    }

}
 
Example #30
Source File: TransportShardAction.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
protected void shardOperationOnReplica(final R shardRequest) {
    KillableCallable<Tuple> callable = new KillableWrapper() {
        @Override
        public Tuple call() throws Exception {
            processRequestItemsOnReplica(shardRequest.shardId(), shardRequest);
            return null;
        }
    };
    wrapOperationInKillable(shardRequest, callable);
}