|
@ -38,20 +38,20 @@ public class SolrUtil {
|
|
/**
|
|
/**
|
|
* 简单查询方法
|
|
* 简单查询方法
|
|
*/
|
|
*/
|
|
public SolrDocumentList query(String tableName, String q, Map<String, String> sort, long start, long rows) throws Exception {
|
|
|
|
return query(tableName, q, null, sort, start, rows, null);
|
|
|
|
|
|
public SolrDocumentList query(String core, String q, Map<String, String> sort, long start, long rows) throws Exception {
|
|
|
|
return query(core, q, null, sort, start, rows, null);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
* 简单查询返回字段
|
|
* 简单查询返回字段
|
|
*/
|
|
*/
|
|
public SolrDocumentList queryReturnFieldList(String tableName, String q, String fq, Map<String, String> sort, long start, long rows, String... fields) throws Exception {
|
|
|
|
return query(tableName, q, fq, sort, start, rows, fields);
|
|
|
|
|
|
public SolrDocumentList queryReturnFieldList(String core, String q, String fq, Map<String, String> sort, long start, long rows, String... fields) throws Exception {
|
|
|
|
return query(core, q, fq, sort, start, rows, fields);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
* Solr查询方法
|
|
* Solr查询方法
|
|
*
|
|
|
|
|
|
* @param core
|
|
* @param q 查询字符串
|
|
* @param q 查询字符串
|
|
* @param fq 过滤查询
|
|
* @param fq 过滤查询
|
|
* @param sort 排序
|
|
* @param sort 排序
|
|
@ -63,32 +63,89 @@ public class SolrUtil {
|
|
public SolrDocumentList query(String core, String q, String fq, Map<String, String> sort, long start, long rows, String... fields) throws Exception {
|
|
public SolrDocumentList query(String core, String q, String fq, Map<String, String> sort, long start, long rows, String... fields) throws Exception {
|
|
SolrClient conn = pool.getConnection(core);
|
|
SolrClient conn = pool.getConnection(core);
|
|
SolrQuery query = new SolrQuery();
|
|
SolrQuery query = new SolrQuery();
|
|
if (null != q && !q.equals("")) { //设置查询条件
|
|
|
|
|
|
//设置查询条件
|
|
|
|
if (StringUtils.isNotEmpty(q)) {
|
|
query.setQuery(q);
|
|
query.setQuery(q);
|
|
} else {
|
|
} else {
|
|
query.setQuery("*:*");
|
|
query.setQuery("*:*");
|
|
}
|
|
}
|
|
if (null != fq && !fq.equals("")) { //设置过滤条件
|
|
|
|
|
|
//设置过滤条件
|
|
|
|
if (StringUtils.isNotEmpty(fq)) {
|
|
query.setFilterQueries(fq);
|
|
query.setFilterQueries(fq);
|
|
}
|
|
}
|
|
query.setFields(fields);
|
|
query.setFields(fields);
|
|
query.setStart(Integer.parseInt(String.valueOf(start)));//设置查询起始行
|
|
|
|
query.setRows(Integer.parseInt(String.valueOf(rows)));//设置查询行数
|
|
|
|
|
|
//设置查询起始行
|
|
|
|
query.setStart(Integer.parseInt(String.valueOf(start)));
|
|
|
|
//设置查询行数
|
|
|
|
query.setRows(Integer.parseInt(String.valueOf(rows)));
|
|
|
|
|
|
//设置排序
|
|
//设置排序
|
|
if (sort != null) {
|
|
if (sort != null) {
|
|
for (Object co : sort.keySet()) {
|
|
for (Object co : sort.keySet()) {
|
|
if (ASC == sort.get(co).toLowerCase() || ASC.equals(sort.get(co).toLowerCase())) {
|
|
|
|
|
|
if (ASC.equals(sort.get(co).toLowerCase())) {
|
|
query.addSort(co.toString(), SolrQuery.ORDER.asc);
|
|
query.addSort(co.toString(), SolrQuery.ORDER.asc);
|
|
} else {
|
|
} else {
|
|
query.addSort(co.toString(), SolrQuery.ORDER.desc);
|
|
query.addSort(co.toString(), SolrQuery.ORDER.desc);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
QueryResponse rsp = conn.query(query);
|
|
QueryResponse rsp = conn.query(query);
|
|
SolrDocumentList docs = rsp.getResults();
|
|
SolrDocumentList docs = rsp.getResults();
|
|
return docs;
|
|
return docs;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Solr查询方法
|
|
|
|
* @param core
|
|
|
|
* @param q 查询字符串
|
|
|
|
* @param fq 过滤查询
|
|
|
|
* @param sort 排序
|
|
|
|
* @param start 查询起始行
|
|
|
|
* @param rows 查询行数
|
|
|
|
* @param hl 高亮字段
|
|
|
|
* @param fields 返回字段
|
|
|
|
* @return
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public QueryResponse highlight(String core, String q, String fq, Map<String, String> sort, long start, long rows, String hl, String... fields) throws Exception {
|
|
|
|
SolrClient conn = pool.getConnection(core);
|
|
|
|
SolrQuery query = new SolrQuery();
|
|
|
|
//设置查询条件
|
|
|
|
if (StringUtils.isNotEmpty(q)) {
|
|
|
|
query.setQuery(q);
|
|
|
|
} else {
|
|
|
|
query.setQuery("*:*");
|
|
|
|
}
|
|
|
|
//设置过滤条件
|
|
|
|
if (StringUtils.isNotEmpty(fq)) {
|
|
|
|
query.setFilterQueries(fq);
|
|
|
|
}
|
|
|
|
query.setFields(fields);
|
|
|
|
//设置查询起始行
|
|
|
|
query.setStart(Integer.parseInt(String.valueOf(start)));
|
|
|
|
//设置查询行数
|
|
|
|
query.setRows(Integer.parseInt(String.valueOf(rows)));
|
|
|
|
|
|
|
|
//设置排序
|
|
|
|
if (sort != null) {
|
|
|
|
for (Object co : sort.keySet()) {
|
|
|
|
if (ASC.equals(sort.get(co).toLowerCase())) {
|
|
|
|
query.addSort(co.toString(), SolrQuery.ORDER.asc);
|
|
|
|
} else {
|
|
|
|
query.addSort(co.toString(), SolrQuery.ORDER.desc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//高亮
|
|
|
|
if (StringUtils.isNotEmpty(hl)) {
|
|
|
|
query.addHighlightField(hl);
|
|
|
|
query.setHighlightSimplePre("<em>");
|
|
|
|
query.setHighlightSimplePost("</em>");
|
|
|
|
}
|
|
|
|
QueryResponse rsp = conn.query(query);
|
|
|
|
return rsp;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@ -167,14 +224,12 @@ public class SolrUtil {
|
|
SolrClient conn = pool.getConnection(core);
|
|
SolrClient conn = pool.getConnection(core);
|
|
SolrQuery query = new SolrQuery();
|
|
SolrQuery query = new SolrQuery();
|
|
|
|
|
|
List<Group> groups = queryDistinctOneField(core, q, fq, sort, start, rows, fields, groupField, groupSort);
|
|
|
|
|
|
|
|
QueryResponse response = conn.query(query);
|
|
QueryResponse response = conn.query(query);
|
|
GroupResponse groupResponse = response.getGroupResponse();
|
|
GroupResponse groupResponse = response.getGroupResponse();
|
|
if (groupResponse != null) {
|
|
if (groupResponse != null) {
|
|
List<GroupCommand> groupList = groupResponse.getValues();
|
|
List<GroupCommand> groupList = groupResponse.getValues();
|
|
for (GroupCommand groupCommand : groupList) {
|
|
for (GroupCommand groupCommand : groupList) {
|
|
groups = groupCommand.getValues();
|
|
|
|
|
|
List<Group> groups = groupCommand.getValues();
|
|
for (Group group : groups) {
|
|
for (Group group : groups) {
|
|
if (group.getResult().size() > 0) {
|
|
if (group.getResult().size() > 0) {
|
|
solrDocumentList.add(group.getResult().get(0));
|
|
solrDocumentList.add(group.getResult().get(0));
|
|
@ -209,8 +264,10 @@ public class SolrUtil {
|
|
query.setFilterQueries(fq);
|
|
query.setFilterQueries(fq);
|
|
}
|
|
}
|
|
|
|
|
|
query.setStart(Integer.parseInt(String.valueOf(start)));//设置查询起始行
|
|
|
|
query.setRows(Integer.parseInt(String.valueOf(rows)));//设置查询行数
|
|
|
|
|
|
//设置查询起始行
|
|
|
|
query.setStart(Integer.parseInt(String.valueOf(start)));
|
|
|
|
//设置查询行数
|
|
|
|
query.setRows(Integer.parseInt(String.valueOf(rows)));
|
|
|
|
|
|
//设置排序
|
|
//设置排序
|
|
if (sort != null) {
|
|
if (sort != null) {
|
|
@ -241,7 +298,8 @@ public class SolrUtil {
|
|
public long count(String core, String q, String fq) throws Exception {
|
|
public long count(String core, String q, String fq) throws Exception {
|
|
SolrClient conn = pool.getConnection(core);
|
|
SolrClient conn = pool.getConnection(core);
|
|
SolrQuery query = new SolrQuery();
|
|
SolrQuery query = new SolrQuery();
|
|
if (null != q && !q.equals("")) { //设置查询条件
|
|
|
|
|
|
//设置查询条件
|
|
|
|
if (null != q && !q.equals("")) {
|
|
query.setQuery(q);
|
|
query.setQuery(q);
|
|
} else {
|
|
} else {
|
|
query.setQuery("*:*");
|
|
query.setQuery("*:*");
|
|
@ -254,9 +312,6 @@ public class SolrUtil {
|
|
|
|
|
|
QueryResponse rsp = conn.query(query);
|
|
QueryResponse rsp = conn.query(query);
|
|
long count = rsp.getResults().getNumFound();
|
|
long count = rsp.getResults().getNumFound();
|
|
//query.setStart(start);
|
|
|
|
//rsp = conn.query(query);
|
|
|
|
//SolrDocumentList docs = rsp.getResults();
|
|
|
|
return count;
|
|
return count;
|
|
|
|
|
|
}
|
|
}
|
|
@ -282,13 +337,17 @@ public class SolrUtil {
|
|
if (null != fq && !fq.equals("")) {
|
|
if (null != fq && !fq.equals("")) {
|
|
query.setFilterQueries(fq);
|
|
query.setFilterQueries(fq);
|
|
}
|
|
}
|
|
query.setFacet(true);//设置facet=on
|
|
|
|
|
|
//设置facet=on
|
|
|
|
query.setFacet(true);
|
|
query.setRows(0);
|
|
query.setRows(0);
|
|
query.addFacetField(groupField);
|
|
query.addFacetField(groupField);
|
|
query.setFacetLimit(limit);//限制每次返回结果数
|
|
|
|
|
|
//限制每次返回结果数
|
|
|
|
query.setFacetLimit(limit);
|
|
query.set(FacetParams.FACET_OFFSET, start);
|
|
query.set(FacetParams.FACET_OFFSET, start);
|
|
query.setFacetMissing(false);//不统计null的值
|
|
|
|
query.setFacetMinCount(0);// 设置返回的数据中每个分组的数据最小值,比如设置为0,则统计数量最小为0,不然不显示
|
|
|
|
|
|
//不统计null的值
|
|
|
|
query.setFacetMissing(false);
|
|
|
|
// 设置返回的数据中每个分组的数据最小值,比如设置为0,则统计数量最小为0,不然不显示
|
|
|
|
query.setFacetMinCount(0);
|
|
QueryResponse rsp = conn.query(query);
|
|
QueryResponse rsp = conn.query(query);
|
|
List<FacetField.Count> countList = rsp.getFacetField(groupField).getValues();
|
|
List<FacetField.Count> countList = rsp.getFacetField(groupField).getValues();
|
|
Map<String, Long> rmap = new HashMap<>();
|
|
Map<String, Long> rmap = new HashMap<>();
|
|
@ -320,13 +379,17 @@ public class SolrUtil {
|
|
query.setFilterQueries(fq);
|
|
query.setFilterQueries(fq);
|
|
}
|
|
}
|
|
|
|
|
|
query.setFacet(true);//设置facet=on
|
|
|
|
|
|
//设置facet=on
|
|
|
|
query.setFacet(true);
|
|
query.setRows(0);
|
|
query.setRows(0);
|
|
query.addFacetField(groupFields);
|
|
query.addFacetField(groupFields);
|
|
query.setFacetLimit(-1); // 限制每次返回结果数
|
|
|
|
|
|
// 限制每次返回结果数
|
|
|
|
query.setFacetLimit(-1);
|
|
query.set(FacetParams.FACET_OFFSET, 0);
|
|
query.set(FacetParams.FACET_OFFSET, 0);
|
|
query.setFacetMissing(false); // 不统计null的值
|
|
|
|
query.setFacetMinCount(0); // 设置返回的数据中每个分组的数据最小值,比如设置为0,则统计数量最小为0,不然不显示
|
|
|
|
|
|
// 不统计null的值
|
|
|
|
query.setFacetMissing(false);
|
|
|
|
// 设置返回的数据中每个分组的数据最小值,比如设置为0,则统计数量最小为0,不然不显示
|
|
|
|
query.setFacetMinCount(0);
|
|
|
|
|
|
QueryResponse rsp = conn.query(query);
|
|
QueryResponse rsp = conn.query(query);
|
|
return rsp.getFacetFields();
|
|
return rsp.getFacetFields();
|
|
@ -354,14 +417,17 @@ public class SolrUtil {
|
|
if (null != fq && !fq.equals("")) {
|
|
if (null != fq && !fq.equals("")) {
|
|
query.setFilterQueries(fq);
|
|
query.setFilterQueries(fq);
|
|
}
|
|
}
|
|
|
|
|
|
query.setFacet(true);//设置facet=on
|
|
|
|
|
|
//设置facet=on
|
|
|
|
query.setFacet(true);
|
|
query.setRows(0);
|
|
query.setRows(0);
|
|
query.addFacetPivotField(groupFields);
|
|
query.addFacetPivotField(groupFields);
|
|
query.set(FacetParams.FACET_OFFSET, start);
|
|
query.set(FacetParams.FACET_OFFSET, start);
|
|
query.setFacetLimit(limit);//限制每次返回结果数
|
|
|
|
query.setFacetMissing(false);//不统计null的值
|
|
|
|
query.setFacetMinCount(0);// 设置返回的数据中每个分组的数据最小值,比如设置为0,则统计数量最小为0,不然不显示
|
|
|
|
|
|
//限制每次返回结果数
|
|
|
|
query.setFacetLimit(limit);
|
|
|
|
//不统计null的值
|
|
|
|
query.setFacetMissing(false);
|
|
|
|
// 设置返回的数据中每个分组的数据最小值,比如设置为0,则统计数量最小为0,不然不显示
|
|
|
|
query.setFacetMinCount(0);
|
|
|
|
|
|
QueryResponse rsp = conn.query(query);
|
|
QueryResponse rsp = conn.query(query);
|
|
NamedList<List<PivotField>> namedList = rsp.getFacetPivot();
|
|
NamedList<List<PivotField>> namedList = rsp.getFacetPivot();
|