Elasticsearch教程(九) elasticsearch 查询数据 | 分页查询

soゝso 2016-08-23 00:23:13 46769

Elasticsearch  目录

Elasticsearch  的查询很灵活,并且有Filter,有分组功能,还有ScriptFilter等等,所以很强大。下面上代码:

一个简单的查询,返回一个List<对象> .。

/**
 * 根据Id 查询 SOBangg
 * @param key
 * @return
 */
public static List findSOBanggById(String id) {

	Client client = ESTools.client;
	SearchResponse response = client.prepareSearch(MappingManager.INDEX)
    .setTypes(MappingManager.B_TYPE)
    .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
    .setQuery(QueryBuilders.termQuery("id",id))                 // Query
//        .setPostFilter(QueryBuilders.rangeQuery("age").from(12).to(18))     // Filter
    .setFrom(0).setSize(60).setExplain(true)
    .execute()
    .actionGet();
	SearchHits hits = response.getHits();
	List list = new ArrayList();
	for (SearchHit searchHit : hits) {
		Map source = searchHit.getSource();
		SOBangg entity = (SOBangg) JSONObject.toBean(JSONObject.fromObject(source) , SOBangg.class);
		list.add(entity);
	}
	return list;
}

下面我把我整个类贴出来。

package com.sojson.core.elasticsearch.manager;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;

import com.sojson.common.model.SOBang;
import com.sojson.common.model.SOBangg;
import com.sojson.common.model.SOBanggKey;
import com.sojson.core.elasticsearch.utils.ESTools;
import com.sojson.core.mybatis.page.Pagination;

public class SelectManager {
	
	
	/**
	 * 分页查询 SOBang
	 * @param resultMap
	 * @param pageSize
	 * @param pageNo
	 * @return
	 */
	public static Pagination findByPage(Map resultMap, 
			Integer pageSize,
			Integer pageNo){
		Pagination page = new Pagination();
		pageNo = null==pageNo?1:pageNo;
		page.setPageNo(pageNo);
		page.setPageSize(pageSize);
		
		
		Client client = ESTools.client;
		SearchRequestBuilder srb = client.prepareSearch(MappingManager.INDEX);
		srb.setSearchType(SearchType.DFS_QUERY_THEN_FETCH);
		srb.setTypes(MappingManager.TYPE);
//		srb.setQuery(resultMap);
		srb.setFrom((pageNo - 1) * pageSize).setSize(pageSize)
		.setExplain(true);
		
		SearchResponse response = srb.execute().actionGet();
		SearchHits hits = response.getHits();
		
		page.setTotalCount((int)hits.getTotalHits());
		List list = new ArrayList();
		for (SearchHit searchHit : hits) {
			Map source = searchHit.getSource();
			SOBang entity = (SOBang)JSONObject.toBean(JSONObject.fromObject(source) 
						, SOBang.class);
			list.add(entity);
		}
		page.setList(list);
		return page;
		
	}
	/**
	 * 分页查询 SOBangg
	 * @param resultMap
	 * @param pageSize
	 * @param pageNo
	 * @return
	 */
	public static Pagination findSOBanggByPage(Map resultMap,
			Integer pageSize,
			Integer pageNo){
		Pagination page = new Pagination();
		pageNo = null==pageNo?1:pageNo;
		page.setPageNo(pageNo);
		page.setPageSize(pageSize);
		
		
		Client client = ESTools.client;
		SearchRequestBuilder srb = client.prepareSearch(MappingManager.INDEX);
		srb.setSearchType(SearchType.DFS_QUERY_THEN_FETCH);
		srb.setTypes(MappingManager.B_TYPE);
		
		srb.setQuery(QueryBuilders.termQuery("status",0));
		srb.setFrom((pageNo - 1) * pageSize).setSize(pageSize)
		.setExplain(true);
		
		SearchResponse response = srb.execute().actionGet();
		SearchHits hits = response.getHits();
		
		page.setTotalCount((int)hits.getTotalHits());
		List list = new ArrayList();
		for (SearchHit searchHit : hits) {
			Map source = searchHit.getSource();
			SOBangg entity = (SOBangg) JSONObject
				.toBean(JSONObject.fromObject(source) , SOBangg.class);
			list.add(entity);
		}
		page.setList(list);
		return page;
		
	}
	/**
	 * 根据ID查询SOBang
	 * @param id
	 * @return
	 */
	public static SOBang findSOBangById(String id){
		GetRequest rq = new GetRequest(MappingManager.INDEX, MappingManager.TYPE, id);
		//GetResponse response = client.prepareGet(MappingManager.INDEX, MappingManager.TYPE, id);
		GetResponse response = ESTools.client.get(rq).actionGet();
		SOBang entity = null;
		//判断非空
		if(!response.isSourceEmpty()){
			Map data = response.getSource();
			entity = (SOBang)JSONObject.toBean(JSONObject.fromObject(data)
						,SOBang.class);
		}
		return entity;
	}
	/**
	 * 根据Key 查询 SOBangg
	 * @param key
	 * @return
	 */
	public static SOBangg findSOBanggByKey(SOBanggKey key) {
		String prefix = "%sx_x%s";
		String id = String.format(prefix, key.getId(),key.getGid());
		GetRequest rq = new GetRequest(MappingManager.INDEX, MappingManager.B_TYPE, id);
		//GetResponse response = client.prepareGet(MappingManager.INDEX, MappingManager.TYPE, id);
		GetResponse response = ESTools.client.get(rq).actionGet();
		SOBangg entity = null;
		//判断非空
		if(!response.isSourceEmpty()){
			Map data = response.getSource();
			entity = (SOBangg)JSONObject.toBean(JSONObject.fromObject(data)
						,SOBangg.class);
		}
		return entity;
	}
	/**
	 * 根据Id 查询 SOBangg
	 * @param key
	 * @return
	 */
	public static List findSOBanggById(String id) {
	
		Client client = ESTools.client;
		SearchResponse response = client.prepareSearch(MappingManager.INDEX)
	    .setTypes(MappingManager.B_TYPE)
	    .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
	    .setQuery(QueryBuilders.termQuery("id",id))                 // Query
	//        .setPostFilter(QueryBuilders.rangeQuery("age").from(12).to(18))     // Filter
	    .setFrom(0).setSize(60).setExplain(true)
	    .execute()
	    .actionGet();
		SearchHits hits = response.getHits();
		List list = new ArrayList();
		for (SearchHit searchHit : hits) {
			Map source = searchHit.getSource();
			SOBangg entity = (SOBangg) JSONObject.toBean(JSONObject.fromObject(source) 
						, SOBangg.class);
			list.add(entity);
		}
		return list;
	}
	
	
	
