Exemplo n.º 1
0
        private static void SetupSolr() {
            var connection = new SolrConnection(_httpSolrTest);
            Startup.InitContainer();

            Startup.Container.Remove<IReadOnlyMappingManager>();

            var mapper = new MappingManager();
            Mappings(mapper);
            Startup.Container.Register<IReadOnlyMappingManager>(c => mapper);

            Startup.Container.Remove<ISolrDocumentPropertyVisitor>();
            var propertyVisitor = new DefaultDocumentVisitor(mapper, Startup.Container.GetInstance<ISolrFieldParser>());
            Startup.Container.Register<ISolrDocumentPropertyVisitor>(c => propertyVisitor);

            Startup.Init<Child>(connection);
            Startup.Init<Parent>(connection);
            Startup.Init<Dictionary<string,object>>(connection);
            Startup.Container.RemoveAll<ISolrDocumentResponseParser<Dictionary<string, object>>>();
            Startup.Container.Register<ISolrDocumentResponseParser<Dictionary<string, object>>>(c => new SolrDictionaryDocumentResponseParser(c.GetInstance<ISolrFieldParser>()));

            var solr = ServiceLocator.Current.GetInstance<ISolrOperations<Child>>();

            solr.Delete(SolrQuery.All);
            solr.Commit();
        }
Exemplo n.º 2
0
 static void Main(string[] args)
 {
     var builder = Startup.GetQueryBuilder();
     var conn = new SolrConnection("http://localhost:9090/solr");
     var query = builder.GetQuery(conn, new SolrQuery("*:*"), new QueryOptions {Rows = 99});
     Console.WriteLine(query);
     Console.ReadLine();
 }
Exemplo n.º 3
0
		public void ActualConnection() {
            var conn = new SolrConnection(solrURL) { HttpWebRequestFactory = new HttpWebRequestFactory() };
			var p = new Dictionary<string, string>();
			p["version"] = "2.1";
			p["indent"] = "on";
			p["q"] = "+video +price:[* TO 400]";
			Console.WriteLine(conn.Get("/select/", p));
		}
Exemplo n.º 4
0
 public void ActualInvalidFieldException()
 {
     var conn = new SolrConnection(solrURL, new HttpWebRequestFactory());
     var p = new Dictionary<string, string>();
     p["version"] = "2.1";
     p["indent"] = "on";
     p["q"] = "idq:123";
     Console.WriteLine(conn.Get("/select/", p));
 }
Exemplo n.º 5
0
 public void Cache()
 {
     var conn = new SolrConnection(solrURL);
     conn.Get("/select/", new Dictionary<string, string> {
         {"q", "*:*"},
     });
     conn.Get("/select/", new Dictionary<string, string> {
         {"q", "*:*"},
     });
 }
Exemplo n.º 6
0
 public void Cache()
 {
     var conn = new SolrConnection(solrURL, new HttpWebRequestFactory());
     var response1 = conn.Get("/select/", new Dictionary<string, string> {
         {"q", "*:*"},
     });
     var response2 = conn.Get("/select/", new Dictionary<string, string> {
         {"q", "*:*"},
     });
 }
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

            var solrServerUrl = ConfigurationManager.AppSettings["SolrServerUrl"];
            var solrConnection = new SolrConnection(solrServerUrl);
            Startup.Init<SearchResultItem>(solrConnection);
        }
Exemplo n.º 8
0
		public void ActualConnectionWithException() {
            var conn = new SolrConnection(solrURL);
			var p = new Dictionary<string, string>();
			p["version"] = "2.1";
			p["indent"] = "on";
			p["q"] = "idq:123";
            try {
                conn.Get("/select/", p);
                Assert.Fail("Should have thrown");
            } catch (SolrConnectionException e) {
                Console.WriteLine(e);
                Console.WriteLine(e.Url);
            }
		}
