public override Query Process(RangeQueryOptions options, AzureQueryMapper mapper)
 {
     string lowerTerm = (string)null;
     string upperTerm = (string)null;
     if (options.FieldFromValue != null)
         lowerTerm = this.NormalizeValue(mapper.ValueFormatter.FormatValueForIndexStorage(options.FieldFromValue, options.FieldName).ToString(), mapper);
     if (options.FieldToValue != null)
         upperTerm = this.NormalizeValue(mapper.ValueFormatter.FormatValueForIndexStorage(options.FieldToValue, options.FieldName).ToString(), mapper);
     TermRangeQuery termRangeQuery = new TermRangeQuery(options.FieldName, lowerTerm, upperTerm, options.IncludeLower, options.IncludeUpper);
     termRangeQuery.Boost = options.Boost;
     return (Query)termRangeQuery;
 }
Beispiel #2
0
        public BooleanQuery MakeQuery()
        {
            //Fill BooleanQeury with anonymous terms. term.Field=critList.Key term.Term=critList.Value
            foreach (var pair in _searchCriterion)
            {
                string searchString = pair.Value.ToString();

                if (!String.IsNullOrEmpty(searchString))
                {

                    switch (pair.Key)
                    {

                        case "FindAll":

                            string[] searchFields = { "CardKind", "CardName", "CardEdition" };
                            var multiFieldQueryParser = new MultiFieldQueryParser(Version.LUCENE_30, searchFields,
                                _analyzer);

                            _finalQuery.Add(ParseQuery(searchString, multiFieldQueryParser),
                                searchString.Contains("+") ? Occur.MUST : Occur.SHOULD);
                            break;

                        case "CardAdoptionDateMin": //must be compile time constant!
                            string upperDate = DateTools.DateToString((DateTime)_kvpMax.Value, DateTools.Resolution.DAY);
                            var lowerRange = new TermRangeQuery("CardAdoptionDate", null, upperDate, true, true);
                            //{CardAdoptionDate:[* TO 20140718]}
                            _finalQuery.Add(new BooleanClause(lowerRange, Occur.MUST));
                            break;

                        case "CardAdoptionDateMax":
                            string lowerDate = DateTools.DateToString((DateTime)_kvpMin.Value, DateTools.Resolution.DAY);
                            var upperRange = new TermRangeQuery("CardAdoptionDate", lowerDate, null, true, true);
                            //{CardAdoptionDate:[20140718 TO *]}
                            _finalQuery.Add(new BooleanClause(upperRange, Occur.MUST));
                            break;

                        default:
                            _finalQuery.Add(new TermQuery(new Term(pair.Key, (string)pair.Value)), Occur.MUST);
                            break;
                    }

                    #region Source and links

                    //http://stackoverflow.com/questions/17503119/how-to-index-search-the-datetime-searchField-in-lucene-net
                    //http://stackoverflow.com/questions/3294772/storing-datetime-searchField-in-lucene-document

                    #endregion
                }
            }
            return _finalQuery;
        }
        public void TestInclusive()
        {
            using (var dir = FSDirectory.Open(TestEnvironment.TestIndexDirectory))
            using (var indexSearcher = new IndexSearcher(dir))
            {
                var termRangeQuery = new TermRangeQuery(field: "title2", lowerTerm: "d", upperTerm: "j", includeLower: true, includeUpper: true);
                var topDocs = indexSearcher.Search(termRangeQuery, 100);
                Assert.Equal(3, topDocs.TotalHits);

//                foreach (var scoreDoc in topDocs.ScoreDocs)
//                {
//                    Console.WriteLine("  match: {0}:{1}", indexSearcher.Doc(scoreDoc.Doc).Get("author"));
//                }
            }

        }
		public virtual void  TestMultiValuedNRQ()
		{
			System.Random rnd = NewRandom();
			
			RAMDirectory directory = new RAMDirectory();
			IndexWriter writer = new IndexWriter(directory, new WhitespaceAnalyzer(), true, MaxFieldLength.UNLIMITED);
			
			//DecimalFormat format = new DecimalFormat("00000000000", new System.Globalization.CultureInfo("en-US").NumberFormat);
			
			for (int l = 0; l < 5000; l++)
			{
				Document doc = new Document();
				for (int m = 0, c = rnd.Next(10); m <= c; m++)
				{
					int value_Renamed = rnd.Next(System.Int32.MaxValue);
                    doc.Add(new Field("asc", value_Renamed.ToString().PadLeft(11, '0'), Field.Store.NO, Field.Index.NOT_ANALYZED));
					doc.Add(new NumericField("trie", Field.Store.NO, true).SetIntValue(value_Renamed));
				}
				writer.AddDocument(doc);
			}
			writer.Close();
			
			Searcher searcher = new IndexSearcher(directory, true);
			for (int i = 0; i < 50; i++)
			{
				int lower = rnd.Next(System.Int32.MaxValue);
				int upper = rnd.Next(System.Int32.MaxValue);
				if (lower > upper)
				{
					int a = lower; lower = upper; upper = a;
				}
				TermRangeQuery cq = new TermRangeQuery("asc", lower.ToString().PadLeft(11, '0'),  upper.ToString().PadLeft(11, '0'), true, true);
				System.Int32 tempAux = (System.Int32) lower;
				System.Int32 tempAux2 = (System.Int32) upper;
				NumericRangeQuery tq = NumericRangeQuery.NewIntRange("trie", tempAux, tempAux2, true, true);
				TopDocs trTopDocs = searcher.Search(cq, 1);
				TopDocs nrTopDocs = searcher.Search(tq, 1);
				Assert.AreEqual(trTopDocs.totalHits, nrTopDocs.totalHits, "Returned count for NumericRangeQuery and TermRangeQuery must be equal");
			}
			searcher.Close();
			
			directory.Close();
		}
 public virtual void TestAllDocs()
 {
     InitializeIndex(new string[] { "A", "B", "C", "D" });
     IndexReader reader = DirectoryReader.Open(Dir);
     IndexSearcher searcher = NewSearcher(reader);
     TermRangeQuery query = new TermRangeQuery("content", null, null, true, true);
     Terms terms = MultiFields.GetTerms(searcher.IndexReader, "content");
     Assert.IsFalse(query.GetTermsEnum(terms) is TermRangeTermsEnum);
     Assert.AreEqual(4, searcher.Search(query, null, 1000).ScoreDocs.Length);
     query = new TermRangeQuery("content", null, null, false, false);
     Assert.IsFalse(query.GetTermsEnum(terms) is TermRangeTermsEnum);
     Assert.AreEqual(4, searcher.Search(query, null, 1000).ScoreDocs.Length);
     query = TermRangeQuery.NewStringRange("content", "", null, true, false);
     Assert.IsFalse(query.GetTermsEnum(terms) is TermRangeTermsEnum);
     Assert.AreEqual(4, searcher.Search(query, null, 1000).ScoreDocs.Length);
     // and now anothe one
     query = TermRangeQuery.NewStringRange("content", "B", null, true, false);
     Assert.IsTrue(query.GetTermsEnum(terms) is TermRangeTermsEnum);
     Assert.AreEqual(3, searcher.Search(query, null, 1000).ScoreDocs.Length);
     reader.Dispose();
 }
		public virtual void  TestExclusive()
		{
			Query query = new TermRangeQuery("content", "A", "C", false, false);
			InitializeIndex(new System.String[]{"A", "B", "C", "D"});
			IndexSearcher searcher = new IndexSearcher(dir);
			ScoreDoc[] hits = searcher.Search(query, null, 1000).ScoreDocs;
			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, null, 1000).ScoreDocs;
			Assert.AreEqual(1, hits.Length, "A,B,D, only B in range");
			searcher.Close();
			
			AddDoc("C");
			searcher = new IndexSearcher(dir);
			hits = searcher.Search(query, null, 1000).ScoreDocs;
			Assert.AreEqual(1, hits.Length, "C added, still only B in range");
			searcher.Close();
		}
		public virtual void  TestInclusive()
		{
			Query query = new TermRangeQuery("content", "A", "C", true, true);
			
			InitializeIndex(new System.String[]{"A", "B", "C", "D"});
			IndexSearcher searcher = new IndexSearcher(dir, true);
			ScoreDoc[] hits = searcher.Search(query, null, 1000).ScoreDocs;
			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, true);
			hits = searcher.Search(query, null, 1000).ScoreDocs;
			Assert.AreEqual(2, hits.Length, "A,B,D - A and B in range");
			searcher.Close();
			
			AddDoc("C");
			searcher = new IndexSearcher(dir, true);
			hits = searcher.Search(query, null, 1000).ScoreDocs;
			Assert.AreEqual(3, hits.Length, "C added - A, B, C in range");
			searcher.Close();
		}
