3

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.

Rusty Nail
  • 2,692
  • 3
  • 34
  • 55
  • Are you sure SolrNET supports the XML output? Sounds like it might expect the JSON output? – MatsLindh Oct 19 '17 at 08:05
  • Yes, XDocument, XElement and so on. See: https://github.com/SolrNet/SolrNet/blob/master/SolrNet/Impl/ISolrResponseParser.cs as an example. Weather I use '&wt=xml' or not, the error is present. – Rusty Nail Oct 19 '17 at 08:21
  • 1
    Do you get anything useful in the Solr log? And you probably meant `*:*` as the query, not `*.*`. I'm not sure you should include `global` in your URL - according to the SolrNet docs it seems that is provided by the model / the model name? That would give a 404, which would result in a parse error when attempting to parse the returned 404 HTML as XML. – MatsLindh Oct 19 '17 at 08:25
  • global is the core name, random name, I had techproducts before, gives the same error. without the core name I get 404 not found. Logs are logging errors as I make them, but nothing against the query, only if I make a mistake. – Rusty Nail Oct 19 '17 at 08:40
  • I've been having the same issue. The logs show that it is having the following problem: Error adding field 'end'='2017-08-05' – John Ten Cate Oct 20 '17 at 13:33
  • @John Ten Cate - Thanks for sharing. I do not get an error. My logs are error free for this particular issue, so I think my XML is being returned successfully. SolrNET is creating an XML byte-order mark (BOM) Error `?` where there should not be a preceding `?` http://www.w3.org/International/questions/qa-byte-order-mark - The solution for me was to Write my Own SolrNET. – Rusty Nail Oct 20 '17 at 22:27
  • To add, this problem appears to be created because the way the data is handled. Encoding: UTF-8 without signature. Simply, the difference between XmlDocument.LoadXml("Path") and XmlDocument.Load("Path") can change this situation. – Rusty Nail Oct 20 '17 at 22:39
  • Best links I have found: https://stackoverflow.com/questions/291455/xml-data-at-root-level-is-invalid and https://stackoverflow.com/questions/17795167/xml-loaddata-data-at-the-root-level-is-invalid-line-1-position-1/32713239#32713239 - Special thanks to those posters, they are vary helpful! – Rusty Nail Oct 20 '17 at 22:41

2 Answers2

5

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);
ryan
  • 1,451
  • 11
  • 27
2

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.

John Ten Cate
  • 475
  • 5
  • 16