Example #1
0
        private TopDocs BooleanQuery(string keyword, string field)
        {
            string[] words = keyword.Trim().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

            TopDocs docs = null;
            int     n    = 10;//最多返回多少个结果

            SetOutput(string.Format("正在检索关键字:{0}", keyword));
            try
            {
                BooleanQuery boolQuery = new BooleanQuery();
                Term         beginT    = new Term("id", "3");
                Term         endT      = new Term("id", "5");
                RangeQuery   rq        = new RangeQuery(beginT, endT, true); //rangequery id从3到5
                for (int i = 0; i < words.Length; i++)
                {
                    TermQuery tq = new TermQuery(new Term(field, words[i]));//termquery
                    boolQuery.Add(tq, BooleanClause.Occur.MUST);
                }
                boolQuery.Add(rq, BooleanClause.Occur.MUST);

                Stopwatch watch = new Stopwatch();
                watch.Start();
                docs = searcher.Search(boolQuery, (Filter)null, n);
                watch.Stop();
                StringBuffer sb = "BooleanQuery搜索完成,共用时:" + watch.Elapsed.Hours + "时 " + watch.Elapsed.Minutes + "分 " + watch.Elapsed.Seconds + "秒 " + watch.Elapsed.Milliseconds + "毫秒";
                SetOutput(sb);
            }
            catch (Exception ex)
            {
                SetOutput(ex.Message);
                docs = null;
            }
            return(docs);
        }
Example #2
0
        /// <summary>
        /// RangeQuery
        /// </summary>
        /// <param name="field"></param>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <param name="isInclude">是否包含左边界</param>
        /// <returns></returns>
        private TopDocs RangeQuery(string field, string start, string end, bool isInclude)
        {
            TopDocs docs = null;
            int     n    = 10;//最多返回多少个结果

            SetOutput(string.Format("正在检索,id范围为{0}~{1}", start, end));
            try
            {
                Term      beginT = new Term(field, start);
                Term      endT   = new Term(field, end);
                Query     query  = new RangeQuery(beginT, endT, isInclude);
                Stopwatch watch  = new Stopwatch();
                watch.Start();
                docs = searcher.Search(query, (Filter)null, n);
                watch.Stop();
                StringBuffer sb = "RangeQuery搜索完成,共用时:" + watch.Elapsed.Hours + "时 " + watch.Elapsed.Minutes + "分 " + watch.Elapsed.Seconds + "秒 " + watch.Elapsed.Milliseconds + "毫秒";
                SetOutput(sb);
            }
            catch (Exception ex)
            {
                SetOutput(ex.Message);
                docs = null;
            }
            return(docs);
        }
Example #3
0
        public void Range_query_must_transform_correclty_to_ES()
        {
            var query = new RangeQuery("date", "now-1d", "now");

            Assert.IsTrue(query.Field == "date");
            Assert.IsTrue(query.Values.From.Value.ToString() == "now-1d");
            Assert.IsTrue(query.Values.To.Value.ToString() == "now");

            string json = query.ToString();
            var    jo   = JsonObject.Parse(json);
            var    q    = jo.ToQuery();

            Assert.IsTrue(q.Type == QueryType.RangeQuery);
            query = q as RangeQuery;
            Assert.IsTrue(query.Field == "date");
            Assert.IsTrue(query.Values.From.Value.ToString() == "now-1d");
            Assert.IsTrue(query.Values.To.Value.ToString() == "now");

            query = new RangeQuery("date", new RangeValues(new RangeValue("now-1d", false), null));
            Assert.IsTrue(query.Field == "date");
            Assert.IsTrue(query.Values.From.Value.ToString() == "now-1d");
            Assert.IsFalse(query.Values.From.Include);

            json = query.ToString();
            jo   = JsonObject.Parse(json);
            q    = jo.ToQuery();
            Assert.IsTrue(q.Type == QueryType.RangeQuery);
            query = q as RangeQuery;
            Assert.IsTrue(query.Field == "date");
            Assert.IsTrue(query.Values.From.Value.ToString() == "now-1d");
            Assert.IsFalse(query.Values.From.Include);
        }
        protected void AddNumericRangeQuery(BooleanQuery query, NumericRangeField range, BooleanClause.Occur occurance)
        {
            var startTerm  = new Term(range.FieldName, NumberTools.LongToString(range.Start));
            var endTerm    = new Term(range.FieldName, NumberTools.LongToString(range.End));
            var rangeQuery = new RangeQuery(startTerm, endTerm, true);

            query.Add(rangeQuery, occurance);
        }
 public FlightTrackerClient InRangeOf(double lat, double lng, int distnaceInKM)
 {
     range = new RangeQuery(lat, lng, distnaceInKM);
     queryParams.Add("lat", lat);
     queryParams.Add("lng", lng);
     queryParams.Add("distance", distnaceInKM);
     return(this);
 }
Example #6
0
        protected void AddNumericRangeQuery(BooleanQuery query, NumericRangeSearchParam.NumericRangeField range, BooleanClause.Occur occurance)
        {
            var startTerm  = new Term(range.FieldName, SearchHelper.FormatNumber(range.Start));
            var endTerm    = new Term(range.FieldName, SearchHelper.FormatNumber(range.End));
            var rangeQuery = new RangeQuery(startTerm, endTerm, true);

            query.Add(rangeQuery, occurance);
        }
