1

我正在尝试使用 solrj 在集合中添加/删除文档。当我在 SolrCloudClient 实例上设置默认集合时,该过程成功完成。但是,当我尝试使用请求发送集合名称而不是设置默认集合时,会引发异常:

org.apache.solr.client.solrj.SolrServerException: No collection param specified on request and no default collection has been set.

我已确保集合名称确实是从我的代码中发送的。

这是代码:

public void createDocument(Document document, String collectionName) throws BusinessException 
{
    SolrInputDocument solrInputDocument = new SolrInputDocument();
    //cloudSolrClient.setDefaultCollection(collectionName);

    Map<String,Object> fieldsData = document.getData();
    for( String fieldName : fieldsData.keySet() )
    {
        Object fieldValue = fieldsData.get(fieldName);
        solrInputDocument.addField(fieldName, fieldValue);
    }

    try 
    {
        System.out.println(collectionName);
        UpdateResponse documentAdditionResponse = cloudSolrClient.add(collectionName, solrInputDocument);
        if(documentAdditionResponse.getStatus() != 0)
        {
            throw new BusinessException(StaticResponseCode.SOLR_SERVER_ERROR);
        }   
        cloudSolrClient.commit();
    }
    catch(SolrException e)
    {
        throw new BusinessException(StaticResponseCode.NON_EXISTENT_COLLECTION);
    }
    catch (SolrServerException | IOException e) 
    {
        e.printStackTrace();
        throw new BusinessException(StaticResponseCode.SOLR_SERVER_ERROR);
    } 
}

我在这里想念什么?

4

1 回答 1

0

你必须设置cloudSolrClient.setDefaultCollection(collectionName);

我测试了下面的代码,它仅在我们设置默认集合时才有效。

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.CloudSolrClient;
import org.apache.solr.client.solrj.response.UpdateResponse;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.SolrInputDocument;
import org.apache.zookeeper.KeeperException;

public class Test {
    private static final String collectionName = "gettingstarted";

    public static void main(String[] args)
            throws Exception {
        String solrZkHostPort = "localhost:2195";
        CloudSolrClient cloudSolrClient;
        List<String> zk_Hosts = Arrays.asList(solrZkHostPort.split(","));
        cloudSolrClient = new CloudSolrClient.Builder(zk_Hosts, Optional.empty()).build();
        SolrInputDocument solrInputDocument = new SolrInputDocument();
        cloudSolrClient.setDefaultCollection(collectionName);
        solrInputDocument.addField("id", "value");
        System.out.println(collectionName);
        UpdateResponse documentAdditionResponse = cloudSolrClient.add(collectionName, solrInputDocument);
        if (documentAdditionResponse.getStatus() != 0) {
            System.out.println(documentAdditionResponse.getStatus());
        }
        cloudSolrClient.commit();

    }
}

请参考以下链接。

JIRA 链接

于 2019-09-10T12:30:10.513 回答