Пример #1
0
        public override Query Rewrite(IndexReader reader)
        {
            // Map to RangeFilter semantics which are slightly different...
            RangeFilter rangeFilt = new RangeFilter(fieldName, lowerVal != null ? lowerVal : "", upperVal, (System.Object)lowerVal == (System.Object) "" ? false : includeLower, upperVal == null ? false : includeUpper);
            Query       q         = new ConstantScoreQuery(rangeFilt);

            q.SetBoost(GetBoost());
            return(q);
        }
Пример #2
0
		public virtual void  TestBefore()
		{
			// create an index
			RAMDirectory indexStore = new RAMDirectory();
			IndexWriter writer = new IndexWriter(indexStore, new SimpleAnalyzer(), true);
			
			long now = (long) (DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalMilliseconds;
			
			Lucene.Net.Documents.Document doc = new Lucene.Net.Documents.Document();
			// add time that is in the past
			doc.Add(new Field("datefield", Lucene.Net.Documents.DateTools.TimeToString(now - 1000 * 100000, Lucene.Net.Documents.DateTools.Resolution.MILLISECOND), Field.Store.YES, Field.Index.UN_TOKENIZED));
			doc.Add(new Field("body", "Today is a very sunny day in New York City", Field.Store.YES, Field.Index.TOKENIZED));
			writer.AddDocument(doc);
			writer.Optimize();
			writer.Close();
			
			IndexSearcher searcher = new IndexSearcher(indexStore);
			
			// filter that should preserve matches
			//DateFilter df1 = DateFilter.Before("datefield", now);
			RangeFilter df1 = new RangeFilter("datefield", Lucene.Net.Documents.DateTools.TimeToString(now - 2000 * 100000, Lucene.Net.Documents.DateTools.Resolution.MILLISECOND), Lucene.Net.Documents.DateTools.TimeToString(now, Lucene.Net.Documents.DateTools.Resolution.MILLISECOND), false, true);
			// filter that should discard matches
			//DateFilter df2 = DateFilter.Before("datefield", now - 999999);
			RangeFilter df2 = new RangeFilter("datefield", Lucene.Net.Documents.DateTools.TimeToString(0, Lucene.Net.Documents.DateTools.Resolution.MILLISECOND), Lucene.Net.Documents.DateTools.TimeToString(now - 2000 * 100000, Lucene.Net.Documents.DateTools.Resolution.MILLISECOND), true, false);
			
			// search something that doesn't exist with DateFilter
			Query query1 = new TermQuery(new Term("body", "NoMatchForThis"));
			
			// search for something that does exists
			Query query2 = new TermQuery(new Term("body", "sunny"));
			
			Hits result;
			
			// ensure that queries return expected results without DateFilter first
			result = searcher.Search(query1);
			Assert.AreEqual(0, result.Length());
			
			result = searcher.Search(query2);
			Assert.AreEqual(1, result.Length());
			
			
			// run queries with DateFilter
			result = searcher.Search(query1, df1);
			Assert.AreEqual(0, result.Length());
			
			result = searcher.Search(query1, df2);
			Assert.AreEqual(0, result.Length());
			
			result = searcher.Search(query2, df1);
			Assert.AreEqual(1, result.Length());
			
			result = searcher.Search(query2, df2);
			Assert.AreEqual(0, result.Length());
		}
 public override Query Rewrite(IndexReader reader)
 {
     // Map to RangeFilter semantics which are slightly different...
     RangeFilter rangeFilt = new RangeFilter(fieldName, lowerVal != null ? lowerVal : "", upperVal, (System.Object) lowerVal == (System.Object) ""?false:includeLower, upperVal == null?false:includeUpper);
     Query q = new ConstantScoreQuery(rangeFilt);
     q.SetBoost(GetBoost());
     return q;
 }
Пример #4
0
        public void StraightFilters()
        {
            CreateData();
            IFullTextSession s = Search.CreateFullTextSession(OpenSession());
            s.Transaction.Begin();
            BooleanQuery query = new BooleanQuery();
            query.Add(new TermQuery(new Term("teacher", "andre")), BooleanClause.Occur.SHOULD);
            query.Add(new TermQuery(new Term("teacher", "max")), BooleanClause.Occur.SHOULD);
            query.Add(new TermQuery(new Term("teacher", "aaron")), BooleanClause.Occur.SHOULD);

            IFullTextQuery ftQuery = s.CreateFullTextQuery(query, typeof(Driver));
            ftQuery.EnableFullTextFilter("bestDriver");
            Lucene.Net.Search.Filter dateFilter = new RangeFilter("delivery", "2001", "2005", true, true);
            ftQuery.SetFilter(dateFilter);
            Assert.AreEqual(1, ftQuery.ResultSize, "Should select only liz");

            s.Transaction.Commit();
            s.Close();
            DeleteData();
        }