public LogQueryResults Search(LogQuery query) { if (query.DateTime_From == DateTime.MinValue || query.DateTime_To == DateTime.MinValue) { throw new ArgumentException("DateTime_From or DateTime_To Not Set"); } var qryHeader = new BooleanQuery(); if (!string.IsNullOrWhiteSpace(query.Text)) { var textBooleanQuery = new BooleanQuery(); for (int i = 0; i < textQueryFields.Length; i++) { var termQry = new TermQuery(new Term(textQueryFields[i], query.Text)); textBooleanQuery.Add(termQry, BooleanClause.Occur.SHOULD); } qryHeader.Add(textBooleanQuery, BooleanClause.Occur.MUST); } if (query.ClientIP != null) { var ipStr = NAppIndexUpdater.ConvertIPToString(query.ClientIP); var termQry = new TermQuery(new Term(FieldKeys.ClientIP, ipStr)); qryHeader.Add(termQry, BooleanClause.Occur.MUST); } if (query.ServerIP != null) { var ipStr = NAppIndexUpdater.ConvertIPToString(query.ServerIP); var termQry = new TermQuery(new Term(FieldKeys.ServerIP, ipStr)); qryHeader.Add(termQry, BooleanClause.Occur.MUST); } if (query.ShowExceptions == LogQueryExceptions.ExceptionsOnly) { var termQry = new TermQuery(new Term(FieldKeys.Exception, "1")); qryHeader.Add(termQry, BooleanClause.Occur.MUST); } else if (query.ShowExceptions == LogQueryExceptions.SuccessesOnly) { var termQry = new TermQuery(new Term(FieldKeys.Exception, "1")); qryHeader.Add(termQry, BooleanClause.Occur.MUST); } var elapsedQuery = AddFromToQueryString(FieldKeys.Elapsed, query.TotalElapsed_From.Ticks, query.TotalElapsed_To.Ticks, TimeSpan.Zero.Ticks); if (elapsedQuery != null) { qryHeader.Add(elapsedQuery, BooleanClause.Occur.MUST); } var dtlQry = AddFromToQueryString(FieldKeys.Detail_Elapsed, query.DetailElapsed_From.Ticks, query.DetailElapsed_To.Ticks, TimeSpan.Zero.Ticks); if (dtlQry != null) { qryHeader.Add(dtlQry, BooleanClause.Occur.MUST); } var ret = new LogQueryResults() { DateTime_From = query.DateTime_From, DateTime_To = query.DateTime_To, }; if (qryHeader.Clauses().Count > 0) { ret.LogIDs = new List<LogQueryResultDetail>(); GetLogIDsFromMain(qryHeader, ret.DateTime_From, ret.DateTime_To, ret.LogIDs); } return ret; }
/// <summary> /// 构造一个And条件语句 /// </summary> /// <param name="expl">左边条件表达式</param> /// <param name="expr">右边条件表达式</param> /// <returns>条件语句</returns> public static Conditional And(ExpressionEx expl, ExpressionEx expr) { var conditional = new Conditional(); var booleanQuery = new BooleanQuery(); Query ql = Convert(expl); if (ql is BooleanQuery) { foreach (object variable in ((BooleanQuery)ql).Clauses()) { booleanQuery.Clauses().Add(variable); } } else { booleanQuery.Add(ql, BooleanClause.Occur.MUST); } Query qr = Convert(expr); if (qr is BooleanQuery) { foreach (object variable in ((BooleanQuery)qr).Clauses()) { booleanQuery.Clauses().Add(variable); } } else { booleanQuery.Add(qr, BooleanClause.Occur.MUST); } conditional._queryExpression = booleanQuery; return conditional; }
public virtual QueryFilter GenreQueryFilter(Query query, bool isFacet, bool isIdLookup, string termName, string terms) { var tempTerms = terms; var newGuid = new Guid(); var isGuid = Guid.TryParse(terms, out newGuid); if (isGuid) { tempTerms = IdHelper.NormalizeGuid(terms, true); } var genreQueryFilter = new QueryFilter(query); if (!isFacet) { if (termName == "_language" || isIdLookup) { var termValueParse = terms.Split('|')[0].ToLowerInvariant(); if (isIdLookup) { termValueParse = IdHelper.NormalizeGuid(termValueParse, true); } genreQueryFilter = new QueryFilter( new TermQuery(new Term(termName.ToLowerInvariant(), termValueParse))); } else if (termName == "size" || termName == "dimensions") { var term = new BooleanQuery(); term.Add(new TermQuery(new Term(termName, terms)), BooleanClause.Occur.MUST); genreQueryFilter = new QueryFilter(term); } else { genreQueryFilter = new QueryFilter( new TermQuery(new Term(terms.Split('|')[0].ToLowerInvariant(), termName.ToLowerInvariant()))); } } else { if (termName == "__created by") { genreQueryFilter = new QueryFilter(new TermQuery(new Term(termName, tempTerms))); } else { if (Config.ExcludeContextItemFromResult) { if (termName == "_path") { var term = new BooleanQuery(); term.Add(new TermQuery(new Term(termName, tempTerms.ToLowerInvariant())), BooleanClause.Occur.MUST); term.Add(new TermQuery(new Term(BuiltinFields.ID, tempTerms.ToLowerInvariant())), BooleanClause.Occur.MUST_NOT); genreQueryFilter = new QueryFilter(term); } else { var term = new TermQuery(new Term(termName, tempTerms.ToLowerInvariant())); genreQueryFilter = new QueryFilter(term); } } else { var term = new TermQuery(new Term(termName, tempTerms.ToLowerInvariant())); genreQueryFilter = new QueryFilter(term); } } } if (termName == "__smallCreatedDate") { var dateStart = terms.Split('|')[0]; var typeOfDate = terms.Split('|')[1]; var dateEnd = new DateTime(); if (typeOfDate == "Within a Day") { dateEnd = DateTime.Now; } if (typeOfDate == "Within a Week") { dateEnd = DateTime.Now.AddDays(-1); } if (typeOfDate == "Within a Month") { dateEnd = DateTime.Now.AddDays(-7); } if (typeOfDate == "Within a Year") { dateEnd = DateTime.Now.AddMonths(-1); } if (typeOfDate == "Older") { dateEnd = DateTime.Now.AddYears(-1); } var boolQuery = new BooleanQuery(true); SearcherMethods.AddDateRangeQuery(boolQuery, new DateRangeSearchParam.DateRangeField(termName, DateTime.Parse(dateStart), dateEnd) {InclusiveEnd = true, InclusiveStart = true}, BooleanClause.Occur.MUST); genreQueryFilter = new QueryFilter(boolQuery); if (Config.EnableBucketDebug || Constants.EnableTemporaryBucketDebug) { Log.Info("Search Clauses Number: " + boolQuery.Clauses().Count, this); } } return genreQueryFilter; }
// Random rnd is passed in so that the exact same random query may be created // more than once. public static BooleanQuery RandBoolQuery(System.Random rnd, int level, System.String field, System.String[] vals, TestBoolean2.Callback cb) { BooleanQuery current = new BooleanQuery(rnd.Next() < 0); for (int i = 0; i < rnd.Next(vals.Length) + 1; i++) { int qType = 0; // term query if (level > 0) { qType = rnd.Next(10); } Query q; if (qType < 7) q = new TermQuery(new Term(field, vals[rnd.Next(vals.Length)])); else q = RandBoolQuery(rnd, level - 1, field, vals, cb); int r = rnd.Next(10); BooleanClause.Occur occur; if (r < 2) occur = BooleanClause.Occur.MUST_NOT; else if (r < 5) occur = BooleanClause.Occur.MUST; else occur = BooleanClause.Occur.SHOULD; current.Add(q, occur); } if (cb != null) cb.PostCreate(current); ((System.Collections.ArrayList)current.Clauses()).TrimToSize(); return current; }