Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.solr.search.stats;
import java.io.IOException;
import java.lang.invoke.MethodHandles;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.CollectionStatistics;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreMode;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TermStatistics;
import org.apache.solr.client.solrj.SolrResponse;
import org.apache.solr.cloud.CloudDescriptor;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.SolrException.ErrorCode;
import org.apache.solr.common.params.ModifiableSolrParams;
import org.apache.solr.common.params.ShardParams;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.handler.component.ResponseBuilder;
import org.apache.solr.handler.component.ShardRequest;
import org.apache.solr.handler.component.ShardResponse;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.search.SolrIndexSearcher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* This class implements exact caching of statistics. It requires an additional round-trip to parse
* query at shard servers, and return term statistics for query terms (and collection statistics for
* term fields).
*
*
Global statistics are cached in the current request's context and discarded once the
* processing of the current request is complete. There's no support for longer-term caching, and
* each request needs to build the global statistics from scratch, even for repeating queries.
*/
public class ExactStatsCache extends StatsCache {
private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
private static final String CURRENT_GLOBAL_COL_STATS = "solr.stats.globalCol";
private static final String CURRENT_GLOBAL_TERM_STATS = "solr.stats.globalTerm";
private static final String PER_SHARD_TERM_STATS = "solr.stats.shardTerm";
private static final String PER_SHARD_COL_STATS = "solr.stats.shardCol";
@Override
protected StatsSource doGet(SolrQueryRequest req) {
@SuppressWarnings({"unchecked"})
Map currentGlobalColStats =
(Map)
req.getContext().getOrDefault(CURRENT_GLOBAL_COL_STATS, Collections.emptyMap());
@SuppressWarnings({"unchecked"})
Map currentGlobalTermStats =
(Map)
req.getContext().getOrDefault(CURRENT_GLOBAL_TERM_STATS, Collections.emptyMap());
if (log.isDebugEnabled()) {
log.debug(
"Returning StatsSource. Collection stats={}, Term stats size= {}",
currentGlobalColStats,
currentGlobalTermStats.size());
}
return new ExactStatsSource(statsCacheMetrics, currentGlobalTermStats, currentGlobalColStats);
}
@Override
protected ShardRequest doRetrieveStatsRequest(ResponseBuilder rb) {
// always request shard statistics
ShardRequest sreq = new ShardRequest();
sreq.purpose = ShardRequest.PURPOSE_GET_TERM_STATS;
sreq.params = new ModifiableSolrParams(rb.req.getParams());
// don't pass through any shards param
sreq.params.remove(ShardParams.SHARDS);
return sreq;
}
@Override
protected void doMergeToGlobalStats(SolrQueryRequest req, List responses) {
Set allTerms = new HashSet<>();
for (ShardResponse r : responses) {
if ("true".equalsIgnoreCase(req.getParams().get(ShardParams.SHARDS_TOLERANT))
&& r.getException() != null) {
// Can't expect stats if there was an exception for this request on any shard
// this should only happen when using shards.tolerant=true
log.debug("Exception shard response={}", r);
continue;
}
if (log.isDebugEnabled()) {
log.debug(
"Merging to global stats, shard={}, response={}",
r.getShard(),
r.getSolrResponse().getResponse());
}
// response's "shard" is really a shardURL, or even a list of URLs
String shard = r.getShard();
SolrResponse res = r.getSolrResponse();
if (res.getException() != null) {
log.debug("Exception response={}", res);
continue;
}
if (res.getResponse().get(ShardParams.SHARD_NAME) != null) {
shard = (String) res.getResponse().get(ShardParams.SHARD_NAME);
}
NamedList