Java Code Examples for org.elasticsearch.common.settings.ImmutableSettings#settingsBuilder()

The following examples show how to use org.elasticsearch.common.settings.ImmutableSettings#settingsBuilder() . 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: ElasticSearchIndex.java    From titan1withtp3.1 with Apache License 2.0 6 votes vote down vote up
/**
 * If ES already contains this instance's target index, then do nothing.
 * Otherwise, create the index, then wait {@link #CREATE_SLEEP}.
 * <p>
 * The {@code client} field must point to a live, connected client.
 * The {@code indexName} field must be non-null and point to the name
 * of the index to check for existence or create.
 *
 * @param config the config for this ElasticSearchIndex
 * @throws java.lang.IllegalArgumentException if the index could not be created
 */
private void checkForOrCreateIndex(Configuration config) {
    Preconditions.checkState(null != client);

    //Create index if it does not already exist
    IndicesExistsResponse response = client.admin().indices().exists(new IndicesExistsRequest(indexName)).actionGet();
    if (!response.isExists()) {

        ImmutableSettings.Builder settings = ImmutableSettings.settingsBuilder();

        ElasticSearchSetup.applySettingsFromTitanConf(settings, config, ES_CREATE_EXTRAS_NS);

        CreateIndexResponse create = client.admin().indices().prepareCreate(indexName)
                .setSettings(settings.build()).execute().actionGet();
        try {
            final long sleep = config.get(CREATE_SLEEP);
            log.debug("Sleeping {} ms after {} index creation returned from actionGet()", sleep, indexName);
            Thread.sleep(sleep);
        } catch (InterruptedException e) {
            throw new TitanException("Interrupted while waiting for index to settle in", e);
        }
        if (!create.isAcknowledged()) throw new IllegalArgumentException("Could not create index: " + indexName);
    }
}
 
Example 2
Source File: InternalEsClient.java    From io with Apache License 2.0 6 votes vote down vote up
/**
 * インデックスを作成する.
 * @param index インデックス名
 * @param mappings マッピング情報
 * @return 非同期応答
 */
public ActionFuture<CreateIndexResponse> createIndex(String index, Map<String, JSONObject> mappings) {
    this.fireEvent(Event.creatingIndex, index);
    CreateIndexRequestBuilder cirb =
            new CreateIndexRequestBuilder(esTransportClient.admin().indices()).setIndex(index);

    // cjkアナライザ設定
    ImmutableSettings.Builder indexSettings = ImmutableSettings.settingsBuilder();
    indexSettings.put("analysis.analyzer.default.type", "cjk");
    cirb.setSettings(indexSettings);

    if (mappings != null) {
        for (Map.Entry<String, JSONObject> ent : mappings.entrySet()) {
            cirb = cirb.addMapping(ent.getKey(), ent.getValue().toString());
        }
    }
    return cirb.execute();
}
 
Example 3
Source File: ElasticSearchConfigTest.java    From titan1withtp3.1 with Apache License 2.0 5 votes vote down vote up
@Test
public void testIndexCreationOptions() throws InterruptedException, BackendException {
    final int shards = 77;

    ElasticsearchRunner esr = new ElasticsearchRunner(".", "indexCreationOptions.yml");
    esr.start();
    CommonsConfiguration cc = new CommonsConfiguration(new BaseConfiguration());
    cc.set("index." + INDEX_NAME + ".elasticsearch.create.ext.number_of_shards", String.valueOf(shards));
    cc.set("index." + INDEX_NAME + ".elasticsearch.ext.cluster.name", "indexCreationOptions");
    ModifiableConfiguration config =
            new ModifiableConfiguration(GraphDatabaseConfiguration.ROOT_NS,
                    cc, BasicConfiguration.Restriction.NONE);
    config.set(INTERFACE, ElasticSearchSetup.NODE.toString(), INDEX_NAME);
    Configuration indexConfig = config.restrictTo(INDEX_NAME);
    IndexProvider idx = new ElasticSearchIndex(indexConfig);
    simpleWriteAndQuery(idx);



    ImmutableSettings.Builder settingsBuilder = ImmutableSettings.settingsBuilder();
    settingsBuilder.put("discovery.zen.ping.multicast.enabled", "false");
    settingsBuilder.put("discovery.zen.ping.unicast.hosts", "localhost,127.0.0.1:9300");
    settingsBuilder.put("cluster.name", "indexCreationOptions");
    NodeBuilder nodeBuilder = NodeBuilder.nodeBuilder().settings(settingsBuilder.build());
    nodeBuilder.client(true).data(false).local(false);
    Node n = nodeBuilder.build().start();

    GetSettingsResponse response = n.client().admin().indices().getSettings(new GetSettingsRequest().indices("titan")).actionGet();
    assertEquals(String.valueOf(shards), response.getSetting("titan", "index.number_of_shards"));

    idx.close();
    n.stop();
    esr.stop();
}
 
Example 4
Source File: ClientFactory.java    From storm-trident-elasticsearch with Apache License 2.0 5 votes vote down vote up
private Settings buildSettings(String clusterName) {
    ImmutableSettings.Builder sb = ImmutableSettings.settingsBuilder();
    if( StringUtils.isNotEmpty(clusterName)) sb.put("cluster.name", clusterName);
    if( settings != null) sb.put(settings);

    return sb.build();
}
 
