public void TestSimpleBrowser() { var query = new TermQuery(new Term("name", "asp.net")); Console.WriteLine(string.Format("query: <{0}>", query.ToString())); var request = new BrowseRequest() { Count = 10, Offset = 0, Query = query, Sort = new Lucene.Net.Search.Sort(new SortField("price", SortField.DOUBLE, false)).GetSort() }; var facetHandlers = new IFacetHandler[] { new SimpleFacetHandler("category") }; var browser = new BoboBrowser(BoboIndexReader.GetInstance(IndexReader.Open(_indexDir, true), facetHandlers)); var facetSpec = new FacetSpec() { OrderBy = FacetSpec.FacetSortSpec.OrderHitsDesc, MinHitCount = 1 }; request.SetFacetSpec("category", facetSpec); var result = browser.Browse(request); Console.WriteLine(string.Format("total hits:{0}", result.NumHits)); Console.WriteLine("==========================="); foreach (var facet in result.FacetMap["category"].GetFacets()) { var category = _categories.First(k => k.Value == int.Parse(facet.Value.ToString())); Console.WriteLine("{0}:({1})", category.Key, facet.FacetValueHitCount); } Console.WriteLine("==========================="); for (var i = 0; i < result.Hits.Length; i++) { var doc = browser.Doc(result.Hits[i].DocId); var category = _categories.First(k => k.Value == int.Parse(doc.GetField("category").StringValue)).Key; Console.WriteLine(string.Format("{2} - {0} ${1} by {3}", doc.GetField("name").StringValue, doc.GetField("price").StringValue, category, doc.GetField("author").StringValue)); } }
private void DoTest(Random random, PrintWriter @out, bool useCompoundFiles, int MAX_DOCS) { Directory directory = newDirectory(); Analyzer analyzer = new MockAnalyzer(random); IndexWriterConfig conf = newIndexWriterConfig(TEST_VERSION_CURRENT, analyzer); MergePolicy mp = conf.MergePolicy; mp.NoCFSRatio = useCompoundFiles ? 1.0 : 0.0; IndexWriter writer = new IndexWriter(directory, conf); if (VERBOSE) { Console.WriteLine("TEST: now build index MAX_DOCS=" + MAX_DOCS); } for (int j = 0; j < MAX_DOCS; j++) { Document d = new Document(); d.Add(newTextField(PRIORITY_FIELD, HIGH_PRIORITY, Field.Store.YES)); d.Add(newTextField(ID_FIELD, Convert.ToString(j), Field.Store.YES)); writer.addDocument(d); } writer.Dispose(); // try a search without OR IndexReader reader = DirectoryReader.Open(directory); IndexSearcher searcher = newSearcher(reader); Query query = new TermQuery(new Term(PRIORITY_FIELD, HIGH_PRIORITY)); @out.println("Query: " + query.ToString(PRIORITY_FIELD)); if (VERBOSE) { Console.WriteLine("TEST: search query=" + query); } Sort sort = new Sort(SortField.FIELD_SCORE, new SortField(ID_FIELD, SortField.Type.INT)); ScoreDoc[] hits = searcher.Search(query, null, MAX_DOCS, sort).scoreDocs; PrintHits(@out, hits, searcher); CheckHits(hits, MAX_DOCS, searcher); // try a new search with OR searcher = newSearcher(reader); hits = null; BooleanQuery booleanQuery = new BooleanQuery(); booleanQuery.Add(new TermQuery(new Term(PRIORITY_FIELD, HIGH_PRIORITY)), BooleanClause.Occur_e.SHOULD); booleanQuery.Add(new TermQuery(new Term(PRIORITY_FIELD, MED_PRIORITY)), BooleanClause.Occur_e.SHOULD); @out.println("Query: " + booleanQuery.ToString(PRIORITY_FIELD)); hits = searcher.search(booleanQuery, null, MAX_DOCS, sort).scoreDocs; PrintHits(@out, hits, searcher); CheckHits(hits, MAX_DOCS, searcher); reader.Close(); directory.Close(); }