Example #7
0
        public List <Hit> GetEmailResults(StringDictionary queryDic)
        {
            try
            {
                List <Hit> results = new List <Hit>();

                IndexSearcher ids = new IndexSearcher(GlobalData.EmailIndexPath);
                //Iterate over all the properties involved in the query
                foreach (string property in queryDic.Keys)
                {
                    Query q = null;

                    #region Based on the property to be indexed, Create the necessary Analyser
                    switch (property)
                    {
                    case "body":
                    {
                        q = new QueryParser(property, new KeywordAnalyzer()).Parse(queryDic[property]);
                    }
                    break;

                    case "date":
                    {
                        int    idx;
                        string from, to;
                        idx  = queryDic[property].IndexOf(' ');
                        from = queryDic[property].Substring(0, idx);
                        to   = queryDic[property].Substring(idx + 1);
                        q    = new RangeQuery(new Term(property, from), new Term(property, to), true);
                    }
                    break;

                    default:
                        if (queryDic[property].Contains("*") || queryDic[property].Contains("?"))
                        {
                            q = new WildcardQuery(new Term(property, queryDic[property]));
                        }
                        else
                        {
                            //  PerFieldAnalyzerWrapper pfaw = new PerFieldAnalyzerWrapper();
                            q = new QueryParser(property, new KeywordAnalyzer()).Parse(queryDic[property]);
                        }
                        break;
                    }
                    #endregion

                    //Get the results
                    Hits hits = ids.Search(q);
                    foreach (Hit hit in hits)
                    {
                        results.Add(hit); //Add the results to a List
                    }
                }
                // ids.Close();
                return(results);//Return the list
            }
            catch { return(null); }
        }
        public override int GetHashCode()
        {
            int hashCode = 1871698487;

            if (SortedAttributeQuery != null)
            {
                hashCode += SortedAttributeQuery.GetHashCode();
            }

            if (ExactQuery != null)
            {
                hashCode += ExactQuery.GetHashCode();
            }

            if (SetQuery != null)
            {
                hashCode += SetQuery.GetHashCode();
            }

            if (PrefixQuery != null)
            {
                hashCode += PrefixQuery.GetHashCode();
            }

            if (RangeQuery != null)
            {
                hashCode += RangeQuery.GetHashCode();
            }

            if (TextQuery != null)
            {
                hashCode += TextQuery.GetHashCode();
            }

            if (ItemsForTaxQuery != null)
            {
                hashCode += ItemsForTaxQuery.GetHashCode();
            }

            if (ItemsForModifierListQuery != null)
            {
                hashCode += ItemsForModifierListQuery.GetHashCode();
            }

            if (ItemsForItemOptionsQuery != null)
            {
                hashCode += ItemsForItemOptionsQuery.GetHashCode();
            }

            if (ItemVariationsForItemOptionValuesQuery != null)
            {
                hashCode += ItemVariationsForItemOptionValuesQuery.GetHashCode();
            }

            return(hashCode);
        }
 public List <string> GetRecent()
 {
     using (var searcher = new IndexSearcher(Constants.Index.Name))
     {
         var query = new RangeQuery(new Term("__smallUpdatedDate", DateTime.Now.AddDays(-1).ToString("yyyyMMdd")), new Term("__smallUpdatedDate", DateTime.Now.ToString("yyyyMMdd")), true);
         var ret   = searcher.RunQuery(query, 10, 0).Value.Select(i => i.GetItem().Name + "|" + i.GetItem().ID.ToString()).ToList();
         ret.Reverse();
         return(ret.Take(10).ToList());
     }
 }
 public List <string> Process()
 {
     using (var searcher = new IndexSearcher(Constants.Index.Name))
     {
         var query = new RangeQuery(new Term(SearchFieldIDs.UpdatedDate, DateTime.Now.AddDays(-1).ToString("yyyyMMdd")), new Term(SearchFieldIDs.UpdatedDate, DateTime.Now.AddDays(1).ToString("yyyyMMdd")), true);
         var ret   = searcher.RunQuery(query, 10, 1, SearchFieldIDs.UpdatedDate, "desc").Value.Where(item => item.GetItem().IsNotNull()).Select(i => i.GetItem().Name + "|" + i.GetItem().ID.ToString()).ToList();
         return(ret.Any() ? ret : new List <string> {
             "There have been no items recently created within the last day"
         });
     }
 }
        public SearchQueryContext <TFieldNames> Between(Expression <Func <TFieldNames, string> > searchFilterExpression, DateTime startDate, DateTime endDate)
        {
            string searchFieldName = ((ConstantExpression)(searchFilterExpression.Body)).Value.ToString();

            var queryDefinition = new RangeQuery(new Term(searchFieldName, string.Format(SearchIndexer.DateTimeIndexingFormat, startDate)), new Term(searchFieldName, string.Format(SearchIndexer.DateTimeIndexingFormat, endDate)), true);

            IsUsingDefaultQueryDefinition = false;
            MergeQueryDefinitions(queryDefinition);

            return(this);
        }
 protected void ToString(List <string> toStringOutput)
 {
     toStringOutput.Add($"SortedAttributeQuery = {(SortedAttributeQuery == null ? "null" : SortedAttributeQuery.ToString())}");
     toStringOutput.Add($"ExactQuery = {(ExactQuery == null ? "null" : ExactQuery.ToString())}");
     toStringOutput.Add($"SetQuery = {(SetQuery == null ? "null" : SetQuery.ToString())}");
     toStringOutput.Add($"PrefixQuery = {(PrefixQuery == null ? "null" : PrefixQuery.ToString())}");
     toStringOutput.Add($"RangeQuery = {(RangeQuery == null ? "null" : RangeQuery.ToString())}");
     toStringOutput.Add($"TextQuery = {(TextQuery == null ? "null" : TextQuery.ToString())}");
     toStringOutput.Add($"ItemsForTaxQuery = {(ItemsForTaxQuery == null ? "null" : ItemsForTaxQuery.ToString())}");
     toStringOutput.Add($"ItemsForModifierListQuery = {(ItemsForModifierListQuery == null ? "null" : ItemsForModifierListQuery.ToString())}");
     toStringOutput.Add($"ItemsForItemOptionsQuery = {(ItemsForItemOptionsQuery == null ? "null" : ItemsForItemOptionsQuery.ToString())}");
     toStringOutput.Add($"ItemVariationsForItemOptionValuesQuery = {(ItemVariationsForItemOptionValuesQuery == null ? "null" : ItemVariationsForItemOptionValuesQuery.ToString())}");
 }
        public static IQueryExpression FilterByDateRange(this IQueryExpression expression, string fieldName, DateTime startDate, DateTime endDate, bool includeEmptyField = false)
        {
            var strStartDate = DateTools.DateToString(startDate, DateTools.Resolution.SECOND);
            var strEndDate   = DateTools.DateToString(endDate, DateTools.Resolution.SECOND);
            var rangeQuery   = new RangeQuery(strStartDate, strEndDate, fieldName, true);

            if (includeEmptyField)
            {
                var groupQuery = new GroupQuery(LuceneOperator.NOT);
                groupQuery.QueryExpressions.Add(new AllQuery());
                groupQuery.QueryExpressions.Add(new FieldQuery(fieldName, @"[* TO *]"));
                return(expression.And(rangeQuery.Or(groupQuery)));
            }
            return(expression.And(rangeQuery));
        }
