How to get the suggester component working in SolrNet?

前端 未结 5 1133
遥遥无期
遥遥无期 2020-12-30 16:06

I have configured my solrconfig.xml and schema.xml to query for the suggestions.

I am able to get the suggestions from the url

http://localhost:8080/         


        
相关标签:
5条回答
  • 2020-12-30 16:37

    with the new version of SolrNet (.net 4.6 at least), you can change the default handler "/select". Try to call your suggest handler using this way.

    using CommonServiceLocator;
    using SolrNet;
    using SolrNet.Commands.Parameters;
    
    Startup.Init<MwDoc>("http://localhost:8983/solr/mycore");
    var solr = ServiceLocator.Current.GetInstance<ISolrOperations<MyClass>>();
    
    QueryOptions options = new QueryOptions()
                {
                    RequestHandler = new RequestHandlerParameters("/suggest"),
                    // define your other Options here
                };
    solr.Query("keyword to search", options);
    
    0 讨论(0)
  • 2020-12-30 16:45

    Passing the qt parameter does NOT work, at least not in Solr 4.7 even with handleSelect=true in SolrConfig. You can verify by specifying a custom handler that's very dissimilar from the default /select, say make yours use edismax and send debugQuery = true in ExtraParams and catch the results in Fiddler.

    Also if you read the explanation on the handleSelect flag it says "if the request uses "/select" but there is no request handler by that name".

    You don't want to touch or disable the /select handler because Solr uses it itself.

    I ended up using ExtraParams to pass all the values I defined in my custom handler, there weren't that many. Seemed better than just using part of SolrNET and then doing the result parsing.

    0 讨论(0)
  • 2020-12-30 16:50

    I had the exact same requirement but could not find any way to easily handle Suggester results with SolrNet. Unfortunately, SolrNet seems to be built around the default /select request handler and does not currently support any other handler including /suggest for object type mappings (T). It expects all mappings to occur with indexed Solr document results and not suggester results.

    Hence, @Paige Cook's answer did not work for me. T type with mappings is not compatible with a suggester results response. All the standard plumbing code from initializing the request (Startup.Init<T>()) to querying (ISolrQueryResults<T> results = solr.Query()) needs a mapped Solr document type and not a simple array of strings which the suggester provides.

    Therefore, (similar to @dfay) I went with making a web request and parsing out the suggested results from the XML web response. The SolrConnection class was used for this:

    string searchTerm = "ha";
    string solrUrl = "http://localhost:8080/solr/collection1";
    string relativeUrl = "/suggest";
    var parameters = new Dictionary<string, string>
                    {
                        {"q", searchTerm},
                        {"wt", "xml"},
                    };
    
    var solrConnection = new SolrConnection(solrUrl);
    string response = solrConnection.Get(relativeUrl, parameters);
    // then use your favorite XML parser to extract 
    // suggestions from the reponse string
    

    Alternatively, instead of XML, the request can return a JSON response using the wt=json parameter:

    var parameters = new Dictionary<string, string>
                    {
                        {"q", searchTerm},
                        {"wt", "json"}, // change this!
                    };
    // then use your favorite JSON parser
    
    0 讨论(0)
  • 2020-12-30 16:53

    See http://wiki.apache.org/solr/SolrRequestHandler, particularly the section on the old handleSelect=true behavior. If you are running against a newer Solr server, this is most likely your problem. (i.e. setting "qt" has no effect and either the default handler in SolrNet must be changed or the Solr config needs to set handleSelect=true.) Here's how I solved this problem in my case:

    ISolrConnection connection = ServiceLocator.Current.GetInstance<ISolrConnection>();
    List<KeyValuePair<string, string>> termsParams = new List<KeyValuePair<string, string>>();
    termsParams.Add(new KeyValuePair<string, string>("terms.fl", "name"));
    termsParams.Add(new KeyValuePair<string, string>("terms.prefix", mySearchString));
    termsParams.Add(new KeyValuePair<string, string>("terms.sort", "count"));
    string xml = connection.Get("/terms", termsParams);
    
    ISolrAbstractResponseParser<Document> parser = ServiceLocator.Current.GetInstance<ISolrAbstractResponseParser<Document>>();
    SolrQueryResults<Document> results = new SolrQueryResults<Document>();
    parser.Parse(System.Xml.Linq.XDocument.Parse(xml), results);
    
    TermsResults termResults = results.Terms;
    foreach (TermsResult result in termResults)
    {
        foreach (KeyValuePair<string, int> kvp in result.Terms)
        {
            //... do something with keys
        }
    }
    

    Basically I use the SolrNet parser and the connection code but not the query stuff. Hope this helps.

    0 讨论(0)
  • 2020-12-30 17:03

    In order to execute your query against the /suggest request handler that you have setup, you will need to set the qt Solr parameter using the ExtraParameters in your SolrNet QueryOptions like below:

     new SolrBaseRepository.Instance<T>().Start();
     var solr = ServiceLocator.Current.GetInstance<ISolrOperations<T>>();
     var options = new QueryOptions
     {
         FilterQueries = new ISolrQuery[] { new SolrQueryByField("type", type) },
         ExtraParams = new Dictionary<string, string>{{"qt", "suggest"}},
     };
     var results = solr.Query(keyword, options);
     return results;
    

    Otherwise your query is still executing against the standard /select request handler (or whatever you have defined as the default in your solrconfig.xml).

    0 讨论(0)
提交回复
热议问题