Exemplo n.º 9
0
        protected void Application_Start() {
            XmlConfigurator.ConfigureAndWatch(new FileInfo(Path.Combine(Server.MapPath("/"), "log4net.config")));

            RegisterRoutes(RouteTable.Routes);

            var connection = new SolrConnection(solrURL);
            var loggingConnection = new LoggingConnection(connection);
            Startup.Init<Product>(loggingConnection);

            RegisterAllControllers();
            ControllerBuilder.Current.SetControllerFactory(new ServiceProviderControllerFactory(Startup.Container));
            ModelBinders.Binders[typeof (SearchParameters)] = new SearchParametersBinder();
            AddInitialDocuments();
        }
Exemplo n.º 10
0
		public void Get() {
		    var response = new Mocks.HttpWebResponse {
		        dispose = () => {},
                headers = () => new WebHeaderCollection(),
                getResponseStream = () => new MemoryStream(Encoding.UTF8.GetBytes("hello world")),
		    };
		    var request = new Mocks.HttpWebRequest {
		        getResponse = () => response
		    };
		    var reqFactory = new Mocks.HttpWebRequestFactory {
		        create = _ => request
		    };
            var conn = new SolrConnection("https://pepe") {
                HttpWebRequestFactory = reqFactory,
            };
		    var r = conn.Get("", new Dictionary<string, string>());
            Assert.AreEqual("hello world", r);
		}
Exemplo n.º 11
0
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            GlobalConfiguration.Configure(WebApiConfig.Register);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            string solrurl    = ConfigurationManager.AppSettings["solrurl"];
            string solrUser   = ConfigurationManager.AppSettings["solruser"];
            string solrPwd    = ConfigurationManager.AppSettings["solrpassword"];
            var    connection = new SolrNet.Impl.SolrConnection(solrurl)
            {
                HttpWebRequestFactory = new HttpWebAdapters.BasicAuthHttpWebRequestFactory(solrUser, solrPwd)
            };

            SolrNet.Startup.Init <sys_user>(connection);
            SolrNet.Startup.Init <sys_role>(connection);
            SolrNet.Startup.Init <sys_menu>(connection);
            SolrNet.Startup.Init <sys_organize>(connection);
        }
Exemplo n.º 12
0
 public void InvalidHostPost_ShouldThrowException()
 {
     var mocks = new MockRepository();
     var reqFactory = mocks.StrictMock<IHttpWebRequestFactory>();
     var request = mocks.DynamicMock<IHttpWebRequest>();
     With.Mocks(mocks).Expecting(delegate {
         Expect.Call(request.GetRequestStream())
             .Repeat.Once()
             .Throw(new WebException());
         Expect.Call(reqFactory.Create(new Uri("http://lalala:12345/update")))
             .IgnoreArguments()
             .Repeat.Once()
             .Return(request);
     }).Verify(delegate {
         var conn = new SolrConnection("http://lalala:12345", reqFactory);
         conn.Post("/update", "");
     });
 }
Exemplo n.º 13
0
 public void InvalidHostGet_ShouldThrowException()
 {
     var mocks = new MockRepository();
     var reqFactory = mocks.StrictMock<IHttpWebRequestFactory>();
     var request = mocks.DynamicMock<IHttpWebRequest>();
     With.Mocks(mocks).Expecting(delegate {
         Expect.Call(reqFactory.Create(new UriBuilder().Uri))
             .IgnoreArguments()
             .Repeat.Once()
             .Return(request);
         Expect.Call(request.GetResponse())
             .Repeat.Once()
             .Throw(new WebException());
     }).Verify(delegate {
         var conn = new SolrConnection("http://lalala:12345", reqFactory);
         conn.Get("", new Dictionary<string, string>());
     });
 }
Exemplo n.º 14
0
 public void GetWithNullParameters_ShouldAcceptNull()
 {
     var mocks = new MockRepository();
     var reqFactory = mocks.StrictMock<IHttpWebRequestFactory>();
     var request = mocks.DynamicMock<IHttpWebRequest>();
     var response = mocks.DynamicMock<IHttpWebResponse>();
     With.Mocks(mocks).Expecting(delegate {
         Expect.Call(reqFactory.Create(new UriBuilder().Uri))
             .IgnoreArguments()
             .Repeat.Once()
             .Return(request);
         Expect.Call(response.Headers)
             .Repeat.Any()
             .Return(new WebHeaderCollection());
         Expect.Call(request.GetResponse())
             .Repeat.Once()
             .Return(response);
         Expect.Call(response.GetResponseStream())
             .Repeat.Once()
             .Return(new MemoryStream());
     }).Verify(delegate {
         var conn = new SolrConnection("https://pepe", reqFactory);
         conn.Get("", new Dictionary<string, string>());
     });
 }
