// Collection of code snippets by Arne Vajhøj // (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) using System; using System.IO; using System.Net; using Newtonsoft.Json; using Newtonsoft.Json.Linq; namespace TextSearch.Solr.HttpPost { public class Program { private static string Http(string method, string url, string typ, string body) { HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); req.Method = method; req.Accept = typ; if (body != null) { req.ContentType = typ; StreamWriter sw = new StreamWriter(req.GetRequestStream()); sw.WriteLine(body); sw.Flush(); } HttpWebResponse resp = (HttpWebResponse)req.GetResponse(); if (((int)resp.StatusCode) / 100 == 2) { StreamReader sr = new StreamReader(resp.GetResponseStream()); string content = sr.ReadToEnd(); sr.Close(); return content; } else { Console.WriteLine("Error: {0} {1}", resp.StatusCode, resp.StatusDescription); return null; } } private static void Dump(String baseurl, String searchterm) { Console.WriteLine("search for : {0}", searchterm); string rawresp = Http("POST", baseurl + "/chessopening/select?df=alltext", "application/json", "{ 'query': '" + searchterm + "' }"); JObject resp = (JObject)JsonConvert.DeserializeObject(rawresp); foreach (JObject doc in resp["response"]["docs"]) { Console.WriteLine(" {0} - {1}", doc["names"], doc["moves"]); } } public static void Main(string[] args) { // all Dump("http://localhost:8888/solr", "*:*"); // one Dump("http://localhost:8888/solr", "names:defense"); Dump("http://localhost:8888/solr", "moves:d4"); // dual Dump("http://localhost:8888/solr", "alltext:defense"); Dump("http://localhost:8888/solr", "alltext:d4"); Dump("http://localhost:8888/solr", "defense"); // default field Dump("http://localhost:8888/solr", "d4"); // default field // boolean Dump("http://localhost:8888/solr", "names:defense moves:d4"); // implicit or Dump("http://localhost:8888/solr", "names:defense AND moves:d4"); // and Dump("http://localhost:8888/solr", "names:defense NOT moves:d4"); // not Dump("http://localhost:8888/solr", "defense d4"); // default field + implicit or Dump("http://localhost:8888/solr", "defense AND d4"); // default field + and Dump("http://localhost:8888/solr", "defense NOT d4"); // default field + not // wildcard Dump("http://localhost:8888/solr", "names:def*"); Dump("http://localhost:8888/solr", "moves:N*"); } } }