	public static BangDetailsBo findById(String id) {
		BangDetailsBo result = new BangDetailsBo();
		GetRequest rq = new GetRequest(MappingManager.INDEX, MappingManager.TYPE, id);
		
		GetResponse response = ESTools.client.get(rq).actionGet();
		//判断非空
		if(!response.isSourceEmpty()){
			Map data = response.getSource();
			SOBang entity = (SOBang)JSONObject.toBean(JSONObject.fromObject(data)
						,SOBang.class);
			result.setEntity(entity);
		}
		
		return result;
	}
}


自己鼓捣鼓捣吧,有问题加群问我,Pagination对象我没公开,这个是系统里的,和这个查询没关系,你可以替换成你的Page对象即可。


版权所属:SO JSON在线解析

原文地址:https://www.sojson.com/blog/90.html

转载时必须以链接形式注明原始出处及本声明。

本文主题:

如果本文对你有帮助,那么请你赞助我,让我更有激情的写下去,帮助更多的人。

相关文章
Elasticsearch教程elasticsearch 查询数据 | 分页查询
Elasticsearch教程Elasticsearch count 查询Elasticsearch 查询是否存在
Elasticsearch 教程Elasticsearch 日期查询详解,Elasticsearch Date 查询Java API
Elasticsearch 聚合(aggregation)查询返回所有
Elasticsearch教程(八) elasticsearch delete 删除数据(Java)
Elasticsearch教程(七) elasticsearch Insert 插入数据(Java)
Elasticsearch教程(六) elasticsearch Client创建
Elasticsearch教程(五) elasticsearch Mapping的创建
Elasticsearch教程Elasticsearch配置文件 — elasticsearch.yml
Elasticsearch操作数据后马上更新的办法
最新文章
记一次小失误导致的大事件,Mysql SQL Error: 0, SQLState: null 排查过程 39
Springboot Maven 增加本地依赖包,Springboot Maven打包本地包 81
Java 获取图片属性、获取图片EXIF属性操作方法[metadata-extractor] 41
又拍云(Upyun)CDN、云存储刷新链接缓存,API实例讲解 77
Linux Centos 使用 Redis service 启动,Redis service 脚本编写 111
恭喜SOJSON获得阿里云618“上云接力赛”第一名,获得代金券6.18万 329
Springboot 集成Freemarker 自定义标签解决方案 922
七牛云怎么样?七牛云到底有多垃圾?用七牛云需要知道的事,不然不小心就一套房没了 29339
JavaScript怎么识别360浏览器?JS识别360急速模式方案,360流氓浏览器 1236
关于本站所有JavaScript 加密、混淆、解密、美化等安全说明 2322
最热文章
免费天气API,全国天气 JSON API接口,可以获取五天的天气预报 181970
我为什么要选择RabbitMQ ,RabbitMQ简介,各种MQ选型对比 147888
Elasticsearch教程(四) elasticsearch head 插件安装和使用 141500
苹果电脑Mac怎么恢复出厂系统?苹果系统怎么重装系统? 94439
Elasticsearch教程(六) elasticsearch Client创建 82816
Elasticsearch教程(一),全程直播(小白级别) 79684
Elasticsearch教程(二),IK分词器安装 78072
Elasticsearch教程(八) elasticsearch delete 删除数据(Java) 77403
Elasticsearch教程(五) elasticsearch Mapping的创建 71312
免费天气API,天气JSON API,不限次数获取十五天的天气预报 65831

骚码加入我们 / 千人QQ群:259217951

入群需要5元,如果没有QQ钱包,可以先Alipay、微信,赞助然后加群主拉进。

二维码生成 来自 >> 二维码生成器

支付扫码

所有赞助/开支都讲公开明细,用于网站维护:赞助名单查看

正在加载... ...