Beispiel #8
0
        /// <summary>
        /// Adds the content sub query.
        /// </summary>
        /// <param name="query">The boolean query.</param>
        /// <param name="key">The field key.</param>
        /// <param name="value">The field value.</param>
        /// <param name="matchVariant">The match variant.</param>
        /// <param name="condition">The condition.</param>
        /// <param name="isFirst">if set to <c>true</c> [is first].</param>
        private void AddContentSubQuery(LuceneSearch.BooleanQuery query, string key, string value, MatchVariant matchVariant, QueryCondition condition)
        {
            if (matchVariant == MatchVariant.NotEquals)
            {
                query.Add(new LuceneSearch.TermQuery(new Term(key, value)), LuceneSearch.Occur.MUST_NOT);
                return;
            }

            LuceneSearch.Occur occurrence = this.GetOccur(condition);

            LuceneSearch.TermRangeQuery rangeQuery = this.GetRangeQuery(key, value, matchVariant);
            if (rangeQuery != null)
            {
                query.Add(rangeQuery, occurrence);
                return;
            }

            string[] keywords = value.Split(' ');
            if (keywords.Length > 1)
            {
                LuceneSearch.PhraseQuery phraseQuery = new Lucene.Net.Search.PhraseQuery();

                foreach (string keyword in keywords)
                {
                    phraseQuery.Add(new Term(key, keyword));
                }

                query.Add(phraseQuery, occurrence);
            }
            else if (matchVariant == MatchVariant.Like)
            {
                query.Add(new LuceneSearch.WildcardQuery(new Term(key, value + "*")), occurrence);
            }
            else
            {
                query.Add(new LuceneSearch.TermQuery(new Term(key, value)), occurrence);
            }
        }
		public virtual void  TestRangeQuery()
		{
			TermRangeQuery rq = new TermRangeQuery("sorter", "b", "d", true, true);
			
			Query filteredquery = new FilteredQuery(rq, filter);
			ScoreDoc[] hits = searcher.Search(filteredquery, null, 1000).scoreDocs;
			Assert.AreEqual(2, hits.Length);
			QueryUtils.Check(filteredquery, searcher);
		}
		public virtual void  TestExclusiveLowerNull()
		{
			Analyzer analyzer = new SingleCharAnalyzer();
			//http://issues.apache.org/jira/browse/LUCENE-38
			Query query = new TermRangeQuery("content", null, "C", false, false);
			InitializeIndex(new System.String[]{"A", "B", "", "C", "D"}, analyzer);
            IndexSearcher searcher = new IndexSearcher(dir, true);
			int numHits = searcher.Search(query, null, 1000).TotalHits;
			// When Lucene-38 is fixed, use the assert on the next line:
            Assert.AreEqual(3, numHits, "A,B,<empty string>,C,D => A, B & <empty string> are in range");
			// until Lucene-38 is fixed, use this assert:
            //Assert.AreEqual(2, hits.length(),"A,B,<empty string>,C,D => A, B & <empty string> are in range");
			
			searcher.Close();
			InitializeIndex(new System.String[]{"A", "B", "", "D"}, analyzer);
            searcher = new IndexSearcher(dir, true);
            numHits = searcher.Search(query, null, 1000).TotalHits;
			// When Lucene-38 is fixed, use the assert on the next line:
            Assert.AreEqual(3, numHits, "A,B,<empty string>,D => A, B & <empty string> are in range");
			// until Lucene-38 is fixed, use this assert:
            //Assert.AreEqual(2, hits.length(), "A,B,<empty string>,D => A, B & <empty string> are in range");
			searcher.Close();
			AddDoc("C");
            searcher = new IndexSearcher(dir, true);
            numHits = searcher.Search(query, null, 1000).TotalHits;
			// When Lucene-38 is fixed, use the assert on the next line:
            Assert.AreEqual(3, numHits, "C added, still A, B & <empty string> are in range");
			// until Lucene-38 is fixed, use this assert
            //Assert.AreEqual(2, hits.length(), "C added, still A, B & <empty string> are in range");
			searcher.Close();
		}
 protected override ParameterizedSql BuildQuery(TermRangeQuery termRangeQuery)
 {
     // Not handling the TermRangeQuery. Discarding the search term.
     return null;
 }
        private void TestRandomTrieAndClassicRangeQuery(int precisionStep)
        {
            string field = "field" + precisionStep;
            int totalTermCountT = 0, totalTermCountC = 0, termCountT, termCountC;
            int num = TestUtil.NextInt(Random(), 10, 20);
            for (int i = 0; i < num; i++)
            {
                int lower = (int)(Random().NextDouble() * NoDocs * Distance) + StartOffset;
                int upper = (int)(Random().NextDouble() * NoDocs * Distance) + StartOffset;
                if (lower > upper)
                {
                    int a = lower;
                    lower = upper;
                    upper = a;
                }
                BytesRef lowerBytes = new BytesRef(NumericUtils.BUF_SIZE_INT), upperBytes = new BytesRef(NumericUtils.BUF_SIZE_INT);
                NumericUtils.IntToPrefixCodedBytes(lower, 0, lowerBytes);
                NumericUtils.IntToPrefixCodedBytes(upper, 0, upperBytes);

                // test inclusive range
                NumericRangeQuery<int> tq = NumericRangeQuery.NewIntRange(field, precisionStep, lower, upper, true, true);
                TermRangeQuery cq = new TermRangeQuery(field, lowerBytes, upperBytes, true, true);
                TopDocs tTopDocs = Searcher.Search(tq, 1);
                TopDocs cTopDocs = Searcher.Search(cq, 1);
                Assert.AreEqual(cTopDocs.TotalHits, tTopDocs.TotalHits, "Returned count for NumericRangeQuery and TermRangeQuery must be equal");
                totalTermCountT += termCountT = CountTerms(tq);
                totalTermCountC += termCountC = CountTerms(cq);
                CheckTermCounts(precisionStep, termCountT, termCountC);
                // test exclusive range
                tq = NumericRangeQuery.NewIntRange(field, precisionStep, lower, upper, false, false);
                cq = new TermRangeQuery(field, lowerBytes, upperBytes, false, false);
                tTopDocs = Searcher.Search(tq, 1);
                cTopDocs = Searcher.Search(cq, 1);
                Assert.AreEqual(cTopDocs.TotalHits, tTopDocs.TotalHits, "Returned count for NumericRangeQuery and TermRangeQuery must be equal");
                totalTermCountT += termCountT = CountTerms(tq);
                totalTermCountC += termCountC = CountTerms(cq);
                CheckTermCounts(precisionStep, termCountT, termCountC);
                // test left exclusive range
                tq = NumericRangeQuery.NewIntRange(field, precisionStep, lower, upper, false, true);
                cq = new TermRangeQuery(field, lowerBytes, upperBytes, false, true);
                tTopDocs = Searcher.Search(tq, 1);
                cTopDocs = Searcher.Search(cq, 1);
                Assert.AreEqual(cTopDocs.TotalHits, tTopDocs.TotalHits, "Returned count for NumericRangeQuery and TermRangeQuery must be equal");
                totalTermCountT += termCountT = CountTerms(tq);
                totalTermCountC += termCountC = CountTerms(cq);
                CheckTermCounts(precisionStep, termCountT, termCountC);
                // test right exclusive range
                tq = NumericRangeQuery.NewIntRange(field, precisionStep, lower, upper, true, false);
                cq = new TermRangeQuery(field, lowerBytes, upperBytes, true, false);
                tTopDocs = Searcher.Search(tq, 1);
                cTopDocs = Searcher.Search(cq, 1);
                Assert.AreEqual(cTopDocs.TotalHits, tTopDocs.TotalHits, "Returned count for NumericRangeQuery and TermRangeQuery must be equal");
                totalTermCountT += termCountT = CountTerms(tq);
                totalTermCountC += termCountC = CountTerms(cq);
                CheckTermCounts(precisionStep, termCountT, termCountC);
            }

            CheckTermCounts(precisionStep, totalTermCountT, totalTermCountC);
            if (VERBOSE && precisionStep != int.MaxValue)
            {
                Console.WriteLine("Average number of terms during random search on '" + field + "':");
                Console.WriteLine(" Numeric query: " + (((double)totalTermCountT) / (num * 4)));
                Console.WriteLine(" Classical query: " + (((double)totalTermCountC) / (num * 4)));
            }
        }
		private void  TestRandomTrieAndClassicRangeQuery(int precisionStep)
		{
			System.Random rnd = NewRandom();
			System.String field = "field" + precisionStep;
			int termCountT = 0, termCountC = 0;
			for (int i = 0; i < 50; i++)
			{
				long lower = (long) (rnd.NextDouble() * noDocs * distance) + startOffset;
				long upper = (long) (rnd.NextDouble() * noDocs * distance) + startOffset;
				if (lower > upper)
				{
					long a = lower; lower = upper; upper = a;
				}
				// test inclusive range
				System.Int64 tempAux = (long) lower;
				System.Int64 tempAux2 = (long) upper;
				NumericRangeQuery tq = NumericRangeQuery.NewLongRange(field, precisionStep, tempAux, tempAux2, true, true);
				TermRangeQuery cq = new TermRangeQuery(field, NumericUtils.LongToPrefixCoded(lower), NumericUtils.LongToPrefixCoded(upper), true, true);
				TopDocs tTopDocs = searcher.Search(tq, 1);
				TopDocs cTopDocs = searcher.Search(cq, 1);
				Assert.AreEqual(cTopDocs.TotalHits, tTopDocs.TotalHits, "Returned count for NumericRangeQuery and TermRangeQuery must be equal");
				termCountT += tq.GetTotalNumberOfTerms();
				termCountC += cq.GetTotalNumberOfTerms();
				// test exclusive range
				System.Int64 tempAux3 = (long) lower;
				System.Int64 tempAux4 = (long) upper;
				tq = NumericRangeQuery.NewLongRange(field, precisionStep, tempAux3, tempAux4, false, false);
				cq = new TermRangeQuery(field, NumericUtils.LongToPrefixCoded(lower), NumericUtils.LongToPrefixCoded(upper), false, false);
				tTopDocs = searcher.Search(tq, 1);
				cTopDocs = searcher.Search(cq, 1);
				Assert.AreEqual(cTopDocs.TotalHits, tTopDocs.TotalHits, "Returned count for NumericRangeQuery and TermRangeQuery must be equal");
				termCountT += tq.GetTotalNumberOfTerms();
				termCountC += cq.GetTotalNumberOfTerms();
				// test left exclusive range
				System.Int64 tempAux5 = (long) lower;
				System.Int64 tempAux6 = (long) upper;
				tq = NumericRangeQuery.NewLongRange(field, precisionStep, tempAux5, tempAux6, false, true);
				cq = new TermRangeQuery(field, NumericUtils.LongToPrefixCoded(lower), NumericUtils.LongToPrefixCoded(upper), false, true);
				tTopDocs = searcher.Search(tq, 1);
				cTopDocs = searcher.Search(cq, 1);
				Assert.AreEqual(cTopDocs.TotalHits, tTopDocs.TotalHits, "Returned count for NumericRangeQuery and TermRangeQuery must be equal");
				termCountT += tq.GetTotalNumberOfTerms();
				termCountC += cq.GetTotalNumberOfTerms();
				// test right exclusive range
				System.Int64 tempAux7 = (long) lower;
				System.Int64 tempAux8 = (long) upper;
				tq = NumericRangeQuery.NewLongRange(field, precisionStep, tempAux7, tempAux8, true, false);
				cq = new TermRangeQuery(field, NumericUtils.LongToPrefixCoded(lower), NumericUtils.LongToPrefixCoded(upper), true, false);
				tTopDocs = searcher.Search(tq, 1);
				cTopDocs = searcher.Search(cq, 1);
				Assert.AreEqual(cTopDocs.TotalHits, tTopDocs.TotalHits, "Returned count for NumericRangeQuery and TermRangeQuery must be equal");
				termCountT += tq.GetTotalNumberOfTerms();
				termCountC += cq.GetTotalNumberOfTerms();
			}
			if (precisionStep == System.Int32.MaxValue)
			{
				Assert.AreEqual(termCountT, termCountC, "Total number of terms should be equal for unlimited precStep");
			}
			else
			{
				System.Console.Out.WriteLine("Average number of terms during random search on '" + field + "':");
				System.Console.Out.WriteLine(" Trie query: " + (((double) termCountT) / (50 * 4)));
				System.Console.Out.WriteLine(" Classical query: " + (((double) termCountC) / (50 * 4)));
			}
		}
		/// <summary>macro for readability </summary>
		public static Query Csrq(System.String f, System.String l, System.String h, bool il, bool ih, System.Globalization.CompareInfo c)
		{
			TermRangeQuery query = new TermRangeQuery(f, l, h, il, ih, c);
			query.SetRewriteMethod(MultiTermQuery.CONSTANT_SCORE_FILTER_REWRITE);
			return query;
		}
