lucenenet-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mhern...@apache.org
Subject [08/12] Work in progress on QueryParsers.Flexible
Date Sun, 06 Oct 2013 23:06:00 GMT
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/acd41216/src/contrib/QueryParsers/Flexible/Standard/Builders/IStandardQueryBuilder.cs
----------------------------------------------------------------------
diff --git a/src/contrib/QueryParsers/Flexible/Standard/Builders/IStandardQueryBuilder.cs
b/src/contrib/QueryParsers/Flexible/Standard/Builders/IStandardQueryBuilder.cs
new file mode 100644
index 0000000..366568f
--- /dev/null
+++ b/src/contrib/QueryParsers/Flexible/Standard/Builders/IStandardQueryBuilder.cs
@@ -0,0 +1,16 @@
+using Lucene.Net.QueryParsers.Flexible.Core.Builders;
+using Lucene.Net.QueryParsers.Flexible.Core.Nodes;
+using Lucene.Net.Search;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Lucene.Net.QueryParsers.Flexible.Standard.Builders
+{
+    public interface IStandardQueryBuilder : IQueryBuilder
+    {
+        Query Build(IQueryNode queryNode);
+    }
+}

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/acd41216/src/contrib/QueryParsers/Flexible/Standard/QueryParserUtil.cs
----------------------------------------------------------------------
diff --git a/src/contrib/QueryParsers/Flexible/Standard/QueryParserUtil.cs b/src/contrib/QueryParsers/Flexible/Standard/QueryParserUtil.cs
new file mode 100644
index 0000000..1ef8d9d
--- /dev/null
+++ b/src/contrib/QueryParsers/Flexible/Standard/QueryParserUtil.cs
@@ -0,0 +1,100 @@
+using Lucene.Net.Analysis;
+using Lucene.Net.Search;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Lucene.Net.QueryParsers.Flexible.Standard
+{
+    public static class QueryParserUtil
+    {
+        public static Query Parse(string[] queries, string[] fields, Analyzer analyzer)
+        {
+            if (queries.Length != fields.Length)
+                throw new ArgumentException("queries.length != fields.length");
+            BooleanQuery bQuery = new BooleanQuery();
+
+            StandardQueryParser qp = new StandardQueryParser();
+            qp.Analyzer = analyzer;
+
+            for (int i = 0; i < fields.Length; i++)
+            {
+                Query q = qp.Parse(queries[i], fields[i]);
+
+                if (q != null && // q never null, just being defensive
+                    (!(q is BooleanQuery) || ((BooleanQuery)q).Clauses.Length > 0))
+                {
+                    bQuery.Add(q, Occur.SHOULD);
+                }
+            }
+            return bQuery;
+        }
+
+        public static Query Parse(string query, string[] fields, Occur[] flags, Analyzer
analyzer)
+        {
+            if (fields.Length != flags.Length)
+                throw new ArgumentException("fields.length != flags.length");
+            BooleanQuery bQuery = new BooleanQuery();
+
+            StandardQueryParser qp = new StandardQueryParser();
+            qp.Analyzer = analyzer;
+
+            for (int i = 0; i < fields.Length; i++)
+            {
+                Query q = qp.Parse(query, fields[i]);
+
+                if (q != null && // q never null, just being defensive
+                    (!(q is BooleanQuery) || ((BooleanQuery)q).Clauses.Length > 0))
+                {
+                    bQuery.Add(q, flags[i]);
+                }
+            }
+            return bQuery;
+        }
+
+        public static Query Parse(string[] queries, string[] fields, Occur[] flags, Analyzer
analyzer)
+        {
+            if (!(queries.Length == fields.Length && queries.Length == flags.Length))
+                throw new ArgumentException(
+                    "queries, fields, and flags array have have different length");
+            BooleanQuery bQuery = new BooleanQuery();
+
+            StandardQueryParser qp = new StandardQueryParser();
+            qp.Analyzer = analyzer;
+
+            for (int i = 0; i < fields.Length; i++)
+            {
+                Query q = qp.Parse(queries[i], fields[i]);
+
+                if (q != null && // q never null, just being defensive
+                    (!(q is BooleanQuery) || ((BooleanQuery)q).Clauses.Length > 0))
+                {
+                    bQuery.Add(q, flags[i]);
+                }
+            }
+            return bQuery;
+        }
+
+        public static string Escape(string s)
+        {
+            StringBuilder sb = new StringBuilder();
+            for (int i = 0; i < s.Length; i++)
+            {
+                char c = s[i];
+                // These characters are part of the query syntax and must be escaped
+                if (c == '\\' || c == '+' || c == '-' || c == '!' || c == '(' || c == ')'
+                    || c == ':' || c == '^' || c == '[' || c == ']' || c == '\"'
+                    || c == '{' || c == '}' || c == '~' || c == '*' || c == '?'
+                    || c == '|' || c == '&')
+                {
+                    sb.Append('\\');
+                }
+                sb.Append(c);
+            }
+            return sb.ToString();
+        }
+
+    }
+}

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/acd41216/src/contrib/QueryParsers/Flexible/Standard/StandardQueryParser.cs
----------------------------------------------------------------------
diff --git a/src/contrib/QueryParsers/Flexible/Standard/StandardQueryParser.cs b/src/contrib/QueryParsers/Flexible/Standard/StandardQueryParser.cs
new file mode 100644
index 0000000..6fdfd3e
--- /dev/null
+++ b/src/contrib/QueryParsers/Flexible/Standard/StandardQueryParser.cs
@@ -0,0 +1,12 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Lucene.Net.QueryParsers.Flexible.Standard
+{
+    class StandardQueryParser
+    {
+    }
+}


Mime
View raw message