public void TestGetFilterDouble() { NumericRangeFilterBuilder filterBuilder = new NumericRangeFilterBuilder(); filterBuilder.SetStrictMode(true); String xml = "<NumericRangeFilter fieldName='AGE' type='doubLe' lowerTerm='-23.21' upperTerm='60000.00023'/>"; XmlDocument doc = GetDocumentFromString(xml); Filter filter = filterBuilder.GetFilter(doc.DocumentElement); assertTrue(filter is NumericRangeFilter <double>); NumericRangeFilter <double> numRangeFilter = (NumericRangeFilter <double>)filter; assertEquals(Convert.ToDouble(-23.21d), numRangeFilter.Min); assertEquals(Convert.ToDouble(60000.00023d), numRangeFilter.Max); assertEquals("AGE", numRangeFilter.Field); assertTrue(numRangeFilter.IncludesMin()); assertTrue(numRangeFilter.IncludesMax()); String xml2 = "<NumericRangeFilter fieldName='AGE' type='doubLe' lowerTerm='-23.21' upperTerm='60000.00023' includeUpper='false'/>"; XmlDocument doc2 = GetDocumentFromString(xml2); Filter filter2 = filterBuilder.GetFilter(doc2.DocumentElement); assertTrue(filter2 is NumericRangeFilter <double>); NumericRangeFilter <double> numRangeFilter2 = (NumericRangeFilter <double>)filter2; assertEquals(Convert.ToDouble(-23.21d), numRangeFilter2.Min); assertEquals(Convert.ToDouble(60000.00023d), numRangeFilter2.Max); assertEquals("AGE", numRangeFilter2.Field); assertTrue(numRangeFilter2.IncludesMin()); assertFalse(numRangeFilter2.IncludesMax()); }
public static List <Note> Search(string text, User user, bool myOnly, DateTime from, DateTime to, int top, long?gen = null) { var parser = new MultiFieldQueryParser(LuceneVersion.LUCENE_48, SearchFields, analyzer) { DefaultOperator = Operator.AND }; var escapedText = text.TrimToNull().EscapeText() ?? EmptyRequest; var escapedLogin = (myOnly ? user?.Login.EscapeKeyword() : null) ?? EmptyRequest; var query = parser.Parse($"(({escapedText}) OR ({escapedText.HashWords(user?.Key)})) login:{escapedLogin}"); if (gen != null && gen <= trackingWriter.Generation) { reopenThread.WaitForGeneration(gen.Value, 3000); } var searcher = searcherKeeper.Acquire(); try { var filter = NumericRangeFilter.NewInt64Range("time", from.ToUnixTimeMinutes(), to.ToUnixTimeMinutes(), true, true); var hits = searcher.Search(query, filter, top <= 0 ? 1 : top, IndexDescSortOrder, false, false); return(hits.ScoreDocs.Take(top).Select(hit => searcher.Doc(hit.Doc)).Select(doc => new Note { Author = doc.Get("author"), Title = doc.Get("title"), Text = doc.Get("text"), Time = doc.GetField("time")?.GetInt64Value()?.FromUnixTimeMinutes() ?? default }).ToList()); }
public static Filter CreateRangeFilterForValue(string fieldName, string lower, string upper, bool includeLower, bool includeUpper) { Filter result = null; // If both bounds are empty, ignore this range if (!string.IsNullOrEmpty(lower) || !string.IsNullOrEmpty(upper)) { var lowerLong = ConvertToDateTimeTicks(lower); var upperLong = ConvertToDateTimeTicks(upper); if (lowerLong != null || upperLong != null) { result = NumericRangeFilter.NewInt64Range(fieldName, lowerLong, upperLong, includeLower, includeUpper); } else { var lowerDouble = ConvertToDouble(lower); var upperDouble = ConvertToDouble(upper); if (lowerDouble != null || upperDouble != null) { result = NumericRangeFilter.NewDoubleRange(fieldName, lowerDouble, upperDouble, includeLower, includeUpper); } else { result = TermRangeFilter.NewStringRange(fieldName, lower, upper, includeLower, includeUpper); } } } return(result); }
public static LuceneResultNode SearchEx(IndexBase index, SearchNode node) { try { var query = MultiFieldQueryParser.Parse(Lucene.Net.Util.Version.LUCENE_29, node.Queries, node.Fields, node.Occurs, new PanGuAnalyzer(true)); foreach (NumberRangeNode n in node.NumberRangeFiters) { query = new FilteredQuery(query, NumericRangeFilter.NewIntRange(n.FieldName, n.MinValue, n.MaxValue, true, true)); } foreach (LongRangeNode lr in node.LongRnageFilters) { query = new FilteredQuery(query, NumericRangeFilter.NewLongRange(lr.FieldName, lr.MinValue, lr.MaxValue, true, true)); } foreach (ContainFilterNode cf in node.ContainFilters) { query = new FilteredQuery(query, new ContainFilter(new Term(cf.FieldName, cf.Text))); } return(ResponseSearch(index, query, node, 0)); } catch (Lucene.Net.QueryParsers.ParseException) { return(new LuceneResultNode() { AllCount = 0, Result = new List <string>() }); } catch (Exception ex) { throw ex; } }
public void TestGetFilterInt() { NumericRangeFilterBuilder filterBuilder = new NumericRangeFilterBuilder(); filterBuilder.SetStrictMode(true); String xml = "<NumericRangeFilter fieldName='AGE' type='int' lowerTerm='-1' upperTerm='10'/>"; XmlDocument doc = GetDocumentFromString(xml); Filter filter = filterBuilder.GetFilter(doc.DocumentElement); assertTrue(filter is NumericRangeFilter <int>); NumericRangeFilter <int> numRangeFilter = (NumericRangeFilter <int>)filter; assertEquals(Convert.ToInt32(-1), numRangeFilter.Min); assertEquals(Convert.ToInt32(10), numRangeFilter.Max); assertEquals("AGE", numRangeFilter.Field); assertTrue(numRangeFilter.IncludesMin()); assertTrue(numRangeFilter.IncludesMax()); String xml2 = "<NumericRangeFilter fieldName='AGE' type='int' lowerTerm='-1' upperTerm='10' includeUpper='false'/>"; XmlDocument doc2 = GetDocumentFromString(xml2); Filter filter2 = filterBuilder.GetFilter(doc2.DocumentElement); assertTrue(filter2 is NumericRangeFilter <int>); NumericRangeFilter <int> numRangeFilter2 = (NumericRangeFilter <int>)filter2; assertEquals(Convert.ToInt32(-1), numRangeFilter2.Min); assertEquals(Convert.ToInt32(10), numRangeFilter2.Max); assertEquals("AGE", numRangeFilter2.Field); assertTrue(numRangeFilter2.IncludesMin()); assertFalse(numRangeFilter2.IncludesMax()); }
public async Task ShouldAggregateWithoutAggregators() { // Todo: does not find anything // Check: does Aggregate() without Aggregators make any sense? //// TODO: Must be extended and asserted with useful data. var request = new ContentAggregationRequest() { SearchString = "*" }; ObjectAggregationResult result = await _client.Contents.AggregateAsync(request); var numRangeFilter = new NumericRangeFilter() { Field = "ContentType", Range = new NumericRange { From = 2, To = 5 } }; request.Filter = numRangeFilter; result = await _client.Contents.AggregateAsync(request); request.Filter = null; request.LifeCycleFilter = LifeCycleFilter.All; result = await _client.Contents.AggregateAsync(request); request.Aggregators = new List <AggregatorBase>(); result = await _client.Contents.AggregateAsync(request); }
/// <summary> ///根据关键字进行查询,根据int的范围进行搜索 /// </summary> /// <param name="Title"></param> /// <returns></returns> public List <Bpo_JobEntity> SearchJobList3(string Title, int MinId, int MaxId) { List <Bpo_JobEntity> jobList = new List <Bpo_JobEntity>(); string path = "F://LuceneIndexDir"; FSDirectory dir = FSDirectory.Open(path); IndexSearcher searcher = new IndexSearcher(dir);//查询器 /* * Title参数根据判断进行分词,可以支持分词查询,多个词之间是or的关系,搜索的时候中间加个空格, * 如:wcs 单子 这样就会把包含wcs 或 包含单子关键字的工单搜索出来 */ QueryParser parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, "Title", new PanGuAnalyzer()); Query query = parser.Parse(Title); //根据名称查询 //根据id区间区间搜索 NumericRangeFilter <int> intFilter = null; if (MinId > 0 && MaxId == 0) { intFilter = NumericRangeFilter.NewIntRange("Id", MinId, int.MaxValue, true, true); } else if (MaxId > 0 && MinId == 0) { intFilter = NumericRangeFilter.NewIntRange("Id", 0, MaxId, true, true); } else if (MaxId > 0 && MinId > 0) { intFilter = NumericRangeFilter.NewIntRange("Id", MinId, MaxId, true, true); } //定义排序 SortField sortField = new SortField("Id", SortField.INT, false); //降序 SortField sortField2 = new SortField("CompanyId", SortField.INT, false); //降序 Sort sort = new Sort(sortField, sortField2); //取搜索结果方法1: TopDocs docs = searcher.Search(query, intFilter, 1000, sort); //找到的结果取100条数据 foreach (ScoreDoc sd in docs.ScoreDocs) //从docs.ScoreDocs取数据 { Document doc = searcher.Doc(sd.Doc); jobList.Add(new Bpo_JobEntity { Id = Convert.ToInt32(doc.Get("Id") ?? "-100"), Title = doc.Get("Title"), UserId = Convert.ToInt32(doc.Get("UserId") ?? "-100"), UserName = doc.Get("UserName"), CompanyId = Convert.ToInt32(doc.Get("CompanyId") ?? "-100"), CompanyName = doc.Get("CompanyName"), FullAddress = doc.Get("FullAddress"), CreateDate = Convert.ToDateTime(doc.Get("CreateDate")) }); } return(jobList); }
public virtual Filter GetFilter(XmlElement e) { string field = DOMUtils.GetAttributeWithInheritanceOrFail(e, "fieldName"); string lowerTerm = DOMUtils.GetAttributeOrFail(e, "lowerTerm"); string upperTerm = DOMUtils.GetAttributeOrFail(e, "upperTerm"); bool lowerInclusive = DOMUtils.GetAttribute(e, "includeLower", true); bool upperInclusive = DOMUtils.GetAttribute(e, "includeUpper", true); int precisionStep = DOMUtils.GetAttribute(e, "precisionStep", NumericUtils.PRECISION_STEP_DEFAULT); string type = DOMUtils.GetAttribute(e, "type", "int"); try { Filter filter; if (type.Equals("int", StringComparison.OrdinalIgnoreCase)) { filter = NumericRangeFilter.NewInt32Range(field, precisionStep, Convert .ToInt32(lowerTerm, CultureInfo.InvariantCulture), Convert.ToInt32(upperTerm, CultureInfo.InvariantCulture), lowerInclusive, upperInclusive); } else if (type.Equals("long", StringComparison.OrdinalIgnoreCase)) { filter = NumericRangeFilter.NewInt64Range(field, precisionStep, Convert .ToInt64(lowerTerm, CultureInfo.InvariantCulture), Convert.ToInt64(upperTerm, CultureInfo.InvariantCulture), lowerInclusive, upperInclusive); } else if (type.Equals("double", StringComparison.OrdinalIgnoreCase)) { filter = NumericRangeFilter.NewDoubleRange(field, precisionStep, Convert .ToDouble(lowerTerm, CultureInfo.InvariantCulture), Convert.ToDouble(upperTerm, CultureInfo.InvariantCulture), lowerInclusive, upperInclusive); } else if (type.Equals("float", StringComparison.OrdinalIgnoreCase)) { filter = NumericRangeFilter.NewSingleRange(field, precisionStep, Convert .ToSingle(lowerTerm, CultureInfo.InvariantCulture), Convert.ToSingle(upperTerm, CultureInfo.InvariantCulture), lowerInclusive, upperInclusive); } else { throw new ParserException("type attribute must be one of: [long, int, double, float]"); } return(filter); } catch (FormatException nfe) { if (strictMode) { throw new ParserException("Could not parse lowerTerm or upperTerm into a number", nfe); } return(NO_MATCH_FILTER); } }
public List <News> Search(string keywords) { Directory dir = FSDirectory.Open(new io.DirectoryInfo(HttpContext.Current.Server.MapPath("/Indexs/")), new SimpleFSLockFactory()); IndexReader reader = IndexReader.Open(dir, true); IndexSearcher search = new IndexSearcher(reader); MultiFieldQueryParser multifield = new MultiFieldQueryParser(Lucene.Net.Util.Version.LUCENE_30, new string[] { "Title", "Content" }, new PanGuAnalyzer()); multifield.PhraseSlop = 3; multifield.DefaultOperator = QueryParser.Operator.AND; Query muqu = multifield.Parse(keywords); //MultiPhraseQuery multi = new MultiPhraseQuery(); //multi.Add(new Term[] {new Term("Content","中国"), new Term("Content", "智慧"), new Term("Title", "中国"), new Term("Title", "智慧") }); //PhraseQuery query = new PhraseQuery(); //query.Add(new Term("Content", keywords)); NumericRangeFilter <int> filter = NumericRangeFilter.NewIntRange("NewsId", 1, 10, true, true); Sort sort = new Sort(); sort.SetSort(new SortField("OrderId", SortField.LONG, true)); TopFieldDocs fields = search.Search(muqu, filter, 1000, sort); ScoreDoc[] docs = fields.ScoreDocs; List <News> newslist = new List <News>(); for (int i = 0; i < docs.Length; i++) { News news = new News(); Document doc = search.Doc(docs[i].Doc); news.NewsId = Convert.ToInt32(doc.Get("NewsId")); news.Title = doc.Get("Title"); SimpleHTMLFormatter formatter = new SimpleHTMLFormatter("<span style=\"color:red\">", "</span>"); Highlighter high = new Highlighter(formatter, new PanGu.Segment()); high.FragmentSize = 120; news.Content = high.GetBestFragment(keywords, doc.Get("Content")); news.AddTime = Convert.ToDateTime(doc.Get("Date")); news.OrderId = Convert.ToInt64(doc.Get("OrderId")); newslist.Add(news); } return(newslist); }
protected SalaryFieldHandler(string minFieldName, string maxFieldName, IBooster booster) { _minFieldName = minFieldName; _maxFieldName = maxFieldName; _booster = booster; _nullSalaryFilter = NumericRangeFilter.newIntRange(_maxFieldName, new java.lang.Integer(0), new java.lang.Integer(0), true, true); // nonNullSalary <=> NOT nullSalary _nonNullSalaryFilter = new BooleanFilter(); _nonNullSalaryFilter.add(new FilterClause(_nullSalaryFilter, BooleanClause.Occur.MUST_NOT)); }
public static void Show() { FSDirectory dir = FSDirectory.Open(TestIndexPath); IndexSearcher searcher = new IndexSearcher(dir); //查找器 { TermQuery query = new TermQuery(new Term("title", "英文")); //包含 TopDocs docs = searcher.Search(query, null, 10000); //找到的数据 foreach (ScoreDoc sd in docs.ScoreDocs) { Document doc = searcher.Doc(sd.Doc); Console.WriteLine(string.Format("{0} {1} {2} id={3}", doc.Get("title"), doc.Get("time"), doc.Get("price"), doc.Get("id"))); } Console.WriteLine("1一共命中了{0}个", docs.TotalHits); } QueryParser parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, "title", new PanGuAnalyzer());//解析器 { string keyword = "旧时光咖啡打几个给答复是加快了高科技打瞌睡了加工费拉的方式及规范了卡迪夫空间是梵蒂冈加工费"; //string keyword = "旧时光 咖啡 打几个 给答复 是加快了 高科技 打瞌睡 了加工费 拉的方式及规范了卡迪夫空间是梵蒂冈加工费"; Query query = parser.Parse(keyword); { TopDocs docs = searcher.Search(query, null, 10000);//找到的数据 foreach (ScoreDoc sd in docs.ScoreDocs) { Document doc = searcher.Doc(sd.Doc); Console.WriteLine(string.Format("{0} {1} {2} id={3}", doc.Get("title"), doc.Get("time"), doc.Get("price"), doc.Get("id"))); } Console.WriteLine("2一共命中了{0}个", docs.TotalHits); } { NumericRangeFilter <int> timeFilter = NumericRangeFilter.NewIntRange("time", 20150000, 20151822, true, true); //过滤 SortField sortPrice = new SortField("price", SortField.DOUBLE, false); //降序 SortField sortTime = new SortField("time", SortField.INT, true); //升序 Sort sort = new Sort(sortPrice, sortTime); //排序 TopDocs docs = searcher.Search(query, timeFilter, 10000, sort); //找到的数据 string result = ""; foreach (ScoreDoc sd in docs.ScoreDocs) { Document doc = searcher.Doc(sd.Doc); Console.WriteLine(string.Format("{0} {1} {2} id={3}", doc.Get("title"), doc.Get("time"), doc.Get("price"), doc.Get("id"))); if (doc.Get("title").Contains("金币卡")) { result += doc.Get("title"); } } Console.WriteLine("3一共命中了{0}个", docs.TotalHits); //Console.WriteLine("result={0}", result); } } }
/// <summary> /// 通过类型来获取 /// </summary> /// <param name="property"></param> /// <param name="min"></param> /// <param name="max"></param> /// <returns></returns> public static Filter GetNumberFilter(PropertyInfo property, double min, double max) { if (property.PropertyType == typeof(double) || property.PropertyType == typeof(decimal)) { return(NumericRangeFilter.NewDoubleRange(property.Name, min, max, true, true)); } else if (property.PropertyType == typeof(float)) { return(NumericRangeFilter.NewFloatRange(property.Name, (float)min, (float)max, true, true)); } else if (property.PropertyType == typeof(int)) { return(NumericRangeFilter.NewIntRange(property.Name, (int)min, (int)max, true, true)); } else { return(null); } }
public List <Product> Search(string keyWords) { List <Product> product = new List <Product>(); Query query = new TermQuery(new Term("Content", keyWords)); System.IO.DirectoryInfo IndexDir = new System.IO.DirectoryInfo(path); Directory dict = FSDirectory.Open(IndexDir, new SimpleFSLockFactory()); //搜索器 IndexSearcher search = new IndexSearcher(dict, true); //过滤器 NumericRangeFilter <int> filter = NumericRangeFilter.NewIntRange("ProductId", 1, 6, true, true); //排序字段 Sort sort = new Sort(new SortField("OrderId", SortField.LONG, true)); //执行搜索 TopFieldDocs docs = search.Search(query, null, 1000, sort); foreach (var p in docs.ScoreDocs) { Product pro = new Product(); Document doc = search.Doc(p.Doc); pro.ProductId = Convert.ToInt32(doc.Get("ProductId")); pro.ProductName = $"{doc.Get("ProductName")}文档ID:{p.Doc},内部ID:{pro.ProductId}"; SimpleHTMLFormatter html = new SimpleHTMLFormatter("<span style=\"color:red\">", "</span>"); Highlighter high = new Highlighter(html, new PanGu.Segment()); high.FragmentSize = 120; pro.Detail = high.GetBestFragment(keyWords, doc.Get("Content")); pro.Detail = doc.Get("Content"); pro.CreateTime = doc.Get("CreateTime"); pro.OrderId = doc.Get("OrderId"); product.Add(pro); } search.Dispose(); dict.Dispose(); return(product); }
public NumericRangeFilterInstance <Double> CreateDoubleRangeFilter(string fieldName, int precisionStep, object min, object max, bool minInclusive, bool maxInclusive) { double?doubleMin = null; if (min != null && min != Null.Value && min != Undefined.Value && min is int) { doubleMin = Convert.ToDouble(min); } double?doubleMax = null; if (max != null && max != Null.Value && max != Undefined.Value && max is int) { doubleMax = Convert.ToDouble(max); } var filter = NumericRangeFilter.NewDoubleRange(fieldName, precisionStep, doubleMin, doubleMax, minInclusive, maxInclusive); return(new NumericRangeFilterInstance <double>(this.Engine.Object.InstancePrototype, filter)); }
public NumericRangeFilterInstance <int> CreateIntRangeFilter(string fieldName, int precisionStep, object min, object max, bool minInclusive, bool maxInclusive) { int?intMin = null; if (min != null && min != Null.Value && min != Undefined.Value && min is int) { intMin = Convert.ToInt32(min); } int?intMax = null; if (max != null && max != Null.Value && max != Undefined.Value && max is int) { intMax = Convert.ToInt32(max); } var filter = NumericRangeFilter.NewIntRange(fieldName, precisionStep, intMin, intMax, minInclusive, maxInclusive); return(new NumericRangeFilterInstance <int>(this.Engine.Object.InstancePrototype, filter)); }
public List <News> Search(string Keywords) { List <News> newsList = new List <News>(); Directory dir = FSDirectory.Open(new System.IO.DirectoryInfo(Server.MapPath("/Index")), new SimpleFSLockFactory()); IndexSearcher search = new IndexSearcher(dir); PhraseQuery query = new PhraseQuery(); query.Add(new Term("Content", Keywords)); //query.Slop = 8; NumericRangeFilter <int> range = NumericRangeFilter.NewIntRange("NewsId", 1, 10, true, true); Sort sort = new Sort(new SortField("OrderId", SortField.LONG, true)); TopFieldDocs fileds = search.Search(query, range, 1000, sort); ScoreDoc[] docs = fileds.ScoreDocs; for (int i = 0; i < docs.Length; i++) { News news = new News(); int docid = docs[i].Doc; Document doc = search.Doc(docid); news.NewsId = Convert.ToInt32(doc.Get("NewsId")); news.Title = doc.Get("Title"); SimpleHTMLFormatter html = new SimpleHTMLFormatter("<span style=\"color:red\" >", "</span>"); Highlighter high = new Highlighter(html, new PanGu.Segment()); high.FragmentSize = 120; news.Content = high.GetBestFragment(Keywords, doc.Get("Content")); news.AddTime = Convert.ToDateTime(doc.Get("AddTime")); news.OrderId = Convert.ToInt64(doc.Get("OrderId")); newsList.Add(news); } return(newsList); }
/// <summary> /// Do search on a numeric field base on specific condition. /// </summary> /// <typeparam name="T"></typeparam> /// <typeparam name="TValueType"></typeparam> /// <param name="items"></param> /// <param name="property"></param> /// <param name="range"></param> /// <returns></returns> public static IQueryable <T> WithNumericRangeSearch <T, TValueType>(this IQueryable <T> items, Expression <Func <T, TValueType> > property, NumericRangeFilter <TValueType, TValueType> range) { if (range == null) { return(items); } if (property == null) { return(items); } // Find the start value. items = items.WithNumericSearch(property, range?.From); // Find the end value. items = items.WithNumericSearch(property, range?.To); return(items); }
static void Main(string[] args) { Directory dir = new RAMDirectory(); Analyzer analyzer = new SimpleAnalyzer(); var indexWriter = new IndexWriter(dir, analyzer, IndexWriter.MaxFieldLength.UNLIMITED); foreach (var document in CreateDocuments()) { indexWriter.AddDocument(document); } indexWriter.Commit(); indexWriter.Dispose(); var parser = new QueryParser(Version, TextFieldName, analyzer); Query q = parser.Parse("Anders"); var fromFilter = DateTime2Int(new DateTime(2010, 3, 1)); var toFilter = DateTime2Int(new DateTime(2010, 4, 1)); Filter filter = NumericRangeFilter.NewIntRange(DateFieldName, fromFilter, toFilter, true, true); //Filter filter = NumericRangeFilter.NewIntRange(DateFieldName, fromFilter, null, true, true); // openended var searcher = new IndexSearcher(dir, true); TopDocs hits = searcher.Search(q, filter, 5, Sort.RELEVANCE); Console.WriteLine("Found {0} document(s) that matched query '{1}' with filter {2}:", hits.TotalHits, q, filter); foreach (ScoreDoc match in hits.ScoreDocs) { Document doc = searcher.Doc(match.Doc); Console.WriteLine("Matched = {0} with date {1}", doc.Get(TextFieldName), doc.Get(DateFieldName)); } searcher.Dispose(); }
public void TestGetFilterFloat() { NumericRangeFilterBuilder filterBuilder = new NumericRangeFilterBuilder(); filterBuilder.SetStrictMode(true); String xml = "<NumericRangeFilter fieldName='AGE' type='FLOAT' lowerTerm='-2.321432' upperTerm='32432.23'/>"; XmlDocument doc = GetDocumentFromString(xml); Filter filter = filterBuilder.GetFilter(doc.DocumentElement); assertTrue(filter is NumericRangeFilter <float>); NumericRangeFilter <float> numRangeFilter = (NumericRangeFilter <float>)filter; assertEquals(Convert.ToSingle(-2.321432f), numRangeFilter.Min); assertEquals(Convert.ToSingle(32432.23f), numRangeFilter.Max); assertEquals("AGE", numRangeFilter.Field); assertTrue(numRangeFilter.IncludesMin()); assertTrue(numRangeFilter.IncludesMax()); String xml2 = "<NumericRangeFilter fieldName='AGE' type='FLOAT' lowerTerm='-2.321432' upperTerm='32432.23' includeUpper='false' precisionStep='2' />"; XmlDocument doc2 = GetDocumentFromString(xml2); Filter filter2 = filterBuilder.GetFilter(doc2.DocumentElement); assertTrue(filter2 is NumericRangeFilter <float>); NumericRangeFilter <float> numRangeFilter2 = (NumericRangeFilter <float>)filter2; assertEquals(Convert.ToSingle(-2.321432f), numRangeFilter2.Min); assertEquals(Convert.ToSingle(32432.23f), numRangeFilter2.Max); assertEquals("AGE", numRangeFilter2.Field); assertTrue(numRangeFilter2.IncludesMin()); assertFalse(numRangeFilter2.IncludesMax()); }
public virtual void TestRandomDoubles() { Directory dir = NewDirectory(); RandomIndexWriter w = new RandomIndexWriter(Random(), dir, Similarity, TimeZone); int numDocs = AtLeast(1000); double[] values = new double[numDocs]; double minValue = double.PositiveInfinity; double maxValue = double.NegativeInfinity; for (int i = 0; i < numDocs; i++) { Document doc = new Document(); double v = Random().NextDouble(); values[i] = v; doc.Add(new DoubleDocValuesField("field", v)); doc.Add(new DoubleField("field", v, Field.Store.NO)); w.AddDocument(doc); minValue = Math.Min(minValue, v); maxValue = Math.Max(maxValue, v); } IndexReader r = w.Reader; IndexSearcher s = NewSearcher(r); FacetsConfig config = new FacetsConfig(); int numIters = AtLeast(10); for (int iter = 0; iter < numIters; iter++) { if (VERBOSE) { Console.WriteLine("TEST: iter=" + iter); } int numRange = TestUtil.NextInt(Random(), 1, 5); DoubleRange[] ranges = new DoubleRange[numRange]; int[] expectedCounts = new int[numRange]; double minAcceptedValue = double.PositiveInfinity; double maxAcceptedValue = double.NegativeInfinity; for (int rangeID = 0; rangeID < numRange; rangeID++) { double min; if (rangeID > 0 && Random().Next(10) == 7) { // Use an existing boundary: DoubleRange prevRange = ranges[Random().Next(rangeID)]; if (Random().NextBoolean()) { min = prevRange.Min; } else { min = prevRange.Max; } } else { min = Random().NextDouble(); } double max; if (rangeID > 0 && Random().Next(10) == 7) { // Use an existing boundary: DoubleRange prevRange = ranges[Random().Next(rangeID)]; if (Random().NextBoolean()) { max = prevRange.Min; } else { max = prevRange.Max; } } else { max = Random().NextDouble(); } if (min > max) { double x = min; min = max; max = x; } bool minIncl; bool maxIncl; if (min == max) { minIncl = true; maxIncl = true; } else { minIncl = Random().NextBoolean(); maxIncl = Random().NextBoolean(); } ranges[rangeID] = new DoubleRange("r" + rangeID, min, minIncl, max, maxIncl); // Do "slow but hopefully correct" computation of // expected count: for (int i = 0; i < numDocs; i++) { bool accept = true; if (minIncl) { accept &= values[i] >= min; } else { accept &= values[i] > min; } if (maxIncl) { accept &= values[i] <= max; } else { accept &= values[i] < max; } if (accept) { expectedCounts[rangeID]++; minAcceptedValue = Math.Min(minAcceptedValue, values[i]); maxAcceptedValue = Math.Max(maxAcceptedValue, values[i]); } } } FacetsCollector sfc = new FacetsCollector(); s.Search(new MatchAllDocsQuery(), sfc); Filter fastMatchFilter; if (Random().NextBoolean()) { if (Random().NextBoolean()) { fastMatchFilter = NumericRangeFilter.NewDoubleRange("field", minValue, maxValue, true, true); } else { fastMatchFilter = NumericRangeFilter.NewDoubleRange("field", minAcceptedValue, maxAcceptedValue, true, true); } } else { fastMatchFilter = null; } ValueSource vs = new DoubleFieldSource("field"); Facets facets = new DoubleRangeFacetCounts("field", vs, sfc, fastMatchFilter, ranges); FacetResult result = facets.GetTopChildren(10, "field"); Assert.AreEqual(numRange, result.LabelValues.Length); for (int rangeID = 0; rangeID < numRange; rangeID++) { if (VERBOSE) { Console.WriteLine(" range " + rangeID + " expectedCount=" + expectedCounts[rangeID]); } LabelAndValue subNode = result.LabelValues[rangeID]; Assert.AreEqual("r" + rangeID, subNode.Label); Assert.AreEqual(expectedCounts[rangeID], (int)subNode.Value); DoubleRange range = ranges[rangeID]; // Test drill-down: DrillDownQuery ddq = new DrillDownQuery(config); if (Random().NextBoolean()) { if (Random().NextBoolean()) { ddq.Add("field", NumericRangeFilter.NewDoubleRange("field", range.Min, range.Max, range.MinInclusive, range.MaxInclusive)); } else { ddq.Add("field", NumericRangeQuery.NewDoubleRange("field", range.Min, range.Max, range.MinInclusive, range.MaxInclusive)); } } else { ddq.Add("field", range.GetFilter(fastMatchFilter, vs)); } Assert.AreEqual(expectedCounts[rangeID], s.Search(ddq, 10).TotalHits); } } IOUtils.Close(w, r, dir); }
public static void Show() { FSDirectory dir = FSDirectory.Open(StaticConstant.TestIndexPath); IndexSearcher searcher = new IndexSearcher(dir); //查找器 { TermQuery query = new TermQuery(new Term("title", "图书馆")); //包含 TopDocs docs = searcher.Search(query, null, 10000); //找到的数据 foreach (ScoreDoc sd in docs.ScoreDocs) { Document doc = searcher.Doc(sd.Doc); Console.WriteLine("***************************************"); Console.WriteLine(string.Format("id={0}", doc.Get("id"))); Console.WriteLine(string.Format("title={0}", doc.Get("title"))); Console.WriteLine(string.Format("time={0}", doc.Get("time"))); Console.WriteLine(string.Format("price={0}", doc.Get("price"))); Console.WriteLine(string.Format("content={0}", doc.Get("content"))); } Console.WriteLine("1一共命中了{0}个", docs.TotalHits); } QueryParser parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, "title", new PanGuAnalyzer());//解析器 { //string keyword = "高中政治人教新课标选修生活中的法律常识"; string keyword = "高中政治 人 教 新课 标 选修 生活 中的 法律常识"; { Query query = parser.Parse(keyword); TopDocs docs = searcher.Search(query, null, 10000);//找到的数据 int i = 0; foreach (ScoreDoc sd in docs.ScoreDocs) { if (i++ < 1000) { Document doc = searcher.Doc(sd.Doc); Console.WriteLine("***************************************"); Console.WriteLine(string.Format("id={0}", doc.Get("id"))); Console.WriteLine(string.Format("title={0}", doc.Get("title"))); Console.WriteLine(string.Format("time={0}", doc.Get("time"))); Console.WriteLine(string.Format("price={0}", doc.Get("price"))); } } Console.WriteLine($"一共命中{docs.TotalHits}"); } { Query query = parser.Parse(keyword); NumericRangeFilter <int> timeFilter = NumericRangeFilter.NewIntRange("time", 20190101, 20191231, true, true); //过滤 SortField sortPrice = new SortField("price", SortField.DOUBLE, false); //降序 SortField sortTime = new SortField("time", SortField.INT, true); //升序 Sort sort = new Sort(sortTime, sortPrice); //排序 哪个前哪个后 TopDocs docs = searcher.Search(query, timeFilter, 10000, sort); //找到的数据 int i = 0; foreach (ScoreDoc sd in docs.ScoreDocs) { if (i++ < 1000) { Document doc = searcher.Doc(sd.Doc); Console.WriteLine("***************************************"); Console.WriteLine(string.Format("id={0}", doc.Get("id"))); Console.WriteLine(string.Format("title={0}", doc.Get("title"))); Console.WriteLine(string.Format("time={0}", doc.Get("time"))); Console.WriteLine(string.Format("price={0}", doc.Get("price"))); } } Console.WriteLine("3一共命中了{0}个", docs.TotalHits); } } }
/// <summary> /// 查询 /// </summary> /// <param name="keyWord"></param> /// <returns></returns> public List <News> search(string keyWord) { //定义分词器 Analyzer analyzer = new PanGuAnalyzer(); //定义索引目录路径 string path = Server.MapPath("/Indexs"); //定义索引用到的目录 FSDirectory directory = FSDirectory.Open(new DirectoryInfo(path), new NoLockFactory()); //返回读取给定目录中索引的IndexReader。 //您应该传递readOnly =true,因为它提供了更好的并发性能,除非您打算对读取器执行写操作(删除文档或更改规范)。 IndexReader reader = IndexReader.Open(directory, true); IndexSearcher searcher = new IndexSearcher(reader); TopScoreDocCollector collector = TopScoreDocCollector.Create(1000, true); //============完整设置查询条件、过滤器、结果排序的使用方法============== //设置查询条件 PhraseQuery query = new PhraseQuery(); query.Slop = 8; query.Add(new Term("Content", keyWord)); //设置过滤器 NumericRangeFilter <int> IdRange = NumericRangeFilter.NewIntRange("Id", 1, 6, true, true); //结果排序 Sort sort = new Sort(new SortField("OrderId", SortField.LONG, true)); // 使用query这个查询条件进行搜索,搜索结果放入collector TopFieldDocs tt = searcher.Search(query, null, 1000, sort); //按排序来取 ScoreDoc[] docs = tt.ScoreDocs; //============ 完整设置查询条件、过滤器、结果排序的使用方法 ============== // 从查询结果中取出第m条到第n条的数据 //collector.GetTotalHits()表示总的结果条数 //searcher.Search(query, null, collector); //ScoreDoc[] docs = collector.TopDocs(0, collector.TotalHits).ScoreDocs; // 遍历查询结果 List <News> resultList = new List <News>(); for (int i = 0; i < docs.Length; i++) { // 拿到文档的id,因为Document可能非常占内存(DataSet和DataReader的区别) int docId = docs[i].Doc; // 所以查询结果中只有id,具体内容需要二次查询 // 根据id查询内容:放进去的是Document,查出来的还是Document Document doc = searcher.Doc(docId); News result = new News(); result.Id = Convert.ToInt32(doc.Get("Id")); result.Title = doc.Get("Title"); result.AddTime = Convert.ToDateTime(doc.Get("AddTime")); SimpleHTMLFormatter formatter = new SimpleHTMLFormatter("<font color='red'>", "</font>"); //构造一个高亮对象,它将应用改革才创建的格式化 Highlighter highter = new Highlighter(formatter, new PanGu.Segment()); //设置片段的长度,应该是格式化搜索词后带html标签的长度 highter.FragmentSize = 120; //调用方法,替换数据title中的关键词,也就是高亮此关键词 result.Content = highter.GetBestFragment(keyWord, doc.Get("Content")); resultList.Add(result); } //reader.Close(); reader.Dispose(); return(resultList); }
/// <summary> ///根据关键字进行查询,根据int的范围进行搜索 /// </summary> /// <param name="Title"></param> /// <returns></returns> public List <Bpo_JobEntity> SearchJobList4(string Title, string FullAddress, int MinId, int MaxId) { List <Bpo_JobEntity> jobList = new List <Bpo_JobEntity>(); string path = "F://LuceneIndexDir"; FSDirectory dir = FSDirectory.Open(path); //IndexSearcher searcher = new IndexSearcher(dir);//声明一个查询器,或者以下面一种方式声明 IndexReader reader = IndexReader.Open(dir, true);//查询器 IndexSearcher searcher = new IndexSearcher(reader); //搜索条件,BooleanQuery可以同时制定多个搜索条件 BooleanQuery booleanQuery = new BooleanQuery(); //Query query = new TermQuery(new Term("Title", $"*{Title}*"));//不支持通配符 //Query query = new WildcardQuery(new Term("Title", $"*{Title}*")); // 通配符 if (!string.IsNullOrEmpty(Title))//空格隔开,按照多个词进行搜索 { Title = new LuceneAnalyze().AnalyzerKeyword("Title", Title); QueryParser parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, "Title", new PanGuAnalyzer()); Query query = parser.Parse(Title); booleanQuery.Add(query, Occur.MUST); } if (MinId > 0)//空格隔开,按照多个词进行搜索 { //string idStr = new LuceneAnalyze().AnalyzerKeyword("Id", MinId.ToString()); QueryParser parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, "Id", new PanGuAnalyzer()); Query query = parser.Parse(MinId.ToString()); booleanQuery.Add(query, Occur.MUST); } if (!string.IsNullOrEmpty(FullAddress)) { FullAddress = new LuceneAnalyze().AnalyzerKeyword("FullAddress", FullAddress); QueryParser parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, "FullAddress", new PanGuAnalyzer()); Query query = parser.Parse(FullAddress); booleanQuery.Add(query, Occur.MUST); //使用WildcardQuery,相当于sql的like //Query query2 = new WildcardQuery(new Term("FullAddress", $"*{FullAddress}*")); // 通配符 //booleanQuery.Add(query2, Occur.MUST); } //根据id区间区间搜索(此时id存储索引时要是NumericField类型) NumericRangeFilter <int> intFilter = null; //if (MinId > 0 && MaxId == 0) //{ // intFilter = NumericRangeFilter.NewIntRange("Id", MinId, int.MaxValue, true, true); //} //else if (MaxId > 0 && MinId == 0) //{ // intFilter = NumericRangeFilter.NewIntRange("Id", 0, MaxId, true, true); //} //else if (MaxId > 0 && MinId > 0) //{ // intFilter = NumericRangeFilter.NewIntRange("Id", MinId, MaxId, true, true); //} //定义排序 SortField sortField = new SortField("Id", SortField.STRING, false); //降序 SortField sortField2 = new SortField("CompanyId", SortField.INT, false); //降序 Sort sort = new Sort(sortField, sortField2); //取搜索结果方法1: TopDocs docs = searcher.Search(booleanQuery, intFilter, 10000, sort); //找到的结果取100条数据 foreach (ScoreDoc sd in docs.ScoreDocs) //从docs.ScoreDocs取数据 { Document doc = searcher.Doc(sd.Doc); jobList.Add(new Bpo_JobEntity { Id = Convert.ToInt32(doc.Get("Id") ?? "-100"), Title = doc.Get("Title"), UserId = Convert.ToInt32(doc.Get("UserId") ?? "-100"), UserName = doc.Get("UserName"), CompanyId = Convert.ToInt32(doc.Get("CompanyId") ?? "-100"), CompanyName = doc.Get("CompanyName"), FullAddress = doc.Get("FullAddress"), CreateDate = Convert.ToDateTime(doc.Get("CreateDate")) }); } return(jobList); }
static Filter GetFilter() { return(NumericRangeFilter.NewIntRange("ID", 1, 50, true, false)); // [2; 5) range }
/// <summary> /// Given a latitude and longitude (in degrees) and the /// maximum great circle (surface of the earth) distance, /// returns a simple Filter bounding box to "fast match" /// candidates. /// </summary> public static Filter GetBoundingBoxFilter(double originLat, double originLng, double maxDistanceKM) { // Basic bounding box geo math from // http://JanMatuschek.de/LatitudeLongitudeBoundingCoordinates, // licensed under creative commons 3.0: // http://creativecommons.org/licenses/by/3.0 // TODO: maybe switch to recursive prefix tree instead // (in lucene/spatial)? It should be more efficient // since it's a 2D trie... // Degrees -> Radians: double originLatRadians = originLat.ToRadians(); double originLngRadians = originLng.ToRadians(); double angle = maxDistanceKM / (SloppyMath.EarthDiameter(originLat) / 2.0); double minLat = originLatRadians - angle; double maxLat = originLatRadians + angle; double minLng; double maxLng; if (minLat > -90.ToRadians() && maxLat < 90.ToRadians()) { double delta = Math.Asin(Math.Sin(angle) / Math.Cos(originLatRadians)); minLng = originLngRadians - delta; if (minLng < -180.ToRadians()) { minLng += 2 * Math.PI; } maxLng = originLngRadians + delta; if (maxLng > 180.ToRadians()) { maxLng -= 2 * Math.PI; } } else { // The query includes a pole! minLat = Math.Max(minLat, -90.ToRadians()); maxLat = Math.Min(maxLat, 90.ToRadians()); minLng = -180.ToRadians(); maxLng = 180.ToRadians(); } BooleanFilter f = new BooleanFilter(); // Add latitude range filter: f.Add(NumericRangeFilter.NewDoubleRange("latitude", minLat.ToDegrees(), maxLat.ToDegrees(), true, true), Occur.MUST); // Add longitude range filter: if (minLng > maxLng) { // The bounding box crosses the international date // line: BooleanFilter lonF = new BooleanFilter(); lonF.Add(NumericRangeFilter.NewDoubleRange("longitude", minLng.ToDegrees(), null, true, true), Occur.SHOULD); lonF.Add(NumericRangeFilter.NewDoubleRange("longitude", null, maxLng.ToDegrees(), true, true), Occur.SHOULD); f.Add(lonF, Occur.MUST); } else { f.Add(NumericRangeFilter.NewDoubleRange("longitude", minLng.ToDegrees(), maxLng.ToDegrees(), true, true), Occur.MUST); } return(f); }
/// <summary> /// 分页获取商品信息数据 /// </summary> /// <param name="queryString"></param> /// <param name="pageIndex">第一页为1</param> /// <param name="pageSize"></param> /// <param name="totalCount"></param> /// <returns></returns> public List <Commodity> QueryIndexPage(string queryString, int pageIndex, int pageSize, out int totalCount, string priceFilter, string priceOrderBy) { totalCount = 0; IndexSearcher searcher = null; try { List <Commodity> ciList = new List <Commodity>(); FSDirectory dir = FSDirectory.Open(StaticConstant.IndexPath); searcher = new IndexSearcher(dir); Analyzer analyzer = new PanGuAnalyzer(); //--------------------------------------这里配置搜索条件 QueryParser parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, "title", analyzer); Query query = parser.Parse(queryString); pageIndex = Math.Max(1, pageIndex);//索引从1开始 int startIndex = (pageIndex - 1) * pageSize; int endIndex = pageIndex * pageSize; NumericRangeFilter <float> numPriceFilter = null; if (!string.IsNullOrWhiteSpace(priceFilter)) { bool isContainStart = priceFilter.StartsWith("["); bool isContainEnd = priceFilter.EndsWith("]"); string[] floatArray = priceFilter.Replace("[", "").Replace("]", "").Replace("{", "").Replace("}", "").Split(','); float start = 0; float end = 0; if (!float.TryParse(floatArray[0], out start) || !float.TryParse(floatArray[1], out end)) { throw new Exception("Wrong priceFilter"); } numPriceFilter = NumericRangeFilter.NewFloatRange("price", start, end, isContainStart, isContainEnd); } Sort sort = new Sort(); if (!string.IsNullOrWhiteSpace(priceOrderBy)) { SortField sortField = new SortField("price", SortField.FLOAT, priceOrderBy.EndsWith("asc", StringComparison.CurrentCultureIgnoreCase)); sort.SetSort(sortField); } TopDocs docs = searcher.Search(query, numPriceFilter, 10000, sort); //TopDocs docs = searcher.Search(query, null, 10000); totalCount = docs.TotalHits; //PrintScores(docs, startIndex, endIndex, searcher); for (int i = startIndex; i < endIndex && i < totalCount; i++) { Document doc = searcher.Doc(docs.ScoreDocs[i].Doc); ciList.Add(DocumentToCommodityInfo(doc)); } return(ciList); } finally { if (searcher != null) { searcher.Dispose(); } } }
public virtual void TestRandomFloats() { Directory dir = NewDirectory(); RandomIndexWriter w = new RandomIndexWriter( #if FEATURE_INSTANCE_TESTDATA_INITIALIZATION this, #endif Random, dir); int numDocs = AtLeast(1000); float[] values = new float[numDocs]; float minValue = float.PositiveInfinity; float maxValue = float.NegativeInfinity; for (int i = 0; i < numDocs; i++) { Document doc = new Document(); float v = Random.NextSingle(); values[i] = v; doc.Add(new SingleDocValuesField("field", v)); doc.Add(new SingleField("field", v, Field.Store.NO)); w.AddDocument(doc); minValue = Math.Min(minValue, v); maxValue = Math.Max(maxValue, v); } IndexReader r = w.GetReader(); IndexSearcher s = NewSearcher(r); FacetsConfig config = new FacetsConfig(); int numIters = AtLeast(10); for (int iter = 0; iter < numIters; iter++) { if (VERBOSE) { Console.WriteLine("TEST: iter=" + iter); } int numRange = TestUtil.NextInt32(Random, 1, 5); DoubleRange[] ranges = new DoubleRange[numRange]; int[] expectedCounts = new int[numRange]; float minAcceptedValue = float.PositiveInfinity; float maxAcceptedValue = float.NegativeInfinity; if (VERBOSE) { Console.WriteLine("TEST: " + numRange + " ranges"); } for (int rangeID = 0; rangeID < numRange; rangeID++) { double min; if (rangeID > 0 && Random.Next(10) == 7) { // Use an existing boundary: DoubleRange prevRange = ranges[Random.Next(rangeID)]; if (Random.NextBoolean()) { min = prevRange.Min; } else { min = prevRange.Max; } } else { min = Random.NextDouble(); } double max; if (rangeID > 0 && Random.Next(10) == 7) { // Use an existing boundary: DoubleRange prevRange = ranges[Random.Next(rangeID)]; if (Random.NextBoolean()) { max = prevRange.Min; } else { max = prevRange.Max; } } else { max = Random.NextDouble(); } if (min > max) { double x = min; min = max; max = x; } // Must truncate to float precision so that the // drill-down counts (which use NRQ.newFloatRange) // are correct: min = (float)min; max = (float)max; bool minIncl; bool maxIncl; if (min == max) { minIncl = true; maxIncl = true; } else { minIncl = Random.NextBoolean(); maxIncl = Random.NextBoolean(); } ranges[rangeID] = new DoubleRange("r" + rangeID, min, minIncl, max, maxIncl); if (VERBOSE) { Console.WriteLine("TEST: range " + rangeID + ": " + ranges[rangeID]); } // Do "slow but hopefully correct" computation of // expected count: for (int i = 0; i < numDocs; i++) { bool accept = true; if (minIncl) { accept &= values[i] >= min; } else { accept &= values[i] > min; } if (maxIncl) { accept &= values[i] <= max; } else { accept &= values[i] < max; } if (VERBOSE) { Console.WriteLine("TEST: check doc=" + i + " val=" + values[i] + " accept=" + accept); } if (accept) { expectedCounts[rangeID]++; minAcceptedValue = Math.Min(minAcceptedValue, values[i]); maxAcceptedValue = Math.Max(maxAcceptedValue, values[i]); } } } FacetsCollector sfc = new FacetsCollector(); s.Search(new MatchAllDocsQuery(), sfc); Filter fastMatchFilter; if (Random.NextBoolean()) { if (Random.NextBoolean()) { fastMatchFilter = NumericRangeFilter.NewSingleRange("field", minValue, maxValue, true, true); } else { fastMatchFilter = NumericRangeFilter.NewSingleRange("field", minAcceptedValue, maxAcceptedValue, true, true); } } else { fastMatchFilter = null; } ValueSource vs = new SingleFieldSource("field"); Facets facets = new DoubleRangeFacetCounts("field", vs, sfc, fastMatchFilter, ranges); FacetResult result = facets.GetTopChildren(10, "field"); Assert.AreEqual(numRange, result.LabelValues.Length); for (int rangeID = 0; rangeID < numRange; rangeID++) { if (VERBOSE) { Console.WriteLine("TEST: verify range " + rangeID + " expectedCount=" + expectedCounts[rangeID]); } LabelAndValue subNode = result.LabelValues[rangeID]; Assert.AreEqual("r" + rangeID, subNode.Label); Assert.AreEqual(expectedCounts[rangeID], (int)subNode.Value); DoubleRange range = ranges[rangeID]; // Test drill-down: DrillDownQuery ddq = new DrillDownQuery(config); if (Random.NextBoolean()) { if (Random.NextBoolean()) { ddq.Add("field", NumericRangeFilter.NewSingleRange("field", (float)range.Min, (float)range.Max, range.MinInclusive, range.MaxInclusive)); } else { ddq.Add("field", NumericRangeQuery.NewSingleRange("field", (float)range.Min, (float)range.Max, range.MinInclusive, range.MaxInclusive)); } } else { ddq.Add("field", range.GetFilter(fastMatchFilter, vs)); } Assert.AreEqual(expectedCounts[rangeID], s.Search(ddq, 10).TotalHits); } } IOUtils.Dispose(w, r, dir); }
public virtual void TestRandomLongs() { Directory dir = NewDirectory(); var w = new RandomIndexWriter( #if FEATURE_INSTANCE_TESTDATA_INITIALIZATION this, #endif Random, dir); int numDocs = AtLeast(1000); if (Verbose) { Console.WriteLine("TEST: numDocs=" + numDocs); } long[] values = new long[numDocs]; long minValue = long.MaxValue; long maxValue = long.MinValue; for (int i = 0; i < numDocs; i++) { Document doc = new Document(); long v = Random.NextInt64(); values[i] = v; doc.Add(new NumericDocValuesField("field", v)); doc.Add(new Int64Field("field", v, Field.Store.NO)); w.AddDocument(doc); minValue = Math.Min(minValue, v); maxValue = Math.Max(maxValue, v); } IndexReader r = w.GetReader(); IndexSearcher s = NewSearcher(r); FacetsConfig config = new FacetsConfig(); int numIters = AtLeast(10); for (int iter = 0; iter < numIters; iter++) { if (Verbose) { Console.WriteLine("TEST: iter=" + iter); } int numRange = TestUtil.NextInt32(Random, 1, 100); Int64Range[] ranges = new Int64Range[numRange]; int[] expectedCounts = new int[numRange]; long minAcceptedValue = long.MaxValue; long maxAcceptedValue = long.MinValue; for (int rangeID = 0; rangeID < numRange; rangeID++) { long min; if (rangeID > 0 && Random.Next(10) == 7) { // Use an existing boundary: Int64Range prevRange = ranges[Random.Next(rangeID)]; if (Random.NextBoolean()) { min = prevRange.Min; } else { min = prevRange.Max; } } else { min = Random.NextInt64(); } long max; if (rangeID > 0 && Random.Next(10) == 7) { // Use an existing boundary: Int64Range prevRange = ranges[Random.Next(rangeID)]; if (Random.NextBoolean()) { max = prevRange.Min; } else { max = prevRange.Max; } } else { max = Random.NextInt64(); } if (min > max) { long x = min; min = max; max = x; } bool minIncl; bool maxIncl; if (min == max) { minIncl = true; maxIncl = true; } else { minIncl = Random.NextBoolean(); maxIncl = Random.NextBoolean(); } ranges[rangeID] = new Int64Range("r" + rangeID, min, minIncl, max, maxIncl); if (Verbose) { Console.WriteLine(" range " + rangeID + ": " + ranges[rangeID]); } // Do "slow but hopefully correct" computation of // expected count: for (int i = 0; i < numDocs; i++) { bool accept = true; if (minIncl) { accept &= values[i] >= min; } else { accept &= values[i] > min; } if (maxIncl) { accept &= values[i] <= max; } else { accept &= values[i] < max; } if (accept) { expectedCounts[rangeID]++; minAcceptedValue = Math.Min(minAcceptedValue, values[i]); maxAcceptedValue = Math.Max(maxAcceptedValue, values[i]); } } } FacetsCollector sfc = new FacetsCollector(); s.Search(new MatchAllDocsQuery(), sfc); Filter fastMatchFilter; if (Random.NextBoolean()) { if (Random.NextBoolean()) { fastMatchFilter = NumericRangeFilter.NewInt64Range("field", minValue, maxValue, true, true); } else { fastMatchFilter = NumericRangeFilter.NewInt64Range("field", minAcceptedValue, maxAcceptedValue, true, true); } } else { fastMatchFilter = null; } ValueSource vs = new Int64FieldSource("field"); Facets facets = new Int64RangeFacetCounts("field", vs, sfc, fastMatchFilter, ranges); FacetResult result = facets.GetTopChildren(10, "field"); Assert.AreEqual(numRange, result.LabelValues.Length); for (int rangeID = 0; rangeID < numRange; rangeID++) { if (Verbose) { Console.WriteLine(" range " + rangeID + " expectedCount=" + expectedCounts[rangeID]); } LabelAndValue subNode = result.LabelValues[rangeID]; Assert.AreEqual("r" + rangeID, subNode.Label); Assert.AreEqual(expectedCounts[rangeID], (int)subNode.Value); Int64Range range = ranges[rangeID]; // Test drill-down: DrillDownQuery ddq = new DrillDownQuery(config); if (Random.NextBoolean()) { if (Random.NextBoolean()) { ddq.Add("field", NumericRangeFilter.NewInt64Range("field", range.Min, range.Max, range.MinInclusive, range.MaxInclusive)); } else { ddq.Add("field", NumericRangeQuery.NewInt64Range("field", range.Min, range.Max, range.MinInclusive, range.MaxInclusive)); } } else { ddq.Add("field", range.GetFilter(fastMatchFilter, vs)); } Assert.AreEqual(expectedCounts[rangeID], s.Search(ddq, 10).TotalHits); } } IOUtils.Dispose(w, r, dir); }