es 集群配置文件示例


主节点

# ======================== Elasticsearch Configuration =========================
#
# NOTE: Elasticsearch comes with reasonable defaults for most settings.
# Before you set out to tweak and tune the configuration, make sure you
# understand what are you trying to accomplish and the consequences.
#
# The primary way of configuring a node is via this file. This template lists
# the most important settings you may want to configure for a production cluster.
#
# Please see the documentation for further information on configuration options:
# <http://www.elastic.co/guide/en/elasticsearch/reference/current/setup-configuration.html>
#
# ---------------------------------- Cluster -----------------------------------
#
# Use a descriptive name for your cluster:
#
# cluster.name: my-application
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#
# node.name: node-1
#
# Add custom attributes to the node:
#
# node.rack: r1
#
# ----------------------------------- Paths ------------------------------------
#
# Path to directory where to store the data (separate multiple locations by comma):
#
# path.data: /path/to/data
#
# Path to log files:
#
# path.logs: /path/to/logs
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
# bootstrap.mlockall: true
#
# Make sure that the `ES_HEAP_SIZE` environment variable is set to about half the memory
# available on the system and that the owner of the process is allowed to use this limit.
#
# Elasticsearch performs poorly when the system is swapping the memory.
#
# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
# network.host: 192.168.0.1
#
# Set a custom port for HTTP:
#
# http.port: 9200
#
# For more information, see the documentation at:
# <http://www.elastic.co/guide/en/elasticsearch/reference/current/modules-network.html>
#
# --------------------------------- Discovery ----------------------------------
#
# Pass an initial list of hosts to perform discovery when new node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
#
# discovery.zen.ping.unicast.hosts: ["host1", "host2"]
#
# Prevent the "split brain" by configuring the majority of nodes (total number of nodes / 2 + 1):
#
# discovery.zen.minimum_master_nodes: 3
#
# For more information, see the documentation at:
# <http://www.elastic.co/guide/en/elasticsearch/reference/current/modules-discovery.html>
#
# ---------------------------------- Gateway -----------------------------------
#
# Block initial recovery after a full cluster restart until N nodes are started:
#
# gateway.recover_after_nodes: 3
#
# For more information, see the documentation at:
# <http://www.elastic.co/guide/en/elasticsearch/reference/current/modules-gateway.html>
#
# ---------------------------------- Various -----------------------------------
#
# Disable starting multiple nodes on a single system:
#
# node.max_local_storage_nodes: 1
#
# Require explicit names when deleting indices:
#
# action.destructive_requires_name: true

# ======================== Elasticsearch Configuration =========================
#
# NOTE: Elasticsearch comes with reasonable defaults for most settings.
# Before you set out to tweak and tune the configuration, make sure you
# understand what are you trying to accomplish and the consequences.
#
# The primary way of configuring a node is via this file. This template lists
# the most important settings you may want to configure for a production cluster.
#
# Please see the documentation for further information on configuration options:
# <http://www.elastic.co/guide/en/elasticsearch/reference/current/setup-configuration.html>
#
# ---------------------------------- Cluster -----------------------------------
#
# Use a descriptive name for your cluster:
#
# cluster.name: my-application
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#
# node.name: node-1
#
# Add custom attributes to the node:
#
# node.rack: r1
#
# ----------------------------------- Paths ------------------------------------
#
# Path to directory where to store the data (separate multiple locations by comma):
#
# path.data: /path/to/data
#
# Path to log files:
#
# path.logs: /path/to/logs
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
# bootstrap.mlockall: true
#
# Make sure that the `ES_HEAP_SIZE` environment variable is set to about half the memory
# available on the system and that the owner of the process is allowed to use this limit.
#
# Elasticsearch performs poorly when the system is swapping the memory.
#
# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
# network.host: 192.168.0.1
#
# Set a custom port for HTTP:
#
# http.port: 9200
#
# For more information, see the documentation at:
# <http://www.elastic.co/guide/en/elasticsearch/reference/current/modules-network.html>
#
# --------------------------------- Discovery ----------------------------------
#
# Pass an initial list of hosts to perform discovery when new node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
#
# discovery.zen.ping.unicast.hosts: ["host1", "host2"]
#
# Prevent the "split brain" by configuring the majority of nodes (total number of nodes / 2 + 1):
#
# discovery.zen.minimum_master_nodes: 3
#
# For more information, see the documentation at:
# <http://www.elastic.co/guide/en/elasticsearch/reference/current/modules-discovery.html>
#
# ---------------------------------- Gateway -----------------------------------
#
# Block initial recovery after a full cluster restart until N nodes are started:
#
# gateway.recover_after_nodes: 3
#
# For more information, see the documentation at:
# <http://www.elastic.co/guide/en/elasticsearch/reference/current/modules-gateway.html>
#
# ---------------------------------- Various -----------------------------------
#
# Disable starting multiple nodes on a single system:
#
# node.max_local_storage_nodes: 1
#
# Require explicit names when deleting indices:
#
# action.destructive_requires_name: true

