package com.wenjin.zhu.elasticsearch;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.sort.SortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
/**
*
*
* @Title: EsJavaApi.java
* @Package com.wenjin.elasticsearch
* @Description: elastic api
* @author: wenjin.zhu
* @date: 2018年11月14日 下午2:18:24
* @version V1.0
*/
public class EsJavaApiByPageOrder {
private static TransportClient client = TransprostClientManager.getInstance();
public static String index = "yuqing";
public static String type = "targetData";
public static void main(String[] args) {
Map<String, String> map2 = new HashMap<String, String>();
map2.put("title", "*泄露个人隐*");
map2.put("is_sen", "1");
map2.put("over_sea", "1");
// 查询满足条件后第一页的数据
List<TargetData> targetlist = getDataByMuchillegible(type, map2, 1,"0","1");
// 查询满足条件后的数据总记录数
int totalCount = getDataByMuchillegibleCount(type, map2, "0", "1");
// 总页数
int pageCount = (totalCount + 10 - 1) / 10;
}
/**
*
* @Title: getDataByMuchillegible
* @Description: 多条件 模糊查询
* @param: @param type
* @param: @param map
* @param: @param page
* @param: @return
* @return: List<Map<String,Object>>
* @throws
*/
public static List<TargetData> getDataByMuchillegible(String type, Map<String, String> map, int page,
String sortType, String timeType) {
int from = 0;
if (page == 0) {
from = 0;
} else if (page == 1) {
from = 0;
} else {
from = page * 10 - 10;
}
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
if (("2").equals(map.get("over_sea"))) {
// 全部 国内+国外
map.remove("over_sea");
}
if (("2").equals(map.get("is_sen"))) {
// 全部 敏感+非敏感
map.remove("is_sen");
}
for (String in : map.keySet()) {
// map.keySet()返回的是所有key的值
String str = map.get(in);// 得到每个key多对用value的值
boolQueryBuilder.must(QueryBuilders.matchQuery(in, str));
}
SearchResponse response = null;
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String currentTime = dateFormat.format(new java.util.Date()) + " 00:00";
long time = 0, endtime = 0;
try {
time = String2Date.stringToDate2(currentTime).getTime();
} catch (ParseException e) {
e.printStackTrace();
}
SortBuilder<?> sortBuilder = SortBuilders.fieldSort("releaseDate").order(SortOrder.DESC);
if ("0".equals(sortType)) {
// 时间降序
sortBuilder = SortBuilders.fieldSort("releaseDate").order(SortOrder.DESC);
} else if ("1".equals(sortType)) {
// 时间升序
sortBuilder = SortBuilders.fieldSort("releaseDate").order(SortOrder.ASC);
} else {
// 入库时间
sortBuilder = SortBuilders.fieldSort("createDate").order(SortOrder.DESC);
}
if ("0".equals(timeType)) {
response = client.prepareSearch(index).setTypes(type).setQuery(boolQueryBuilder).setFrom(from)
.addSort(sortBuilder).setSize(10).setExplain(true).execute().actionGet();
} else if ("1".equals(timeType)) {
// 今天
endtime = time + 86400000;
response = client.prepareSearch(index).setTypes(type).setQuery(boolQueryBuilder)
.setPostFilter(QueryBuilders.rangeQuery("releaseDate").from(time).to(endtime)).setFrom(from)
.addSort(sortBuilder).setSize(10).setExplain(true).execute().actionGet();
} else if ("2".equals(timeType)) {
// 2天
endtime = time + 86400000;
time = time - 86400000;
response = client.prepareSearch(index).setTypes(type).setQuery(boolQueryBuilder)
.setPostFilter(QueryBuilders.rangeQuery("releaseDate").from(time).to(endtime)).setFrom(from)
.addSort(sortBuilder).setSize(10).setExplain(true).execute().actionGet();
} else if ("3".equals(timeType)) {
// 3天
endtime = time + 86400000;
time = time - 86400000 * 2;
response = client.prepareSearch(index).setTypes(type).setQuery(boolQueryBuilder)
.setPostFilter(QueryBuilders.rangeQuery("releaseDate").from(time).to(endtime)).setFrom(from)
.addSort(sortBuilder).setSize(10).setExplain(true).execute().actionGet();
} else if ("7".equals(timeType)) {
// 7天
endtime = time + 86400000;
time = time - 86400000 * 6;
response = client.prepareSearch(index).setTypes(type).setQuery(boolQueryBuilder)
.setPostFilter(QueryBuilders.rangeQuery("releaseDate").from(time).to(endtime)).setFrom(from)
.addSort(sortBuilder).setSize(10).setExplain(true).execute().actionGet();
} else if ("10".equals(timeType)) {
// 10天
endtime = time + 86400000;
time = time - 86400000 * 9;
response = client.prepareSearch(index).setTypes(type).setQuery(boolQueryBuilder)
.setPostFilter(QueryBuilders.rangeQuery("releaseDate").from(time).to(endtime)).setFrom(from)
.addSort(sortBuilder).setSize(10).setExplain(true).execute().actionGet();
}
return responseToList(client, response);
}
/**
*
* @Title: getDataByMuchillegibleCount
* @Description: 多条件 模糊查询 总数
* @param: @param type
* @param: @param map
* @param: @return
* @return: int
* @throws
*/
public static int getDataByMuchillegibleCount(String type, Map<String, String> map, String sortType,
String timeType) {
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
if (("2").equals(map.get("over_sea"))) {
// 全部 国内+国外
map.remove("over_sea");
}
if (("2").equals(map.get("is_sen"))) {
// 全部 敏感+非敏感
map.remove("is_sen");
}
for (String in : map.keySet()) {
// map.keySet()返回的是所有key的值
String str = map.get(in);// 得到每个key多对用value的值
boolQueryBuilder.must(QueryBuilders.matchQuery(in, str));
}
SearchResponse response = null;
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String currentTime = dateFormat.format(new java.util.Date()) + " 00:00";
long time = 0, endtime = 0;
try {
time = String2Date.stringToDate2(currentTime).getTime();
endtime = time + 86400000;
} catch (ParseException e) {
e.printStackTrace();
}
if ("0".equals(timeType)) {
response = client.prepareSearch(index).setTypes(type).setQuery(boolQueryBuilder).setFrom(0).setSize(2000)
.setExplain(true).execute().actionGet();
} else if ("1".equals(timeType)) {
// 今天
response = client.prepareSearch(index).setTypes(type).setQuery(boolQueryBuilder)
.setPostFilter(QueryBuilders.rangeQuery("releaseDate").from(time).to(endtime)).setFrom(0)
.setSize(2000).setExplain(true).execute().actionGet();
} else if ("2".equals(timeType)) {
// 2天
endtime = time + 86400000;
time = time - 86400000 * 1;
response = client.prepareSearch(index).setTypes(type).setQuery(boolQueryBuilder)
.setPostFilter(QueryBuilders.rangeQuery("releaseDate").from(time).to(endtime)).setFrom(0)
.setSize(2000).setExplain(true).execute().actionGet();
} else if ("3".equals(timeType)) {
// 3天
endtime = time + 86400000;
time = time - 86400000 * 2;
response = client.prepareSearch(index).setTypes(type).setQuery(boolQueryBuilder)
.setPostFilter(QueryBuilders.rangeQuery("releaseDate").from(time).to(endtime)).setFrom(0)
.setSize(2000).setExplain(true).execute().actionGet();
} else if ("7".equals(timeType)) {