Exemplo n.º 1
0
        public void QueryWithPagination()
        {
            const string qstring    = "id:123";
            const int    start      = 10;
            const int    rows       = 20;
            var          connection = new MSolrConnection();

            connection.get += (url, param) => {
                Assert.AreEqual("/select", url);
                var expectedParams = new Dictionary <string, string> {
                    { "q", qstring },
                    { "start", start.ToString() },
                    { "rows", rows.ToString() },
                };
                CollectionAssert.AreEquivalent(expectedParams, param);
                return(EmbeddedResource.GetEmbeddedString(GetType(), "Resources.response.xml"));
            };

            var querySerializer = new MSolrQuerySerializer();

            querySerializer.serialize += _ => qstring;

            var resultParser = new MSolrAbstractResponseParser <TestDocumentWithUniqueKey>();

            resultParser.parse &= x => x.Stub();

            var executer = new SolrQueryExecuter <TestDocumentWithUniqueKey>(resultParser, connection, querySerializer, null, null);
            var solr     = new SolrBasicServer <TestDocumentWithUniqueKey>(connection, executer, null, null, null, null, null, null);
            var r        = solr.Query(new SolrQuery(qstring), new QueryOptions {
                Start = start, Rows = rows
            });

            Assert.AreEqual(1, connection.get.Calls);
        }
Exemplo n.º 2
0
        public void QueryAll_with_pagination()
        {
            var querySerializer = new MSolrQuerySerializer();

            querySerializer.serialize += _ => "id:123456";

            var serviceLocator = new MServiceLocator();

            serviceLocator.getService = MServiceLocator.Table(new Dictionary <System.Type, object> {
                { typeof(ISolrReadOnlyOperations <Entity>), mockSolr },
                { typeof(ISolrQuerySerializer), querySerializer },
            });

            ServiceLocator.SetLocatorProvider(() => serviceLocator);

            mockSolr.queryStringOptions += (s, o) => {
                Assert.Equal("id:123456", s);
                Assert.Equal(5, o.Rows);
                Assert.Equal(3, o.Start);
                Assert.Equal(1, o.OrderBy.Count);
                Assert.Equal("pepe asc", o.OrderBy.First().ToString());
                return(new SolrQueryResults <Entity>());
            };
            mockSolr.queryStringOptions &= x => x.Expect(1);

            using (var session = new SolrSession(sessionFactory.OpenSession())) {
                var entities = session.CreateSolrQuery(new SolrQueryByField("id", "123456"))
                               .SetMaxResults(5)
                               .SetFirstResult(3)
                               .SetSort(Order.Asc("pepe"))
                               .List <Entity>();
            }
        }
Exemplo n.º 3
0
        public void Execute()
        {
            const string queryString = "id:123456";
            var          q           = new Dictionary <string, string>();

            q["q"] = queryString;
            var conn       = new MockConnection(q);
            var serializer = new MSolrQuerySerializer();

            serializer.serialize += _ => queryString;
            var parser = new MSolrAbstractResponseParser <TestDocument>();

            parser.parse &= x => x.Stub();

            var queryExecuter = new SolrQueryExecuter <TestDocument>(parser, conn, serializer, null, null);
            var r             = queryExecuter.Execute(new SolrQuery(queryString), null);

            Assert.AreEqual(1, serializer.serialize.Calls);
        }
Exemplo n.º 4
0
        public void DeleteByQuery()
        {
            var connection = new MSolrConnection();

            connection.post += (url, content) => {
                Assert.AreEqual("/update", url);
                Assert.AreEqual("<delete><query>id:123</query></delete>", content);
                return(EmbeddedResource.GetEmbeddedString(GetType(), "Resources.response.xml"));
            };
            var headerParser = new MSolrHeaderResponseParser();

            headerParser.parse += _ => null;
            var querySerializer = new MSolrQuerySerializer();

            querySerializer.serialize += _ => "id:123";
            var ops = new SolrBasicServer <TestDocumentWithUniqueKey>(connection, null, null, null, headerParser, querySerializer, null, null);

            ops.Delete(null, new SolrQuery("id:123"));
            Assert.AreEqual(1, connection.post.Calls);
        }
