SearchContext.java

// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

package org.apache.doris.datasource.es;

import org.apache.doris.catalog.Column;
import org.apache.doris.catalog.EsTable;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * This class encapsulates the state needed to execute a query on ES table such as fields���doc_values���resolved index���
 * search shards etc.
 * Since then, we would add more state or runtime information to this class such as
 * query builder���slice scroll context���aggregation info etc.
 **/
public class SearchContext {

    private static final Logger LOG = LogManager.getLogger(SearchContext.class);

    private EsFieldContext fieldContext = new EsFieldContext();

    // sourceIndex is the name of index when creating ES external table
    private final String sourceIndex;

    // when the `sourceIndex` is `alias` or `wildcard` matched index, this maybe involved two or more indices
    // `resolvedIndices` would return the matched underlying indices
    private List<String> resolvedIndices = Collections.emptyList();

    // `type` of the `sourceIndex`
    private final String type;


    private EsTable table;

    // all columns which user created for ES external table
    private final List<Column> fullSchema;

    // represent `resolvedIndices`'s searchable shards
    private EsShardPartitions shardPartitions;

    // the ES cluster version
    private EsMajorVersion version;

    // whether the nodes needs to be discovered
    private boolean nodesDiscovery;


    public SearchContext(EsTable table) {
        this.table = table;
        fullSchema = table.getFullSchema();
        sourceIndex = table.getIndexName();
        type = table.getMappingType();
        nodesDiscovery = table.isNodesDiscovery();
    }


    public String sourceIndex() {
        return sourceIndex;
    }

    public List<String> resolvedIndices() {
        return resolvedIndices;
    }


    public String type() {
        return type;
    }

    public List<Column> columns() {
        return fullSchema;
    }

    public EsTable esTable() {
        return table;
    }

    public Map<String, String> fetchFieldsContext() {
        return fieldContext.getFetchFieldsContext();
    }

    public Map<String, String> docValueFieldsContext() {
        return fieldContext.getDocValueFieldsContext();
    }

    public List<String> needCompatDateFields() {
        return fieldContext.getNeedCompatDateFields();
    }

    public EsFieldContext fieldContext() {
        return fieldContext;
    }

    public void setFieldContext(EsFieldContext fieldContext) {
        this.fieldContext = fieldContext;
    }

    public void version(EsMajorVersion version) {
        this.version = version;
    }

    public EsMajorVersion version() {
        return version;
    }

    public void partitions(EsShardPartitions shardPartitions) {
        this.shardPartitions = shardPartitions;
    }

    public EsShardPartitions partitions() {
        return shardPartitions;
    }

    // this will be refactor soon
    public EsTablePartitions tablePartitions() throws Exception {
        return EsTablePartitions.fromShardPartitions(table, shardPartitions);
    }

    public boolean nodesDiscovery() {
        return nodesDiscovery;
    }
}