Beispiel #1
0
        /// <summary>Returns true iff <code>o</code> is equal to this. </summary>
        public override bool Equals(System.Object o)
        {
            if (this == o)
            {
                return(true);
            }
            if (!(o is RangeQuery))
            {
                return(false);
            }

            RangeQuery other = (RangeQuery)o;

            if (this.GetBoost() != other.GetBoost())
            {
                return(false);
            }
            if (this.inclusive != other.inclusive)
            {
                return(false);
            }
            // one of lowerTerm and upperTerm can be null
            if (this.lowerTerm != null?!this.lowerTerm.Equals(other.lowerTerm):other.lowerTerm != null)
            {
                return(false);
            }
            if (this.upperTerm != null?!this.upperTerm.Equals(other.upperTerm):other.upperTerm != null)
            {
                return(false);
            }
            return(true);
        }
 protected void AddNumericRangeQuery(BooleanQuery query, 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);
 }
 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 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" };
     }
 }
Beispiel #5
0
        public virtual void  TestRangeQuery()
        {
            RangeQuery rq = new RangeQuery(new Term("sorter", "b"), new Term("sorter", "d"), true);

            Query filteredquery = new FilteredQuery(rq, filter);
            Hits  hits          = searcher.Search(filteredquery);

            Assert.AreEqual(2, hits.Length());
        }
Beispiel #6
0
        /// <summary>Returns true iff <code>o</code> is equal to this. </summary>
        public override bool Equals(System.Object o)
        {
            if (this == o)
            {
                return(true);
            }
            if (!(o is RangeQuery))
            {
                return(false);
            }

            RangeQuery other = (RangeQuery)o;

            return(this.delegate_Renamed.Equals(other.delegate_Renamed));
        }
		public virtual void  TestDeprecatedCstrctors()
		{
			Query query = new RangeQuery(null, new Term("content", "C"), false);
			InitializeIndex(new System.String[]{"A", "B", "C", "D"});
			IndexSearcher searcher = new IndexSearcher(dir);
			ScoreDoc[] hits = searcher.Search(query, null, 1000).ScoreDocs;
			Assert.AreEqual(2, hits.Length, "A,B,C,D, only B in range");
			searcher.Close();
			
			query = new RangeQuery(new Term("content", "C"), null, false);
			InitializeIndex(new System.String[]{"A", "B", "C", "D"});
			searcher = new IndexSearcher(dir);
			hits = searcher.Search(query, null, 1000).ScoreDocs;
			Assert.AreEqual(1, hits.Length, "A,B,C,D, only B in range");
			searcher.Close();
		}
Beispiel #8
0
        public virtual void  TestDeprecatedCstrctors()
        {
            Query query = new RangeQuery(null, new Term("content", "C"), false);

            InitializeIndex(new System.String[] { "A", "B", "C", "D" });
            IndexSearcher searcher = new IndexSearcher(dir);

            ScoreDoc[] hits = searcher.Search(query, null, 1000).scoreDocs;
            Assert.AreEqual(2, hits.Length, "A,B,C,D, only B in range");
            searcher.Close();

            query = new RangeQuery(new Term("content", "C"), null, false);
            InitializeIndex(new System.String[] { "A", "B", "C", "D" });
            searcher = new IndexSearcher(dir);
            hits     = searcher.Search(query, null, 1000).scoreDocs;
            Assert.AreEqual(1, hits.Length, "A,B,C,D, only B in range");
            searcher.Close();
        }
		public virtual void  TestExclusive()
		{
			Query query = new RangeQuery(new Term("content", "A"), new Term("content", "C"), false);
			InitializeIndex(new System.String[]{"A", "B", "C", "D"});
			IndexSearcher searcher = new IndexSearcher(dir);
			Hits hits = searcher.Search(query);
			Assert.AreEqual(1, hits.Length(), "A,B,C,D, only B in range");
			searcher.Close();
			
			InitializeIndex(new System.String[]{"A", "B", "D"});
			searcher = new IndexSearcher(dir);
			hits = searcher.Search(query);
			Assert.AreEqual(1, hits.Length(), "A,B,D, only B in range");
			searcher.Close();
			
			AddDoc("C");
			searcher = new IndexSearcher(dir);
			hits = searcher.Search(query);
			Assert.AreEqual(1, hits.Length(), "C added, still only B in range");
			searcher.Close();
		}
        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);
        }