Exemplo n.º 15
0
 public void NoCache_performance()
 {
     var conn = new SolrConnection(solrURL) {
         Cache = new NullCache(),
     };
     TestCache(conn);
 }
Exemplo n.º 16
0
 public void Get_Compressed_Gzip()
 {
     var mocks = new MockRepository();
     var reqFactory = mocks.StrictMock<IHttpWebRequestFactory>();
     var request = mocks.DynamicMock<IHttpWebRequest>();
     var response = mocks.DynamicMock<IHttpWebResponse>();
     With.Mocks(mocks).Expecting(delegate
     {
         Expect.Call(reqFactory.Create(new UriBuilder().Uri))
             .IgnoreArguments()
             .Repeat.Once()
             .Return(request);
         Expect.Call(request.Headers)
             .Repeat.Any()
             .Return(new WebHeaderCollection());
         Expect.Call(response.ContentEncoding)
             .Repeat.Any()
             .Return("gzip");
         Expect.Call(response.Headers)
             .Repeat.Any()
             .Return(new WebHeaderCollection());
         Expect.Call(request.GetResponse())
             .Repeat.Once()
             .Return(response);
         Expect.Call(response.GetResponseStream())
             .Repeat.Once()
             .Return(CompressionUtils.GzipCompressStream("Testing compression"));
     }).Verify(delegate {
         var conn = new SolrConnection("http://localhost") { HttpWebRequestFactory = reqFactory };
         Assert.AreEqual("Testing compression", conn.Get("", new Dictionary<string, string>()));
     });
 }
Exemplo n.º 17
0
		public void InvalidHostGet_ShouldThrowException() {
		    var reqFactory = new Mocks.HttpWebRequestFactory {
		        create = _ => new Mocks.HttpWebRequest {
		            getResponse = () => { throw new WebException();}
		        }
		    };
            var conn = new SolrConnection("http://lalala:12345") { HttpWebRequestFactory = reqFactory };
            conn.Get("", new Dictionary<string, string>());
		}
Exemplo n.º 18
0
 public void UndefinedFieldQueryError_ShouldThrow()
 {
     var mocks = new MockRepository();
     var reqFactory = mocks.StrictMock<IHttpWebRequestFactory>();
     var request = mocks.DynamicMock<IHttpWebRequest>();
     With.Mocks(mocks).Expecting(delegate {
         Expect.Call(reqFactory.Create(new UriBuilder().Uri))
             .IgnoreArguments()
             .Repeat.Once()
             .Return(request);
         var r = new WebResponseStub {StatusCode = HttpStatusCode.BadRequest};
         Expect.Call(request.GetResponse())
             .Repeat.Once()
             .Throw(new WebException("(400) Bad Request", new ApplicationException(), WebExceptionStatus.ProtocolError, r));
     }).Verify(delegate {
         var conn = new SolrConnection("https://pepe", reqFactory);
         conn.Get("", new Dictionary<string, string>());
     });
 }
 private ISolrCoreAdmin BuildCoreAdmin()
 {
     var connection = new SolrConnection(SolrContentSearchManager.ServiceAddress);
     return _operations.BuildCoreAdmin(connection);
 }
Exemplo n.º 20
0
        public void Cache_mocked() {
            var cache = new Mocks.MSolrCache();
            cache.get += url => {
                Assert.AreEqual("http://localhost:8983/solr/select/?q=*:*&version=2.2", url);
                return new SolrCacheEntity(url, "", "");
            };
            cache.add &= x => x.Stub();

            var response = new Mocks.HttpWebResponse {
                dispose = () => {},
                headers = () => new WebHeaderCollection {
                    {HttpResponseHeader.ETag, "123"},
                },
                getResponseStream = () => new MemoryStream(),
            };
            var getResponseCalls = 0;
            var conn = new SolrConnection(solrURL) {
                Cache = cache,
                HttpWebRequestFactory = new Mocks.HttpWebRequestFactory {
                    create = _ => new Mocks.HttpWebRequest {
                        getResponse = () => {
                            getResponseCalls++;
                            if (getResponseCalls == 1)
                                return response;
                            throw new Exception();
                        },
                        Headers = new WebHeaderCollection(),
                    },
                }
            };

            conn.Get("/select/", new Dictionary<string, string> {
                {"q", "*:*"},
            });

            conn.Get("/select/", new Dictionary<string, string> {
                {"q", "*:*"},
            });
        }
