首页 技术 正文
技术 2022年11月6日
0 收藏 475 点赞 1,036 浏览 4391 个字

多条件设置

//多条件设置
MatchPhraseQueryBuilder mpq1 = QueryBuilders
.matchPhraseQuery("pointid","W3.UNIT1.10LBG01CP301");
MatchPhraseQueryBuilder mpq2 = QueryBuilders
.matchPhraseQuery("inputtime","2016-07-21 00:00:01");
QueryBuilder qb2 = QueryBuilders.boolQuery().must(mpq1).must(mpq2);

Demo:

package com.zx.znyd.es;import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;import org.apache.commons.lang.StringUtils;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.Requests;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.mustache.Mustache;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.MatchQueryBuilder.Operator;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import com.alibaba.fastjson.JSONObject;
import com.zx.znyd.common.MD5;
import com.zx.znyd.common.SpringContextUtil;
import com.zx.znyd.dao.AutoReplyDao;
import com.zx.znyd.data.model.AutoReply;
import com.zx.znyd.data.model.LogIndex;
import com.zx.znyd.service.AutoReplyDeployService;@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class TestES { @Autowired
@Qualifier("esClient")
private Client client; @Test
public void testSearch()
{
String index="index_log_daily";
String type="uq_log";
String uquestion = "139";
String province = "江苏";
SearchResponse searchResponse = client.prepareSearch(index)
.setTypes(type)
.setQuery(QueryBuilders.matchAllQuery()) //查询所有
.setQuery(QueryBuilders.boolQuery()
.must(QueryBuilders.matchQuery("uquestion", uquestion))//查询uquestion为139的
.must(QueryBuilders.matchQuery("province", province)))//查询省份为江苏的
.setSearchType(SearchType.QUERY_THEN_FETCH)
.setFrom(0).setSize(10)//分页
//.addSort("age", SortOrder.DESC)//排序
.get(); SearchHits hits = searchResponse.getHits();
long total = hits.getTotalHits();
System.out.println(total);
SearchHit[] searchHits = hits.hits();
for(SearchHit s : searchHits)
{
System.out.println(s.getSourceAsString());
String []logindex=s.getSourceAsString().split(","); }
}
}

  贴下我的代码:

       /**
* 错误日志统计
*/
@Override
public List<TomcatModel> errorTomcatRequest(Page page, String index,String startDate, String endDate) {
TransportClient client = getClient();
if (index.isEmpty() == true) {
return null;
}
DateInterval dateInterval = RegexUtil.getDateInterval(new DateInterval(startDate,endDate),"yyyyMMdd");
List<TomcatModel> list = new ArrayList<TomcatModel>();
try {
SearchResponse response = client.prepareSearch(index).setTypes(getIndexType()).setFrom(page.getStart())
.setSize(page.getLength())
.setQuery(QueryBuilders.boolQuery().must(QueryBuilders.regexpQuery("response", "[4-6][0-9][0-9]"))
.must(QueryBuilders.rangeQuery("@timestamp").format("yyyyMMdd").from(dateInterval.getStartDate()).to(dateInterval.getEndDate())))
.addSort("@timestamp", SortOrder.ASC)
.execute().actionGet();
SearchHits myhits = response.getHits();
page.setRecordsFiltered((int) myhits.getTotalHits());
page.setRecordsTotal((int) myhits.getTotalHits());int i = 1;
for (SearchHit hit : myhits.getHits()) {
TomcatModel tomcat = new TomcatModel();
Map<String, Object> map = hit.getSource();
tomcat.setClientip((String) map.get("clientip"));
tomcat.setResponse((String) map.get("response"));
tomcat.setMessage((String) map.get("message"));
tomcat.setType((String) map.get("verb"));
tomcat.setTimestamp((String) map.get("timestamp"));
tomcat.setRowId(i);
list.add(tomcat);
i++;
}
} catch (IndexNotFoundException e) {
System.err.println("此索引不存在!");
}catch (Exception e) {
e.printStackTrace();
}
return list;
}

  


    					
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,996
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,510
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,353
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,137
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,770
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,848