Beispiel #11
0
        public virtual void  TestInclusive()
        {
            Query query = new RangeQuery(new Term("content", "A"), new Term("content", "C"), true);

            InitializeIndex(new System.String[] { "A", "B", "C", "D" });
            IndexSearcher searcher = new IndexSearcher(dir);
            Hits          hits     = searcher.Search(query);

            Assert.AreEqual(3, hits.Length(), "A,B,C,D - A,B,C in range");
            searcher.Close();

            InitializeIndex(new System.String[] { "A", "B", "D" });
            searcher = new IndexSearcher(dir);
            hits     = searcher.Search(query);
            Assert.AreEqual(2, hits.Length(), "A,B,D - A and B in range");
            searcher.Close();

            AddDoc("C");
            searcher = new IndexSearcher(dir);
            hits     = searcher.Search(query);
            Assert.AreEqual(3, hits.Length(), "C added - A, B, C in range");
            searcher.Close();
        }
        public Query Translate(Expression expression)
        {
            // visit the expression first to generate the query
            this.Visit(expression);

            Query result = null;

            // inspect the root for clause length
            int length = _root.GetClauses().Length;

            switch (length) {
                case 0:
                    result = new RangeQuery(new Term("","0"),null,true);
                    break;
                case 1:
                    result = _root.GetClauses()[0].GetQuery();
                    break;
                default:
                    result = _root;
                    break;
            }

            return result;
        }
 private static Lucene.Net.Search.Query GetRangeQuery(RangeQuery rangeQuery)
 {
     return new TermRangeQuery(rangeQuery.FieldName, rangeQuery.FromValue, rangeQuery.ToValue, rangeQuery.Inclusive, rangeQuery.Inclusive);
 }
Beispiel #14
0
 private static Query GetExactQuery()
 {
     BooleanQuery queryRet = new BooleanQuery();
     foreach (FilterCondition fc in filterList)
     {
         //SupportClass.FileUtil.WriteLog("fc :" + fc.ToString());
         QueryParser parser = new QueryParser(fc.GetString(), analyzer);
         foreach (string value in fc.Values)
         {
             //SupportClass.FileUtil.WriteLog("fc loop,value of fc.values:" + value);
             string[] wordArray = SupportClass.String.Split(value);
             foreach (string words in wordArray)
             {
                 //SupportClass.FileUtil.WriteLog("word loop,word of value split:" + words);
                 List<string> wordList = ISUtils.CSegment.Segment.SegmentStringEx(words);
                 foreach (string word in wordList)
                 {
                     Query query = parser.Parse(word);
                     queryRet.Add(query, BooleanClause.Occur.MUST);
                     //SupportClass.FileUtil.WriteLog(queryRet.ToString());
                 }
             }
         }
     }
     foreach (ExcludeCondition ec in excludeList)
     {
         //SupportClass.FileUtil.WriteLog("ec :" + ec.ToString());
         QueryParser parser = new QueryParser(ec.GetString(), analyzer);
         foreach (string value in ec.Values)
         {
             //SupportClass.FileUtil.WriteLog("ec loop,value of ec.values:" + value);
             string[] wordArray = SupportClass.String.Split(value);
             foreach (string words in wordArray)
             {
                 //SupportClass.FileUtil.WriteLog("word loop,word of value split:" + words);
                 List<string> wordList = ISUtils.CSegment.Segment.SegmentStringEx(words);
                 foreach (string word in wordList)
                 {
                     Query query = parser.Parse(word);
                     queryRet.Add(query, BooleanClause.Occur.MUST_NOT);
                     //SupportClass.FileUtil.WriteLog(queryRet.ToString());
                 }
             }
         }
     }
     foreach (RangeCondition rc in rangeList)
     {
         SupportClass.FileUtil.WriteLog("rc:" + rc.ToString());
         RangeQuery query = new RangeQuery(new Term(rc.GetString(), rc.RangeFrom), new Term(rc.GetString(), rc.RangeTo), rc.IntervalType);
         queryRet.Add(query, BooleanClause.Occur.MUST);
         SupportClass.FileUtil.WriteLog(queryRet.ToString());
     }
     return queryRet;
 }
 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<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
        }
        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; }
        }
