当前位置: 首页>>代码示例>>C#>>正文


C# QueryBuilder.AddBooleanClause方法代码示例

本文整理汇总了C#中QueryBuilder.AddBooleanClause方法的典型用法代码示例。如果您正苦于以下问题:C# QueryBuilder.AddBooleanClause方法的具体用法?C# QueryBuilder.AddBooleanClause怎么用?C# QueryBuilder.AddBooleanClause使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在QueryBuilder的用法示例。


在下文中一共展示了QueryBuilder.AddBooleanClause方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: CreateBuilderFromExpression

        private static QueryBuilder CreateBuilderFromExpression(string expression, out ClauseOccurrence occurrence)
        {
            // FirstName# = 'Tim
            // +(LastName+ = 'James', Type+ = '0')

            occurrence = ClauseOccurrence.Default;
            if (string.IsNullOrEmpty(expression))
                return new QueryBuilder();
            QueryBuilder builder = new QueryBuilder();

            if (expression.StartsWith("+")) {
                occurrence = ClauseOccurrence.MustOccur;
                expression = expression.Substring(1);
            }
            else if (expression.StartsWith("#")) {
                occurrence = ClauseOccurrence.ShouldOccur;
                expression = expression.Substring(1);
            }
            else if (expression.StartsWith("-")) {
                occurrence = ClauseOccurrence.MustNotOccur;
                expression = expression.Substring(1);
            }

            if (expression.StartsWith("("))
                expression = expression.Substring(1);
            if (expression.EndsWith(")"))
                expression = expression.Substring(0, expression.Length - 1);

            string[] parts = expression.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (var part in parts) {
                string searchTerm;
                string boolOperator;
                string searchValue;
                ClauseOccurrence co = ClauseOccurrence.Default;

                string[] words = part.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                if (words.Length < 3)
                    throw new ArgumentNullException("expression", "Malformed expression");
                searchTerm = words[0];
                boolOperator = words[1];
                searchValue = string.Join(" ", words, 2, words.Length - 2).Replace("\'", "");
                if (searchTerm.EndsWith("+")) {
                    co = ClauseOccurrence.MustOccur;
                    searchTerm = searchTerm.Substring(0, searchTerm.Length - 1);
                }
                else if (searchTerm.EndsWith("#")) {
                    co = ClauseOccurrence.ShouldOccur;
                    searchTerm = searchTerm.Substring(0, searchTerm.Length - 1);
                }
                else if (searchTerm.EndsWith("-")) {
                    co = ClauseOccurrence.MustNotOccur;
                    searchTerm = searchTerm.Substring(0, searchTerm.Length - 1);
                }

                if (boolOperator.Equals("=")) {
                    builder.AddBooleanClause(new SearchTerm(searchTerm, searchValue, StaticValues.DefaultBoost, true), co);
                }
                else if (boolOperator.Equals(">")) {
                    throw new NotSupportedException("Boolean operator not yet supported");
                }
                else if (boolOperator.Equals(">=")) {
                    throw new NotSupportedException("Boolean operator not yet supported");
                }
                else if (boolOperator.Equals("<")) {
                    throw new NotSupportedException("Boolean operator not yet supported");
                }
                else if (boolOperator.Equals("<=")) {
                    throw new NotSupportedException("Boolean operator not yet supported");
                }
                else if (boolOperator.Equals("!=") || boolOperator.Equals("<>")) {
                    if (co == ClauseOccurrence.MustNotOccur)
                        co = ClauseOccurrence.MustOccur;
                    else if (co == ClauseOccurrence.MustOccur)
                        co = ClauseOccurrence.MustNotOccur;
                    builder.AddBooleanClause(new SearchTerm(searchTerm, searchValue, StaticValues.DefaultBoost, true), co);
                }
                else {
                    throw new ArgumentException("Malformed boolean operator in expression");
                }
            }

            return builder;
        }
开发者ID:DigenGada,项目名称:lucene-dotnet-api,代码行数:83,代码来源:WhereExpression.cs


注:本文中的QueryBuilder.AddBooleanClause方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。