Beispiel #15
0
        public static IndexItemCollection Browse(
            int siteId,
            Guid featureGuid,
            DateTime modifiedBeginDate,
            DateTime modifiedEndDate,
            int pageNumber,
            int pageSize,
            out int totalHits)
        {
            totalHits = 0;

            IndexItemCollection results = new IndexItemCollection();

            using (Lucene.Net.Store.Directory searchDirectory = GetDirectory(siteId))
            {
                Filter filter = null;
                BooleanQuery filterQuery = null;

                if ((modifiedBeginDate.Date > DateTime.MinValue.Date) || (modifiedEndDate.Date < DateTime.MaxValue.Date))
                {
                    filterQuery = new BooleanQuery(); // won't be used to score the results

                    TermRangeQuery lastModifiedDateFilter = new TermRangeQuery(
                        "LastModUtc",
                        modifiedBeginDate.Date.ToString("s"),
                        modifiedEndDate.Date.ToString("s"),
                        true,
                        true);

                    filterQuery.Add(lastModifiedDateFilter, Occur.MUST);

                }

                if (featureGuid != Guid.Empty)
                {
                    if (filterQuery == null) { filterQuery = new BooleanQuery(); }

                    BooleanQuery featureFilter = new BooleanQuery();

                    featureFilter.Add(new TermQuery(new Term("FeatureId", featureGuid.ToString())), Occur.MUST);

                    filterQuery.Add(featureFilter, Occur.MUST);
                }

                if (filterQuery != null)
                {
                    filter = new QueryWrapperFilter(filterQuery); // filterQuery won't affect result scores
                }

                MatchAllDocsQuery matchAllQuery = new MatchAllDocsQuery();

                using (IndexSearcher searcher = new IndexSearcher(searchDirectory))
                {
                    int maxResults = int.MaxValue;

                    TopDocs hits = searcher.Search(matchAllQuery, filter, maxResults);

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

                    totalHits = hits.TotalHits;

                    if (startHit > totalHits)
                    {
                        startHit = totalHits;
                    }

                    int end = startHit + pageSize;
                    if (totalHits <= end)
                    {
                        end = totalHits;
                    }

                    int itemsAdded = 0;
                    int itemsToAdd = end;

                    for (int i = startHit; i < itemsToAdd; i++)
                    {
                        Document doc = searcher.Doc(hits.ScoreDocs[i].Doc);
                        IndexItem indexItem = new IndexItem(doc, hits.ScoreDocs[i].Score);

                        results.Add(indexItem);
                        itemsAdded += 1;

                    }

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

                    results.ExecutionTime = DateTime.Now.Ticks; // -0;

                }

                //    using (IndexReader reader = IndexReader.Open(searchDirectory, false))
                //    {

                //        totalHits = reader.NumDocs();
                //        int startHit = 0;
                //        int itemsToAdd = pageSize;
                //        if (pageNumber > 1)
                //        {
                //            startHit = ((pageNumber - 1) * pageSize);
                //            int end = startHit + pageSize;
                //            if (totalHits <= end)
                //            {
                //                end = totalHits;
                //            }
                //            itemsToAdd = end;
                //        }

                //        for (int i = startHit; i < itemsToAdd; i++)
                //        {
                //            Document doc = reader.Document(i);
                //            IndexItem indexItem = new IndexItem(doc, 1);
                //            results.Add(indexItem);
                //        }

                //    }

            }

            return results;
        }
		public virtual void  TestDanish()
		{
			System.Globalization.CompareInfo collator = new System.Globalization.CultureInfo("da" + "-" + "dk").CompareInfo;
			// Danish collation orders the words below in the given order (example taken
			// from TestSort.testInternationalSort() ).
			System.String[] words = new System.String[]{"H\u00D8T", "H\u00C5T", "MAND"};
			Query query = new TermRangeQuery("content", "H\u00D8T", "MAND", false, false, collator);
			
			// Unicode order would not include "H\u00C5T" in [ "H\u00D8T", "MAND" ],
			// but Danish collation does.
			InitializeIndex(words);
			IndexSearcher searcher = new IndexSearcher(dir);
			ScoreDoc[] hits = searcher.Search(query, null, 1000).ScoreDocs;
			Assert.AreEqual(1, hits.Length, "The index Term should be included.");
			
			query = new TermRangeQuery("content", "H\u00C5T", "MAND", false, false, collator);
			hits = searcher.Search(query, null, 1000).ScoreDocs;
			Assert.AreEqual(0, hits.Length, "The index Term should not be included.");
			searcher.Close();
		}
		public virtual void  TestEqualsHashcode()
		{
			Query query = new TermRangeQuery("content", "A", "C", true, true);
			
			query.SetBoost(1.0f);
			Query other = new TermRangeQuery("content", "A", "C", true, 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 TermRangeQuery("notcontent", "A", "C", true, true);
			Assert.IsFalse(query.Equals(other), "Different fields are not equal");
			
			other = new TermRangeQuery("content", "X", "C", true, true);
			Assert.IsFalse(query.Equals(other), "Different lower terms are not equal");
			
			other = new TermRangeQuery("content", "A", "Z", true, true);
			Assert.IsFalse(query.Equals(other), "Different upper terms are not equal");
			
			query = new TermRangeQuery("content", null, "C", true, true);
			other = new TermRangeQuery("content", null, "C", true, 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 TermRangeQuery("content", "C", null, true, true);
			other = new TermRangeQuery("content", "C", null, true, true);
			Assert.AreEqual(query, other, "equivalent queries with null upperterms are equal()");
			Assert.AreEqual(query.GetHashCode(), other.GetHashCode(), "hashcode returns same value");
			
			query = new TermRangeQuery("content", null, "C", true, true);
			other = new TermRangeQuery("content", "C", null, true, true);
			Assert.IsFalse(query.Equals(other), "queries with different upper and lower terms are not equal");
			
			query = new TermRangeQuery("content", "A", "C", false, false);
			other = new TermRangeQuery("content", "A", "C", true, true);
			Assert.IsFalse(query.Equals(other), "queries with different inclusive are not equal");
			
			query = new TermRangeQuery("content", "A", "C", false, false);
			other = new TermRangeQuery("content", "A", "C", false, false, System.Globalization.CultureInfo.CurrentCulture.CompareInfo);
			Assert.IsFalse(query.Equals(other), "a query with a collator is not equal to one without");
		}
Beispiel #18
0
        /// <summary>Constructs a query selecting all terms greater than
        /// <c>lowerTerm</c> but less than <c>upperTerm</c>.
        /// There must be at least one term and either term may be null,
        /// in which case there is no bound on that side, but if there are
        /// two terms, both terms <b>must</b> be for the same field.
        /// <p/>
        /// If <c>collator</c> is not null, it will be used to decide whether
        /// index terms are within the given range, rather than using the Unicode code
        /// point order in which index terms are stored.
        /// <p/>
        /// <strong>WARNING:</strong> Using this constructor and supplying a non-null
        /// value in the <c>collator</c> parameter will cause every single 
        /// index Term in the Field referenced by lowerTerm and/or upperTerm to be
        /// examined.  Depending on the number of index Terms in this Field, the 
        /// operation could be very slow.
        /// 
        /// </summary>
        /// <param name="lowerTerm">The Term at the lower end of the range
        /// </param>
        /// <param name="upperTerm">The Term at the upper end of the range
        /// </param>
        /// <param name="inclusive">If true, both <c>lowerTerm</c> and
        /// <c>upperTerm</c> will themselves be included in the range.
        /// </param>
        /// <param name="collator">The collator to use to collate index Terms, to determine
        /// their membership in the range bounded by <c>lowerTerm</c> and
        /// <c>upperTerm</c>.
        /// </param>
        public RangeQuery(Term lowerTerm, Term upperTerm, bool inclusive, System.Globalization.CompareInfo collator)
        {
            if (lowerTerm == null && upperTerm == null)
                throw new System.ArgumentException("At least one term must be non-null");
            if (lowerTerm != null && upperTerm != null && (System.Object) lowerTerm.Field() != (System.Object) upperTerm.Field())
                throw new System.ArgumentException("Both terms must have the same field");

            delegate_Renamed = new TermRangeQuery((lowerTerm == null)?upperTerm.Field():lowerTerm.Field(), (lowerTerm == null)?null:lowerTerm.Text(), (upperTerm == null)?null:upperTerm.Text(), inclusive, inclusive, collator);
            delegate_Renamed.SetRewriteMethod(TermRangeQuery.SCORING_BOOLEAN_QUERY_REWRITE);
        }
        /// <summary>
        ///     Builds the query.
        /// </summary>
        /// <param name="criteria">The criteria.</param>
        /// <returns></returns>
        public override object BuildQuery(ISearchCriteria criteria)
        {

            var builder = base.BuildQuery(criteria) as QueryBuilder;
            var query = builder.Query as BooleanQuery;
            var analyzer = new StandardAnalyzer(u.Version.LUCENE_30);

            if (criteria is CatalogIndexedSearchCriteria)
            {
                var c = criteria as CatalogIndexedSearchCriteria;
                var datesFilterStart = new TermRangeQuery(
                    "startdate", c.StartDateFrom.HasValue ? DateTools.DateToString(c.StartDateFrom.Value, DateTools.Resolution.SECOND) : null, DateTools.DateToString(c.StartDate, DateTools.Resolution.SECOND), false, true);
                query.Add(datesFilterStart, Occur.MUST);

                if (c.EndDate.HasValue)
                {
                    var datesFilterEnd = new TermRangeQuery(
                        "enddate",
                        DateTools.DateToString(c.EndDate.Value, DateTools.Resolution.SECOND),
                        null,
                        true,
                        false);

                    query.Add(datesFilterEnd, Occur.MUST);
                }

                if (c.Outlines != null && c.Outlines.Count > 0)
                {
                    AddQuery("__outline", query, c.Outlines);
                }

                query.Add(new TermQuery(new Term("__hidden", "false")), Occur.MUST);

                if (!String.IsNullOrEmpty(c.Catalog))
                {
                    AddQuery("catalog", query, c.Catalog);
                }

                // Add search
                if (!String.IsNullOrEmpty(c.SearchPhrase))
                {
                    var searchPhrase = c.SearchPhrase;
                    if (c.IsFuzzySearch)
                    {

                        var keywords = c.SearchPhrase.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                        searchPhrase = string.Empty;
                        searchPhrase = keywords.Aggregate(
                            searchPhrase,
                            (current, keyword) =>
                                current + String.Format("{0}~{1}", keyword.Replace("~", ""), c.FuzzyMinSimilarity.ToString(CultureInfo.InvariantCulture)));
                    }

                    var fields = new List<string> { "__content" };
                    if (c.Locale != null)
                    {
                        var contentField = string.Format("__content_{0}", c.Locale.ToLower());
                        fields.Add(contentField);
                    }

                    var parser = new MultiFieldQueryParser(u.Version.LUCENE_30, fields.ToArray(), analyzer)
                                     {
                                         DefaultOperator =
                                             QueryParser
                                             .Operator.OR
                                     };

                    var searchQuery = parser.Parse(searchPhrase);
                    query.Add(searchQuery, Occur.MUST);

                }
            }
            //else if (criteria is OrderSearchCriteria)
            //{
            //	var c = criteria as OrderSearchCriteria;

            //	if (!String.IsNullOrEmpty(c.CustomerId))
            //	{
            //		AddQuery("customerid", query, c.CustomerId);
            //	}
            //}

            return builder;
        }
		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 TermRangeQuery which uses term expansion so
			// docs with more terms in range get higher scores
			
			Query rq = new TermRangeQuery("data", "1", "4", T, T);
			
			ScoreDoc[] expected = search.Search(rq, null, 1000).scoreDocs;
			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);
			
			ScoreDoc[] actual = search.Search(q, null, 1000).scoreDocs;
			
			AssertEquals("wrong numebr of hits", numHits, actual.Length);
			for (int i = 0; i < numHits; i++)
			{
				AssertEquals("mismatch in docid for hit#" + i, expected[i].doc, actual[i].doc);
			}
		}
		public virtual void  TestFarsi()
		{
			// Neither Java 1.4.2 nor 1.5.0 has Farsi Locale collation available in
			// RuleBasedCollator.  However, the Arabic Locale seems to order the Farsi
			// characters properly.
			System.Globalization.CompareInfo collator = new System.Globalization.CultureInfo("ar").CompareInfo;
			Query query = new TermRangeQuery("content", "\u062F", "\u0698", true, true, collator);
			// Unicode order would include U+0633 in [ U+062F - U+0698 ], but Farsi
			// orders the U+0698 character before the U+0633 character, so the single
			// index Term below should NOT be returned by a TermRangeQuery with a Farsi
			// Collator (or an Arabic one for the case when Farsi is not supported).
			InitializeIndex(new System.String[]{"\u0633\u0627\u0628"});
			IndexSearcher searcher = new IndexSearcher(dir);
			ScoreDoc[] hits = searcher.Search(query, null, 1000).ScoreDocs;
			Assert.AreEqual(0, hits.Length, "The index Term should not be included.");
			
			query = new TermRangeQuery("content", "\u0633", "\u0638", true, true, collator);
			hits = searcher.Search(query, null, 1000).ScoreDocs;
			Assert.AreEqual(1, hits.Length, "The index Term should be included.");
			searcher.Close();
		}
        /// <summary>
        /// 添加字符串类型的RangeQuery.
        /// </summary>
        /// <param name="fieldName">字段名称</param>
        /// <param name="min">最小值</param>
        /// <param name="max">最大值 </param>
        /// <returns>LuceneSearchBuilder</returns>
        public LuceneSearchBuilder WithinRange(string fieldName, string min, string max, bool asFilter = false)
        {
            Query query = new TermRangeQuery(fieldName, min, max, true, true);

            if (asFilter)
                filters.Add(new BooleanClause(query, BooleanClause.Occur.MUST));
            else
                clauses.Add(new BooleanClause(query, BooleanClause.Occur.MUST));

            return this;
        }
		public virtual void  TestInclusiveLowerNull()
		{
			//http://issues.apache.org/jira/browse/LUCENE-38
			Analyzer analyzer = new SingleCharAnalyzer();
			Query query = new TermRangeQuery("content", null, "C", true, true);
			InitializeIndex(new System.String[]{"A", "B", "", "C", "D"}, analyzer);
			IndexSearcher searcher = new IndexSearcher(dir);
			Hits hits = searcher.Search(query);
			// When Lucene-38 is fixed, use the assert on the next line:
			Assert.AreEqual(4, hits.Length(), "A,B,<empty string>,C,D => A,B,<empty string>,C in range");
			// until Lucene-38 is fixed, use this assert
            //Assert.AreEqual(3, hits.length(), "A,B,<empty string>,C,D => A,B,<empty string>,C in range");
			searcher.Close();
			InitializeIndex(new System.String[]{"A", "B", "", "D"}, analyzer);
			searcher = new IndexSearcher(dir);
			hits = searcher.Search(query);
			// When Lucene-38 is fixed, use the assert on the next line:
			Assert.AreEqual(3, hits.Length(), "A,B,<empty string>,D - A, B and <empty string> in range");
			// until Lucene-38 is fixed, use this assert
            //Assert.AreEqual(2, hits.length(), "A,B,<empty string>,D => A, B and <empty string> in range");
			searcher.Close();
			AddDoc("C");
			searcher = new IndexSearcher(dir);
			hits = searcher.Search(query);
			// When Lucene-38 is fixed, use the assert on the next line:
			Assert.AreEqual(4, hits.Length(), "C added => A,B,<empty string>,C in range");
			// until Lucene-38 is fixed, use this assert
            //Assert.AreEqual(3, hits.length(), "C added => A,B,<empty string>,C in range");
			searcher.Close();
		}
 private static void MultiTermQuery(TermRangeQuery query, AzureQueryLogger.IndentedTextWriter writer)
 {
     writer.WriteLine("Field: {0}", (object)query.Field);
     writer.WriteLine("Collator: {0}", (object)query.Collator);
     writer.WriteLine("IncludesLower: {0}", (query.IncludesLower ? 1 : 0));
     writer.WriteLine("IncludesUpper: {0}", (query.IncludesUpper ? 1 : 0));
     writer.WriteLine("LowerTerm: {0}", (object)query.LowerTerm);
     writer.WriteLine("UpperTerm: {0}", (object)query.UpperTerm);
 }
