c# - 您可以将更新 URL 上的软提交传递给 SOLR 吗?

标签 c# solr solr4

我想通过软提交向 solr 发送更新。

像这样:

http://xxx:8983/solr/corename/update?softcommit=true

我在 C# 中工作,所以代码如下所示:

public void PostToSolr( string solrXML, SolrCommitType commitType )
    {
        string uri = solrConfig.UpdateURL;  //something like  "http://xxxx:8983/solr/corename/update";

        switch( commitType )
        {
            case SolrCommitType.SOFT:
                uri = uri + "?softcommit=true";
                break;

            case SolrCommitType.HARD:
                uri = uri + "?commit=true";
                break;
        }

        HttpWebResponse response = null;
        WebRequest request = WebRequest.Create( uri );

        try
        {

            request.ContentType = "application/xml";
            request.Method = "POST";
            using( var rs = request.GetRequestStream( ) )
            {
                byte[] byteArray = Encoding.UTF8.GetBytes( solrXML );
                rs.Write( byteArray, 0, byteArray.Length );
            }

            // get response
            response = request.GetResponse( ) as HttpWebResponse;

            HttpStatusCode statusCode = response.StatusCode;

            if( statusCode != HttpStatusCode.OK )
            {
                throw new Exception( String.Format( "HttpStatusCode={0}.", statusCode.ToString( ) ) );
            }
        }
        catch( Exception ex )
        {
            throw new Exception( String.Format( "Uri={0}. Post Data={1}", uri, solrXML ), ex );
        }
        finally
        {
            if( null != response )
            {
                response.Close( );
                response = null;
            }

            request = null;
        }
    }

当我通过软提交发布到 SOLR 时,更新的文档不会立即可见。在 solr 配置中,我将 autosoftcommit 设置为每分钟发生一次,因此最终更新的文档确实可见。

如何通过更新发送新文档并使其立即可见而不进行提交并重新打开搜索器?有没有办法强制软提交?还是仅根据配置文件中设置的策略进行软提交?

最佳答案

您需要做的就是使用参数 softCommit ( Camel 案例),这将解决问题。示例请求是:

curl http://localhost:8983/solr/collection1/update?softCommit=true -H "Content-Type: text/xml" --data-binary '<add><doc><field name="id">testdoc2</field></doc></add>'

它可以在添加文档后使用,只是为了提交您尝试时尚未提交的内容,但使用驼峰式 softCommit bool 参数。

来自 documentation :

softCommit = "true" | "false" - default is false - perform a soft commit - this will refresh the 'view' of the index in a more performant manner, but without "on-disk" guarantees. Solr (!) 4.0

关于c# - 您可以将更新 URL 上的软提交传递给 SOLR 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20667240/

相关文章:

c# - 停止双重回发

php - 为什么 PHP Solr 扩展给出异常 "Unsuccessful query request"

c# - 在 C# 中为对象指定动态属性

solr4 - 查找文件无法索引的原因?

java - 如何在 solr 4.8 中获取字段的总和

c# - 如何向表达式添加另一个条件?

c# - 在 C++/CLI 中将新值分配给不可变值结构

c# - 不是 nHibernate 标准中的关联 (Asp.Net MVC4)

Solr - 集合 API 超时

solr - 在 Solr 4 中配置健康检查