Example #1
0
        public ActionResult Search(object entity)
        {
            var query = Request.QueryString["q"];
            var results = new SearchResults();
            results.QueryText = query;
            if (!String.IsNullOrEmpty(query))
            {
                //Initialize the connection to the search index
                Connection conn = new Connection("http://localhost:8080/solr/collection1");

                var parameters = new NameValueCollection();
                //Set the query
                parameters["q"] = String.Format("\"{0}\"", query);
                //Add highlighting - enables summary field be auto-generated if empty
                parameters.Add("hl", "true");
                //Add publication id
                parameters.Add("fq", "publicationid:4");
                //Set page size
                parameters.Add("rows", "5"); 
                //Add page number (default is 1)
                String start = Request.QueryString["start"];
                if (!String.IsNullOrEmpty(start))
                {
                    parameters["start"] = start;
                }
                results = conn.ExecuteQuery(parameters);
            }
            results.QueryText = query;
            return View("SearchResults",results);
        }
Example #2
0
        /// <summary>
        /// Run a query
        /// </summary>
        /// <param name="parameters">querystring parameters for the query URL</param>
        /// <returns>matching results</returns>
		public SearchResults ExecuteQuery(NameValueCollection parameters)
        {
            var results = new SearchResults();
            try
            {
                string url = BuildQueryUrl(SetParameters(parameters));
                //_log.DebugFormat("Query: {0}", url);
                results.QueryUrl = url;
                string data = GetData(url);
                results = ProcessResults(results, data);
                results.PageSize = Int32.Parse(parameters["rows"]);
                //_log.DebugFormat("{0} Results from Query: {1}", results.Total, url);
            }
            catch (Exception ex)
            {
                results.HasError = true;
                results.ErrorDetail = ex.Message + " : " + ex.StackTrace;
                //_log.ErrorFormat("Query resulted in error: {1}", results.ErrorDetail);
            }
            return results;
        }
Example #3
0
		protected SearchResults ProcessResults(SearchResults results, string data)
		{
			if (!String.IsNullOrEmpty(data))
			{
                XDocument xmlData = XDocument.Parse(data);
				//All statuses other than zero are error states
                string status = xmlData.Root.Elements("lst").Where(a => a.Attribute("name").Value == "responseHeader").Elements("int").Where(a => a.Attribute("name").Value == "status").FirstOrDefault().Value;
                if (status != "0")
				{
                    results.HasError = true;
                    results.ErrorDetail = "Solr error status: " + status;
                    //_log.ErrorFormat("Query resulted in error: {1}", results.ErrorDetail);
                    return results;
				}
                var highlighting = xmlData.Root.Elements("lst").Where(a => a.Attribute("name").Value == "highlighting").FirstOrDefault();
                XElement result = xmlData.Root.Element("result");
                results.Total = ((int?)result.Attribute("numFound") ?? 0);
				//Solr uses 0-based indexing
                results.Start = ((int?)result.Attribute("start") ?? 0) +1;
				foreach (var item in result.Elements("doc"))
				{
					results.Items.Add(ProcessResult(item,highlighting));
				}
                
			}
			return results;
		}