cluster.name: myapplication
node.name: node-1
http.cors.enabled: true
http.cors.allow-origin : "*"
network.host: 172.24.20.22
network.publish_host: 172.24.20.22
node.master: true
discovery.zen.ping.multicast.enabled: false
discovery.zen.ping_timeout: 120s
client.transport.ping_timeout: 60s


从节点 node-2

# ======================== Elasticsearch Configuration =========================
#
# NOTE: Elasticsearch comes with reasonable defaults for most settings.
# Before you set out to tweak and tune the configuration, make sure you
# understand what are you trying to accomplish and the consequences.
#
# The primary way of configuring a node is via this file. This template lists
# the most important settings you may want to configure for a production cluster.
#
# Please see the documentation for further information on configuration options:
# <http://www.elastic.co/guide/en/elasticsearch/reference/current/setup-configuration.html>
#
# ---------------------------------- Cluster -----------------------------------
#
# Use a descriptive name for your cluster:
#
# cluster.name: my-application
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#
# node.name: node-1
#
# Add custom attributes to the node:
#
# node.rack: r1
#
# ----------------------------------- Paths ------------------------------------
#
# Path to directory where to store the data (separate multiple locations by comma):
#
# path.data: /path/to/data
#
# Path to log files:
#
# path.logs: /path/to/logs
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
# bootstrap.mlockall: true
#
# Make sure that the `ES_HEAP_SIZE` environment variable is set to about half the memory
# available on the system and that the owner of the process is allowed to use this limit.
#
# Elasticsearch performs poorly when the system is swapping the memory.
#
# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
# network.host: 192.168.0.1
#
# Set a custom port for HTTP:
#
# http.port: 9200
#
# For more information, see the documentation at:
# <http://www.elastic.co/guide/en/elasticsearch/reference/current/modules-network.html>
#
# --------------------------------- Discovery ----------------------------------
#
# Pass an initial list of hosts to perform discovery when new node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
#
# discovery.zen.ping.unicast.hosts: ["host1", "host2"]
#
# Prevent the "split brain" by configuring the majority of nodes (total number of nodes / 2 + 1):
#
# discovery.zen.minimum_master_nodes: 3
#
# For more information, see the documentation at:
# <http://www.elastic.co/guide/en/elasticsearch/reference/current/modules-discovery.html>
#
# ---------------------------------- Gateway -----------------------------------
#
# Block initial recovery after a full cluster restart until N nodes are started:
#
# gateway.recover_after_nodes: 3
#
# For more information, see the documentation at:
# <http://www.elastic.co/guide/en/elasticsearch/reference/current/modules-gateway.html>
#
# ---------------------------------- Various -----------------------------------
#
# Disable starting multiple nodes on a single system:
#
# node.max_local_storage_nodes: 1
#
# Require explicit names when deleting indices:
#
# action.destructive_requires_name: true

