When querying Solr using SolrNet, what is the easiest way to see the actual Url that gets requested?

StackOverflow https://stackoverflow.com/questions/21579935

  •  07-10-2022
  •  | 
  •  

Question

I'm trying to debug a application that is using SolrNet to query Solr. I'm trying to figure out what url that actually gets requested from SolrNet so that I can debug it more easilly in a web browser.

Was it helpful?

Solution 2

Just to save you the effort of heading out to the forum :

var serializer = ServiceLocator.Current.GetInstance<ISolrQuerySerializer>(); 
queries = new List<ISolrQuery>
{
new SolrQueryByField("category_facet", "Fjärrin"),
new SolrQueryByField("branch_facet", sigel),
new SolrQueryByField("isArchived", "false"),
SolrQuery.All
};
var q = new SolrMultipleCriteriaQuery(queries,"AND");
var queryRaw = serializer.Serialize(q);
Debug.WriteLine(queryRaw);

OTHER TIPS

There is a question on the SolrNet Google Group - Get Raw Solr Query that provides a couple of ways to get this output.

Wrote both things (query only, or all parameters) into a LinqPad script for easier, complete reference:

void Main()
{
    //Reset from scratch since doesn't play great with linqpad
    Startup.InitContainer();
    //Setup the container contents
    Startup.Init<FakeModel>("http://localhost:8983/solr");


    var queries = new List<ISolrQuery>
        {
        new SolrQueryByField("category_facet", "Fjärrin"),
        new SolrQueryByField("branch_facet", "sigel"),
        new SolrQueryByField("isArchived", "false"),
        SolrQuery.All
        };
    var q = new SolrMultipleCriteriaQuery(queries, "AND");


    var opts = new QueryOptions
    {
        Start = 20,
        Rows = 15,
        OrderBy = new[] { new SolrNet.SortOrder("myFakeField") }
    };


    DumpQuery(q);
    DumpAllParameters(q, opts);
}

private void DumpQuery(ISolrQuery q)
{
    var serializer = ServiceLocator.Current.GetInstance<ISolrQuerySerializer>();

    var queryRaw = serializer.Serialize(q);
    //Dump is a LinqpadMethod, running elsewhere this needs to be modified
    queryRaw.Dump("Query only");
}
private void DumpAllParameters(ISolrQuery q, QueryOptions opts)
{
    var queryExecuterInterface = ServiceLocator.Current.GetInstance<ISolrQueryExecuter<FakeModel>>();
    var queryExecuter = queryExecuterInterface as SolrQueryExecuter<FakeModel>;
    queryExecuter.GetAllParameters(q, opts).Dump("All Parameters");
}


public class FakeModel
{
}

If you're not using .Net Core nor dependency injection this is how I did it.

using SolrNet.Impl.FieldSerializers;
using SolrNet.Impl.QuerySerializers;

public void function()
{
    var serializer = new DefaultQuerySerializer(new DefaultFieldSerializer());
    string rawQuery = serializer.Serialize(abstractSolrQuery);
    // do something
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top