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.handler.component;
import java.io.IOException;
import java.lang.invoke.MethodHandles;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
import org.apache.lucene.search.BooleanQuery;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.params.CommonParams;
import org.apache.solr.common.params.ModifiableSolrParams;
import org.apache.solr.common.params.MoreLikeThisParams;
import org.apache.solr.common.params.ShardParams;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.common.util.SimpleOrderedMap;
import org.apache.solr.handler.MoreLikeThisHandler;
import org.apache.solr.schema.IndexSchema;
import org.apache.solr.search.DocIterator;
import org.apache.solr.search.DocList;
import org.apache.solr.search.DocListAndSet;
import org.apache.solr.search.ReturnFields;
import org.apache.solr.search.SolrIndexSearcher;
import org.apache.solr.search.SolrReturnFields;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static org.apache.solr.common.params.CommonParams.SORT;
/**
* TODO!
*
*
* @since solr 1.3
*/
public class MoreLikeThisComponent extends SearchComponent {
public static final String COMPONENT_NAME = "mlt";
public static final String DIST_DOC_ID = "mlt.dist.id";
private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
@Override
public void prepare(ResponseBuilder rb) throws IOException {
if (rb.req.getParams().getBool(MoreLikeThisParams.MLT, false)) {
rb.setNeedDocList(true);
}
}
@Override
public void process(ResponseBuilder rb) throws IOException {
SolrParams params = rb.req.getParams();
if (params.getBool(MoreLikeThisParams.MLT, false)) {
ReturnFields returnFields = new SolrReturnFields( rb.req );
int flags = 0;
if (returnFields.wantsScore()) {
flags |= SolrIndexSearcher.GET_SCORES;
}
rb.setFieldFlags(flags);
log.debug("Starting MoreLikeThis.Process. isShard: "
+ params.getBool(ShardParams.IS_SHARD));
SolrIndexSearcher searcher = rb.req.getSearcher();
if (params.getBool(ShardParams.IS_SHARD, false)) {
if (params.get(MoreLikeThisComponent.DIST_DOC_ID) == null) {
if (rb.getResults().docList.size() == 0) {
// return empty response
rb.rsp.add("moreLikeThis", new NamedList());
return;
}
MoreLikeThisHandler.MoreLikeThisHelper mlt = new MoreLikeThisHandler.MoreLikeThisHelper(
params, searcher);
NamedList bQuery = mlt.getMoreLikeTheseQuery(rb
.getResults().docList);
NamedList temp = new NamedList<>();
Iterator> idToQueryIt = bQuery.iterator();
while (idToQueryIt.hasNext()) {
Entry idToQuery = idToQueryIt.next();
String s = idToQuery.getValue().toString();
log.debug("MLT Query:" + s);
temp.add(idToQuery.getKey(), idToQuery.getValue().toString());
}
rb.rsp.add("moreLikeThis", temp);
} else {
NamedList sim = getMoreLikeThese(rb, rb.req.getSearcher(),
rb.getResults().docList, flags);
rb.rsp.add("moreLikeThis", sim);
}
} else {
// non distrib case
NamedList sim = getMoreLikeThese(rb, rb.req.getSearcher(), rb.getResults().docList,
flags);
rb.rsp.add("moreLikeThis", sim);
}
}
}
@Override
public void handleResponses(ResponseBuilder rb, ShardRequest sreq) {
if ((sreq.purpose & ShardRequest.PURPOSE_GET_TOP_IDS) != 0
&& rb.req.getParams().getBool(COMPONENT_NAME, false)) {
log.debug("ShardRequest.response.size: " + sreq.responses.size());
for (ShardResponse r : sreq.responses) {
if (r.getException() != null) {
// This should only happen in case of using shards.tolerant=true. Omit this ShardResponse
continue;
}
NamedList moreLikeThisReponse = (NamedList) r.getSolrResponse()
.getResponse().get("moreLikeThis");
log.debug("ShardRequest.response.shard: " + r.getShard());
if (moreLikeThisReponse != null) {
for (Entry entry : moreLikeThisReponse) {
log.debug("id: \"" + entry.getKey() + "\" Query: \""
+ entry.getValue() + "\"");
ShardRequest s = buildShardQuery(rb, (String) entry.getValue(),
entry.getKey());
rb.addRequest(this, s);
}
}
}
}
if ((sreq.purpose & ShardRequest.PURPOSE_GET_MLT_RESULTS) != 0) {
for (ShardResponse r : sreq.responses) {
log.debug("MLT Query returned: "
+ r.getSolrResponse().getResponse().toString());
}
}
}
@Override
public void finishStage(ResponseBuilder rb) {
// Handling Responses in finishStage, because solrResponse will put
// moreLikeThis xml
// segment ahead of result/response.
if (rb.stage == ResponseBuilder.STAGE_GET_FIELDS
&& rb.req.getParams().getBool(COMPONENT_NAME, false)) {
Map