Example #14
0
 public static RangeQuery BuildRangeQuery(Aliyun.TableStore.DataModel.Search.Query.RangeQuery query)
 {
     RangeQuery.Builder builder = RangeQuery.CreateBuilder();
     builder.SetFieldName(query.FieldName);
     if (query.From != null)
     {
         builder.SetRangeFrom(ByteString.CopyFrom(SearchVariantType.toVariant(query.From)));
         builder.SetIncludeLower(query.IncludeLower);
     }
     if (query.To != null)
     {
         builder.SetRangeTo(ByteString.CopyFrom(SearchVariantType.toVariant(query.To)));
         builder.SetIncludeUpper(query.IncludeUpper);
     }
     return(builder.Build());
 }
        /// <summary>
        /// 范围查询。通过设置一个范围(from,to),查询该范围内的所有数据。
        /// </summary>
        /// <param name="otsClient"></param>
        public static void RangeQuery(OTSClient otsClient)
        {
            Console.WriteLine("\n Start range query...");

            var searchQuery = new SearchQuery();

            searchQuery.GetTotalCount = true;
            var rangeQuery = new RangeQuery(Long_type_col, new ColumnValue(0), new ColumnValue(6));

            //包括下边界(大于等于0)
            rangeQuery.IncludeLower = true;
            searchQuery.Query       = rangeQuery;
            var request  = new SearchRequest(TableName, IndexName, searchQuery);
            var response = otsClient.Search(request);

            Console.WriteLine("Total Count:" + response.TotalCount);
            foreach (var row in response.Rows)
            {
                PrintRow(row);
            }
        }
Example #16
0
        protected void AddDateRangeQuery(BooleanQuery query, DateRangeSearchParam.DateRangeField dateRangeField, BooleanClause.Occur occurance)
        {
            var startDateTime = dateRangeField.StartDate;

            if (dateRangeField.InclusiveStart)
            {
                startDateTime = startDateTime.AddDays(1);
            }
            var startDate = startDateTime.ToString(IndexConstants.DateTimeFormat);

            var endDateTime = dateRangeField.EndDate;

            if (dateRangeField.InclusiveStart)
            {
                endDateTime = endDateTime.AddDays(1);
            }
            var endDate = endDateTime.ToString(IndexConstants.DateTimeFormat);

            var rangeQuery = new RangeQuery(new Term(dateRangeField.FieldName, startDate), new Term(dateRangeField.FieldName, endDate), true);

            query.Add(rangeQuery, occurance);
        }
Example #17
0
        static void TestRt()
        {
            string wiffPath = @"C:\Work\primaqdev\testdata\swathtest\20141201_BASF5_1SW01.wiff";
            string runID    = "sample=0";

            using (var reader = new WiffFileReader(wiffPath))
                using (ITransactionScope txn = reader.BeginTransaction())
                {
                    var rti       = reader.BuildRtIndex(runID);
                    var rtQuery   = new RangeQuery(56.8867263793945, 10.0);
                    var mzQueries = new RangeQuery[]
                    {
                        new RangeQuery(890.4764, 0.01),
                        new RangeQuery(677.3651, 0.01),
                        new RangeQuery(989.5448, 0.01),
                        new RangeQuery(776.4335, 0.01)
                    };

                    var profile = reader.RtProfiles(rti, rtQuery, mzQueries);
                    var profil  = reader.RtProfile(rti, rtQuery, new RangeQuery(776.4335, 0.01));
                }
        }
Example #18
0
        protected void AddDateRangeQuery(BooleanQuery query, DateRange dateRangeField, BooleanClause.Occur occurance)
        {
            var startDateTime = dateRangeField.StartDate;
            var endDateTime   = dateRangeField.EndDate;

            if (dateRangeField.InclusiveStart && startDateTime > DateTime.MinValue.AddDays(1))
            {
                startDateTime = startDateTime.AddDays(-1);
            }

            if (dateRangeField.InclusiveEnd && endDateTime < DateTime.MaxValue.AddDays(-1))
            {
                endDateTime = endDateTime.AddDays(1);
            }

            // converting to lucene format
            var startDate = DateTools.DateToString(startDateTime, DateTools.Resolution.DAY);
            var endDate   = DateTools.DateToString(endDateTime, DateTools.Resolution.DAY);

            var rangeQuery = new RangeQuery(new Term(dateRangeField.FieldName, startDate), new Term(dateRangeField.FieldName, endDate), true);

            query.Add(rangeQuery, occurance);
        }
Example #19
0
        protected void Page_Load(object sender, EventArgs e)
        {
            //定义盘古分词的xml引用路径
            PanGu.Segment.Init(PanGuXmlPath);
            switch (Action)
            {
            case "CreateIndex": CreateIndex(Cover); break;

            case "SearchIndex": SearchIndex(); break;
            }
            //Term t = new Term("content", "刘备");
            //Query query = new TermQuery(t);

            //TermQuery termQuery1 = new TermQuery(new Term("content", "刘备"));
            //TermQuery termQuery2 = new TermQuery(new Term("title", "三国"));
            //BooleanQuery booleanQuery = new BooleanQuery();
            //booleanQuery.Add(termQuery1, BooleanClause.Occur.SHOULD);
            //booleanQuery.Add(termQuery2, BooleanClause.Occur.SHOULD);

            //Query query = new WildcardQuery(new Term("content", "三国*"));

            RangeQuery query = new RangeQuery(new Term("time", "20060101"), new Term("time", "20060130"), true);
        }
        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return(false);
            }

            if (obj == this)
            {
                return(true);
            }

            return(obj is CatalogQuery other &&
                   ((SortedAttributeQuery == null && other.SortedAttributeQuery == null) || (SortedAttributeQuery?.Equals(other.SortedAttributeQuery) == true)) &&
                   ((ExactQuery == null && other.ExactQuery == null) || (ExactQuery?.Equals(other.ExactQuery) == true)) &&
                   ((SetQuery == null && other.SetQuery == null) || (SetQuery?.Equals(other.SetQuery) == true)) &&
                   ((PrefixQuery == null && other.PrefixQuery == null) || (PrefixQuery?.Equals(other.PrefixQuery) == true)) &&
                   ((RangeQuery == null && other.RangeQuery == null) || (RangeQuery?.Equals(other.RangeQuery) == true)) &&
                   ((TextQuery == null && other.TextQuery == null) || (TextQuery?.Equals(other.TextQuery) == true)) &&
                   ((ItemsForTaxQuery == null && other.ItemsForTaxQuery == null) || (ItemsForTaxQuery?.Equals(other.ItemsForTaxQuery) == true)) &&
                   ((ItemsForModifierListQuery == null && other.ItemsForModifierListQuery == null) || (ItemsForModifierListQuery?.Equals(other.ItemsForModifierListQuery) == true)) &&
                   ((ItemsForItemOptionsQuery == null && other.ItemsForItemOptionsQuery == null) || (ItemsForItemOptionsQuery?.Equals(other.ItemsForItemOptionsQuery) == true)) &&
                   ((ItemVariationsForItemOptionValuesQuery == null && other.ItemVariationsForItemOptionValuesQuery == null) || (ItemVariationsForItemOptionValuesQuery?.Equals(other.ItemVariationsForItemOptionValuesQuery) == true)));
        }