Example 5
Source File: SearchIndexingServiceImpl.java    From bearchoke with Apache License 2.0 5 votes vote down vote up
@PostConstruct
public void init() throws Exception {
    log.info("Instantiating jestSearchIndexingService...");
    log.info(String.format("Checking if %s needs to be created", LOCATION_INDEX_NAME));
    IndicesExists indicesExists = new IndicesExists.Builder(LOCATION_INDEX_NAME).build();
    JestResult r = jestClient.execute(indicesExists);

    if (!r.isSucceeded()) {
        log.info("Index does not exist. Creating index...");
        // create new index (if u have this in elasticsearch.yml and prefer
        // those defaults, then leave this out
        ImmutableSettings.Builder settings = ImmutableSettings.settingsBuilder();
        settings.put("number_of_shards", 1);
        settings.put("number_of_replicas", 0);

        CreateIndex.Builder builder = new CreateIndex.Builder(LOCATION_INDEX_NAME);
        builder.settings(settings.build().getAsMap());
        CreateIndex createIndex = builder.build();

        log.info(createIndex.toString());

        jestClient.execute(createIndex);

        log.info("Index created");
    } else {
        log.info("Index already exist!");
    }
}
 
Example 6
Source File: NodeTestHelper.java    From elasticsearch-graphite-plugin with Do What The F*ck You Want To Public License 5 votes vote down vote up
public static Node createNode(String clusterName, int graphitePort, String refreshInterval, String includeRegex,
                              String excludeRegex, String prefix) throws IOException {
    ImmutableSettings.Builder settingsBuilder = ImmutableSettings.settingsBuilder();

    settingsBuilder.put("path.conf", NodeTestHelper.class.getResource("/").getFile());

    settingsBuilder.put("gateway.type", "none");
    settingsBuilder.put("cluster.name", clusterName);
    settingsBuilder.put("index.number_of_shards", 1);
    settingsBuilder.put("index.number_of_replicas", 1);

    settingsBuilder.put("metrics.graphite.host", "localhost");
    settingsBuilder.put("metrics.graphite.port", graphitePort);
    settingsBuilder.put("metrics.graphite.every", refreshInterval);
    if (!Strings.isEmpty(prefix)) {
        settingsBuilder.put("metrics.graphite.prefix", prefix);
    }

    if (Strings.hasLength(includeRegex)) {
        settingsBuilder.put("metrics.graphite.include", includeRegex);
    }

    if (Strings.hasLength(excludeRegex)) {
        settingsBuilder.put("metrics.graphite.exclude", excludeRegex);
    }

    LogConfigurator.configure(settingsBuilder.build());

    return NodeBuilder.nodeBuilder().settings(settingsBuilder.build()).node();
}
 
Example 7
Source File: ESTimedRotatingAdapter.java    From opensoc-streaming with Apache License 2.0 4 votes vote down vote up
@Override
public boolean initializeConnection(String ip, int port,
		String cluster_name, String index_name, String document_name,
		int bulk_size, String date_format) throws Exception {

	bulk_set = new HashBag();

	_LOG.trace("[OpenSOC] Initializing ESBulkAdapter...");

	try {
		_ip = ip;
		_port = port;
		_cluster_name = cluster_name;
		_index_name = index_name;
		_document_name = document_name;
		_bulk_size = bulk_size;
		

		dateFormat = new SimpleDateFormat(date_format);

		System.out.println("Bulk indexing is set to: " + _bulk_size);

		ImmutableSettings.Builder builder = ImmutableSettings.settingsBuilder() ;	
		
		if(tuning_settings != null && tuning_settings.size() > 0)
		{
				builder.put(tuning_settings);
		}
		
		builder.put("cluster.name", _cluster_name);
		builder.put("client.transport.ping_timeout","500s");
		
		
		settings = builder.build();
				
		client = new TransportClient(settings)
				.addTransportAddress(new InetSocketTransportAddress(_ip,
						_port));

		return true;
	} catch (Exception e) {
		e.printStackTrace();
		return false;
	}
}
 
Example 8
Source File: ElasticIndexWriter.java    From nutch-htmlunit with Apache License 2.0 4 votes vote down vote up
@Override
public void open(JobConf job, String name) throws IOException {
  clusterName = job.get(ElasticConstants.CLUSTER);
  host = job.get(ElasticConstants.HOST);
  port = job.getInt(ElasticConstants.PORT, -1);

  Builder settingsBuilder = ImmutableSettings.settingsBuilder();
  
  BufferedReader reader = new BufferedReader(job.getConfResourceAsReader("elasticsearch.conf"));
  String line;
  String parts[];

  while ((line = reader.readLine()) != null) {
    if (StringUtils.isNotBlank(line) && !line.startsWith("#")) {
      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
  Settings settings = settingsBuilder.put("cluster.name", clusterName).build();
  
  // Prefer TransportClient
  if (host != null && port > 1) {
    client = new TransportClient(settings).addTransportAddress(new InetSocketTransportAddress(host, port));
  } else if (clusterName != null) {
    node = nodeBuilder().settings(settings).client(true).node();
    client = node.client();
  }

  bulk = client.prepareBulk();
  defaultIndex = job.get(ElasticConstants.INDEX, "nutch");
  maxBulkDocs = job.getInt(
          ElasticConstants.MAX_BULK_DOCS, DEFAULT_MAX_BULK_DOCS);
  maxBulkLength = job.getInt(
          ElasticConstants.MAX_BULK_LENGTH, DEFAULT_MAX_BULK_LENGTH);
}