# ======================== Elasticsearch Configuration =========================
#
# NOTE: Elasticsearch comes with reasonable defaults for most settings.
# Before you set out to tweak and tune the configuration, make sure you
# understand what are you trying to accomplish and the consequences.
#
# The primary way of configuring a node is via this file. This template lists
# the most important settings you may want to configure for a production cluster.
#
# Please see the documentation for further information on configuration options:
# <http://www.elastic.co/guide/en/elasticsearch/reference/current/setup-configuration.html>
#
# ---------------------------------- Cluster -----------------------------------
#
# Use a descriptive name for your cluster:
#
# cluster.name: my-application
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#
# node.name: node-1
#
# Add custom attributes to the node:
#
# node.rack: r1
#
# ----------------------------------- Paths ------------------------------------
#
# Path to directory where to store the data (separate multiple locations by comma):
#
# path.data: /path/to/data
#
# Path to log files:
#
# path.logs: /path/to/logs
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
# bootstrap.mlockall: true
#
# Make sure that the `ES_HEAP_SIZE` environment variable is set to about half the memory
# available on the system and that the owner of the process is allowed to use this limit.
#
# Elasticsearch performs poorly when the system is swapping the memory.
#
# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
# network.host: 192.168.0.1
#
# Set a custom port for HTTP:
#
# http.port: 9200
#
# For more information, see the documentation at:
# <http://www.elastic.co/guide/en/elasticsearch/reference/current/modules-network.html>
#
# --------------------------------- Discovery ----------------------------------
#
# Pass an initial list of hosts to perform discovery when new node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
#
# discovery.zen.ping.unicast.hosts: ["host1", "host2"]
#
# Prevent the "split brain" by configuring the majority of nodes (total number of nodes / 2 + 1):
#
# discovery.zen.minimum_master_nodes: 3
#
# For more information, see the documentation at:
# <http://www.elastic.co/guide/en/elasticsearch/reference/current/modules-discovery.html>
#
# ---------------------------------- Gateway -----------------------------------
#
# Block initial recovery after a full cluster restart until N nodes are started:
#
# gateway.recover_after_nodes: 3
#
# For more information, see the documentation at:
# <http://www.elastic.co/guide/en/elasticsearch/reference/current/modules-gateway.html>
#
# ---------------------------------- Various -----------------------------------
#
# Disable starting multiple nodes on a single system:
#
# node.max_local_storage_nodes: 1
#
# Require explicit names when deleting indices:
#
# action.destructive_requires_name: true

cluster.name: myapplication
node.name: node-2
http.cors.enabled: true
http.cors.allow-origin : "*"
network.host: 172.24.20.25
network.publish_host: 172.24.20.25
network.host: 172.24.20.25
node.master: false
discovery.zen.ping.multicast.enabled: false
discovery.zen.ping_timeout: 120s
client.transport.ping_timeout: 60s
discovery.zen.ping.unicast.hosts: ["172.24.20.22"]

从节点 node-3

# ======================== Elasticsearch Configuration =========================
#
# NOTE: Elasticsearch comes with reasonable defaults for most settings.
# Before you set out to tweak and tune the configuration, make sure you
# understand what are you trying to accomplish and the consequences.
#
# The primary way of configuring a node is via this file. This template lists
# the most important settings you may want to configure for a production cluster.
#
# Please see the documentation for further information on configuration options:
# <http://www.elastic.co/guide/en/elasticsearch/reference/current/setup-configuration.html>
#
# ---------------------------------- Cluster -----------------------------------
#
# Use a descriptive name for your cluster:
#
# cluster.name: my-application
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#
# node.name: node-1
#
# Add custom attributes to the node:
#
# node.rack: r1
#
# ----------------------------------- Paths ------------------------------------
#
# Path to directory where to store the data (separate multiple locations by comma):
#
# path.data: /path/to/data
#
# Path to log files:
#
# path.logs: /path/to/logs
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
# bootstrap.mlockall: true
#
# Make sure that the `ES_HEAP_SIZE` environment variable is set to about half the memory
# available on the system and that the owner of the process is allowed to use this limit.
#
# Elasticsearch performs poorly when the system is swapping the memory.
#
# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
# network.host: 192.168.0.1
#
# Set a custom port for HTTP:
#
# http.port: 9200
#
# For more information, see the documentation at:
# <http://www.elastic.co/guide/en/elasticsearch/reference/current/modules-network.html>
#
# --------------------------------- Discovery ----------------------------------
#
# Pass an initial list of hosts to perform discovery when new node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
#
# discovery.zen.ping.unicast.hosts: ["host1", "host2"]
#
# Prevent the "split brain" by configuring the majority of nodes (total number of nodes / 2 + 1):
#
# discovery.zen.minimum_master_nodes: 3
#
# For more information, see the documentation at:
# <http://www.elastic.co/guide/en/elasticsearch/reference/current/modules-discovery.html>
#
# ---------------------------------- Gateway -----------------------------------
#
# Block initial recovery after a full cluster restart until N nodes are started:
#
# gateway.recover_after_nodes: 3
#
# For more information, see the documentation at:
# <http://www.elastic.co/guide/en/elasticsearch/reference/current/modules-gateway.html>
#
# ---------------------------------- Various -----------------------------------
#
# Disable starting multiple nodes on a single system:
#
# node.max_local_storage_nodes: 1
#
# Require explicit names when deleting indices:
#
# action.destructive_requires_name: true

