Elasticsearch 2.3.2 java客户端操作

1         JAVA客户端连接

Eclipse测试时候

需要在在main方法的类中右键选择run configurations,在Arguments标签页下的vm arguments输入:-Des.path.home=-Des.path.home即可。比如:

-Des.path.home=Eelasticsearch安装目录

 

1.1   JAR

Jar可以从elasticsearch服务端 lib文件夹下拷取

 

1.2   连接方式

 

方式一 节点方式连接

// on startup

Node node = nodeBuilder().node();

Client client = node.client();

// on shutdown

node.close();

 

方式二 远程连接方式 推荐

// on startup

Client client =   TransportClient.builder().build()

          .addTransportAddress(new   InetSocketTransportAddress(InetAddress.getByName("localhost"),   9300));

// on shutdown

client.close();

 

方式三 自动添加集群

Settings settings =   Settings.settingsBuilder().put("client.transport.sniff",   true).build();

TransportClient client =   TransportClient.builder().settings(settings).build();

 

1.3   添加索引

String type = "one";

XContentBuilder mapping = XContentFactory

          .jsonBuilder()

          .startObject()

          .startObject("settings")

          .field("number_of_shards", 1)

          // 设置分片数量

          .field("number_of_replicas", 0)

          // 设置副本数量

          .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();

 

String indexName = "this_one";

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.");

}

 

1.4   添加文档

 

// 添加文档

IndexResponse response = client

.prepareIndex(indexName, type,   "1")

.setSource(// 这里可以直接用son字符串

XContentFactory.jsonBuilder().startObject().field("type",   "syslog").field("eventCount", 1)

.field("eventDate", new   Date()).field("message", "sec log insert doc   test").endObject())

          .get();

System.out.println("index:" +   response.getIndex() + " insert doc id:" + response.getId() + "   result:"

          + response.isCreated());

 

1.5   查询文档

1.5.1     指定ID查询

// 查询文档

GetResponse response =   client.prepareGet("this_one", "one",   "1").get();

String source =   response.getSource().toString();

long version = response.getVersion();

String indexName_ = response.getIndex();

String type_ = response.getType();

String id = response.getId();

System.out.println(response.getSource().get("message"));

System.out.println(source);

System.out.println("index:" +   indexName_ + " insert doc id:" + id + " type_:" + type_ +   " version:" + version);

1.5.2     指定字段查询

SearchRequestBuilder sbuilder =   client.prepareSearch(indexName) // index name

.setTypes(type) // type name

.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)

.setQuery(QueryBuilders.termQuery("message",   "中文")) // Query

//.setPostFilter(QueryBuilders.rangeQuery("eventCount").from(1).to(18))   // Filter

.setFrom(0).setSize(20).setExplain(true);

System.out.println(sbuilder.toString());

SearchResponse response =   sbuilder.execute().actionGet();

System.out.println(response.toString());

1.5.3     表达式查询

 

QueryBuilder query =   QueryBuilders.queryStringQuery("content=中国 or author=无名");

SearchRequestBuilder sbuilder =   client.prepareSearch(indexName) // index name

.setTypes(type) // type name

.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)

.setQuery(query)

//.setPostFilter(QueryBuilders.rangeQuery("eventCount").from(1).to(18))   // Filter

.setFrom(0).setSize(20).setExplain(true);

System.out.println(sbuilder.toString());

SearchResponse response =   sbuilder.execute().actionGet();

System.out.println(response.toString());






你可能感兴趣的:(Elasticsearch,Elasticsearch)