package com.yeshi.buwan.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.yeshi.buwan.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/buwan/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 pageIndex) {
|
List<VideoInfo> list = new ArrayList<VideoInfo>();
|
try {
|
String url = String.format("http://%s:8983/solr/buwan/select?q=%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"));
|
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> searchZhengPian(String key, int pageIndex) {
|
List<VideoInfo> list = new ArrayList<VideoInfo>();
|
try {
|
String url = String.format("http://%s:8983/solr/buwan/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/buwan/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.setUpdatetime(obj.optString("updatetime"));
|
info.setCreatetime(obj.optLong("createtime"));
|
info.setCommentCount(StringUtil.isNullOrEmpty(obj.optString("commentcount")) ? 0
|
: Integer.parseInt(obj.optString("commentcount")));
|
info.setVideocount(obj.optInt("videocount"));
|
return info;
|
}
|
|
@SuppressWarnings("deprecation")
|
public static void deleteVideoIndex(String id) {
|
String url = String.format(
|
"http://%s:8983/solr/buwan/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 Map<String, Object> getCategoryVideoList(int pageIndex, String key, int contenttype,
|
List<Long> videoTypeIds, String sort) {
|
Map<String, Object> map = new HashMap<String, Object>();
|
|
String q = "";
|
q += "q=";
|
if (videoTypeIds != null && videoTypeIds.size() > 0) {
|
|
String qparmas = "(";
|
for (Long type : videoTypeIds) {
|
qparmas += String.format("videotypeid:%s OR ", type + "");
|
}
|
|
if (qparmas.endsWith("OR "))
|
qparmas = qparmas.substring(0, qparmas.length() - 3);
|
|
qparmas += ")";
|
if (!StringUtil.isNullOrEmpty(key)) {
|
qparmas += String.format(" AND (name:%s)", "\"" + key + "\"");
|
}
|
|
try {
|
q += URLEncoder.encode(qparmas, "UTF-8");
|
} catch (UnsupportedEncodingException e) {
|
e.printStackTrace();
|
}
|
} else {
|
String qparmas = "";
|
if (!StringUtil.isNullOrEmpty(key)) {
|
qparmas += String.format("name:%s", "\"" + key + "\"");
|
} else {
|
qparmas += "*:*";
|
}
|
try {
|
q += URLEncoder.encode(qparmas, "UTF-8");
|
} catch (UnsupportedEncodingException e) {
|
e.printStackTrace();
|
}
|
|
}
|
|
if (sort != null) {
|
try {
|
sort = URLEncoder.encode(sort, "UTF-8");
|
} catch (UnsupportedEncodingException e) {
|
e.printStackTrace();
|
}
|
}
|
|
String fq = "";
|
|
if (contenttype == 1)
|
fq += "contenttype:1";
|
else if (contenttype > 1) {
|
fq += "contenttype:[2 TO 100]";
|
}
|
|
fq = StringUtil.isNullOrEmpty(fq) ? "" : "fq=" + URLEncoder.encode(fq);
|
|
String url = String.format(
|
"http://%s:8983/solr/buwan_videotype_video/select?%s&%s&group=true&group.field=videoid&sort=%s&start="
|
+ (pageIndex - 1) * Constant.pageCount + "&rows=" + Constant.pageCount
|
+ "&wt=json&indent=true&_=" + System.currentTimeMillis(),
|
Constant.IsOutNet ? IP_IN : IP_OUT, q, fq, sort);
|
|
try {
|
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());
|
int count = data.optJSONObject("grouped").optJSONObject("videoid").optInt("matches");
|
map.put("count", count);
|
JSONArray array = data.optJSONObject("grouped").optJSONObject("videoid").optJSONArray("groups");
|
List<Long> videoIdList = new ArrayList<Long>();
|
for (int i = 0; i < array.size(); i++) {
|
JSONArray docsArray = array.optJSONObject(i).optJSONObject("doclist").optJSONArray("docs");
|
if (docsArray.size() > 0) {
|
videoIdList.add(docsArray.optJSONObject(0).optLong("videoid"));
|
}
|
}
|
|
if (videoIdList.size() > 0) {// 有视频数据
|
List<VideoInfo> videoList = new ArrayList<VideoInfo>();
|
|
q = "q=";
|
String qparams = "";
|
for (long videoid : videoIdList) {
|
qparams += String.format("id:%s or ", videoid);
|
}
|
if (qparams.endsWith("or "))
|
qparams = qparams.substring(0, qparams.length() - 3);
|
q += URLEncoder.encode(qparams, "UTF-8");
|
url = String.format(
|
"http://%s:8983/solr/buwan/select?%s&sort=%s&rows=" + Constant.pageCount + ""
|
+ "&wt=json&indent=true&_t=" + System.currentTimeMillis(),
|
Constant.IsOutNet ? IP_IN : IP_OUT, q, sort);
|
is = HttpUtil.getAsInputStream(url);
|
in = new BufferedReader(new InputStreamReader(is, "UTF-8"));
|
buffer = new StringBuffer();
|
line = "";
|
try {
|
while ((line = in.readLine()) != null) {
|
buffer.append(line);
|
}
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
|
data = JSONObject.fromObject(buffer.toString());
|
array = data.optJSONObject("response").optJSONArray("docs");
|
for (int i = 0; i < array.size(); i++) {
|
JSONObject obj = array.optJSONObject(i);
|
videoList.add(convertJSONTOVideo(obj));
|
}
|
map.put("data", videoList);
|
return map;
|
}
|
|
} catch (Exception e) {
|
e.fillInStackTrace();
|
}
|
return null;
|
|
}
|
|
public static Map<String, Object> getCategoryVideoList(int pageIndex, String key, int contenttype,
|
List<Long> videoTypeIds) {
|
Map<String, Object> map = new HashMap<String, Object>();
|
|
String q = "";
|
q += "q=";
|
if (videoTypeIds != null && videoTypeIds.size() > 0) {
|
|
String qparmas = "(";
|
for (Long type : videoTypeIds) {
|
qparmas += String.format("videotypeid:%s or ", type + "");
|
}
|
|
if (qparmas.endsWith("or "))
|
qparmas = qparmas.substring(0, qparmas.length() - 3);
|
|
qparmas += ")";
|
if (!StringUtil.isNullOrEmpty(key)) {
|
qparmas += String.format(" and (name:%s)", key);
|
}
|
|
try {
|
q += URLEncoder.encode(qparmas, "UTF-8");
|
} catch (UnsupportedEncodingException e) {
|
e.printStackTrace();
|
}
|
} else {
|
String qparmas = "";
|
if (!StringUtil.isNullOrEmpty(key)) {
|
qparmas += String.format("name:%s", key);
|
} else {
|
qparmas += "*:*";
|
}
|
try {
|
q += URLEncoder.encode(qparmas, "UTF-8");
|
} catch (UnsupportedEncodingException e) {
|
e.printStackTrace();
|
}
|
|
}
|
|
String sort = "";
|
try {
|
if (StringUtil.isNullOrEmpty(key))
|
sort = URLEncoder.encode("createtime desc", "UTF-8");
|
} catch (UnsupportedEncodingException e) {
|
e.printStackTrace();
|
}
|
|
String fq = "";
|
|
if (contenttype == 1)
|
fq += "contenttype:1";
|
else if (contenttype > 1) {
|
fq += "contenttype:[2 TO 100]";
|
}
|
|
fq = StringUtil.isNullOrEmpty(fq) ? "" : "fq=" + URLEncoder.encode(fq);
|
|
String url = String.format(
|
"http://%s:8983/solr/buwan_videotype_video/select?%s&%s&group=true&group.field=videoid&sort=%s&start="
|
+ (pageIndex - 1) * Constant.pageCount + "&rows=" + Constant.pageCount
|
+ "&wt=json&indent=true&_=" + System.currentTimeMillis(),
|
Constant.IsOutNet ? IP_IN : IP_OUT, q, fq, sort);
|
|
try {
|
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());
|
int count = data.optJSONObject("grouped").optJSONObject("videoid").optInt("matches");
|
map.put("count", count);
|
JSONArray array = data.optJSONObject("grouped").optJSONObject("videoid").optJSONArray("groups");
|
List<Long> videoIdList = new ArrayList<Long>();
|
for (int i = 0; i < array.size(); i++) {
|
JSONArray docsArray = array.optJSONObject(i).optJSONObject("doclist").optJSONArray("docs");
|
if (docsArray.size() > 0) {
|
videoIdList.add(docsArray.optJSONObject(0).optLong("videoid"));
|
}
|
}
|
|
if (videoIdList.size() > 0) {// 有视频数据
|
List<VideoInfo> videoList = new ArrayList<VideoInfo>();
|
|
q = "q=";
|
String qparams = "";
|
for (long videoid : videoIdList) {
|
qparams += String.format("id:%s or ", videoid);
|
}
|
if (qparams.endsWith("or "))
|
qparams = qparams.substring(0, qparams.length() - 3);
|
q += URLEncoder.encode(qparams, "UTF-8");
|
url = String.format(
|
"http://%s:8983/solr/buwan/select?%s&sort=%s&rows=" + Constant.pageCount + ""
|
+ "&wt=json&indent=true&_t=" + System.currentTimeMillis(),
|
Constant.IsOutNet ? IP_IN : IP_OUT, q, sort);
|
is = HttpUtil.getAsInputStream(url);
|
in = new BufferedReader(new InputStreamReader(is, "UTF-8"));
|
buffer = new StringBuffer();
|
line = "";
|
try {
|
while ((line = in.readLine()) != null) {
|
buffer.append(line);
|
}
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
|
data = JSONObject.fromObject(buffer.toString());
|
array = data.optJSONObject("response").optJSONArray("docs");
|
for (int i = 0; i < array.size(); i++) {
|
JSONObject obj = array.optJSONObject(i);
|
videoList.add(convertJSONTOVideo(obj));
|
}
|
map.put("data", videoList);
|
return map;
|
}
|
|
} catch (Exception e) {
|
|
}
|
return null;
|
}
|
|
// 增量更新Video
|
public static void deltaimportVideo() {
|
String url = String.format(
|
"http://%s:8983/solr/buwan/deltaimport?command=delta-import&commit=true&wt=json&indent=true&verbose=false&clean=false&optimize=false&debug=false",
|
Constant.IsOutNet ? IP_IN : IP_OUT);
|
HttpUtil.get(url);
|
// 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/buwan/deltaimport",
|
// map);
|
}
|
|
// 全量更新
|
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 * 20);// 间隔1分钟
|
} catch (InterruptedException e) {
|
e.printStackTrace();
|
}
|
}
|
|
}
|
|
// 全量更新
|
public static void dataimportCategoryVideo() {
|
String url = String.format(
|
"http://%s:8983/solr/buwan_videotype_video/dataimport?command=full-import&commit=true&wt=json&indent=true&verbose=false&optimize=false&debug=false",
|
Constant.IsOutNet ? IP_IN : IP_OUT);
|
HttpUtil.get(url);
|
}
|
|
// 增量更新
|
public static void deltaimportCategoryVideo() {
|
String url = String.format(
|
"http://%s:8983/solr/buwan_videotype_video/deltaimport?command=delta-import&commit=true&wt=json&indent=true&verbose=false&clean=false&optimize=false&debug=false",
|
Constant.IsOutNet ? IP_IN : IP_OUT);
|
HttpUtil.get(url);
|
};
|
|
}
|