yujian
2019-12-26 012232edc4dac15517c69c2af3934f901e818b7c
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package com.yeshi.fanli.dao;
 
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
 
import org.apache.http.HttpHost;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.stereotype.Repository;
import org.yeshi.utils.JsonUtil;
 
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
 
@Repository
public abstract class ElasticBaseDao<T> {
    // 地址
    private static String host = "192.168.1.200";
    // 端口
    private static Integer port = 9200;
    // 数据库名称
    public static String index = "order";
 
    // 初始化api客户端
    public static RestHighLevelClient client = new RestHighLevelClient(
            RestClient.builder(new HttpHost(host, port, "http")));
    
    /**
     * 插入数据
     * 
     * @param bean
     * @return
     */
    public void save(T bean) {
        String json = JsonUtil.getSimpleGson().toJson(bean);
        IndexRequest request = new IndexRequest(index);
        request.source(json, XContentType.JSON);
        // 同步执行
        try {
            client.index(request, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 插入数据
     * 
     * @param bean
     * @return
     */
    public void save(T bean, String id) {
        String json = JsonUtil.getSimpleGson().toJson(bean);
        IndexRequest request = new IndexRequest(index).id(id);
        request.source(json, XContentType.JSON);
        // 同步执行
        try {
            client.index(request, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
    /**
     * 更新
     * @param Document id
     * @param bean
     */
    public void update(String id, T bean) {
        UpdateRequest request = new UpdateRequest(index, id);
        String json = new GsonBuilder().create().toJson(bean);
        request.doc(json, XContentType.JSON);
        try {
            client.update(request, RequestOptions.DEFAULT);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
 
    /**
     *删除
     * 
     * @param Document id
     */
    public void delete(String id) {
        DeleteRequest request = new DeleteRequest(index, id);
        try {
            client.delete(request, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 查询
     * 
     * @param Document id
     * @return
     */
    public T get(String id, Class<T> entityClass) {
        GetRequest request = new GetRequest(index, id);
        try {
            GetResponse getResponse = client.get(request, RequestOptions.DEFAULT);
            if (getResponse.isExists()) {
                String content = getResponse.getSourceAsString();        
                return new Gson().fromJson(content, entityClass);
            } 
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    
    /**
     * 查询
     * @param document 列名
     * @param key 匹配词-将分开单个字
     * @param start
     * @param count
     * @param entityClass
     * @return
     */
    public List<T> query(String document, String key, int start, int count, Class<T> entityClass) {
        List<T> list = new ArrayList<>();
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        sourceBuilder.query(QueryBuilders.termQuery(document, key));
        sourceBuilder.from(start); // 索引以开始搜索的选项。预设为0。
        sourceBuilder.size(count); // 返回的搜索命中次数的选项。默认为10
        sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS)); // 设置一个可选的超时时间,以控制允许搜索的时间。
 
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.indices(index);
        searchRequest.source(sourceBuilder);
        try {
            SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
            SearchHits hits = searchResponse.getHits();
            SearchHit[] searchHits = hits.getHits();
            
            Gson gson = new Gson();
            for (SearchHit hit : searchHits) {
                String content = hit.getSourceAsString();
                list.add(gson.fromJson(content, entityClass));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }
    
    
    
    
    
    
}