java使用elasticsearch进行多个条件模糊查询
文章说明:
1、本篇文章,本人会从java连接elasticsearch到查询结果生成并映射到具体实体类(涵盖分页功能)
2、代码背景:elasticsearch版本为:5.2.0;
3、本人以下代码是分别从两个索引中查询数据,再将两个数据进行整合,如果大家只需要分组查询,那么则选取文章中的分组查询部分代码
4、本人的实体类主要是按照layUI分页框架进行设计;实体大家可以根据自己的具体需求进行设计
一、java连接elasticsearch工具类
public classESClientConnectionUtil {public static TransportClient client=null;public final static String HOST = "192.168.200.200"; //服务器部署ip 根据自己ip进行更改
public final static Integer PORT = 9301; //端口
public staticTransportClient getESClient(){
System.setProperty("es.set.netty.runtime.available.processors", "false");if (client == null) {synchronized (ESClientConnectionUtil.class) {try{//设置集群名称
Settings settings = Settings.builder().put("cluster.name", "es5").put("client.transport.sniff", true).build();//创建client
client = new PreBuiltTransportClient(settings).addTransportAddress(newInetSocketTransportAddress(InetAddress.getByName(HOST), PORT));
}catch(Exception ex) {
ex.printStackTrace();
System.out.println(ex.getMessage());
}
}
}returnclient;
}public staticTransportClient getESClientConnection(){if (client == null) {
System.setProperty("es.set.netty.runtime.available.processors", "false");try{//设置集群名称
Settings settings = Settings.builder().put("cluster.name", "es5").put("client.transport.sniff", true).build();//创建client
client = new PreBuiltTransportClient(settings).addTransportAddress(newInetSocketTransportAddress(InetAddress.getByName(HOST), PORT));
}catch(Exception ex) {
ex.printStackTrace();
System.out.println(ex.getMessage());
}
}returnclient;
}//判断索引是否存在
public static booleanjudgeIndex(String index){
client=getESClientConnection();
IndicesAdminClient adminClient;//查询索引是否存在
adminClient=client.admin().indices();
IndicesExistsRequest request= newIndicesExistsRequest(index);
IndicesExistsResponse responses=adminClient.exists(request).actionGet();if(responses.isExists()) {return true;
}return false;
}
}
二、实体类
(一)分页实体总类
public classKnowledgeTopicListDTO {private Long totalCount;//总条数
private Integer page;//页数
private Integer limit;//每页查询条数
private List topicDTOList;//每页显示数据的对象集合
publicLong getTotalCount() {returntotalCount;
}public voidsetTotalCount(Long totalCount) {this.totalCount =totalCount;
}publicInteger getPage() {returnpage;
}public voidsetPage(Integer page) {this.page =page;
}publicInteger getLimit() {returnlimit;
}public voidsetLimit(Integer limit) {this.limit =limit;
}public ListgetTopicDTOList() {returntopicDTOList;
}public void setTopicDTOList(ListtopicDTOList) {this.topicDTOList =topicDTOList;
}
}
(二)页面显示数据对象实体
public classKnowledgeTopicDTO {private Long id;//知识主题id
private String name;//知识主题名称
private Boolean active;//有效无效 true,false
private String activeString;//有效无效
private Boolean noSubscription;//是否需要订阅 true,false
private String noSubscriptionString;//是否需要订阅
private Long quantity;//数据量
privateString _id;privateString ids;publicString getIds() {returnids;
}public voidsetIds(String ids) {this.ids =ids;
}publicLong getId() {returnid;
}public voidsetId(Long id) {this.id =id;
}publicString getName() {returnname;
}public voidsetName(String name) {this.name =name;
}publicBoolean getActive() {returnactive;
}public voidsetActive(Boolean active) {this.active =active;
}publicString getActiveString() {returnactiveString;
}public voidsetActiveString(String activeString) {this.activeString =activeString;
}publicBoolean getNoSubscription() {returnnoSubscription;
}public voidsetNoSubscription(Boolean noSubscription) {this.noSubscription =noSubscription;
}publicString getNoSubscriptionString() {returnnoSubscriptionString;
}public voidsetNoSubscriptionString(String noSubscriptionString) {this.noSubscriptionString =noSubscriptionString;
}publicLong getQuantity() {returnquantity;
}public voidsetQuantity(Long quantity) {this.quantity =quantity;
}publicString get_id() {return_id;
}public voidset_id(String _id) {this._id =_id;
}
}
三、后台service层代码
publicKnowledgeTopicListDTO selectTopicByName(String name, Integer page, Integer limit) {
SearchResponse searchResponse=null;
Map map = new HashMap<>();
TransportClient transportClient=ESClientConnectionUtil.getESClientConnection();
SearchRequestBuilder requestBuilder= client.prepareSearch("knowledge").setTypes("knowledge_theme");//声明where条件
BoolQueryBuilder qbs =QueryBuilders.boolQuery();/**此处使用模糊匹配查询 类比数据库中 like*/QueryBuilder qb1= QueryBuilders.matchPhraseQuery("name", name);
BoolQueryBuilder bqb1=QueryBuilders.boolQuery().must(qb1);
qbs.must(bqb1);
requestBuilder.setQuery(qbs);int num=limit*(page-1);
SearchResponse response= requestBuilder.setFrom(0).setSize(10).execute().actionGet();//获取总条数//long totalCount = searchResponse.getHits().getTotalHits();
List list = new ArrayList();for(SearchHit hit : response.getHits().getHits()) {//获取到当前页的数据
JSONObject obj = new JSONObject().fromObject(hit.getSourceAsString());//将json字符串转换为json对象
KnowledgeTopicDTO topic = (KnowledgeTopicDTO) JSONObject.toBean(obj, KnowledgeTopicDTO.class);//将建json对象转换为Person对象
list.add(topic);
}//查询主题总数
Terms terms= ESGroupByUtil.GroupByOne(client,"hottopic","hot","sum","tasktitleid");
list= groupList(list,terms);//调用组合主题总数方法
KnowledgeTopicListDTO knowledgeTopicListDTO = newKnowledgeTopicListDTO();
knowledgeTopicListDTO.setLimit(limit);
knowledgeTopicListDTO.setPage(page);
knowledgeTopicListDTO.setTopicDTOList(list);returnknowledgeTopicListDTO;
}
五、根据单个字段分组查询
public classESGroupByUtil {/***@description: 根据单个字段分组求和
*@author:cyb
*@date: 2018-11-16 17:31
*@param: client ES连接
*@param: indices 索引
*@param: types 类型
*@param: alias 分组求和别名
*@param: DomName 分组目标字段名
*@return: org.elasticsearch.search.aggregations.bucket.terms.Terms*/
public staticTerms GroupByOne(TransportClient client,String indices,String types,String alias,String DomName){
SearchRequestBuilder sbuilder=client.prepareSearch(indices).setTypes(types);
TermsAggregationBuilder termsBuilder=AggregationBuilders.terms(alias).field(DomName);
sbuilder.addAggregation(termsBuilder);
SearchResponse responses=sbuilder.execute().actionGet();
Terms terms=responses.getAggregations().get(alias);returnterms;
}
}
六 、将分组查询的数据进行整合到已查询到的集合中
/***@description:将查询的总数合并到list中
*@author:cyb
*@date: 2018-11-16 17:51
*@param: list
*@param: terms
*@return: java.util.List*/
public List groupList(Listlist,Terms terms){
List lists = new ArrayList<>();for(int i=0;i
String id =terms.getBuckets().get(i).getKey().toString();//id
Long sum =terms.getBuckets().get(i).getDocCount();//数量
BsKnowledgeInfoDTO bsKnowledgeInfoDTO1 = newBsKnowledgeInfoDTO();
bsKnowledgeInfoDTO1.setId(id);
bsKnowledgeInfoDTO1.setSum(sum);
lists.add(bsKnowledgeInfoDTO1);
System.out.println("=="+ terms.getBuckets().get(i).getDocCount()+"------"+terms.getBuckets().get(i).getKey());
}for(int i=0;i
list.get(j).setQuantity(lists.get(i).getSum());
}
}
}returnlist;
}
总结:以上代码是本人的亲自测试通过的,分页后期建议大家不用使用from,size格式,当数据量超过1w的时候,速度会越来越慢,并可能造成宕机。
精准条件查询
MatchPhraseQueryBuilder mpq1 =QueryBuilders
.matchPhraseQuery("id",knowledgeId);
qbs.must(mpq1);//主题id
模糊条件查询
QueryBuilder qb1 = QueryBuilders.matchPhraseQuery("title", keyword);
qbs.must(qb1);
requestBuilder.setQuery(qbs);