use of com.qlangtech.tis.manage.servlet.QueryCloudSolrClient in project tis by qlangtech.
the class CollectionAction method doQuery.
/**
* 利用solr的disMax QP进行查询
*
* @param context
* @throws Exception
*/
public void doQuery(Context context) throws Exception {
// this.parseJsonPost();
JSONObject post = getIndexWithPost();
// if (StringUtils.isEmpty(post.getString(KEY_INDEX_NAME))) {
// throw new IllegalArgumentException("indexName can not be null");
// }
// this.indexName = TISCollectionUtils.NAME_PREFIX + post.getString(KEY_INDEX_NAME);
JSONArray searchFields = post.getJSONArray(KEY_QUERY_SEARCH_FIELDS);
Objects.requireNonNull(searchFields, "param " + KEY_QUERY_SEARCH_FIELDS + " can not be null ");
if (searchFields.size() < 1) {
throw new IllegalArgumentException(KEY_QUERY_SEARCH_FIELDS + " relevant field can not be empty");
}
final List<SubCriteria> andQueryCriteria = Lists.newArrayList();
searchFields.forEach((f) -> {
SubCriteria subCriteria = new SubCriteria();
JSONObject o = (JSONObject) f;
Option opt = null;
String word = null;
for (String key : o.keySet()) {
word = o.getString(key);
if (StringUtils.isEmpty(word)) {
throw new IllegalArgumentException("query field:" + key + ",relevant:" + word + ",val can not be null");
}
opt = new Option(key, word);
subCriteria.addOr(opt);
}
andQueryCriteria.add(subCriteria);
});
JSONArray storedFields = post.getJSONArray(KEY_QUERY_FIELDS);
if (storedFields == null) {
throw new IllegalArgumentException("param 'fields' can not be null");
}
storedFields.stream().map((r) -> (String) r).collect(Collectors.toList());
// final String fields = post.getString(KEY_QUERY_FIELDS);
// if (StringUtils.isEmpty(fields)) {
// throw new IllegalArgumentException("param 'fields' can not be null");
// }
final Integer limit = post.getInteger(KEY_QUERY_LIMIT);
if (limit == null) {
throw new IllegalArgumentException("param limit can not be null");
}
// final String queryFields = post.getString(KEY_QUERY_QUERY_FIELDS);
// if (StringUtils.isEmpty(queryFields)) {
// throw new IllegalArgumentException("'queryFields' can not be null");
// }
final String orderBy = post.getString(KEY_QUERY_ORDER_BY);
Integer rowsOffset = post.getInteger(KEY_QUERY_ROWS_OFFSET);
AppDomainInfo app = getAppDomain();
final QueryResutStrategy queryResutStrategy = QueryIndexServlet.createQueryResutStrategy(app, this.getRequest(), getResponse(), getDaoContext());
final List<ServerJoinGroup> serverlist = queryResutStrategy.queryProcess();
for (ServerJoinGroup server : serverlist) {
// 组装url
final String url = server.getIpAddress();
QueryCloudSolrClient solrClient = new QueryCloudSolrClient(url);
SolrQuery query = new SolrQuery();
query.set(CommonParams.FL, storedFields.stream().map((r) -> (String) r).collect(Collectors.joining(",")));
// query.setParam(QUERY_PARSIING_DEF_TYPE, "dismax");
// query.setParam(DisMaxParams.QF, queryFields);
query.setQuery(this.createQuery(andQueryCriteria));
query.setRows(limit);
if (rowsOffset != null) {
query.setStart(rowsOffset);
}
if (StringUtils.isNotEmpty(orderBy)) {
query.add(CommonParams.SORT, orderBy);
}
QueryResponse result = solrClient.query(indexName.getCollectionName(), query, SolrRequest.METHOD.POST);
solrClient.close();
Map<String, Object> biz = Maps.newHashMap();
long c = result.getResults().getNumFound();
biz.put(RESULT_KEY_ROWS_COUNT, c);
List<Map<String, Object>> resultList = Lists.newArrayList();
Map<String, Object> row = null;
for (SolrDocument doc : result.getResults()) {
row = Maps.newHashMap();
for (Map.Entry<String, Object> f : doc.entrySet()) {
row.put(f.getKey(), f.getValue());
}
resultList.add(row);
}
biz.put(RESULT_KEY_ROWS, resultList);
this.setBizResult(context, biz);
return;
}
}
use of com.qlangtech.tis.manage.servlet.QueryCloudSolrClient in project tis by qlangtech.
the class CoreAction method getAllRowsCount.
public static long getAllRowsCount(String collectionName, String coreURL) throws SolrServerException, IOException {
QueryCloudSolrClient solrClient = new QueryCloudSolrClient(coreURL);
SolrQuery query = new SolrQuery();
query.setRows(0);
query.setQuery("*:*");
return solrClient.query(collectionName, query).getResults().getNumFound();
}
Aggregations