Elasticsearch Java API—多条件查询(must)
多条件设置
//多条件设置
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;
}
Elasticsearch Java API—多条件查询(must)的更多相关文章
- [ElasticSearch]Java API 之 词条查询(Term Level Query)
1. 词条查询(Term Query) 词条查询是ElasticSearch的一个简单查询.它仅匹配在给定字段中含有该词条的文档,而且是确切的.未经分析的词条.term 查询 会查找我们设定的准确值 ...
- Elasticsearch java api 常用查询方法QueryBuilder构造举例
转载:http://m.blog.csdn.net/u012546526/article/details/74184769 Elasticsearch java api 常用查询方法QueryBuil ...
- Elasticsearch java api 基本搜索部分详解
文档是结合几个博客整理出来的,内容大部分为转载内容.在使用过程中,对一些疑问点进行了整理与解析. Elasticsearch java api 基本搜索部分详解 ElasticSearch 常用的查询 ...
- 第08章 ElasticSearch Java API
本章内容 使用客户端对象(client object)连接到本地或远程ElasticSearch集群. 逐条或批量索引文档. 更新文档内容. 使用各种ElasticSearch支持的查询方式. 处理E ...
- Elasticsearch Java API 很全的整理
Elasticsearch 的API 分为 REST Client API(http请求形式)以及 transportClient API两种.相比来说transportClient API效率更高, ...
- Elasticsearch Java API深入详解
0.题记 之前Elasticsearch的应用比较多,但大多集中在关系型.非关系型数据库与Elasticsearch之间的同步.以上内容完成了Elasticsearch所需要的基础数据量的供给.但想要 ...
- [搜索]ElasticSearch Java Api(一) -添加数据创建索引
转载:http://blog.csdn.net/napoay/article/details/51707023 ElasticSearch JAVA API官网文档:https://www.elast ...
- 第四部分 数据搜索之使用HBASE的API实现条件查询
因为数据清洗部分需要用到Mapreduce,所以先解决hbase的问题,可以用命令先在hbase存一下简单的数据进行查询,之后只要替换数据就可以实现了原本功能 在看该部分前,确保Hase API看了, ...
- ElasticSearch AggregationBuilders java api常用聚会查询
以球员信息为例,player索引的player type包含5个字段,姓名,年龄,薪水,球队,场上位置.index的mapping为: "mappings": { "pl ...
随机推荐
- 剑指offer——35复杂链表的复制
这题很是巧妙. 突破了常规思维. 竟然可以把传入进来的链表和复制的链表链在一起.然后再算出slibling指针.最后在分离. 直接把空间复杂度变为O(1)了. 很巧妙,很实用. 题目: 请实现函数Co ...
- Post Office Protocol --- pop协议
https://en.wikipedia.org/wiki/Simple_Mail_Transfer_Protocol
- C# AES 加密与解密
AES 算法加密(ECB模式) 将明文加密,加密后进行base64编码,返回密文 /// <summary> /// AES 算法加密(ECB模式) 将明文加密,加密后进行base64编码 ...
- Bulk Convert DOC to DOCX
原文链接 :http://blogs.msdn.com/b/ericwhite/archive/2008/09/19/bulk-convert-doc-to-docx.aspx 帮助文档:http:/ ...
- Node.js 文件系统fs模块
Node.js 文件系统封装在 fs 模块是中,它提供了文件的读取.写入.更名.删除.遍历目录.链接等POSIX 文件系统操作. 与其他模块不同的是,fs 模块中所有的操作都提供了异步的和 同步的两个 ...
- vue--动态路由和get传值
动态路由: <template> <div id="News"> <v-header></v-header> <hr> ...
- 服务端渲染(ssr)初了解
之前接触的比较多的是SPA单页面应用,前端路由渲染,对于node服务端渲染刚开始了解到,服务端渲染的话相对于SPA来说有助于SEO优化,首屏加载更快. 和之前的SPA项目不同,之前公司spa的发布部署 ...
- .net framework 类库中必须掌握的命名空间(或者类)
Web开发常用命名空间和类. System.Collections //命名空间包含接口和类,这些接口和类定义各种对象(如列表.队列.位数组.哈希表和字典)的集合.System.Collections ...
- hdu6393Traffic Network in Numazu【树状数组】【LCA】
Traffic Network in Numazu Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (J ...
- 使用Pangolon在同一副图中,画出两个轨迹,比较误差
使用 code/ground-truth.txt 和 code/estimate.txt 两条轨迹.请你根据上面公式,实现 RMSE的计算代码,给出最后的 RMSE 结果.作为验算,参考答案为:2.2 ...