yujian
2020-05-06 eb1adc13eda955e8ef0fc9fe41fb246fa89b722d
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package com.yeshi.fanli.dao;
 
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
 
import javax.annotation.Resource;
 
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.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 org.yeshi.utils.elasticsearch.Document;
 
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.yeshi.fanli.service.manger.ElasticManger;
 
 
 
@Repository
public abstract class ElasticBaseDao<T> {
 
    @Resource 
    protected  ElasticManger elasticManger;
    
    
    /**
     * 获取index
     * @param bean
     * @return
     */
    public String getDocument(Class<?> entityClass) {
        if (entityClass.isAnnotationPresent(Document.class)) {
            Document doc = (Document)entityClass.getAnnotation(Document.class);
            return doc.indexName();
        }
        String name = entityClass.getName();
        return name.substring(name.lastIndexOf(".") + 1, name.length());
    }
 
    
    
    /**
     * 插入数据
     * 
     * @param bean
     * @return
     */
    public void save(T bean) {
        String document = getDocument(bean.getClass());
        String json = JsonUtil.getSimpleGson().toJson(bean);
        IndexRequest request = new IndexRequest(document);
        request.source(json, XContentType.JSON);
        // 同步执行
        try {
            elasticManger.getClient().index(request, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 插入数据
     * 
     * @param bean
     * @return
     */
    public void save(T bean, String id) {
        String document = getDocument(bean.getClass());
        
        String json = JsonUtil.getSimpleGson().toJson(bean);
        IndexRequest request = new IndexRequest(document).id(id);
        request.source(json, XContentType.JSON);
        // 同步执行
        try {
            elasticManger.getClient().index(request, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
    /**
     * 更新
     * @param Document id
     * @param bean
     */
    public void update(String id, T bean) {
        String document = getDocument(bean.getClass());
        
        UpdateRequest request = new UpdateRequest(document, id);
        String json = new GsonBuilder().create().toJson(bean);
        request.doc(json, XContentType.JSON);
        try {
            elasticManger.getClient().update(request, RequestOptions.DEFAULT);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
 
    /**
     *删除
     * 
     * @param Document id
     */
    public void delete(String id, Class<T> entityClass) {
        String document = getDocument(entityClass);
        DeleteRequest request = new DeleteRequest(document, id);
        try {
            elasticManger.getClient().delete(request, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 查询
     * 
     * @param Document id
     * @return
     */
    public T get(String id, Class<T> entityClass) {
        String document = getDocument(entityClass);
        GetRequest request = new GetRequest(document, id);
        try {
            GetResponse getResponse = elasticManger.getClient().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) {
        String index = getDocument(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 = elasticManger.getClient().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;
    }
    
}