Search in sources :

Example 26 with PreBuiltTransportClient

use of org.elasticsearch.transport.client.PreBuiltTransportClient in project fess by codelibs.

the class FessEsClient method open.

@PostConstruct
public void open() {
    final FessConfig fessConfig = ComponentUtil.getFessConfig();
    final String transportAddressesValue = System.getProperty(Constants.FESS_ES_TRANSPORT_ADDRESSES);
    if (StringUtil.isNotBlank(transportAddressesValue)) {
        for (final String transportAddressValue : transportAddressesValue.split(",")) {
            final String[] addressPair = transportAddressValue.trim().split(":");
            if (addressPair.length < 3) {
                final String host = addressPair[0];
                int port = 9300;
                if (addressPair.length == 2) {
                    port = Integer.parseInt(addressPair[1]);
                }
                addTransportAddress(host, port);
            } else {
                logger.warn("Invalid address format: " + transportAddressValue);
            }
        }
    }
    if (transportAddressList.isEmpty()) {
        if (runner == null) {
            runner = new ElasticsearchClusterRunner();
            final Configs config = newConfigs().clusterName(fessConfig.getElasticsearchClusterName()).numOfNode(1).useLogger();
            final String esDir = System.getProperty("fess.es.dir");
            if (esDir != null) {
                config.basePath(esDir);
            }
            config.disableESLogger();
            runner.onBuild((number, settingsBuilder) -> {
                final File pluginDir = new File(esDir, "plugins");
                if (pluginDir.isDirectory()) {
                    settingsBuilder.put("path.plugins", pluginDir.getAbsolutePath());
                } else {
                    settingsBuilder.put("path.plugins", new File(System.getProperty("user.dir"), "plugins").getAbsolutePath());
                }
                if (settings != null) {
                    settingsBuilder.put(settings);
                }
            });
            runner.build(config);
        }
        client = runner.client();
        addTransportAddress("localhost", runner.node().settings().getAsInt("transport.tcp.port", 9300));
    } else {
        final Builder settingsBuilder = Settings.builder();
        settingsBuilder.put("cluster.name", fessConfig.getElasticsearchClusterName());
        settingsBuilder.put("client.transport.sniff", fessConfig.isElasticsearchTransportSniff());
        settingsBuilder.put("client.transport.ping_timeout", fessConfig.getElasticsearchTransportPingTimeout());
        settingsBuilder.put("client.transport.nodes_sampler_interval", fessConfig.getElasticsearchTransportNodesSamplerInterval());
        final Settings settings = settingsBuilder.build();
        final TransportClient transportClient = new PreBuiltTransportClient(settings);
        for (final TransportAddress address : transportAddressList) {
            transportClient.addTransportAddress(address);
        }
        client = transportClient;
    }
    if (StringUtil.isBlank(transportAddressesValue)) {
        final StringBuilder buf = new StringBuilder();
        for (final TransportAddress transportAddress : transportAddressList) {
            if (transportAddress instanceof InetSocketTransportAddress) {
                if (buf.length() > 0) {
                    buf.append(',');
                }
                final InetSocketTransportAddress inetTransportAddress = (InetSocketTransportAddress) transportAddress;
                buf.append(inetTransportAddress.address().getHostName());
                buf.append(':');
                buf.append(inetTransportAddress.address().getPort());
            }
        }
        if (buf.length() > 0) {
            System.setProperty(Constants.FESS_ES_TRANSPORT_ADDRESSES, buf.toString());
        }
    }
    waitForYellowStatus();
    indexConfigList.forEach(configName -> {
        final String[] values = configName.split("/");
        if (values.length == 2) {
            final String configIndex = values[0];
            final String configType = values[1];
            final String indexName;
            final boolean isFessIndex = configIndex.equals("fess");
            if (isFessIndex) {
                indexName = fessConfig.getIndexDocumentUpdateIndex();
            } else {
                indexName = configIndex;
            }
            boolean exists = existsIndex(indexName);
            if (!exists) {
                final String createdIndexName;
                if (isFessIndex) {
                    createdIndexName = generateNewIndexName(configIndex);
                } else {
                    createdIndexName = configIndex;
                }
                createIndex(configIndex, configType, createdIndexName);
                createAlias(configIndex, createdIndexName);
            }
            final String updatedIndexName;
            if (isFessIndex) {
                client.admin().cluster().prepareHealth(fessConfig.getIndexDocumentUpdateIndex()).setWaitForYellowStatus().execute().actionGet(fessConfig.getIndexIndicesTimeout());
                final GetIndexResponse response = client.admin().indices().prepareGetIndex().addIndices(fessConfig.getIndexDocumentUpdateIndex()).execute().actionGet(fessConfig.getIndexIndicesTimeout());
                final String[] indices = response.indices();
                if (indices.length == 1) {
                    updatedIndexName = indices[0];
                } else {
                    updatedIndexName = configIndex;
                }
            } else {
                updatedIndexName = configIndex;
            }
            addMapping(configIndex, configType, updatedIndexName);
        } else {
            logger.warn("Invalid index config name: " + configName);
        }
    });
}
Also used : ElasticsearchClusterRunner(org.codelibs.elasticsearch.runner.ElasticsearchClusterRunner) TransportClient(org.elasticsearch.client.transport.TransportClient) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) InetSocketTransportAddress(org.elasticsearch.common.transport.InetSocketTransportAddress) TransportAddress(org.elasticsearch.common.transport.TransportAddress) DeleteRequestBuilder(org.elasticsearch.action.delete.DeleteRequestBuilder) MultiSearchRequestBuilder(org.elasticsearch.action.search.MultiSearchRequestBuilder) TermVectorsRequestBuilder(org.elasticsearch.action.termvectors.TermVectorsRequestBuilder) ClearScrollRequestBuilder(org.elasticsearch.action.search.ClearScrollRequestBuilder) ExplainRequestBuilder(org.elasticsearch.action.explain.ExplainRequestBuilder) HighlightBuilder(org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder) ActionRequestBuilder(org.elasticsearch.action.ActionRequestBuilder) TermsAggregationBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder) CollapseBuilder(org.elasticsearch.search.collapse.CollapseBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) MultiGetRequestBuilder(org.elasticsearch.action.get.MultiGetRequestBuilder) GetRequestBuilder(org.elasticsearch.action.get.GetRequestBuilder) Builder(org.elasticsearch.common.settings.Settings.Builder) MultiTermVectorsRequestBuilder(org.elasticsearch.action.termvectors.MultiTermVectorsRequestBuilder) FilterAggregationBuilder(org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder) FieldCapabilitiesRequestBuilder(org.elasticsearch.action.fieldcaps.FieldCapabilitiesRequestBuilder) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) BulkRequestBuilder(org.elasticsearch.action.bulk.BulkRequestBuilder) FieldStatsRequestBuilder(org.elasticsearch.action.fieldstats.FieldStatsRequestBuilder) UpdateRequestBuilder(org.elasticsearch.action.update.UpdateRequestBuilder) IndicesAliasesRequestBuilder(org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequestBuilder) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) InnerHitBuilder(org.elasticsearch.index.query.InnerHitBuilder) SearchScrollRequestBuilder(org.elasticsearch.action.search.SearchScrollRequestBuilder) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) InetSocketTransportAddress(org.elasticsearch.common.transport.InetSocketTransportAddress) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) GetIndexResponse(org.elasticsearch.action.admin.indices.get.GetIndexResponse) ElasticsearchClusterRunner.newConfigs(org.codelibs.elasticsearch.runner.ElasticsearchClusterRunner.newConfigs) Configs(org.codelibs.elasticsearch.runner.ElasticsearchClusterRunner.Configs) File(java.io.File) Settings(org.elasticsearch.common.settings.Settings) PostConstruct(javax.annotation.PostConstruct)