# ======================== Elasticsearch Configuration =========================
#
# NOTE: Elasticsearch comes with reasonable defaults for most settings.
# Before you set out to tweak and tune the configuration, make sure you
# understand what are you trying to accomplish and the consequences.
#
# The primary way of configuring a node is via this file. This template lists
# the most important settings you may want to configure for a production cluster.
#
# Please see the documentation for further information on configuration options:
# <http://www.elastic.co/guide/en/elasticsearch/reference/current/setup-configuration.html>
#
# ---------------------------------- Cluster -----------------------------------
#
# Use a descriptive name for your cluster:
#
# cluster.name: my-application
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#
# node.name: node-1
#
# Add custom attributes to the node:
#
# node.rack: r1
#
# ----------------------------------- Paths ------------------------------------
#
# Path to directory where to store the data (separate multiple locations by comma):
#
# path.data: /path/to/data
#
# Path to log files:
#
# path.logs: /path/to/logs
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
# bootstrap.mlockall: true
#
# Make sure that the `ES_HEAP_SIZE` environment variable is set to about half the memory
# available on the system and that the owner of the process is allowed to use this limit.
#
# Elasticsearch performs poorly when the system is swapping the memory.
#
# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
# network.host: 192.168.0.1
#
# Set a custom port for HTTP:
#
# http.port: 9200
#
# For more information, see the documentation at:
# <http://www.elastic.co/guide/en/elasticsearch/reference/current/modules-network.html>
#
# --------------------------------- Discovery ----------------------------------
#
# Pass an initial list of hosts to perform discovery when new node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
#
# discovery.zen.ping.unicast.hosts: ["host1", "host2"]
#
# Prevent the "split brain" by configuring the majority of nodes (total number of nodes / 2 + 1):
#
# discovery.zen.minimum_master_nodes: 3
#
# For more information, see the documentation at:
# <http://www.elastic.co/guide/en/elasticsearch/reference/current/modules-discovery.html>
#
# ---------------------------------- Gateway -----------------------------------
#
# Block initial recovery after a full cluster restart until N nodes are started:
#
# gateway.recover_after_nodes: 3
#
# For more information, see the documentation at:
# <http://www.elastic.co/guide/en/elasticsearch/reference/current/modules-gateway.html>
#
# ---------------------------------- Various -----------------------------------
#
# Disable starting multiple nodes on a single system:
#
# node.max_local_storage_nodes: 1
#
# Require explicit names when deleting indices:
#
# action.destructive_requires_name: true

cluster.name: myapplication
node.name: node-3
http.cors.enabled: true
http.cors.allow-origin : "*"
network.host: 172.24.20.72
network.publish_host: 172.24.20.72
node.master: false
discovery.zen.ping.multicast.enabled: false
discovery.zen.ping_timeout: 120s
client.transport.ping_timeout: 60s
discovery.zen.ping.unicast.hosts: ["172.24.20.22"]


java 客户端程序

import static org.elasticsearch.node.NodeBuilder.nodeBuilder;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;

import org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.node.Node;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.junit.Test;

public class ESTest525 {

public static Client getTransportClient() throws UnknownHostException{
Settings settings = Settings.settingsBuilder()
.put("cluster.name", "myapplication")
.put("client.transport.sniff", true)
.build();
//Client client = TransportClient.builder().settings(settings).build();
Client client = TransportClient.builder().settings(settings).build()
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("172.24.20.22"), 9300)) //远程
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("172.24.20.25"), 9300))
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("172.24.20.72"), 9300))
;
return client;
}


public static Client getNodeClient() {
Node node = nodeBuilder().clusterName("myapplication").node();
Client client = node.client();
return client;

}