Exemplo n.º 5
0
        public void ExecuteAsyncWithBodyContent()
        {
            const string queryJson = "{ \"query\": \"id:123456\" }";
            var          q         = new Dictionary <string, string>();

            q["rows"] = SolrQueryExecuter <SolrQueryExecuterTests.TestDocument> .ConstDefaultRows.ToString();

            var conn       = new MockConnection(q, SimpleJsonQueryBody.ApplicationJson, queryJson);
            var serializer = new MSolrQuerySerializer();

            serializer.serialize += _ => string.Empty;
            var parser = new MSolrAbstractResponseParser <SolrQueryExecuterTests.TestDocument>();

            parser.parse &= x => x.Stub();

            var queryExecuter = new SolrQueryExecuter <SolrQueryExecuterTests.TestDocument>(parser, conn, serializer, null, null);
            var r             = queryExecuter.ExecuteAsync(new SolrQuery(null), new SimpleJsonQueryBody(queryJson), new QueryOptions());

            Assert.Equal(1, serializer.serialize.Calls);
        }
Exemplo n.º 6
0
        public void QueryWithSortAndPagination()
        {
            const string qstring = "id:123";
            const int    start   = 10;
            const int    rows    = 20;

            var connection = new MSolrConnection();

            connection.get += new MFunc <string, IEnumerable <KeyValuePair <string, string> >, string>((url, param) => {
                Assert.Equal("/select", url);
                var expectedParams = new Dictionary <string, string> {
                    { "q", qstring },
                    { "start", start.ToString() },
                    { "rows", rows.ToString() },
                    { "sort", "id asc,name desc" },
                };
                Assert.Equal(expectedParams, param); //should ignore order
                return(EmbeddedResource.GetEmbeddedString(GetType(), "Resources.response.xml"));
            });

            var querySerializer = new MSolrQuerySerializer();

            querySerializer.serialize += _ => qstring;

            var resultParser = new MSolrAbstractResponseParser <TestDocumentWithUniqueKey>();

            resultParser.parse &= x => x.Stub();

            var executer = new SolrQueryExecuter <TestDocumentWithUniqueKey>(resultParser, connection, querySerializer, null, null);
            var solr     = new SolrBasicServer <TestDocumentWithUniqueKey>(connection, executer, null, null, null, null, null, null);
            var r        = solr.Query(new SolrQuery(qstring), new QueryOptions {
                Start   = start,
                Rows    = rows,
                OrderBy = new[] {
                    new SortOrder("id", Order.ASC),
                    new SortOrder("name", Order.DESC)
                }
            });

            Assert.Equal(1, connection.get.Calls);
        }
Exemplo n.º 7
0
        public void FacetField()
        {
            var connection = new MSolrConnection();

            connection.get += (url, param) => {
                Assert.AreEqual("/select", url);
                var expectedParams = new Dictionary <string, string> {
                    { "q", "" },
                    { "rows", SolrQueryExecuter <TestDocumentWithUniqueKey> .ConstDefaultRows.ToString() },
                    { "facet", "true" },
                    { "facet.field", "id" },
                    { "f.id.facet.limit", "3" },
                };
                CollectionAssert.AreEquivalent(expectedParams, param);
                return(EmbeddedResource.GetEmbeddedString(GetType(), "Resources.response.xml"));
            };

            var parser = new MSolrAbstractResponseParser <TestDocumentWithUniqueKey>();

            parser.parse &= x => x.Stub();
            var querySerializer = new MSolrQuerySerializer();

            querySerializer.serialize += _ => "";
            var facetQuerySerializer = new DefaultFacetQuerySerializer(querySerializer, new DefaultFieldSerializer());

            var executer = new SolrQueryExecuter <TestDocumentWithUniqueKey>(parser, connection, querySerializer, facetQuerySerializer, null);
            var solr     = new SolrBasicServer <TestDocumentWithUniqueKey>(connection, executer, null, null, null, null, null, null);
            var r        = solr.Query(new SolrQuery(""), new QueryOptions {
                Facet = new FacetParameters {
                    Queries = new ISolrFacetQuery[] {
                        new SolrFacetFieldQuery("id")
                        {
                            Limit = 3
                        },
                    },
                }
            });

            Assert.AreEqual(1, connection.get.Calls);
        }
        public void RequestHandler()
        {
            const string queryString = "id:123456";
            var          q           = new Dictionary <string, string>();

            q["q"]       = queryString;
            q["handler"] = "/update";
            q["rows"]    = SolrQueryExecuter <TestDocument> .ConstDefaultRows.ToString();

            var queryOptions = new QueryOptions {
                RequestHandler = new RequestHandlerParameters("/update")
            };
            var conn       = new MockConnection(q);
            var serializer = new MSolrQuerySerializer();

            serializer.serialize += _ => queryString;
            var parser = new MSolrAbstractResponseParser <TestDocument>();

            parser.parse &= x => x.Stub();

            var queryExecuter = new SolrQueryExecuter <TestDocument>(parser, conn, serializer, null, null);
            var r             = queryExecuter.Execute(new SolrQuery(queryString), queryOptions);
        }
