将文档从AngularJS发布到SOLR 4

jbd*_*ask 5 rest solr http angularjs


我对此深陷兔子洞.我正在创建一个简单的应用程序,它使用SOLR 4作为NoSQL数据存储区,使用AngularJS v1.2.2作为接口.我从命令行加载了一堆文件,AngularJS使搜索/查看这些文件变得非常容易.我想允许文档编辑,但无法使POST工作.Chrome控制台显示400个错误,"网络"标签显示OPTIONS方法失败.

网络标题:
Request URL:http://localhost:8983/solr/mrm_assay/update Request Method:OPTIONS Status Code:400 Bad Request Request Headers Accept:*/* Accept-Encoding:gzip,deflate,sdch Accept-Language:en-US,en;q=0.8,es;q=0.6 Access-Control-Request-Headers:accept, content-type Access-Control-Request-Method:POST Cache-Control:no-cache Connection:keep-alive Host:localhost:8983 Origin:http://localhost:63342 Pragma:no-cache Referer:http://localhost:63342/angular-solr2/app/index.html User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36 Response Headers Access-Control-Allow-Credentials:true Access-Control-Allow-Headers:origin, content-type, cache-control, accept, options Access-Control-Allow-Methods:GET,POST,DELETE,PUT,HEAD,OPTIONS Access-Control-Allow-Origin:http://localhost:63342 Access-Control-Max-Age:1800 Content-Type:application/xml; charset=UTF-8 Transfer-Encoding:chunked

架构快速概述:
SOLR和AngularJS应用程序都在我的Mac上运行.SOLR使用默认的Jetty实例,Angular应用程序在WebStorm IDE的服务器内运行.CORS已启用,包括GET,POST,DELETE,PUT,HEAD,OPTIONS.

更新工作时:

  • 使用SOLR的仪表板
  • 使用命令行(示例)
    $ curl http://localhost:8983/solr/mrm_assay/update -H 'Content-type:application/json' -d '[ { "keep_in_assay" {"set" : "N", "detected_endog": "N", "problems_w_is": "removed b/c requires addition post-Oasis", "onc_std_set": "set1", "fraction_id": "7", "onclistgene": "UBL3", "geneid": "UBL3_IS6", "taxonid": "9606", "peptide": "SSNVPADMINLR", "degen_human": "1", "gene_degeneracy": "1", "percnt_id": "66.7", "uuid": "6d20eb03-d3ee-4eb2-bc16-27cfaabab989" } ]'

我的Angular控制器代码如下所示:
assayControllers.controller('AssayUpdateController', ['$scope', '$http', function($scope, $http){ $scope.processForm = function(){ console.log($scope.assay); $http({ method:'POST', url:'http://localhost:8983/solr/mrm_assay/update', data : $scope.assay, headers : {'Content-Type': 'application/json' } }) .success(function(data, status, headers){ console.log(data.message); }) .error(function(data, status, headers, config){ console.log(status); }); }; } ]);

数据从表单中成功发送,因为我可以在控制台上看到它(虽然JSON对象没有打包在一个数组中,SOLR似乎期望...我还尝试将JSON格式的数据推送到数组并POST但没有运气)

我感谢您的帮助 - 即使只是为了指导我的故障排除!

jbd*_*ask 6

关于如何使用AngularJS v1.2.2将单个文档更新为SOLR v4.7的答案是多部分的.这只在我的localhost上测试过!

I.在SOLR
solr-4.7.0/example/solr-webapp/webapp/WEB-INF/web.xml 附带的Jetty服务器上配置CORS

注意: CrossOriginFilter有一个参数chainPreflight,默认情况下设置为true.这需要设置为false.这是CORS转发POST而不是SOLTIONS的OPTIONS的关键..此外,订单很重要!

<filter>
    <filter-name>cross-origin</filter-name>
    <filter-class>org.eclipse.jetty.servlets.CrossOriginFilter</filter-class>
    <init-param>
         <param-name>allowedOrigins</param-name>
         <param-value>http://localhost*</param-value>
    </init-param>
     <init-param>
         <param-name>allowedMethods</param-name>
         <param-value>GET,POST,DELETE,PUT,HEAD,OPTIONS</param-value>
     </init-param>
     <init-param>
         <param-name>allowedHeaders</param-name>
         <param-value>origin, content-type, cache-control, accept, options, authorization, x-requested-with</param-value>
     </init-param>
    <init-param>
        <param-name>supportsCredentials</param-name>
        <param-value>true</param-value>
    </init-param>
    <init-param>
      <param-name>chainPreflight</param-name>
      <param-value>false</param-value>
    </init-param>
</filter>

<filter-mapping>
  <filter-name>cross-origin</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>
Run Code Online (Sandbox Code Playgroud)

II.SOLR期望以数组格式的有效载荷,因此您需要将Angular对象推入一个.基本上,$ scope.data变为[$ scope.data]

assayControllers.controller('AssayUpdateController', ['$scope', '$http', '$location',
    function($scope,  $http, $location){
        $scope.processForm = function(){
            $http.post("http://localhost:8983/solr/mrm_assay/update?commit=true", [$scope.assay])
                .success(function(data, status, headers){
                    console.log(data.message);
                    $location.path("/assays")
                })
                .error(function(data, status, headers, config){
                    console.log(status);
                });
        };
    }
]);
Run Code Online (Sandbox Code Playgroud)

III.SOLR文档包含可能导致POST出现"冲突"错误的版本字段.这是由于我无法追踪的缓存问题(卷曲显示当前值但浏览器有旧版;甚至强制刷新).无论如何,这对SOLR来说比我更有用,所以最好的办法是不要首先将它归还给客户.我不认为字段排除是一个选项,因此我将所有要在solrconfig.xml中返回的字段列入白名单

 <!-- A request handler that returns indented JSON by default -->
  <requestHandler name="/query" class="solr.SearchHandler">
     <lst name="defaults">
       <str name="echoParams">explicit</str>
       <str name="wt">json</str>
       <str name="indent">true</str>
       <str name="df">text</str>
      <str name="fl">uuid,keep_in_assay,detected_endog,problems_w_is,onc_std_set,fraction_id,detected_by_orbi_experiments,onclistgene,geneid,taxonid,peptide,\
degen_human,degen_mouse,gene_degeneracy,department,protein_ac,pepid,protid,percnt_id,peptide_ordered</str>
Run Code Online (Sandbox Code Playgroud)

现在该应用程序就像一个魅力!