elasticsearch java api demo

原创 2015年07月09日 13:49:28

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

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

2)通过name进行查询;

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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

1
2
3
4
5
6
7
8
9
10
11
12
13
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

 

补张ppt。

 


 

20140630 补充

TransportClient

The transport client allows to create a client that is not part of the cluster, but simply connects to one or more nodes directly by adding their respective addresses using addTransportAddress(org.elasticsearch.common.transport.TransportAddress).

addTransportAddress方法

Adds a transport address that will be used to connect to.The Node this transport address represents will be used if its possible to connect to it. If it is unavailable, it will be automatically connected to once it is up.In order to get the list of all the current connected nodes, please see connectedNodes().

从上文可知,可以为transportClient添加多个transportAddress,添加多个的目的是什么呢?

当一个es服务(对应一个transportAddress)不可用时,client会自动发现当前可用的nodes(the current connected nodes),从以下这段代码可知:

TransportClientNodesService

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int index = randomNodeGenerator.incrementAndGet();
        if (index < 0) {
            index = 0;
            randomNodeGenerator.set(0);
        }
        RetryListener<Response> retryListener = new RetryListener<Response>(callback, listener, nodes, index);
        try {
            callback.doWithNode(nodes.get((index) % nodes.size()), retryListener);
        } catch (ElasticsearchException e) {
            if (e.unwrapCause() instanceof ConnectTransportException) {
                retryListener.onFailure(e);
            } else {
                throw e;
            }
        }

retryListener保证当前可用的nodes列表。

index是一个自增的int(针对同一个client),nodes.get((index) % nodes.size()可以将请求均发到nodes上。注意这里和索引的分片不是一回事。

理论上,Client可以添加ES集群中部分或全部nodes,然后轮询“拿到”一个node,届时client可以和ES集群进行通信,并进行相应的操作。

至于具体的操作——

Index:分片(sharding,分片策略)->选定具体的node(Master)Index ->同步到对应的slave node

Search:从replSet中选定node(负载策略)->请求分发 ->结果集合并

等,后面再分析。

java elasticsearch5.1.1,java Demo

  • 2017年04月23日 21:14
  • 23.91MB
  • 下载

ElasticSearch Java API

  • 2017年12月09日 13:33
  • 18KB
  • 下载

elasticsearch2.3.3 java client demo

elasticsearch2.3.3 入门 及java apiCRUD
  • hunanlzg
  • hunanlzg
  • 2016年06月13日 18:03
  • 12117

elasticsearch java操作的api实例

  • 2017年09月29日 16:39
  • 14.56MB
  • 下载

JAVA API对Elasticsearch的增删改查

  • 2017年10月27日 09:03
  • 1KB
  • 下载

实时搜索引擎Elasticsearch(5)——Java API的使用

前一篇有关ES的文章介绍了使用Rest方式调用ES的聚合API。本文介绍ES的Java API调用。...

ElasticSearch java API

  • 2017年03月27日 16:21
  • 237KB
  • 下载

分布式搜索elasticsearch java API 之(七)------与MongoDB同步数据

原文:http://blog.csdn.net/laigood/article/details/7691068

ElasticSearch之Java Api聚合分组实战

转自:http://qindongliang.iteye.com/blog/2289590#rd 最近有个日志收集监控的项目采用的技术栈是ELK+JAVA+Spring,客户端语言使用的是Java,以...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:elasticsearch java api demo
举报原因:
原因补充:

(最多只允许输入30个字)