Exemplo n.º 21
0
        public void CacheMaxAge_ShouldCallGetResponseWhenExpired()
        {
            var cache = new Mocks.MSolrCache();
            cache.get += url =>
            {
                Assert.AreEqual("http://localhost:8983/solr/select/?q=*:*&version=2.2", url);
                return new SolrCacheEntity(url, "", "", new DateTime(2013, 5, 12, 12, 30, 30));
            };
            cache.add &= x => x.Stub();

            var response = new Mocks.HttpWebResponse
            {
                dispose = () => { },
                headers = () => new WebHeaderCollection(),
                getResponseStream = () => new MemoryStream(),
            };
            var getResponseCalls = 0;
            var conn = new SolrConnection(solrURL)
            {
                Cache = cache,
                HttpWebRequestFactory = new Mocks.HttpWebRequestFactory
                {
                    create = _ => new Mocks.HttpWebRequest
                    {
                        getResponse = () =>
                        {
                            getResponseCalls++;
                            return response;
                        },
                        Headers = new WebHeaderCollection(),
                    },
                }
            };

            SystemTime.UtcNow = () => new DateTime(2013, 5, 12, 12, 30, 29);
            conn.Get("/select/", new Dictionary<string, string> {
                {"q", "*:*"},
            });
            Assert.AreEqual(getResponseCalls, 0, "Should not call getResponse when valid expiration");

            SystemTime.UtcNow = () => new DateTime(2013, 5, 12, 12, 30, 35);
            conn.Get("/select/", new Dictionary<string, string> {
                {"q", "*:*"},
            });

            Assert.AreEqual(getResponseCalls, 1, "Should call getResponse if the cache entry is expired");
        }
Exemplo n.º 22
0
        private void RegisterSingleCore(ContainerBuilder builder) {
            RegisterCommonComponents(builder);

            SolrConnection solrConnection = new SolrConnection(ServerUrl);
            if (HttpWebRequestFactory != null) {
                solrConnection.HttpWebRequestFactory = HttpWebRequestFactory;
            }
            builder.RegisterInstance(solrConnection).As<ISolrConnection>();

            builder.RegisterGeneric(typeof (SolrBasicServer<>))
                .As(typeof (ISolrBasicOperations<>), typeof (ISolrBasicReadOnlyOperations<>))
                .SingleInstance();
            builder.RegisterGeneric(typeof (SolrServer<>))
                .As(typeof (ISolrOperations<>), typeof (ISolrReadOnlyOperations<>))
                .SingleInstance();
        }
Exemplo n.º 23
0
		public void InvalidHostPost_ShouldThrowException() {
		    var reqFactory = new Mocks.HttpWebRequestFactory {
		        create = _ => new Mocks.HttpWebRequest {
		            getRequestStream = () => { throw new WebException(); },
                    requestUri = () => new Uri("http://lalala:12345/update"),
		        }
		    };
            var conn = new SolrConnection("http://lalala:12345") { HttpWebRequestFactory = reqFactory };
            conn.Post("/update", "");
		}
Exemplo n.º 24
0
 public void NoCache_performance()
 {
     var conn = new SolrConnection(solrURL, new HttpWebRequestFactory()) {
         Cache = new NullCache(),
     };
     TestCache(conn);
 }