Beispiel #18
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);
             BooleanQuery.SetMaxClauseCount(Int32.MaxValue);
             var rangeQuery = new RangeQuery(new Term(dateRangeField.FieldName, startDate), new Term(dateRangeField.FieldName, endDate), true);
             query.Add(rangeQuery, occurance);
        }
 public static void AddRangeQuery(
     this BooleanQuery query, string field, string lowerValue, string upperValue, BooleanClause.Occur occur)
 {
     var rangeQuery = new RangeQuery(new Term(field, lowerValue), new Term(field, upperValue), true);
     query.AddClause(rangeQuery, occur);
 }
		public virtual void  TestEqualsHashcode()
		{
			Query query = new RangeQuery(new Term("content", "A"), new Term("content", "C"), true);
			query.SetBoost(1.0f);
			Query other = new RangeQuery(new Term("content", "A"), new Term("content", "C"), true);
			other.SetBoost(1.0f);
			
			Assert.AreEqual(query, query, "query equals itself is true");
			Assert.AreEqual(query, other, "equivalent queries are equal");
			Assert.AreEqual(query.GetHashCode(), other.GetHashCode(), "hashcode must return same value when equals is true");
			
			other.SetBoost(2.0f);
			Assert.IsFalse(query.Equals(other), "Different boost queries are not equal");
			
			other = new RangeQuery(new Term("notcontent", "A"), new Term("notcontent", "C"), true);
			Assert.IsFalse(query.Equals(other), "Different fields are not equal");
			
			other = new RangeQuery(new Term("content", "X"), new Term("content", "C"), true);
			Assert.IsFalse(query.Equals(other), "Different lower terms are not equal");
			
			other = new RangeQuery(new Term("content", "A"), new Term("content", "Z"), true);
			Assert.IsFalse(query.Equals(other), "Different upper terms are not equal");
			
			query = new RangeQuery(null, new Term("content", "C"), true);
			other = new RangeQuery(null, new Term("content", "C"), true);
			Assert.AreEqual(query, other, "equivalent queries with null lowerterms are equal()");
			Assert.AreEqual(query.GetHashCode(), other.GetHashCode(), "hashcode must return same value when equals is true");
			
			query = new RangeQuery(new Term("content", "C"), null, true);
			other = new RangeQuery(new Term("content", "C"), null, true);
			Assert.AreEqual(query, other, "equivalent queries with null upperterms are equal()");
			Assert.AreEqual(query.GetHashCode(), other.GetHashCode(), "hashcode returns same value");
			
			query = new RangeQuery(null, new Term("content", "C"), true);
			other = new RangeQuery(new Term("content", "C"), null, true);
			Assert.IsFalse(query.Equals(other), "queries with different upper and lower terms are not equal");
			
			query = new RangeQuery(new Term("content", "A"), new Term("content", "C"), false);
			other = new RangeQuery(new Term("content", "A"), new Term("content", "C"), true);
			Assert.IsFalse(query.Equals(other), "queries with different inclusive are not equal");
		}
		public virtual void  TestBooleanOrderUnAffected()
		{
			// NOTE: uses index build in *this* SetUp
			
			IndexReader reader = IndexReader.Open(small);
			IndexSearcher search = new IndexSearcher(reader);
			
			// first do a regular RangeQuery which uses term expansion so
			// docs with more terms in range get higher scores
			
			Query rq = new RangeQuery(new Term("data", "1"), new Term("data", "4"), T);
			
			Hits expected = search.Search(rq);
			int numHits = expected.Length();
			
			// now do a boolean where which also contains a
			// ConstantScoreRangeQuery and make sure hte order is the same
			
			BooleanQuery q = new BooleanQuery();
			q.Add(rq, BooleanClause.Occur.MUST); //T, F);
			q.Add(Csrq("data", "1", "6", T, T), BooleanClause.Occur.MUST); //T, F);
			
			Hits actual = search.Search(q);
			
			Assert.AreEqual(numHits, actual.Length(), "wrong numebr of hits");
			for (int i = 0; i < numHits; i++)
			{
				Assert.AreEqual(expected.Id(i), actual.Id(i), "mismatch in docid for hit#" + i);
			}
		}
Beispiel #22
0
 public virtual Query VisitRangeQuery(RangeQuery rangeq) { throw new NotImplementedException(); }
Beispiel #23
0
 public override Query VisitRangeQuery(RangeQuery rangeq) { throw new NotImplementedException(); }
Beispiel #24
0
		public virtual void  TestRangeQuery()
		{
			RangeQuery rq = new RangeQuery(new Term("sorter", "b"), new Term("sorter", "d"), true);
			
			Query filteredquery = new FilteredQuery(rq, filter);
			Hits hits = searcher.Search(filteredquery);
			Assert.AreEqual(2, hits.Length());
		}