import java.util.Calendar;
import java.util.Date;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.action.ActionFuture;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.search.SearchHit;
public class Test {
private static String audit_log_index = "merchant_audit_log_20190610";
public static void main(String[] args) throws Exception{
Date date = new Date(1558407585176L);
System.out.println("date==="+date);
}
// @org.junit.Test
public void test() throws Exception{
long t1 = System.currentTimeMillis();
Client client = createInstance();
for(int i=0;i<100;i++){
AuditLog log = new AuditLog();
log.setChannel("channel");
log.setDetail("修改绑定手机号,修改前手机号码:137888888"+i+",修改后手机号码:137666666"+(100-i));
log.setFunc("func");
log.setIpMac("ipMac");
log.setMessage("message");
log.setOperation("operation");
log.setProjectName("projectName");
log.setResult("result");
log.setUser("user1");
log.setUserId("userId");
log.setCreateTime(System.currentTimeMillis());
String source = JSONUtil.encode(log);
IndexResponse response = client.prepareIndex(audit_log_index,"type").setSource(source,XContentType.JSON).get();
// System.out.println(i+"------"+response.getVersion());
}
long t2 = System.currentTimeMillis();
System.out.println("t2-t1:"+(t2-t1));
//146337ms
// String source = "{\"action\":\"action\",\"code\":\"000\",\"content\":\"content\",\"create_date\":\"2019-05-16 00:00:01\",\"message\":\"message\",\"operator\":\"operator\",\"system_id\":\"system_id\"}";
// IndexResponse response = client.prepareIndex("merchant_audit_log_20190516", "type").setSource(source,XContentType.JSON).get();
// System.out.println(response.getVersion());
// BoolQueryBuilder mustQuery = QueryBuilders.boolQuery();
// mustQuery.must(QueryBuilders.termQuery("action", "action"));
// try{
// SearchResponse response = client.prepareSearch("merchant_audit_log_20190516").setQuery(mustQuery).execute().actionGet();
// SearchHit[] hits = response.getHits().getHits();
// for(SearchHit hit : hits){
// String sourceAsString = hit.getSourceAsString();
// System.out.println("sourceAsString="+sourceAsString);
// }
// }catch(Exception e){
//
// }
}
// @org.junit.Test
public void test0() throws Exception{
long t1 = System.currentTimeMillis();
Client client = createInstance();
BulkRequestBuilder bulkRequest = client.prepareBulk();
for(int i=0;i<10000;i++){
AuditLog log = new AuditLog();
log.setChannel("channel");
log.setDetail("修改绑定手机号,修改前手机号码:137888888"+i+",修改后手机号码:137666666"+(100-i));
log.setFunc("func");
log.setIpMac("ipMac");
log.setMessage("message");
log.setOperation("operation");
log.setProjectName("projectName");
log.setResult("result");
log.setUser("user");
log.setUserId("userId");
log.setCreateTime(System.currentTimeMillis());
String source = JSONUtil.encode(log);
bulkRequest.add(client.prepareIndex(audit_log_index,"type").setSource(source,XContentType.JSON));
}
BulkResponse response = bulkRequest.get();
System.out.println("response==="+response.buildFailureMessage());
long t2 = System.currentTimeMillis();
System.out.println("t2-t1:"+(t2-t1));
//24499ms
}
// @org.junit.Test
//单个字段精确查询 termQuery一般用来做精确查询 比如数字,日期,布尔值或 not_analyzed 的字符串(未经分析的文本数据类型)
//setExplain作用为设置按照查询匹配度排序
public void test1() throws Exception{
Client client = createInstance();
SearchResponse response = client.prepareSearch(audit_log_index)
.setQuery(QueryBuilders.termQuery("func", "action1"))
.setFrom(0).setSize(50).setExplain(true).execute().actionGet();
SearchHit[] hits = response.getHits().getHits();
System.out.println("total==="+response.getHits().getTotalHits());
for(SearchHit hit : hits){
String sourceAsString = hit.getSourceAsString();
System.out.println("sourceAsString="+sourceAsString);
}
}
// @org.junit.Test
//(1)多条件查询,使用boolQuery must和filter的区别,filter只筛选数据,must会对数据进行评分
//此处对使用query还是filter做个简单总结,全文检索以及任何使用相关性评分的场景使用query检索,其他情况使用filter检索。过滤查询(filter)是对集合包含/排除的简单检查,这使得它们计算速度非常快,当至少有一个过滤查询是“稀疏”(仅有少量匹配的文档)时,可以利用各种优化,并且可以将缓存经常使用的filter过滤查询缓存在内存中以加快访问速度。
//对比之下,query检索(评分查询)不仅要查找匹配的文档,还要计算每个文档的相关程度,这通常会使其比非评分文档更复杂。 另外,查询结果不可缓存。
//(2)使用should时应该注意顺序,如果想搜索action为action,code不为000并且system_id为system1或者system2的数据,应使用以下方式
public void test2() throws Exception{
Client client = createInstance();
BoolQueryBuilder mustQuery = QueryBuilders.boolQuery();
mustQuery.should(QueryBuilders.termQuery("projectName", "system1"))
.filter(QueryBuilders.termQuery("func", "action"))
.mustNot(QueryBuilders.termQuery("result","000"))
.should(QueryBuilders.termQuery("projectName", "system2"))
.filter(QueryBuilders.termQuery("func", "action"))
.mustNot(QueryBuilders.termQuery("code","000"));
SearchResponse response = client.prepareSearch(audit_log_index).setQuery(mustQuery).setFrom(50).setSize(50).execute().actionGet();
SearchHit[] hits = response.getHits().getHits();
System.out.println("total==="+response.getHits().getTotalHits());
for(SearchHit hit : hits){
String sourceAsString = hit.getSourceAsString();
System.out.println("sourceAsString="+sourceAsString);
}
}
@org.junit.Test
//如果某个字段使用了分词,可以使用matchQuery进行分词查询
//matchPhraseQuery和matchQuery等的区别,在使用matchQuery等时,在执行查询时,搜索的词会被分词器分词,而使用matchPhraseQuery时,不会被分词器分词,而是直接以一个短语的形式查询,而如果你在创建索引所使用的field的value中没有这么一个短语(顺序无差,且连接在一起),那么将查询不出任何结果。
//比如上边的content如果类型设置为text,支持分词,查询时可以使用matchQuery进行,其中 matchQuery为单字段,multiMatchQuery支持多个字段
public void test3() throws Exception{
Client client = createInstance();
SearchResponse response = client.prepareSearch(audit_log_index)
// .setQuery(QueryBuilders.matchPhraseQuery("message", "成功"))
// .setQuery(QueryBuilders.multiMatchQuery("12345", "info","message"))
.setQuery(QueryBuilders.matchQuery("detail", "13788888895")).setFrom(0).setSize(100)
.execute().actionGet();
SearchHit[] hits = response.getHits().getHits();
System.out.println("total==="+response.getHits().getTotalHits());
for(SearchHit hit : hits){
String sourceAsString = hit.getSourceAsString();
System.out.println("sourceAsString="+sourceAsString);
}
}
// @org.junit.Test
//日期查询和分页
public void test4() throws Exception{
Client client = createInstance();
Calendar cal = Calendar.getInstance();
long end = cal.getTime().getTime();
cal.add(Calendar.HOUR_OF_DAY, -1);
long start = cal.getTime().getTime();
RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("createTime").from(start).to(end);
SearchResponse response = client.prepareSearch(audit_log_index).
setQuery(QueryBuilders.boolQuery()
.must(rangeQueryBuilder)
// .must(QueryBuilders.matchPhraseQuery("成功", "message"))
)
.setFrom(0).setSize(50).execute().actionGet();
SearchHit[] hits = response.getHits().getHits();
System.out.println("total==="+response.getHits().getTotalHits());
for(SearchHit hit : hits){
String sourceAsString = hit.getSourceAsString();
System.out.println("sourceAsString="+sourceAsString);
}
}
// @org.junit.Test
//前缀查询
public void test5() throws Exception{
Client client = createInstance();
SearchResponse response = client.prepareSearch(audit_log_index)
.setQuery(QueryBuilders.prefixQuery("user", "user1"))
.setFrom(0).setSize(50).execute().actionGet();
SearchHit[] hits = response.getHits().getHits();
System.out.println("total==="+response.getHits().getTotalHits());
for(SearchHit hit : hits){
String sourceAsString = hit.getSourceAsString();
System.out.println("sourceAsString="+sourceAsString);
}
}
// @org.junit.Test
//通配查询 由于?用来匹配字符的,而非字符串。查询中文的使用?是无效的。
public void test6() throws Exception{
Client client = createInstance();
SearchResponse response = client.prepareSearch(audit_log_index)
.setQuery(QueryBuilders.wildcardQuery("user", "?user2*"))
.setFrom(0).setSize(50).execute().actionGet();
SearchHit[] hits = response.getHits().getHits();
System.out.println("total==="+response.getHits().getTotalHits());
for(SearchHit hit : hits){
String sourceAsString = hit.getSourceAsString();
System.out.println("sourceAsString="+sourceAsString);
}
}
//@org.junit.Test
//使用addSort进行排序
public void test7() throws Exception{
Client client = createInstance();
//解决索引中内容为空时,按照排序字段查询数据异常问题
FieldSortBuilder order = SortBuilders.fieldSort("createTime") .unmappedType("long").order(SortOrder.DESC);
SearchResponse response = client.prepareSearch(audit_log_index)
.setQuery(QueryBuilders.termQuery("user", "user1")).addSort(order)
.setFrom(0).setSize(50).setExplain(true).execute().actionGet();
SearchHit[] hits = response.getHits().getHits();
System.out.println("total==="+response.getHits().getTotalHits());
for(SearchHit hit : hits){
String sourceAsString = hit.getSourceAsString();
System.out.println("sourceAsString="+sourceAsString);
}
}
@org.junit.Test
//使用es的id获取数据
public void test8() throws Exception{
Client client = createInstance();
GetResponse response = client.prepareGet().setIndex(audit_log_index)
.setId("AWt0PYfjjnzkeD-RnWiX").get();
System.out.println(response.getIndex());
String s = response.getSourceAsString();
System.out.println(s);
}
protected static Client createInstance() throws Exception {
try {
//创建对象实例方法
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}