Exemplo n.º 25
0
 public void Post()
 {
     var mocks = new MockRepository();
     var reqFactory = mocks.StrictMock<IHttpWebRequestFactory>();
     var request = mocks.DynamicMock<IHttpWebRequest>();
     var response = mocks.DynamicMock<IHttpWebResponse>();
     With.Mocks(mocks).Expecting(delegate {
         Expect.Call(request.GetRequestStream())
             .Repeat.Once()
             .Return(new MemoryStream());
         Expect.Call(reqFactory.Create(new Uri("https://pepe")))
             .Repeat.Once()
             .Return(request);
         request.Method = HttpWebRequestMethod.POST;
         LastCall.On(request).Repeat.Once();
         Expect.Call(request.GetResponse())
             .Repeat.Once()
             .Return(response);
         Expect.Call(response.Headers)
             .Repeat.Any()
             .Return(new WebHeaderCollection());
         Expect.Call(response.GetResponseStream())
             .Repeat.Once()
             .Return(new MemoryStream());
     }).Verify(delegate {
         var conn = new SolrConnection("https://pepe", reqFactory);
         conn.Post("", "");
     });
 }
Exemplo n.º 26
0
 /// <summary>
 /// Builds an instance of core admin manager with the specified URL
 /// </summary>
 /// <param name="url"></param>
 /// <returns></returns>
 public ISolrCoreAdmin BuildCoreAdmin(string url) {
     var conn = new SolrConnection(url);
     return BuildCoreAdmin(conn);
 }
Exemplo n.º 27
0
        private static void Main(string[] args)
        {

            var connection = new SolrConnection(solrURL);

            Startup.Init<Product>(connection);

            
            var p = new Product
            {
                Id = "SP2514N",
                Manufacturer = "Samsung Electronics Co. Ltd.",
                Categories = new[]
                {
                    "electronics",
                    "hard drive333333333111",
                },
                Price = 92,
                InStock = true,
            };

            var solr = ServiceLocator.Current.GetInstance<ISolrOperations<Product>>();
            solr.Add(p);
            solr.Commit();



            p = new Product
            {
                Id = "SP2514N2",
                Manufacturer = "AAA Electronics Co. Ltd.",
                Categories = new[]
                {
                    "electronics",
                    "hard 777777777",
                },
                Price = 92,
                InStock = true,
            };

            
            solr.Add(p);
            solr.Commit();
            

            var results = solr.Query(new SolrQueryByField("id", "SP2514N"));
            
            Console.WriteLine(results[0].Price);

            

            var baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
            
            string archiveFolder = Path.Combine(baseDirectory, "docs");


            try
            {
                solr = ServiceLocator.Current.GetInstance<ISolrOperations<Product>>();
                solr.Delete(SolrQuery.All);
                connection = (SolrConnection) ServiceLocator.Current.GetInstance<ISolrConnection>();
                foreach (var file in Directory.GetFiles(archiveFolder, "*.xml"))
                {
                    connection.Post("/update", File.ReadAllText(file, Encoding.UTF8));
                }
                solr.Commit();
                solr.BuildSpellCheckDictionary();
            }
            catch (SolrConnectionException e)
            {
                throw new Exception(string.Format("Couldn't connect to Solr. Please make sure that Solr is running on '{0}' or change the address in your web.config, then restart the application.", solrURL), e);
            }

        }
Exemplo n.º 28
0
 public void Cache_performance()
 {
     var conn = new SolrConnection(solrURL) {
         Cache = new HttpRuntimeCache(),
     };
     TestCache(conn);
 }
Exemplo n.º 29
0
 public void Cache_mocked()
 {
     var conn = new SolrConnection(solrURL, new HttpWebRequestFactory());
     var cache = MockRepository.GenerateMock<ISolrCache>();
     cache.Expect(x => x["http://localhost:8983/solr/select/?q=*:*"])
         .Repeat.Once()
         .Return(null);
     cache.Expect(x => x.Add(null)).Repeat.Once();
     conn.Cache = cache;
     var response1 = conn.Get("/select/", new Dictionary<string, string> {
         {"q", "*:*"},
     });
     var response2 = conn.Get("/select/", new Dictionary<string, string> {
         {"q", "*:*"},
     });
 }
 public void Setup()
 {
     solrconnection = new SolrConnection("http://localhost:8983/solr");
     collections = new SolrCollectionsAdmin(solrconnection, new HeaderResponseParser<string>());
 }