Beispiel #25
0
        public static List<IndexItem> GetRecentModifiedContent(
            int siteId,
            Guid[] featureGuids,
            DateTime modifiedSinceDate,
            int maxItems)
        {
            int totalHits = 0;

            List<IndexItem> results = new List<IndexItem>();

            using (Lucene.Net.Store.Directory searchDirectory = GetDirectory(siteId))
            {
                Filter filter = null;
                BooleanQuery filterQuery = new BooleanQuery(); // won't be used to score the results

                BooleanQuery excludeFilter = new BooleanQuery();
                excludeFilter.Add(new TermQuery(new Term("ExcludeFromRecentContent", "false")), Occur.MUST);
                filterQuery.Add(excludeFilter, Occur.MUST);

                TermRangeQuery lastModifiedDateFilter = new TermRangeQuery(
                    "LastModUtc",
                    modifiedSinceDate.Date.ToString("s"),
                    DateTime.MaxValue.ToString("s"),
                    true,
                    true);

                filterQuery.Add(lastModifiedDateFilter, Occur.MUST);

                // we only want public content, that is both page and module roles must have "All Users"
                // which means even unauthenticated users
                Term pageRole = new Term("Role", "All Users");
                TermQuery pageRoleFilter = new TermQuery(pageRole);
                filterQuery.Add(pageRoleFilter, Occur.MUST);

                Term moduleRole = new Term("ModuleRole", "All Users");
                TermQuery moduleRoleFilter = new TermQuery(moduleRole);

                filterQuery.Add(moduleRoleFilter, Occur.MUST);

                if ((featureGuids != null)&&(featureGuids.Length > 0))
                {
                    BooleanQuery featureFilter = new BooleanQuery();

                    foreach (Guid featureGuid in featureGuids)
                    {
                        featureFilter.Add(new TermQuery(new Term("FeatureId", featureGuid.ToString())), Occur.SHOULD);

                    }

                    filterQuery.Add(featureFilter, Occur.MUST);

                }

                filter = new QueryWrapperFilter(filterQuery); // filterQuery won't affect result scores

                MatchAllDocsQuery matchAllQuery = new MatchAllDocsQuery();

                using (IndexSearcher searcher = new IndexSearcher(searchDirectory))
                {

                    int maxResults = int.MaxValue;
                    TopDocs hits = searcher.Search(matchAllQuery, filter, maxResults);
                    totalHits = hits.TotalHits;

                    for (int i = 0; i < totalHits; i++)
                    {
                        Document doc = searcher.Doc(hits.ScoreDocs[i].Doc);
                        IndexItem indexItem = new IndexItem(doc, hits.ScoreDocs[i].Score);

                        results.Add(indexItem);

                    }

                }

            }

            // sort all descending on lastmodutc
            results.Sort();

            if (results.Count <= maxItems)
            {
                return results;
            }
            else
            {
                List<IndexItem> finalResults = new List<IndexItem>();
                for (int i = 0; i < maxItems; i++)
                {
                    finalResults.Add(results[i]);
                }

                return finalResults;

            }
        }