Example 27 with PreBuiltTransportClient

use of org.elasticsearch.transport.client.PreBuiltTransportClient in project nutch by apache.

the class ElasticIndexWriter method makeClient.

/**
 * Generates a TransportClient or NodeClient
 */
protected Client makeClient(Configuration conf) throws IOException {
    String clusterName = conf.get(ElasticConstants.CLUSTER);
    String[] hosts = conf.getStrings(ElasticConstants.HOSTS);
    int port = conf.getInt(ElasticConstants.PORT, DEFAULT_PORT);
    Settings.Builder settingsBuilder = Settings.builder();
    BufferedReader reader = new BufferedReader(conf.getConfResourceAsReader("elasticsearch.conf"));
    String line;
    String[] parts;
    while ((line = reader.readLine()) != null) {
        if (StringUtils.isNotBlank(line) && !line.startsWith("#")) {
            line = line.trim();
            parts = line.split("=");
            if (parts.length == 2) {
                settingsBuilder.put(parts[0].trim(), parts[1].trim());
            }
        }
    }
    // Set the cluster name and build the settings
    if (StringUtils.isNotBlank(clusterName))
        settingsBuilder.put("cluster.name", clusterName);
    Settings settings = settingsBuilder.build();
    Client client = null;
    // Prefer TransportClient
    if (hosts != null && port > 1) {
        TransportClient transportClient = new PreBuiltTransportClient(settings);
        for (String host : hosts) transportClient.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(host), port));
        client = transportClient;
    } else if (clusterName != null) {
        node = new Node(settings);
        client = node.client();
    }
    return client;
}
Also used : TransportClient(org.elasticsearch.client.transport.TransportClient) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) Node(org.elasticsearch.node.Node) BufferedReader(java.io.BufferedReader) TransportClient(org.elasticsearch.client.transport.TransportClient) Client(org.elasticsearch.client.Client) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) InetSocketTransportAddress(org.elasticsearch.common.transport.InetSocketTransportAddress) Settings(org.elasticsearch.common.settings.Settings)

Example 28 with PreBuiltTransportClient

use of org.elasticsearch.transport.client.PreBuiltTransportClient in project metacat by Netflix.

