Provides utilities for turning query form input (such as from a web page or Swing gui) into Lucene XML queries by using XSL templates. This approach offers a convenient way of externalizing and changing how user input is turned into Lucene queries. Database applications often adopt similar practices by externalizing SQL in template files that can be easily changed/optimized by a DBA. The static methods can be used on their own or by creating an instance of this class you can store and re-use compiled stylesheets for fast use (e.g. in a server environment)
コード例 #1
0
        public void TestFormTransforms()
        {
            //// Sun 1.5 suffers from http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6240963
            //if (Constants.JAVA_VENDOR.StartsWith("Sun") && Constants.JAVA_VERSION.StartsWith("1.5")) {
            //  String defLang = Locale.getDefault().getLanguage();
            //  assumeFalse("Sun JRE 1.5 suffers from http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6240963 under Turkish locale", defLang.equals("tr") || defLang.equals("az"));
            //}
            //Cache all the query templates we will be referring to.
            QueryTemplateManager qtm      = new QueryTemplateManager();
            Assembly             assembly = GetType().Assembly;

            using (var stream = assembly.GetManifestResourceStream(TestParser.RESOURCE_PATH + "albumBooleanQuery.xsl"))
            {
                qtm.AddQueryTemplate("albumBooleanQuery", stream);
            }
            using (var stream = assembly.GetManifestResourceStream(TestParser.RESOURCE_PATH + "albumFilteredQuery.xsl"))
            {
                qtm.AddQueryTemplate("albumFilteredQuery", stream);
            }
            using (var stream = assembly.GetManifestResourceStream(TestParser.RESOURCE_PATH + "albumLuceneClassicQuery.xsl"))
            {
                qtm.AddQueryTemplate("albumLuceneClassicQuery", stream);
            }

            //Run all of our test queries
            foreach (String queryForm in queryForms)
            {
                IDictionary <string, string> queryFormProperties = getPropsFromString(queryForm);

                //Get the required query XSL template for this test
                //      Templates template=getTemplate(queryFormProperties.getProperty("template"));

                //Transform the queryFormProperties into a Lucene XML query
                XmlDocument doc = qtm.GetQueryAsDOM(queryFormProperties, queryFormProperties["template"]);

                //Parse the XML query using the XML parser
                Query q = builder.GetQuery(doc.DocumentElement);

                //Run the query
                int h = searcher.Search(q, null, 1000).TotalHits;

                //Check we have the expected number of results
                int expectedHits = int.Parse(queryFormProperties["expectedMatches"]);
                assertEquals("Number of results should match for query " + queryForm, expectedHits, h);
            }
        }
コード例 #2
0
        public void TestFormTransforms()
        {
            //// Sun 1.5 suffers from http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6240963
            //if (Constants.JAVA_VENDOR.StartsWith("Sun") && Constants.JAVA_VERSION.StartsWith("1.5")) {
            //  String defLang = Locale.getDefault().getLanguage();
            //  assumeFalse("Sun JRE 1.5 suffers from http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6240963 under Turkish locale", defLang.equals("tr") || defLang.equals("az"));
            //}
            //Cache all the query templates we will be referring to.
            QueryTemplateManager qtm = new QueryTemplateManager();
            Assembly assembly = GetType().Assembly;
            using (var stream = assembly.GetManifestResourceStream(TestParser.RESOURCE_PATH + "albumBooleanQuery.xsl"))
            {
                qtm.AddQueryTemplate("albumBooleanQuery", stream);
            }
            using (var stream = assembly.GetManifestResourceStream(TestParser.RESOURCE_PATH + "albumFilteredQuery.xsl"))
            {
                qtm.AddQueryTemplate("albumFilteredQuery", stream);
            }
            using (var stream = assembly.GetManifestResourceStream(TestParser.RESOURCE_PATH + "albumLuceneClassicQuery.xsl"))
            {
                qtm.AddQueryTemplate("albumLuceneClassicQuery", stream);
            }

            //Run all of our test queries
            foreach (String queryForm in queryForms)
            {
                IDictionary<string, string> queryFormProperties = getPropsFromString(queryForm);

                //Get the required query XSL template for this test
                //      Templates template=getTemplate(queryFormProperties.getProperty("template"));

                //Transform the queryFormProperties into a Lucene XML query
                XmlDocument doc = qtm.GetQueryAsDOM(queryFormProperties, queryFormProperties["template"]);

                //Parse the XML query using the XML parser
                Query q = builder.GetQuery(doc.DocumentElement);

                //Run the query
                int h = searcher.Search(q, null, 1000).TotalHits;

                //Check we have the expected number of results
                int expectedHits = int.Parse(queryFormProperties["expectedMatches"]);
                assertEquals("Number of results should match for query " + queryForm, expectedHits, h);

            }
        }