java esclient query,elasticsearch 口水篇(4)java客户端 – 原生esClient – MR-fox

具体参考:

526459aef9d8fc914157f2b4b023db76.jpg

下面我们做一个很简单的实例,以下几个功能:

1)批量添加1000个user对象;

2)通过name进行查询;

package com.fox.c1;import java.io.IOException;import org.elasticsearch.client.Client;import org.elasticsearch.client.transport.TransportClient;import org.elasticsearch.common.transport.InetSocketTransportAddress;import org.elasticsearch.common.xcontent.XContentBuilder;import org.elasticsearch.common.xcontent.XContentFactory;/** * @author huangfox * @date 2014年2月10日 下午3:27:43 * */public class ESClient {private Client client;public void init() {client = new TransportClient().addTransportAddress(new InetSocketTransportAddress("localhost", 9300));}public void close() {client.close();}/** * index */public void createIndex() {for (int i = 0; i < 1000; i++) {User user = new User();user.setId(new Long(i));user.setName("huang fox " + i);user.setAge(i % 100);client.prepareIndex("users", "user").setSource(generateJson(user)).execute().actionGet();}}/** * 转换成json对象 * * @param user * @return */private String generateJson(User user) {String json = "";try {XContentBuilder contentBuilder = XContentFactory.jsonBuilder().startObject();contentBuilder.field("id", user.getId() + "");contentBuilder.field("name", user.getName());contentBuilder.field("age", user.getAge() + "");json = contentBuilder.endObject().string();} catch (IOException e) {e.printStackTrace();}return json;}public static void main(String[] args) {ESClient client = new ESClient();client.init();client.createIndex();client.close();}}

这里有两点需要注意下:

1)NodeClinet 和 TransportClient

Instantiating a node based client is the simplest way to get a  Client  that can execute operations against elasticsearch.

The TransportClient connects remotely to an elasticsearch cluster using the transport module. It does not join the cluster , but simply gets one or more initial transport addresses and communicates with them in round robin fashion on each action (though most actions will probably be " two hop " operations).

2)generate json document

There are different way of generating JSON document:

Manually (aka do it yourself) using native  byte[]  or as a  String

Using  Map  that will be automatically converted to its JSON equivalent

Using a third party library to serialize your beans such as  Jackson

Using built-in helpers XContentFactory.jsonBuilder()

————————————————

search

public void search() {SearchResponse response = client.prepareSearch("users").setTypes("user").setSearchType(SearchType.DFS_QUERY_THEN_FETCH).setQuery(QueryBuilders.termQuery("name", "fox")) // Query.setFilter(FilterBuilders.rangeFilter("age").from(20).to(30)) // Filter.setFrom(0).setSize(60).setExplain(true).execute().actionGet();SearchHits hits = response.getHits();System.out.println(hits.getTotalHits());for (int i = 0; i < hits.getHits().length; i++) {System.out.println(hits.getHits()[i].getSourceAsString());}}

这两篇简单应用了esClient和jest,至于两者的区别目前还没有定论。

后续还有更多功能:

index:create、update、delete

search:query(queryParser)、filter、sort、paging、highlight、facet

multiSearch

cache

keywords

1)elasticsearch java api VS rest api

2)elasticsearch nodeBuilder VS transportClient

欢迎加入我爱机器学习QQ14群:336582044

getqrcode.jpg

微信扫一扫,关注我爱机器学习公众号

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值