【问题标题】:Solr & SolrNET connection not workingSolr & SolrNET 连接不工作
【发布时间】:2014-06-24 21:50:21
【问题描述】:

我正在尝试学习如何使用 Solr 和用于 C# 的 SolrNET 客户端适配器。

我无法超越在Basics 找到的第一个示例。

我在这里做错了吗?我可以导航到我在代码中使用的 url,它已经启动并正在运行。该服务是使用来自Installer的 Windows 安装程序设置的

我的 Solr 实例:

我的对象:

public class Product
{
    [SolrUniqueKey("id")]
    public string Id { get; set; }

    [SolrField("manu_exact")]
    public string Manufacturer { get; set; }

    [SolrField("cat")]
    public ICollection<string> Categories { get; set; }

    [SolrField("price")]
    public decimal Price { get; set; }

    [SolrField("inStock")]
    public bool InStock { get; set; }

    #region Overrides of Object

    /// <summary>
    /// Returns a string that represents the current object.
    /// </summary>
    /// <returns>
    /// A string that represents the current object.
    /// </returns>
    public override string ToString()
    {
        var sb = new StringBuilder("Product\n");
        sb.AppendLine(string.Format("Id - {0}", Id));
        sb.AppendLine(string.Format("Manufacturer - {0}", Manufacturer));
        sb.AppendLine(string.Format("Categories - {0}", Categories));
        sb.AppendLine(string.Format("Price - {0}", Price));
        sb.AppendLine(string.Format("In Stock - {0}", InStock));

        return sb.ToString();
    }

    #endregion

我的连接尝试。我只是在一个单元测试项目中进行练习。

[TestClass]
public class BasicExample
{
    private const string SOLR_CONNECTION = "http://localhost:9001/solr";

    [TestInitialize]
    public void TestInitialize()
    {
        Startup.Init<Product>(SOLR_CONNECTION);
    }

    [TestMethod]
    public void Add()
    {
        var product = new Product
        {
            Id = "SomeId",
            Manufacturer = "Samdung of poop",
            InStock = true,
            Price = 92,
            Categories = new[]{ "electronics", "hard drive"}
        };

        var solr = ServiceLocator.Current.GetInstance<ISolrOperations<Product>>();
        solr.Add(product);
        solr.Commit();
    }

    [TestMethod]
    public void Query()
    {
        var solr = ServiceLocator.Current.GetInstance<ISolrOperations<Product>>();
        var result = solr.Query(new SolrQueryByField("id", "SomeId"));
        result.Should().NotBeNull();
        result.Count.ShouldBeEquivalentTo(1);
        Console.WriteLine(result.First().ToString());
    }
}

我从 Add 方法得到的错误:

测试方法 Solr.Playground.BasicExample.Add 抛出异常: SolrNet.Exceptions.SolrConnectionException:远程服务器返回错误:(400)错误请求。 ---> System.Net.WebException:远程服务器返回错误:(400)错误请求。

查询产生的错误(我知道这会失败,因为添加没有发生,但消息并未表明这是问题):

初始化方法 Solr.Playground.BasicExample.TestInitialize 抛出异常。 System.ApplicationException: System.ApplicationException: Key 'SolrNet.Impl.SolrConnection.Solr.Playground.Objects.Product.SolrNet.Impl.SolrConnection' 已在容器中注册。

编辑

我发现我的 NuGet 包没有安装最新版本。我拉了更新,现在我的错误不同了。

当代码调用.Commit() 方法时,我收到以下错误消息:

<?xml version="1.0" encoding="UTF-8"?>
<response>
<lst name="responseHeader"><int name="status">400</int><int name="QTime">0</int></lst><lst name="error"><str name="msg">Unknown commit parameter 'waitFlush'</str><int name="code">400</int></lst>
</response>
 ---> System.Net.WebException: The remote server returned an error: (400) Bad Request.

【问题讨论】:

标签: c# solr


【解决方案1】:

您应该在 URL 中指定要连接到您的核心。该安装中打包的默认内核的名称是“collection1”,因此您的网址将是:http://localhost:9001/solr/collection1

此外,您还需要更改初始化连接的方式。 TestInitialize 会将该方法设置为在每次测试之前调用一次,但如果您多次调用Startup.Init(),您将得到一个异常。您可以改用 ClassInitialize 属性,该属性只会为该类中的测试运行一次。尝试将您的测试类更改为如下所示:

private const string SOLR_CONNECTION = "http://localhost:9001/solr/collection1";

[ClassInitialize]
public static void TestInitialize(TestContext context)
{
    Startup.Init<Product>(SOLR_CONNECTION);
}

希望这样做!干杯。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多