admin
2021-01-25 d182390205a9828bd1091b06fa712e028004c687
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package com.newvideo.util;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import com.newvideo.domain.VideoInfo;
 
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
 
public class SolrUtil {
 
    static String IP_OUT = "203.195.196.115";
    static String IP_IN = "172.16.16.12";// 10.165.56.110 10.129.37.198
 
    private static String escapeQueryChars(String s) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            // These characters are part of the query syntax and must be escaped
            if (c == '\\' || c == '+' || c == '-' || c == '!' || c == '(' || c == ')' || c == ':' || c == '^'
                    || c == '[' || c == ']' || c == '\"' || c == '{' || c == '}' || c == '~' || c == '*' || c == '?'
                    || c == '|' || c == '&' || c == ';' || c == '/' || Character.isWhitespace(c)) {
                sb.append('\\');
            }
            sb.append(c);
        }
        return sb.toString();
    }
 
    public static List<VideoInfo> searchStarVideos(String key, int pageIndex) {
        List<VideoInfo> list = new ArrayList<VideoInfo>();
        try {
            String url = String.format("http://%s:8983/solr/new_video/select?q=%s&fq=show:1&fq=contenttype:1&start="
                    + (pageIndex - 1) * Constant.pageCount + "&rows=" + Constant.pageCount + "&wt=json&indent=true&_="// 10.165.56.110
            // 121.42.58.49
                    + System.currentTimeMillis(), Constant.IsOutNet ? IP_IN : IP_OUT,
                    "mainactor:" + URLEncoder.encode(escapeQueryChars(key), "UTF-8"));
            Map<String, String> map = new HashMap<String, String>();
            map.put("Accept", "application/json, text/javascript, */*; q=0.01");
 
            InputStream is = HttpUtil.getAsInputStream(url);
            BufferedReader in = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            StringBuffer buffer = new StringBuffer();
            String line = "";
            try {
                while ((line = in.readLine()) != null) {
                    buffer.append(line);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
 
            JSONObject data = JSONObject.fromObject(buffer.toString());
            JSONArray array = data.optJSONObject("response").optJSONArray("docs");
            for (int i = 0; i < array.size(); i++) {
                JSONObject obj = array.optJSONObject(i);
                list.add(convertJSONTOVideo(obj));
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return list;
    }
 
    public static List<VideoInfo> search(String key, int count, int pageIndex) {
        List<VideoInfo> list = new ArrayList<VideoInfo>();
        try {
            String url = String.format("http://%s:8983/solr/new_video/select?q=%s&fq=show:1&start="
                    + (pageIndex - 1) * count + "&rows=" + count + "&wt=json&indent=true&_="// 10.165.56.110
            // 121.42.58.49
                    + System.currentTimeMillis(), Constant.IsOutNet ? IP_IN : IP_OUT,
                    "name:" + URLEncoder.encode(escapeQueryChars(key), "UTF-8"));
            Map<String, String> map = new HashMap<String, String>();
            map.put("Accept", "application/json, text/javascript, */*; q=0.01");
 
            InputStream is = HttpUtil.getAsInputStream(url);
            BufferedReader in = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            StringBuffer buffer = new StringBuffer();
            String line = "";
            try {
                while ((line = in.readLine()) != null) {
                    buffer.append(line);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
 
            JSONObject data = JSONObject.fromObject(buffer.toString());
            JSONArray array = data.optJSONObject("response").optJSONArray("docs");
            for (int i = 0; i < array.size(); i++) {
                JSONObject obj = array.optJSONObject(i);
                list.add(convertJSONTOVideo(obj));
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return list;
    }
 
    public static List<VideoInfo> search(String key, int pageIndex) {
        return search(key, Constant.pageCount, pageIndex);
    }
 
    public static List<VideoInfo> searchZhengPian(String key, int pageIndex) {
        List<VideoInfo> list = new ArrayList<VideoInfo>();
        try {
            String url = String.format("http://%s:8983/solr/new_video/select?q=%s&fq=%s&fq=show:1&start="
                    + (pageIndex - 1) * Constant.pageCount + "&rows=" + Constant.pageCount + "&wt=json&indent=true&_="// 10.165.56.110
                                                                                                                        // //
                                                                                                                        // 121.42.58.49
                    + System.currentTimeMillis(), Constant.IsOutNet ? IP_IN : IP_OUT,
                    "name:" + URLEncoder.encode(escapeQueryChars(key), "UTF-8"), "contenttype:1");
            Map<String, String> map = new HashMap<String, String>();
            map.put("Accept", "application/json, text/javascript, */*; q=0.01");
 
            InputStream is = HttpUtil.getAsInputStream(url);
            BufferedReader in = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            StringBuffer buffer = new StringBuffer();
            String line = "";
            try {
                while ((line = in.readLine()) != null) {
                    buffer.append(line);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
 
            JSONObject data = JSONObject.fromObject(buffer.toString());
            JSONArray array = data.optJSONObject("response").optJSONArray("docs");
            for (int i = 0; i < array.size(); i++) {
                JSONObject obj = array.optJSONObject(i);
                list.add(convertJSONTOVideo(obj));
            }
 
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return list;
    }
 
    public static List<VideoInfo> searchZhuanJi(String key, int pageIndex) {
        List<VideoInfo> list = new ArrayList<VideoInfo>();
        try {
            String url = String.format("http://%s:8983/solr/new_video/select?q=%s&fq=%s&fq=show:1&start="
                    + (pageIndex - 1) * Constant.pageCount + "&rows=" + Constant.pageCount + "&wt=json&indent=true&_="// 10.165.56.110
                                                                                                                        // //
                                                                                                                        // 121.42.58.49
                    + System.currentTimeMillis(), Constant.IsOutNet ? IP_IN : IP_OUT,
                    "name:" + URLEncoder.encode(escapeQueryChars(key), "UTF-8"),
                    URLEncoder.encode(("videocount:[2 TO *]"), "UTF-8"));
            Map<String, String> map = new HashMap<String, String>();
            map.put("Accept", "application/json, text/javascript, */*; q=0.01");
 
            InputStream is = HttpUtil.getAsInputStream(url);
            BufferedReader in = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            StringBuffer buffer = new StringBuffer();
            String line = "";
            try {
                while ((line = in.readLine()) != null) {
                    buffer.append(line);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
 
            JSONObject data = JSONObject.fromObject(buffer.toString());
            JSONArray array = data.optJSONObject("response").optJSONArray("docs");
            for (int i = 0; i < array.size(); i++) {
                JSONObject obj = array.optJSONObject(i);
                list.add(convertJSONTOVideo(obj));
            }
 
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return list;
    }
 
    private static VideoInfo convertJSONTOVideo(JSONObject obj) {
        VideoInfo info = new VideoInfo();
        info.setId(obj.optString("id"));
        info.setArea(obj.optString("area"));
        info.setContentType(obj.optInt("contenttype"));
        info.setDirector(obj.optString("director"));
        info.setDuration(obj.optString("duration"));
        info.setHpicture(obj.optString("hpicture"));
        info.setMainActor(obj.optString("mainactor"));
        info.setName(obj.optString("name"));
        info.setPicture(obj.optString("picture"));
        info.setScore(obj.optString("score"));
        info.setShow(obj.optString("show"));
        info.setTag(obj.optString("tag"));
        info.setVpicture(obj.optString("vpicture"));
        info.setWatchCount(obj.optString("watchcount"));
        info.setCommentCount(StringUtil.isNullOrEmpty(obj.optString("commentcount")) ? 0
                : Integer.parseInt(obj.optString("commentcount")));
        return info;
    }
 
    @SuppressWarnings("deprecation")
    public static void deleteVideoIndex(String id) {
        String url = String.format(
                "http://%s:8983/solr/new_video/update?stream.body=%s&stream.contentType=text/xml;charset=utf-8&commit=true",
                Constant.IsOutNet ? IP_IN : IP_OUT, URLEncoder.encode("<delete><id>" + id + "</id></delete>"));
        String result = HttpUtil.get(url);
        System.out.println(result);
    }
 
    // 增量更新
    public static void deltaUpdate() {
        Map<String, String> map = new HashMap<String, String>();
        map.put("command", "delta-import");
        map.put("commit", "true");
        map.put("wt", "json");
        map.put("indent", "true");
        map.put("entity", "video");
        map.put("start", "0");
        map.put("rows", "50000");
        map.put("verbose", "false");
        map.put("clean", "false");
        map.put("optimize", "false");
        map.put("debug", "false");
        String result = HttpUtil
                .post("http://" + (Constant.IsOutNet ? IP_IN : IP_OUT) + ":8983/solr/new_video/deltaimport", map);
        System.out.println(result);
    }
    
 
    // 全量更新
    public static void dataimportVideo() {
        // 分页请求
        int pageSize = 10000;
        for (int i = 0; i < 200; i++) {
            String url = String.format("http://%s:8983/solr/new_video/dataimport?_=%s&indent=on&wt=json",
                    Constant.IsOutNet ? IP_IN : IP_OUT, System.currentTimeMillis() + "");
 
            Map<String, String> map = new HashMap<>();
            map.put("command", "full-import");
            map.put("verbose", "false");
            map.put("clean", "false");
            map.put("commit", "true");
            map.put("core", "new_video");
            map.put("entity", "video");
            map.put("startIndex", i * pageSize + "");
            map.put("count", pageSize + "");
            map.put("name", "dataimport");
            HttpUtil.post(url, map);
            try {
                Thread.sleep(1000 * 30);// 间隔1分钟
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}