@Test
public void test25() {
Client client = null;
try {

client = getTransportClient();
adddoc22(client,"test22","test22");
/*for (int i = 0; i < 100; i++) {


client = getTransportClient();
// client = getNodeClient();
// createindex(client,"test22","test22");




}*/








System.out.println("end");

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
client.close();
}
}
@Test
public void testgetalldoc() throws Exception {
Client client = getTransportClient();

//Client client = getNodeClient();
System.out.println(client);
getalldoc(client);
//getalldoc(client,"test2k2","test2k2");

client.close();
}
@Test
public void testbulkinsert() throws Exception {
Client client = getTransportClient();
System.out.println(client);
//Client client = getNodeClient();

bulkinsert(client,"test44","test44");

client.close();
}


// on shutdown


// public static void main(String[] args) {
/*
* //启动节点 Node node = nodeBuilder().node();
*
* Client client = node.client();
* System.out.println(client.admin().indices().prepareStats());
*
* System.out.println(client);
*
* SearchResponse response = client.prepareSearch("mmm")
* //这个在prepareSearch中指定还不行,必须使用setTypes .setTypes("jjj") //设置查询条件,
* .setPostFilter(FilterBuilders.matchAllFilter()) .setFrom(0).setSize(1000)
* .execute() .actionGet(); SearchHits shs = response.getHits();
* for(SearchHit hit : shs){ System.out.println(hit); } // client.close();
*
* }
*/

public static void main(String[] args) throws IOException {

Client client = TransportClient.builder().build()
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("172.24.20.25"), 9300))
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("172.24.20.22"), 9300));

// 启动节点
/*
* Node node = nodeBuilder().node();
*
* Client client = node.client();
*/

/*
* Settings settings = ImmutableSettings.settingsBuilder() //指定集群名称
* .put("cluster.name", "myapplication") //探测集群中机器状态 .build();
*
*
*
* Client client = new TransportClient(settings)
*/

// .addTransportAddress(new InetSocketTransportAddress("172.24.20.68",
// 9300));

/*
* Client client = TransportClient.builder().build()
*
* .addTransportAddress(new
* InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300))
* .addTransportAddress(new
* InetSocketTransportAddress(InetAddress.getByName("172.24.20.68"),
* 9300));
*/
/*Node node = nodeBuilder().node();
Client client = node.client();
// /createindex("test7",client,"test7");

// adddoc(client,"test7","test7");
getall2(client);*/
/*
* Node node = nodeBuilder().clusterName("myapplication").node(); Client
* client = node.client();
*/
// on shutdown

// Client client = new TransportClient().addTransportAddress(new
// InetSocketTransportAddress("172.24.20.68", 9300));
// System.out.println(client.admin().indices().prepareStats());
/*
* Settings settings = ImmutableSettings.settingsBuilder() //指定集群名称
* .put("cluster.name", "myapplication") //探测集群中机器状态 .build();
*/

/*
* Client client = new TransportClient(settings)
*
*
* .addTransportAddress(new InetSocketTransportAddress("127.0.0.1",
* 9300));
*/
// Client client = new TransportClient().addTransportAddress(new
// InetSocketTransportAddress("127.0.0.1", 9300));
/*
* System.out.println(client);
*
* SearchResponse searchResponse = client.prepareSearch("mmm")
* .setTypes("jjj").setSize(100) .execute().actionGet(); SearchHits
* searchHits = searchResponse.getHits();
*
* for (SearchHit searchHit : searchHits) {
*
* System.out.println(searchHit.getIndex()+searchHit.getSource()); }
*/

client.close();

}

static void createindex(Client client, String indexName,String type)
throws IOException {

XContentBuilder mapping = XContentFactory
.jsonBuilder()
.startObject()
.startObject("settings")
.field("number_of_shards", 2)
// 设置分片数量
.field("number_of_replicas", 2)
// 设置副本数量
.endObject()
.endObject()
.startObject()
.startObject(type)
// type名称
.startObject("properties")
// 下面是设置文档列属性。
.startObject("type").field("type", "string")
.field("store", "yes").endObject().startObject("eventCount")
.field("type", "long").field("store", "yes").endObject()
.startObject("eventDate").field("type", "date")
.field("format", "dateOptionalTime").field("store", "yes")
.endObject().startObject("message").field("type", "string")
.field("index", "not_analyzed").field("store", "yes")
.endObject().endObject().endObject().endObject();

CreateIndexRequestBuilder cirb = client.admin().indices()
.prepareCreate(indexName)// index名称
.setSource(mapping);

CreateIndexResponse response = cirb.execute().actionGet();
if (response.isAcknowledged()) {
System.out.println("Index created.");
} else {
System.err.println("Index creation failed.");
}

}

