简体   繁体   中英

SolrNET - Data at the root level is invalid. Line 1, position 1

I am using Solar : 7.0.1 on: localhost:8983/solr/global

I am using SolrNET 0.8.1 with the following code example:

using SolrNet;
using Microsoft.Practices.ServiceLocation;

Startup.Init<SOLRModel>("http://localhost:8983/solr/global");
var solr = ServiceLocator.Current.GetInstance<ISolrOperations<SOLRModel>>();
var results = solr.Query(new SolrQuery("*:*&wt=xml")); // Throws Error Here.

I am getting an error:

Data at the root level is invalid. Line 1, position 1.

I am using default schema, have also tried the techproducts example, get the same error.

I can navigate to: http://localhost:8983/solr/global/select?q= : and I get the normal JSON Response.

Using the &wt=xml gives me well formatted XML Response:

<?xml version="1.0" encoding="UTF-8" ?> 
- <response>
    - <lst name="responseHeader">
           <int name="status">0</int> 
          <int name="QTime">0</int> 
    - <lst name="params">
        <str name="q">*:*</str> 
        <str name="wt">xml</str> 
    </lst>
    </lst>
<result name="response" numFound="0" start="0" /> 
</response>

Please can someone tell me where I might look to solve this error.

The SolrNet doc page says this:

Whatever you give it is passed straight to Solr's q parameter

The &wt=xml should not be passed to the q parameter, SolrNet needs to treat this as a separate parameter entirely. This goes for any additional parameters you want to set such as start, sort, etc.

The correct way is to use the overload solr.Query(ISolrQuery query, QueryOptions options) and pass these parameters through the ExtraParams property.

A complete example:

using SolrNet;
using Microsoft.Practices.ServiceLocation;

Startup.Init<SOLRModel>("http://localhost:8983/solr/global");
var solr = ServiceLocator.Current.GetInstance<ISolrOperations<SOLRModel>>();

var options = new QueryOptions();

options.ExtraParams = new KeyValuePair<string,string>[] {
    new KeyValuePair<string,string>("wt", "xml")
};

var results = solr.Query(new SolrQuery("*:*"), options);

I figured out that by default solrnet returns json. To fix the issue I passed in extraparams for queryoptions on the query for setting 'wt' to xml. See the documentation for setting extra params.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM