首页 > 代码库 > Elasticsearch教程(九) elasticsearch 查询数据 | 分页查询
Elasticsearch教程(九) 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
* 根据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;
}
.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 = http://www.mamicode.com/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 = http://www.mamicode.com/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 = http://www.mamicode.com/response.getSource();
SOBang entity = (SOBang)JSONObject.toBean(JSONObject.fromObject(data)
,SOBang.class);
result.setEntity(entity);
}
return result;
}
}
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 = http://www.mamicode.com/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 = http://www.mamicode.com/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 = http://www.mamicode.com/response.getSource();
SOBang entity = (SOBang)JSONObject.toBean(JSONObject.fromObject(data)
,SOBang.class);
result.setEntity(entity);
}
return result;
}
}
Elasticsearch教程(九) elasticsearch 查询数据 | 分页查询
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。