Exemplo n.º 9
0
        public void QueryWithSort()
        {
            const string qstring = "id:123";

            var connection = new MSolrConnection();

            connection.get += (url, param) => {
                Assert.AreEqual("/select", url);
                var expectedParams = new Dictionary <string, string> {
                    { "q", qstring },
                    { "rows", SolrQueryExecuter <TestDocumentWithUniqueKey> .ConstDefaultRows.ToString() },
                    { "sort", "id asc,name desc" },
                };
                CollectionAssert.AreEquivalent(expectedParams, param);
                return(EmbeddedResource.GetEmbeddedString(GetType(), "Resources.response.xml"));
            };

            var querySerializer = new MSolrQuerySerializer();

            querySerializer.serialize += _ => qstring;

            var resultParser = new MSolrAbstractResponseParser <TestDocumentWithUniqueKey>();

            resultParser.parse &= x => x.Stub();

            var executer = new SolrQueryExecuter <TestDocumentWithUniqueKey>(resultParser, connection, querySerializer, null, null);
            var solr     = new SolrBasicServer <TestDocumentWithUniqueKey>(connection, executer, null, null, null, null, null, null);
            var r        = solr.Query(new SolrQuery(qstring),
                                      new QueryOptions {
                OrderBy = new[] {
                    new SortOrder("id", Order.ASC),
                    new SortOrder("name", Order.DESC)
                }
            });

            Assert.AreEqual(1, connection.get.Calls);
        }
Exemplo n.º 10
0
        public void MoreLikeThisHandlerQuery()
        {
            const string qstring = "id:123";

            var connection = new MSolrConnection();

            connection.get += (url, param) => {
                Assert.AreEqual("/mlt", url);
                var expectedParams = new Dictionary <string, string> {
                    { "q", qstring },
                    { "rows", SolrQueryExecuter <TestDocumentWithUniqueKey> .ConstDefaultRows.ToString() },
                    { "mlt", "true" },
                    { "mlt.fl", "id" },
                    { "mlt.match.include", "true" },
                };
                CollectionAssert.AreEquivalent(expectedParams, param);
                return(EmbeddedResource.GetEmbeddedString(GetType(), "Resources.responseWithInterestingTermsDetails.xml"));
            };

            var querySerializer = new MSolrQuerySerializer();

            querySerializer.serialize &= x => x.Return(qstring);

            var mlthParser = new MSolrMoreLikeThisHandlerQueryResultsParser <TestDocumentWithUniqueKey>();

            mlthParser.parse += _ => new SolrMoreLikeThisHandlerResults <TestDocumentWithUniqueKey>();

            var executer = new SolrQueryExecuter <TestDocumentWithUniqueKey>(null, connection, querySerializer, null, mlthParser);
            var solr     = new SolrBasicServer <TestDocumentWithUniqueKey>(connection, executer, null, null, null, null, null, null);
            var r        = solr.MoreLikeThis(new SolrMoreLikeThisHandlerQuery(new SolrQuery(qstring)), new MoreLikeThisHandlerQueryOptions(new MoreLikeThisHandlerParameters(new[] { "id" })
            {
                MatchInclude = true
            }));

            Assert.AreEqual(1, connection.get.Calls);
        }
Exemplo n.º 11
0
        public void ExecuteMLT_with_no_body()
        {
            const string queryString = "my query";

            var parser = new MSolrMoreLikeThisHandlerQueryResultsParser <SolrQueryExecuterTests.TestDocument>();
            var q      = new Dictionary <string, string>();

            q["q"]                    = queryString;
            q["mlt"]                  = "true";
            q["mlt.fl"]               = "one,three";
            q["mlt.match.include"]    = "false";
            q["mlt.match.offset"]     = "5";
            q["mlt.interestingTerms"] = InterestingTerms.None.ToString().ToLowerInvariant();
            q["start"]                = "0";
            q["rows"]                 = "5";
            q["fl"]                   = "one,two,three";
            var conn = new MockConnection(q, MediaTypeNames.Text.Plain, string.Empty);

            var serializer = new MSolrQuerySerializer();

            serializer.serialize += _ => queryString;
            var qe = new SolrQueryExecuter <SolrQueryExecuterTests.TestDocument>(null, conn, serializer, null, parser);
            var r  = qe.ExecuteAsync(new SolrMoreLikeThisHandlerQuery(new SolrQuery(queryString)),
                                     new MoreLikeThisHandlerQueryOptions(
                                         new MoreLikeThisHandlerParameters(new[] { "one", "three" })
            {
                MatchInclude = false,
                MatchOffset  = 5,
                ShowTerms    = InterestingTerms.None,
            })
            {
                Start  = 0,
                Rows   = 5,
                Fields = new[] { "one", "two", "three" },
            });
        }