the class ElasticSearchConfig method elasticSearchClient.

/**
 * The ElasticSearch client.
 *
 * @param config System config
 * @return Configured client or error
 */
@Bean
@ConditionalOnMissingBean(Client.class)
public Client elasticSearchClient(final Config config) {
    final String clusterName = config.getElasticSearchClusterName();
    if (StringUtils.isBlank(clusterName)) {
        throw new IllegalStateException("No cluster name set. Unable to continue");
    }
    final Settings settings = Settings.builder().put("cluster.name", clusterName).put("client.transport.sniff", // to dynamically add new hosts and remove old ones
    true).put("transport.tcp.connect_timeout", "60s").build();
    final TransportClient client = new PreBuiltTransportClient(settings);
    // Add the transport address if exists
    final String clusterNodesStr = config.getElasticSearchClusterNodes();
    if (StringUtils.isNotBlank(clusterNodesStr)) {
        final int port = config.getElasticSearchClusterPort();
        final Iterable<String> clusterNodes = Splitter.on(',').split(clusterNodesStr);
        clusterNodes.forEach(clusterNode -> {
            try {
                client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(clusterNode), port));
            } catch (UnknownHostException exception) {
                log.error("Skipping unknown host {}", clusterNode);
            }
        });
    }
    if (client.transportAddresses().isEmpty()) {
        throw new IllegalStateException("No Elasticsearch cluster nodes added. Unable to create client.");
    }
    return client;
}
Also used : TransportClient(org.elasticsearch.client.transport.TransportClient) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) UnknownHostException(java.net.UnknownHostException) InetSocketTransportAddress(org.elasticsearch.common.transport.InetSocketTransportAddress) Settings(org.elasticsearch.common.settings.Settings) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Example 29 with PreBuiltTransportClient

use of org.elasticsearch.transport.client.PreBuiltTransportClient in project leopard by tanhaichao.

the class SearcherImpl method init.

public void init() {
    if (server != null && server.length() > 0) {
        String[] serverInfo = server.split(":");
        this.host = serverInfo[0].trim();
        try {
            this.port = Integer.parseInt(serverInfo[1].trim());
        } catch (NumberFormatException e) {
            logger.error("elasticsearch server:" + server);
            throw e;
        }
    }
    InetAddress inetAddress;
    try {
        inetAddress = InetAddress.getByName(host);
    } catch (UnknownHostException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
    Settings settings = getSettingsBuilder().build();
    this.client = new PreBuiltTransportClient(settings);
    TransportAddress transportAddress = new TransportAddress(inetAddress, port);
    client.addTransportAddress(transportAddress);
}
Also used : UnknownHostException(java.net.UnknownHostException) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) TransportAddress(org.elasticsearch.common.transport.TransportAddress) InetAddress(java.net.InetAddress) Settings(org.elasticsearch.common.settings.Settings)

Example 30 with PreBuiltTransportClient

use of org.elasticsearch.transport.client.PreBuiltTransportClient in project syncope by apache.

the class ElasticsearchClientFactoryBean method getObject.

@Override
public Client getObject() throws Exception {
    synchronized (this) {
        if (client == null) {
            Settings.Builder builder = Settings.builder();
            settings.forEach((key, value) -> {
                builder.put(key, value);
            });
            PreBuiltTransportClient tClient = new PreBuiltTransportClient(builder.build());
            for (Map.Entry<String, Integer> entry : addresses.entrySet()) {
                tClient.addTransportAddress(new TransportAddress(InetAddress.getByName(entry.getKey()), entry.getValue()));
            }
            client = tClient;
        }
    }
    return client;
}
Also used : PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) TransportAddress(org.elasticsearch.common.transport.TransportAddress) Map(java.util.Map) Settings(org.elasticsearch.common.settings.Settings)

Aggregations

PreBuiltTransportClient (org.elasticsearch.transport.client.PreBuiltTransportClient)41 Settings (org.elasticsearch.common.settings.Settings)33 TransportClient (org.elasticsearch.client.transport.TransportClient)25 InetSocketTransportAddress (org.elasticsearch.common.transport.InetSocketTransportAddress)23 TransportAddress (org.elasticsearch.common.transport.TransportAddress)17 UnknownHostException (java.net.UnknownHostException)10 InetSocketAddress (java.net.InetSocketAddress)5 InetAddress (java.net.InetAddress)4 HashMap (java.util.HashMap)4 PostConstruct (javax.annotation.PostConstruct)4 SearchItem (cn.exrick.manager.dto.front.SearchItem)3 IOException (java.io.IOException)3 BulkRequestBuilder (org.elasticsearch.action.bulk.BulkRequestBuilder)3 IndexResponse (org.elasticsearch.action.index.IndexResponse)3 Builder (org.elasticsearch.common.settings.Settings.Builder)3 XmallException (cn.exrick.common.exception.XmallException)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 Logger (org.apache.log4j.Logger)2 ElasticsearchClusterRunner (org.codelibs.elasticsearch.runner.ElasticsearchClusterRunner)2