Example #21
0
        /// <summary>
        /// Adds <see cref="RangeQuery{T}"/> to query.
        /// Only <see cref="int"/>, <see cref="double"/> and <see cref="DateTime"/> are supported.
        /// </summary>
        /// <param name="wrapper">Query wrapper.</param>
        /// <param name="query">Query object.</param>
        /// <param name="path">Expression path to querying field.</param>
        public static QueryWrapper <T> Range <T, TField>(this QueryWrapper <T> wrapper, RangeQuery <TField> query, Expression <Func <T, object> > path) where T : class where TField : struct
        {
            if (query == null || !query.IsSpecified)
            {
                return(wrapper);
            }

            return(wrapper.AddQuery(descriptor => query switch
            {
                RangeQuery <DateTime> r => descriptor.DateRange(q =>
                {
                    q = q.Field(path);

                    if (r.Minimum != null)
                    {
                        q = r.Exclusive ? q.GreaterThan(r.Minimum.Value) : q.GreaterThanOrEquals(r.Minimum.Value);
                    }

                    if (r.Maximum != null)
                    {
                        q = r.Exclusive ? q.LessThan(r.Maximum.Value) : q.LessThanOrEquals(r.Maximum.Value);
                    }

                    return q;
                }),

                RangeQuery <int> r => descriptor.Range(q =>
                {
                    q = q.Field(path);

                    if (r.Minimum != null)
                    {
                        q = r.Exclusive ? q.GreaterThan(r.Minimum.Value) : q.GreaterThanOrEquals(r.Minimum.Value);
                    }

                    if (r.Maximum != null)
                    {
                        q = r.Exclusive ? q.LessThan(r.Maximum.Value) : q.LessThanOrEquals(r.Maximum.Value);
                    }

                    return q;
                }),

                RangeQuery <double> r => descriptor.Range(q =>
                {
                    q = q.Field(path);

                    if (r.Minimum != null)
                    {
                        q = r.Exclusive ? q.GreaterThan(r.Minimum.Value) : q.GreaterThanOrEquals(r.Minimum.Value);
                    }

                    if (r.Maximum != null)
                    {
                        q = r.Exclusive ? q.LessThan(r.Maximum.Value) : q.LessThanOrEquals(r.Maximum.Value);
                    }

                    return q;
                }),

                _ => throw new NotSupportedException($"Unsupported range query type {typeof(T)}.")
            }));
Example #22
0
        /// <summary>
        /// search support multiple modules
        /// </summary>
        /// <param name="siteId"></param>
        /// <param name="isAdmin"></param>
        /// <param name="userRoles"></param>
        /// <param name="queryText"></param>
        /// <param name="highlightResults"></param>
        /// <param name="highlightedFragmentSize"></param>
        /// <param name="pageNumber"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalHits"></param>
        /// <param name="invalidQuery"></param>
        /// <param name="moduleIDs"></param>
        /// <returns></returns>
        public static IndexItemCollection Search(
            int siteId,
            bool isAdmin,
            List <string> userRoles,
            string queryText,
            bool highlightResults,
            int highlightedFragmentSize,
            int pageNumber,
            int pageSize,
            out int totalHits,
            out bool invalidQuery,
            params Guid[] moduleIDs
            )
        {
            invalidQuery = false;
            totalHits    = 0;
            string indexPath            = GetIndexPath(siteId);
            IndexItemCollection results = new IndexItemCollection();

            if (string.IsNullOrEmpty(queryText))
            {
                return(results);
            }

            bool useBackwardCompatibilityMode = true;

            if (
                (ConfigurationManager.AppSettings["SearchUseBackwardCompatibilityMode"] != null) &&
                (ConfigurationManager.AppSettings["SearchUseBackwardCompatibilityMode"] == "false")
                )
            {
                useBackwardCompatibilityMode = false;
            }

            bool IncludeModuleRoleFilters = false;

            if (
                (ConfigurationManager.AppSettings["SearchIncludeModuleRoleFilters"] != null) &&
                (ConfigurationManager.AppSettings["SearchIncludeModuleRoleFilters"] == "true")
                )
            {
                IncludeModuleRoleFilters = true;
            }


            if (IndexReader.IndexExists(indexPath))
            {
                if (log.IsDebugEnabled)
                {
                    log.Debug("Entered Search, indexPath = " + indexPath);
                }

                long startTicks = DateTime.Now.Ticks;

                try
                {
                    BooleanQuery mainQuery = new BooleanQuery();

                    if ((!isAdmin) && (!useBackwardCompatibilityMode))
                    {
                        AddRoleQueries(userRoles, mainQuery);
                    }

                    if ((!isAdmin) && (IncludeModuleRoleFilters))
                    {
                        AddModuleRoleQueries(userRoles, mainQuery);
                    }


                    Query multiQuery = MultiFieldQueryParser.Parse(
                        new string[] { queryText, queryText, queryText, queryText, queryText, queryText.Replace("*", string.Empty) },
                        new string[] { "Title", "ModuleTitle", "contents", "PageName", "PageMetaDesc", "Keyword" },
                        new StandardAnalyzer());

                    mainQuery.Add(multiQuery, BooleanClause.Occur.MUST);


                    if (!useBackwardCompatibilityMode)
                    {
                        Term       beginDateStart = new Term("PublishBeginDate", DateTime.MinValue.ToString("s"));
                        Term       beginDateEnd   = new Term("PublishBeginDate", DateTime.UtcNow.ToString("s"));
                        RangeQuery beginDateQuery = new RangeQuery(beginDateStart, beginDateEnd, true);
                        mainQuery.Add(beginDateQuery, BooleanClause.Occur.MUST);

                        Term       endDateStart = new Term("PublishEndDate", DateTime.UtcNow.ToString("s"));
                        Term       endDateEnd   = new Term("PublishEndDate", DateTime.MaxValue.ToString("s"));
                        RangeQuery endDateQuery = new RangeQuery(endDateStart, endDateEnd, true);
                        mainQuery.Add(endDateQuery, BooleanClause.Occur.MUST);
                    }

                    if (moduleIDs != null && moduleIDs.Length > 0)
                    {
                        BooleanQuery featureFilter = new BooleanQuery();
                        moduleIDs.ToList().ForEach(x => {
                            if (x != Guid.Empty)
                            {
                                featureFilter.Add(new TermQuery(new Term("FeatureId", x.ToString())), BooleanClause.Occur.SHOULD);
                            }
                        });
                        if (featureFilter.Clauses().Count > 0)
                        {
                            mainQuery.Add(featureFilter, BooleanClause.Occur.MUST);
                        }
                    }


                    IndexSearcher searcher = new IndexSearcher(indexPath);
                    // a 0 based colection
                    Hits hits = searcher.Search(mainQuery);

                    int startHit = 0;

                    if (pageNumber > 1)
                    {
                        startHit = ((pageNumber - 1) * pageSize);
                    }


                    totalHits = hits.Length();
                    int end = startHit + pageSize;
                    if (totalHits <= end)
                    {
                        end = totalHits;
                    }
                    int itemsAdded = 0;
                    int itemsToAdd = end;

                    // in backward compatibility mode if multiple pages of results are found we amy not be showing every user the correct
                    // number of hits they can see as we only filter out the current page
                    //we may decrement total hits if filtering results so keep the original count
                    int actualHits = totalHits;

                    if (!useBackwardCompatibilityMode)
                    {
                        // this new way is much cleaner
                        //all filtering is done by query so the hitcount is true
                        //whereas with the old way it could be wrong since there
                        // were possibly results filtered out after the query returned.

                        QueryScorer scorer      = new QueryScorer(multiQuery);
                        Formatter   formatter   = new SimpleHTMLFormatter("<span class='searchterm'>", "</span>");
                        Highlighter highlighter = new Highlighter(formatter, scorer);
                        highlighter.SetTextFragmenter(new SimpleFragmenter(highlightedFragmentSize));


                        for (int i = startHit; i < itemsToAdd; i++)
                        {
                            IndexItem indexItem = new IndexItem(hits.Doc(i), hits.Score(i));

                            if (highlightResults)
                            {
                                try
                                {
                                    TokenStream stream = new StandardAnalyzer().TokenStream("contents", new StringReader(hits.Doc(i).Get("contents")));

                                    string highlightedResult = highlighter.GetBestFragment(stream, hits.Doc(i).Get("contents"));
                                    if (highlightedResult != null)
                                    {
                                        indexItem.Intro = highlightedResult;
                                    }
                                }
                                catch (NullReferenceException) { }
                            }

                            results.Add(indexItem);
                            itemsAdded += 1;
                        }
                    }
                    else
                    {
                        //backward compatible with old indexes
                        int filteredItems = 0;
                        for (int i = startHit; i < itemsToAdd; i++)
                        {
                            bool needToDecrementTotalHits = false;
                            if (
                                (isAdmin) ||
                                (WebUser.IsContentAdmin) ||
                                (WebUser.IsInRoles(hits.Doc(i).Get("ViewRoles")))
                                )
                            {
                                IndexItem indexItem = new IndexItem(hits.Doc(i), hits.Score(i));

                                if (
                                    (DateTime.UtcNow > indexItem.PublishBeginDate) &&
                                    (DateTime.UtcNow < indexItem.PublishEndDate)
                                    )
                                {
                                    results.Add(indexItem);
                                }
                                else
                                {
                                    needToDecrementTotalHits = true;
                                }
                            }
                            else
                            {
                                needToDecrementTotalHits = true;
                            }

                            //filtered out a result so need to decrement
                            if (needToDecrementTotalHits)
                            {
                                filteredItems += 1;
                                totalHits     -= 1;

                                //we also are not getting as many results as the page size so if there are more items
                                //we should increment itemsToAdd
                                if ((itemsAdded + filteredItems) < actualHits)
                                {
                                    itemsToAdd += 1;
                                }
                            }
                        }
                    }



                    searcher.Close();

                    results.ItemCount = itemsAdded;
                    results.PageIndex = pageNumber;

                    results.ExecutionTime = DateTime.Now.Ticks - startTicks;
                }
                catch (ParseException ex)
                {
                    invalidQuery = true;
                    log.Error("handled error for search terms " + queryText, ex);
                    // these parser exceptions are generally caused by
                    // spambots posting too much junk into the search form
                    // heres an option to automatically ban the ip address
                    HandleSpam(queryText, ex);


                    return(results);
                }
                catch (BooleanQuery.TooManyClauses ex)
                {
                    invalidQuery = true;
                    log.Error("handled error for search terms " + queryText, ex);
                    return(results);
                }
            }

            return(results);
        }
Example #23
0
 public SpanMultiQuery(RangeQuery query)
 {
     _query = query;
 }
Example #24
0
 public override Query VisitRangeQuery(RangeQuery rangeq)
 {
     throw new NotImplementedException();
 }
Example #25
0
		public SpanMultiQuery(RangeQuery query)
		{
			_query = query;
		}
Example #26
0
        public List <Hit> GetDocResults(StringDictionary queryDic)
        {
            List <Hit> results = new List <Hit>();

            //Iterate over all the directories present in the %appdata%\EDS\indexes folder
            foreach (string dir in Directory.GetDirectories(GlobalData.IndexRootPath))
            {
                IndexSearcher ids = new IndexSearcher(dir);
                //Iterate over all the properties involved in the query
                foreach (string property in queryDic.Keys)
                {
                    Query q = null;

                    #region Based on the property to be indexed, Create the necessary Analyser
                    switch (property)
                    {
                    case "content":
                    {
                        string value = queryDic["content"].
                                       Replace(@"\", @"\\").Replace("+", @"\+").Replace("-", @"\-").Replace("&", @"\&").
                                       Replace("|", @"\|").Replace("!", @"\!").Replace("(", @"\(").Replace(")", @"\)").
                                       Replace("{", @"\{").Replace("}", @"\}").Replace("[", @"\[").Replace("]", @"\]").
                                       Replace("^", @"\^").Replace("~", @"\~").Replace("*", @"\*").Replace("?", @"\?").Replace(":", @"\:");
                        q = new QueryParser(property, new KeywordAnalyzer()).Parse(value);
                    }
                    break;

                    case "size":
                    {
                        int    idx;
                        string from, to;
                        idx  = queryDic[property].IndexOf(' ');
                        from = queryDic[property].Substring(0, idx);
                        to   = queryDic[property].Substring(idx + 1);
                        q    = new RangeQuery(new Term(property, from.PadLeft(12, '0')), new Term(property, to.PadLeft(12, '0')), true);
                    }
                    break;

                    case "adate":
                    case "cdate":
                    case "mdate":
                    case "length":
                    {
                        int    idx;
                        string from, to;
                        idx  = queryDic[property].IndexOf(' ');
                        from = queryDic[property].Substring(0, idx);
                        to   = queryDic[property].Substring(idx + 1);
                        q    = new RangeQuery(new Term(property, from), new Term(property, to), true);
                    }
                    break;

                    case "all":
                    {
                        q = new QueryParser("name", new KeywordAnalyzer()).Parse(queryDic["all"]);
                    }
                    break;

                    default:
                        if (queryDic[property].Contains("*") || queryDic[property].Contains("?"))
                        {
                            q = new WildcardQuery(new Term(property, queryDic[property]));
                        }
                        else
                        {
                            string value = queryDic[property].
                                           Replace(@"\", @"\\").Replace("+", @"\+").Replace("-", @"\-").Replace("&", @"\&").
                                           Replace("|", @"\|").Replace("!", @"\!").Replace("(", @"\(").Replace(")", @"\)").
                                           Replace("{", @"\{").Replace("}", @"\}").Replace("[", @"\[").Replace("]", @"\]").
                                           Replace("^", @"\^").Replace("~", @"\~").Replace(":", @"\:");
                            q = new QueryParser(property, new KeywordAnalyzer()).Parse(value);
                        }
                        break;
                    }
                    #endregion

#if Log
                    Console.WriteLine("Lucene Query String:" + q.ToString());
#endif
                    //Get the results and sort them based on file name
                    Hits hits = ids.Search(q);//, new Sort("name"));

                    foreach (Hit hit in hits)
                    {
                        results.Add(hit); //Add the results to a List
                    }
                }
                // ids.Close();
            }
            return(results);//Return the list
        }
Example #27
0
        public int GetCountByM(SearchCondition sc)
        {
            int count = 0;
            string indexname = ConfigurationManager.AppSettings["CRMIndex"].ToString();
            string typename = ConfigurationManager.AppSettings["CRMMemberType"].ToString();
            string field = "";
            if (string.IsNullOrEmpty(sc.field))
            { field = ""; }
            else { field = sc.field.ToLower(); }
            string classVaule = "";
            if (string.IsNullOrEmpty(sc.classx))
            {
                sc.classx = "";
            }
            else
            {
                sc.classx = sc.classx.ToLower();
                classVaule = "1";
            }

            //must
            List<QueryContainer> mustquerys = new List<QueryContainer>();
            if (field.Equals("lastcontacttime"))
            {
                string timeG = sc.keywords[0];
                string timeL = DateTime.Parse(timeG).AddDays(-7).ToString("yyyy-MM-dd");
                QueryContainer rangeQuyery = new RangeQuery() { Field = field, GreaterThanOrEqualTo = timeL, LowerThanOrEqualTo = timeG, TimeZone = "+08:00" };
                mustquerys.Add(rangeQuyery);
            }
            else if (field.Equals("enterprise"))
            {

                foreach (string keyw in sc.keywords)
                {
                    List<QueryContainer> shouooo = new List<QueryContainer>();
                    QueryContainer termQ = new TermQuery() { Field = "enterprise", Value = sc.keyword.Replace(',', ' ') };
                    QueryContainer lowerQuery = new WildcardQuery() { Field = "enterprise", Value = string.IsNullOrEmpty(keyw) ? "*" : string.Format("*{0}*", keyw.ToLower()) };
                    QueryContainer notlowerQuery = new WildcardQuery() { Field = "enterprise.lower", Value = string.IsNullOrEmpty(keyw) ? "*" : string.Format("*{0}*", keyw.ToLower()) };
                    shouooo.Add(termQ);
                    shouooo.Add(lowerQuery);
                    shouooo.Add(notlowerQuery);
                    QueryContainer boooQuery = new BoolQuery() { Should = shouooo };
                    mustquerys.Add(boooQuery);
                }
            }
            else
            {
                foreach (string keyw in sc.keywords)
                {
                    QueryContainer wildcardQuery = new WildcardQuery() { Field = field, Value = string.IsNullOrEmpty(keyw) ? "*" : string.Format("*{0}*", keyw) };
                    mustquerys.Add(wildcardQuery);
                }
            }

            QueryContainer matchQueryT = new MatchQuery() { Field = "tracktypeid", Query = Convert.ToString(sc.tracktype) };
            QueryContainer matchQueryC = new MatchQuery() { Field = sc.classx, Query = classVaule };
            QueryContainer queryCountry = new WildcardQuery() { Field = "country", Value = string.Format("*{0}*", sc.country) };
            QueryContainer queryProvince = new WildcardQuery() { Field = "province", Value = string.Format("*{0}*", sc.province) };
            QueryContainer queryCity = new WildcardQuery() { Field = "city", Value = string.Format("*{0}*", sc.city) };
            if (!string.IsNullOrEmpty(sc.tracktype))
            {
                mustquerys.Add(matchQueryT);
            }
            if (!string.IsNullOrEmpty(sc.classx))
            {
                mustquerys.Add(matchQueryC);
            }
            if (!string.IsNullOrEmpty(sc.country))
            {
                mustquerys.Add(queryCountry);
            }
            if (!string.IsNullOrEmpty(sc.province))
            {
                mustquerys.Add(queryProvince);
            }
            if (!string.IsNullOrEmpty(sc.city))
            {
                mustquerys.Add(queryCity);
            }
            QueryContainer boolQuery = new BoolQuery() { Must = mustquerys };
            //QueryContainer wildcardQuery = new WildcardQuery() { Field = field, Value = string.IsNullOrEmpty(sc.keyword) ? "*" : string.Format("*{0}*", sc.keyword) };
            var searchResults = Connect.GetSearchClient().Count<Member>(s => s
                .Index(indexname)
                .Type(typename)
                .Query(boolQuery)
            );
            count = (int)searchResults.Count;
            return count;
        }
Example #28
0
        public static QueryWrapper <T> Range <T, TField>(this QueryWrapper <T> wrapper,
                                                         RangeQuery <TField> query,
                                                         Expression <Func <T, object> > path) where T : class where TField : struct
        => wrapper.QueryInternal(
            query,
            descriptor =>
        {
            QueryContainer container;

            switch (query)
            {
            case RangeQuery <DateTime> dateTimeRange:
                container = descriptor.DateRange(q =>
                {
                    q = q.Field(path);

                    if (dateTimeRange.Minimum != null)
                    {
                        q = dateTimeRange.Exclusive
                                        ? q.GreaterThan(dateTimeRange.Minimum.Value)
                                        : q.GreaterThanOrEquals(dateTimeRange.Minimum.Value);
                    }

                    if (dateTimeRange.Maximum != null)
                    {
                        q = dateTimeRange.Exclusive
                                        ? q.LessThan(dateTimeRange.Maximum.Value)
                                        : q.LessThanOrEquals(dateTimeRange.Maximum.Value);
                    }

                    return(q);
                });

                break;

            //todo: how to DRY
            case RangeQuery <int> intRange:
                container = descriptor.Range(q =>
                {
                    q = q.Field(path);

                    if (intRange.Minimum != null)
                    {
                        q = intRange.Exclusive
                                        ? q.GreaterThan(intRange.Minimum.Value)
                                        : q.GreaterThanOrEquals(intRange.Minimum.Value);
                    }

                    if (intRange.Maximum != null)
                    {
                        q = intRange.Exclusive
                                        ? q.LessThan(intRange.Maximum.Value)
                                        : q.LessThanOrEquals(intRange.Maximum.Value);
                    }

                    return(q);
                });

                break;

            case RangeQuery <double> doubleRange:
                container = descriptor.Range(q =>
                {
                    q = q.Field(path);

                    if (doubleRange.Minimum != null)
                    {
                        q = doubleRange.Exclusive
                                        ? q.GreaterThan(doubleRange.Minimum.Value)
                                        : q.GreaterThanOrEquals(doubleRange.Minimum.Value);
                    }

                    if (doubleRange.Maximum != null)
                    {
                        q = doubleRange.Exclusive
                                        ? q.LessThan(doubleRange.Maximum.Value)
                                        : q.LessThanOrEquals(doubleRange.Maximum.Value);
                    }

                    return(q);
                });

                break;

            default: throw new NotSupportedException($"Unsupported range query type: {query.GetType()}");
            }

            return(container);
        });
Example #29
0
 public virtual Query VisitRangeQuery(RangeQuery rangeq)
 {
     throw new NotImplementedException();
 }
Example #30
0
        /// <summary>
        /// WEBAPI使用方法,直接获取Member
        /// </summary>
        /// <param name="sc"></param>
        /// <param name="em"></param>
        /// <returns></returns>
        public string GetMember(SearchCondition sc, out int em)
        {
            string indexname = ConfigurationManager.AppSettings["CRMIndex"].ToString();
            string typename = ConfigurationManager.AppSettings["CRMMemberType"].ToString();
            string field = "";
            if (string.IsNullOrEmpty(sc.field))
            { field = ""; }
            else { field = sc.field.ToLower(); }
            string classVaule = "";
            if (string.IsNullOrEmpty(sc.classx))
            {
                sc.classx = "";
            }
            else
            {
                sc.classx = sc.classx.ToLower();
                classVaule = "1";
            }
            //must
            List<QueryContainer> mustquerys = new List<QueryContainer>();

            if (field.Equals("lastcontacttime"))
            {
                string timeG = sc.keywords[0];
                string timeL = DateTime.Parse(timeG).AddDays(-7).ToString("yyyy-MM-dd");
                QueryContainer rangeQuyery = new RangeQuery() { Field = field, GreaterThanOrEqualTo = timeL, LowerThanOrEqualTo = timeG, TimeZone = "+08:00" };
                mustquerys.Add(rangeQuyery);
            }
            else if (field.Equals("enterprise"))
            {
                foreach (string keyw in sc.keywords)
                {
                    List<QueryContainer> shouooo = new List<QueryContainer>();
                    QueryContainer termQ = new TermQuery() { Field = "enterprise", Value = sc.keyword.Replace(',', ' ') };
                    QueryContainer lowerQuery = new WildcardQuery() { Field = "enterprise", Value = string.IsNullOrEmpty(keyw) ? "*" : string.Format("*{0}*", keyw.ToLower()) };
                    QueryContainer notlowerQuery = new WildcardQuery() { Field = "enterprise.lower", Value = string.IsNullOrEmpty(keyw) ? "*" : string.Format("*{0}*", keyw.ToLower()) };
                    shouooo.Add(termQ);
                    shouooo.Add(lowerQuery);
                    shouooo.Add(notlowerQuery);
                    QueryContainer boooQuery = new BoolQuery() { Should = shouooo };
                    mustquerys.Add(boooQuery);
                }
            }
            else
            {
                foreach (string keyw in sc.keywords)
                {
                    QueryContainer wildcardQuery = new WildcardQuery() { Field = field, Value = string.IsNullOrEmpty(keyw) ? "*" : string.Format("*{0}*", keyw) };
                    mustquerys.Add(wildcardQuery);
                }
            }
            QueryContainer matchQueryT = new MatchQuery() { Field = "tracktypeid", Query = Convert.ToString(sc.tracktype) };
            QueryContainer matchQueryC = new MatchQuery() { Field = sc.classx, Query = classVaule };
            QueryContainer queryCountry = new WildcardQuery() { Field = "country", Value = string.Format("*{0}*", sc.country) };
            QueryContainer queryProvince = new WildcardQuery() { Field = "province", Value = string.Format("*{0}*", sc.province) };
            QueryContainer queryCity = new WildcardQuery() { Field = "city", Value = string.Format("*{0}*", sc.city) };
            if (!string.IsNullOrEmpty(sc.tracktype))
            {
                mustquerys.Add(matchQueryT);
            }
            if (!string.IsNullOrEmpty(sc.classx))
            {
                mustquerys.Add(matchQueryC);
            }
            if (!string.IsNullOrEmpty(sc.country))
            {
                mustquerys.Add(queryCountry);
            }
            if (!string.IsNullOrEmpty(sc.province))
            {
                mustquerys.Add(queryProvince);
            }
            if (!string.IsNullOrEmpty(sc.city))
            {
                mustquerys.Add(queryCity);
            }
            QueryContainer boolQuery = new BoolQuery() { Must = mustquerys };

            var searchResults = Connect.GetSearchClient().Search<Member>(s => s
                 .Index(indexname)
                 .Type(typename)
                 .Fields(f => f.Id, f => f.Enterprise, f => f.Isstar, f => f.Masterlinkman, f => f.Tracktypeid, f => f.Sourceid, f => f.Lastcontacttime, f => f.Enddate, f => f.Adminname, f => f.Addtime)
                 .Query(boolQuery)
                 .Sort(st => st.OnField(f => f.Lastcontacttime).Order(SortOrder.Descending))  /*排序*/
                 .From(sc.start)
                 .Size(sc.size)
             );
            em = searchResults.ElapsedMilliseconds;
            StringBuilder ResultJson = new StringBuilder("");
            ResultJson.Append("[");
            foreach (var hit in searchResults.Hits)
            {
                ResultJson.Append("{");
                ResultJson.Append(
                    String.Join(",",
                        hit.Fields.FieldValuesDictionary
                            .Select(FVD => "'" + FVD.Key + "':'" + FVD.Value.ToString().Replace("[", "").Replace("]", "") + "'")
                            .ToArray()
                    )
                );
                ResultJson.Append("},");
            }
            if (ResultJson.Length > 1)
                ResultJson.Length = ResultJson.Length - 1;
            ResultJson.Append("]");
            return ResultJson.ToString();
        }
Example #31
0
 public QueryStringQueryToken(RangeQuery query) :
     base(QueryStringTokenType.Query)
 {
     Query = query;
 }