static void adddoc25(Client client, String indexName, String type)
throws IOException {

for (int i = 0; i < 100; i++) {
IndexResponse response = client
.prepareIndex(indexName, type)
.setSource(// 这里可以直接用json字符串
XContentFactory
.jsonBuilder()
.startObject()
.field("type", "syslog"+i)
.field("eventCount", i)
.field("eventDate", new Date())
.field("message",
"secilog insert doc test"+i)
.endObject()).get();
System.out.println("index:" + response.getIndex()
+ " insert doc id:" + response.getId() + " result:"
+ response.isCreated());
}

}
static void adddoc22(Client client, String indexName, String type)
throws IOException {

for (int i = 0; i < 100; i++) {
IndexResponse response = client
.prepareIndex(indexName, type)
.setSource(// 这里可以直接用json字符串
XContentFactory
.jsonBuilder()
.startObject()
.field("type", "syslog22"+i)
.field("eventCount", i)
.field("eventDate", new Date())
.field("message",
"secilog insert doc test22"+i)
.endObject()).get();
System.out.println("index:" + response.getIndex()
+ " insert doc id:" + response.getId() + " result:"
+ response.isCreated());

}

}

/*
* static void getall(Client client){
*
*
* System.out.println(client);
*
* GetResponse searchResponse =
* client.prepareGet("mmm","jjj").execute().actionGet(); SearchHits
* searchHits = (SearchHits) searchResponse.getSource();
*
* for (SearchHit searchHit : searchHits) {
*
* System.out.println(searchHit.getIndex()+searchHit.getSource()); }
*
* client.close();
*
*
*
*
*
*
*
*
*
* }
*/

static void getalldoc(Client client) {


try {






SearchRequestBuilder sbuilder = client.prepareSearch().setFrom(0).setSize(2000); // index name

System.out.println(sbuilder.toString());
SearchResponse response = sbuilder.execute().actionGet();
System.out.println(response.toString());
System.out.println(response.getHits().getTotalHits());

SearchHits hits = response.getHits();

for (SearchHit searchHit : hits) {

System.out.println(searchHit.getIndex() + searchHit.getSource());

}

System.out.println(hits.getTotalHits());










System.out.println("end");

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
client.close();
}


}
static void getalldoc(Client client,String index,String type) {


try {




SearchRequestBuilder sbuilder = client.prepareSearch(index).setFrom(0).setSize(1000) //index name
.setTypes(type) //type name
/* .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.setQuery(QueryBuilders.termQuery("message", "insert")) // Query
.setPostFilter(QueryBuilders.rangeQuery("eventCount").from(1).to(18)) // Filter
*/ // .setFrom(0).setSize(60).setExplain(true)
;
System.out.println(sbuilder.toString());
SearchResponse response = sbuilder.execute().actionGet();
System.out.println(response.toString());


System.out.println(response.getHits().getTotalHits());

SearchHits hits = response.getHits();

for (SearchHit searchHit : hits) {

System.out.println(searchHit.getIndex() + searchHit.getSource());

}












System.out.println("end");

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
client.close();
}



}
static void bulkinsert(Client client,String index,String type) {


try {




BulkRequestBuilder bulkRequest = client.prepareBulk();
for(int i=1001;i<1500;i++){
//业务对象
String json =XContentFactory
.jsonBuilder()
.startObject()
.field("type", "syslog22"+i)
.field("eventCount", i)
.field("eventDate", new Date())
.field("message",
"secilog insert doc test22"+i)
.endObject().string();
IndexRequestBuilder indexRequest = client.prepareIndex(index,type)
//指定不重复的ID
.setSource(json).setId(String.valueOf(i));
System.out.println(json);
//添加到builder中
bulkRequest.add(indexRequest);
}

BulkResponse bulkResponse = bulkRequest.execute().actionGet();
if (bulkResponse.hasFailures()) {
// process failures by iterating through each bulk response item
System.out.println(bulkResponse.buildFailureMessage());
}












System.out.println("end");

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
client.close();
}


}
}





注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
© 2014-2019 ITdaan.com 粤ICP备14056181号