Beispiel #26
0
        public void QueryCaseSensitiveTermRange()
        {
            BooleanQuery originalQuery = new BooleanQuery();
            TermRangeQuery termRangeQuery = new TermRangeQuery("field", "Lower", "Upper", true, true);
            originalQuery.Add(termRangeQuery, Occur.MUST);
            string queryString = originalQuery.ToString();

            QueryBuilder builder = new QueryBuilder{CaseSensitive = true};
            builder.Setup(x => x.TermRange("field", "Lower", "Upper"));
            Query replacementQuery = builder.Build();
            string newQueryString = replacementQuery.ToString();

            Assert.AreEqual(queryString, newQueryString);
            Console.Write(queryString);
        }
Beispiel #27
0
        public static IndexItemCollection Search(
            int siteId,
            bool isAdminContentAdminOrSiteEditor,
            List<string> userRoles,
            Guid[] featureGuids,
            DateTime modifiedBeginDate,
            DateTime modifiedEndDate,
            string queryText,
            bool highlightResults,
            int highlightedFragmentSize,
            int pageNumber,
            int pageSize,
            int maxClauseCount,
            out int totalHits,
            out bool invalidQuery)
        {
            invalidQuery = false;
            totalHits = 0;

            IndexItemCollection results = new IndexItemCollection();

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

            using (Lucene.Net.Store.Directory searchDirectory = GetDirectory(siteId))
            {
                if (!IndexReader.IndexExists(searchDirectory)) { return results; }

                long startTicks = DateTime.Now.Ticks;

                try
                {
                    if (maxClauseCount != 1024)
                    {
                        BooleanQuery.MaxClauseCount = maxClauseCount;
                    }

                    // there are different analyzers for different languages
                    // see LuceneSettings.config in the root of the web
                    LuceneSettingsProvider provider = LuceneSettingsManager.Providers[GetSiteProviderName(siteId)];
                    Analyzer analyzer = provider.GetAnalyzer();

                    Query searchQuery = MultiFieldQueryParser.Parse(
                        Lucene.Net.Util.Version.LUCENE_30,
                        new string[] { queryText, queryText, queryText, queryText, queryText, queryText.Replace("*", string.Empty) },
                        new string[] { "Title", "ModuleTitle", "contents", "PageName", "PageMetaDesc", "Keyword" },
                        analyzer);

                    BooleanQuery filterQuery = new BooleanQuery(); // won't be used to score the results

                    if (!isAdminContentAdminOrSiteEditor) // skip role filters for these users
                    {
                        AddRoleFilters(userRoles, filterQuery);
                        AddModuleRoleFilters(userRoles, filterQuery);
                    }

                    TermRangeQuery beginDateFilter = new TermRangeQuery(
                        "PublishBeginDate",
                        DateTime.MinValue.ToString("s"),
                        DateTime.UtcNow.ToString("s"),
                        true,
                        true);

                    filterQuery.Add(beginDateFilter, Occur.MUST);

                    TermRangeQuery endDateFilter = new TermRangeQuery(
                        "PublishEndDate",
                        DateTime.UtcNow.ToString("s"),
                        DateTime.MaxValue.ToString("s"),
                        true,
                        true);

                    filterQuery.Add(endDateFilter, Occur.MUST);

                    if ((modifiedBeginDate.Date > DateTime.MinValue.Date) || (modifiedEndDate.Date < DateTime.MaxValue.Date))
                    {
                        TermRangeQuery lastModifiedDateFilter = new TermRangeQuery(
                            "LastModUtc",
                            modifiedBeginDate.Date.ToString("s"),
                            modifiedEndDate.Date.ToString("s"),
                            true,
                            true);

                        filterQuery.Add(lastModifiedDateFilter, Occur.MUST);
                    }

                    //if ((!DisableSearchFeatureFilters) && (featureGuid != Guid.Empty))
                    //{
                    //    BooleanQuery featureFilter = new BooleanQuery();

                    //    featureFilter.Add(new TermQuery(new Term("FeatureId", featureGuid.ToString())), Occur.MUST);

                    //    filterQuery.Add(featureFilter, Occur.MUST);
                    //}

                    if ((featureGuids != null) && (featureGuids.Length > 0))
                    {
                        BooleanQuery featureFilter = new BooleanQuery();

                        foreach (Guid featureGuid in featureGuids)
                        {
                            featureFilter.Add(new TermQuery(new Term("FeatureId", featureGuid.ToString())), Occur.SHOULD);
                        }

                        filterQuery.Add(featureFilter, Occur.MUST);
                    }

                    Filter filter = new QueryWrapperFilter(filterQuery); // filterQuery won't affect result scores

                    using (IndexSearcher searcher = new IndexSearcher(searchDirectory))
                    {

                        //http://stackoverflow.com/questions/9872933/migrating-lucene-hitcollector-2-x-to-collector-3-x
                        //TopScoreDocCollector collector = TopScoreDocCollector.Create(maxResults, true);

                        int maxResults = int.MaxValue;
                        TopDocs hits = searcher.Search(searchQuery, filter, maxResults);

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

                        totalHits = hits.TotalHits;

                        int end = startHit + pageSize;
                        if (totalHits <= end)
                        {
                            end = totalHits;
                        }

                        int itemsAdded = 0;
                        int itemsToAdd = end;

                        QueryScorer scorer = new QueryScorer(searchQuery);
                        SimpleHTMLFormatter formatter = new SimpleHTMLFormatter("<span class='searchterm'>", "</span>");
                        Highlighter highlighter = new Highlighter(formatter, scorer);

                        highlighter.TextFragmenter = new SimpleFragmenter(highlightedFragmentSize);

                        for (int i = startHit; i < itemsToAdd; i++)
                        {
                            Document doc = searcher.Doc(hits.ScoreDocs[i].Doc);
                            IndexItem indexItem = new IndexItem(doc, hits.ScoreDocs[i].Score);

                            if (highlightResults)
                            {
                                try
                                {
                                    TokenStream stream = analyzer.TokenStream("contents", new StringReader(doc.Get("contents")));
                                    string highlightedResult = highlighter.GetBestFragment(stream, doc.Get("contents"));

                                    if (highlightedResult != null) { indexItem.Intro = highlightedResult; }
                                }
                                catch (NullReferenceException) { }
                            }

                            results.Add(indexItem);
                            itemsAdded += 1;

                        }

                        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;

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

                }

                return results;
            }
        }
Beispiel #28
0
        public virtual TermRangeQuery TermRange(string fieldName, string rangeStart, string rangeEnd, bool includeLower = true, bool includeUpper = true,
										BooleanClause.Occur occur = null, float? boost = null, string key = null, bool? caseSensitive = null)
        {
            if (caseSensitive.HasValue)
            {
                if (!caseSensitive.Value)
                {
                    rangeStart = rangeStart.ToLowerInvariant();
                    rangeEnd = rangeEnd.ToLowerInvariant();
                }
            }
            else if(!CaseSensitive)
            {
                rangeStart = rangeStart.ToLowerInvariant();
                rangeEnd = rangeEnd.ToLowerInvariant();
            }
            TermRangeQuery query = new TermRangeQuery(QueryParser.Escape(fieldName), rangeStart, rangeEnd, includeLower, includeUpper);
            SetBoostValue(query, boost);
            Add(query, occur, key);
            return query;
        }
        public virtual void TestRewrite()
        {
            IndexReader r = DirectoryReader.Open(dir);
            IndexSearcher s = NewSearcher(r);

            Query q = new TermQuery(new Term(TEXT_FIELD, "first"));
            CustomScoreQuery original = new CustomScoreQuery(q);
            CustomScoreQuery rewritten = (CustomScoreQuery)original.Rewrite(s.IndexReader);
            assertTrue("rewritten query should be identical, as TermQuery does not rewrite", original == rewritten);
            assertTrue("no hits for query", s.Search(rewritten, 1).TotalHits > 0);
            assertEquals(s.Search(q, 1).TotalHits, s.Search(rewritten, 1).TotalHits);

            q = new TermRangeQuery(TEXT_FIELD, null, null, true, true); // everything
            original = new CustomScoreQuery(q);
            rewritten = (CustomScoreQuery)original.Rewrite(s.IndexReader);
            assertTrue("rewritten query should not be identical, as TermRangeQuery rewrites", original != rewritten);
            assertTrue("no hits for query", s.Search(rewritten, 1).TotalHits > 0);
            assertEquals(s.Search(q, 1).TotalHits, s.Search(original, 1).TotalHits);
            assertEquals(s.Search(q, 1).TotalHits, s.Search(rewritten, 1).TotalHits);

            r.Dispose();
        }
        /// <summary>
        /// check that the # of hits is the same as if the query
        /// is run against the inverted index
        /// </summary>
        protected internal virtual void AssertSame(BytesRef lowerVal, BytesRef upperVal, bool includeLower, bool includeUpper)
        {
            Query docValues = new ConstantScoreQuery(DocTermOrdsRangeFilter.NewBytesRefRange(FieldName, lowerVal, upperVal, includeLower, includeUpper));
            MultiTermQuery inverted = new TermRangeQuery(FieldName, lowerVal, upperVal, includeLower, includeUpper);
            inverted.SetRewriteMethod(MultiTermQuery.CONSTANT_SCORE_FILTER_REWRITE);

            TopDocs invertedDocs = Searcher1.Search(inverted, 25);
            TopDocs docValuesDocs = Searcher2.Search(docValues, 25);

            CheckHits.CheckEqual(inverted, invertedDocs.ScoreDocs, docValuesDocs.ScoreDocs);
        }
		public virtual void  TestDanish()
		{
			
			/* build an index */
			RAMDirectory danishIndex = new RAMDirectory();
			IndexWriter writer = new IndexWriter(danishIndex, new SimpleAnalyzer(), T, IndexWriter.MaxFieldLength.LIMITED);
			// Danish collation orders the words below in the given order
			// (example taken from TestSort.testInternationalSort() ).
			System.String[] words = new System.String[]{"H\u00D8T", "H\u00C5T", "MAND"};
			for (int docnum = 0; docnum < words.Length; ++docnum)
			{
				Document doc = new Document();
				doc.Add(new Field("content", words[docnum], Field.Store.YES, Field.Index.NOT_ANALYZED));
				doc.Add(new Field("body", "body", Field.Store.YES, Field.Index.NOT_ANALYZED));
				writer.AddDocument(doc);
			}
			writer.Optimize();
			writer.Close();

            IndexReader reader = IndexReader.Open(danishIndex, true);
			IndexSearcher search = new IndexSearcher(reader);
			Query q = new TermQuery(new Term("body", "body"));
			
			System.Globalization.CompareInfo collator = new System.Globalization.CultureInfo("da" + "-" + "dk").CompareInfo;
			Query query = new TermRangeQuery("content", "H\u00D8T", "MAND", false, false, collator);
			
			// Unicode order would not include "H\u00C5T" in [ "H\u00D8T", "MAND" ],
			// but Danish collation does.
            int numHits =
                search.Search(q, new TermRangeFilter("content", "H\u00D8T", "MAND", F, F, collator), 1000).TotalHits;
			Assert.AreEqual(1, numHits, "The index Term should be included.");
			
			numHits = search.Search(q, new TermRangeFilter("content", "H\u00C5T", "MAND", F, F, collator), 1000).TotalHits;
			Assert.AreEqual(0, numHits, "The index Term should not be included.");
			search.Close();
		}
        private void TestRandomTrieAndClassicRangeQuery(int precisionStep)
        {
            string field = "field" + precisionStep;
            int    totalTermCountT = 0, totalTermCountC = 0, termCountT, termCountC;
            int    num = TestUtil.NextInt(Random(), 10, 20);

            for (int i = 0; i < num; i++)
            {
                long lower = (long)(Random().NextDouble() * NoDocs * Distance) + StartOffset;
                long upper = (long)(Random().NextDouble() * NoDocs * Distance) + StartOffset;
                if (lower > upper)
                {
                    long a = lower;
                    lower = upper;
                    upper = a;
                }
                BytesRef lowerBytes = new BytesRef(NumericUtils.BUF_SIZE_INT64), upperBytes = new BytesRef(NumericUtils.BUF_SIZE_INT64);
                NumericUtils.Int64ToPrefixCodedBytes(lower, 0, lowerBytes);
                NumericUtils.Int64ToPrefixCodedBytes(upper, 0, upperBytes);

                // test inclusive range
                NumericRangeQuery <long> tq = NumericRangeQuery.NewInt64Range(field, precisionStep, lower, upper, true, true);
                TermRangeQuery           cq = new TermRangeQuery(field, lowerBytes, upperBytes, true, true);
                TopDocs tTopDocs            = Searcher.Search(tq, 1);
                TopDocs cTopDocs            = Searcher.Search(cq, 1);
                Assert.AreEqual(cTopDocs.TotalHits, tTopDocs.TotalHits, "Returned count for NumericRangeQuery and TermRangeQuery must be equal");
                totalTermCountT += termCountT = CountTerms(tq);
                totalTermCountC += termCountC = CountTerms(cq);
                CheckTermCounts(precisionStep, termCountT, termCountC);
                // test exclusive range
                tq       = NumericRangeQuery.NewInt64Range(field, precisionStep, lower, upper, false, false);
                cq       = new TermRangeQuery(field, lowerBytes, upperBytes, false, false);
                tTopDocs = Searcher.Search(tq, 1);
                cTopDocs = Searcher.Search(cq, 1);
                Assert.AreEqual(cTopDocs.TotalHits, tTopDocs.TotalHits, "Returned count for NumericRangeQuery and TermRangeQuery must be equal");
                totalTermCountT += termCountT = CountTerms(tq);
                totalTermCountC += termCountC = CountTerms(cq);
                CheckTermCounts(precisionStep, termCountT, termCountC);
                // test left exclusive range
                tq       = NumericRangeQuery.NewInt64Range(field, precisionStep, lower, upper, false, true);
                cq       = new TermRangeQuery(field, lowerBytes, upperBytes, false, true);
                tTopDocs = Searcher.Search(tq, 1);
                cTopDocs = Searcher.Search(cq, 1);
                Assert.AreEqual(cTopDocs.TotalHits, tTopDocs.TotalHits, "Returned count for NumericRangeQuery and TermRangeQuery must be equal");
                totalTermCountT += termCountT = CountTerms(tq);
                totalTermCountC += termCountC = CountTerms(cq);
                CheckTermCounts(precisionStep, termCountT, termCountC);
                // test right exclusive range
                tq       = NumericRangeQuery.NewInt64Range(field, precisionStep, lower, upper, true, false);
                cq       = new TermRangeQuery(field, lowerBytes, upperBytes, true, false);
                tTopDocs = Searcher.Search(tq, 1);
                cTopDocs = Searcher.Search(cq, 1);
                Assert.AreEqual(cTopDocs.TotalHits, tTopDocs.TotalHits, "Returned count for NumericRangeQuery and TermRangeQuery must be equal");
                totalTermCountT += termCountT = CountTerms(tq);
                totalTermCountC += termCountC = CountTerms(cq);
                CheckTermCounts(precisionStep, termCountT, termCountC);
            }

            CheckTermCounts(precisionStep, totalTermCountT, totalTermCountC);
            if (VERBOSE && precisionStep != int.MaxValue)
            {
                Console.WriteLine("Average number of terms during random search on '" + field + "':");
                Console.WriteLine(" Numeric query: " + (((double)totalTermCountT) / (num * 4)));
                Console.WriteLine(" Classical query: " + (((double)totalTermCountC) / (num * 4)));
            }
        }