/// <summary>Empty constructor</summary> public AssociationsFacetsExample() { config = new FacetsConfig(); config.SetMultiValued("tags", true); config.SetIndexFieldName("tags", "$tags"); config.SetMultiValued("genre", true); config.SetIndexFieldName("genre", "$genre"); }
public static void TestFlexLuceneRAM(string[] args) { StandardAnalyzer analyzer = new StandardAnalyzer(); FlexLucene.Store.Directory index = (FlexLucene.Store.Directory) new RAMDirectory(); config = new IndexWriterConfig((Analyzer)analyzer); cnf = new FacetsConfig(); cnf.SetIndexFieldName("title", "facet_title"); cnf.SetIndexFieldName("isbn", "facet_isbn"); LuceneTest.taxoDir = (FlexLucene.Store.Directory) new RAMDirectory(); LuceneTest.taxoWriter = (TaxonomyWriter) new FlexLucene.Facet.Taxonomy.Directory.DirectoryTaxonomyWriter(LuceneTest.taxoDir, IndexWriterConfigOpenMode.CREATE); IndexWriter w = new IndexWriter(index, LuceneTest.config); addDoc(w, "Lucene in Action", "9900001"); addDoc(w, "Lucene for Dummies", "9900002"); addDoc(w, "Lucene for Dummies 2", "9900003"); w.close(); String querystr = "isbn:99*"; Query q = new QueryParser("title", (Analyzer)analyzer).Parse(querystr); int hitsPerPage = 10; IndexReader reader = (IndexReader)DirectoryReader.Open(index); IndexSearcher searcher = new IndexSearcher(reader); TopScoreDocCollector collector = TopScoreDocCollector.Create(hitsPerPage); searcher.Search(q, (Collector)collector); ScoreDoc[] hits = collector.TopDocs().ScoreDocs; Console.WriteLine("Found " + hits.Length + " hits."); for (int i = 0; i < hits.Length; ++i) { int docId = hits [i].Doc; Document d = searcher.Doc(docId); Console.WriteLine(i + 1 + ". " + d.Get("isbn") + "\t" + d.Get("title")); } SortedSetDocValuesReaderState state = (SortedSetDocValuesReaderState) new DefaultSortedSetDocValuesReaderState(reader, "facet_isbn"); FacetsCollector fc = new FacetsCollector(); FacetsCollector.Search(searcher, q, 10, (Collector)fc); Facets facets = (Facets) new SortedSetDocValuesFacetCounts(state, fc); FacetResult result = facets.GetTopChildren(10, "isbn", new String[0]); for (int j = 0; j < result.ChildCount; ++j) { LabelAndValue lv = result.LabelValues [j]; Console.WriteLine(String.Format("Label={0}, Value={1}", lv.Label, lv.Value)); } reader.close(); }
public virtual void TestSeparateIndexedFields() { Store.Directory indexDir = NewDirectory(); Store.Directory taxoDir = NewDirectory(); var taxoWriter = new DirectoryTaxonomyWriter(taxoDir); IndexWriter iw = new IndexWriter(indexDir, NewIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random()))); FacetsConfig config = new FacetsConfig(); config.SetIndexFieldName("b", "$b"); for (int i = AtLeast(30); i > 0; --i) { Document doc = new Document(); doc.Add(new StringField("f", "v", Field.Store.NO)); doc.Add(new FacetField("a", "1")); doc.Add(new FacetField("b", "1")); iw.AddDocument(config.Build(taxoWriter, doc)); } DirectoryReader r = DirectoryReader.Open(iw, true); var taxoReader = new DirectoryTaxonomyReader(taxoWriter); FacetsCollector sfc = new FacetsCollector(); NewSearcher(r).Search(new MatchAllDocsQuery(), sfc); Facets facets1 = GetTaxonomyFacetCounts(taxoReader, config, sfc); Facets facets2 = GetTaxonomyFacetCounts(taxoReader, config, sfc, "$b"); Assert.AreEqual(r.MaxDoc, (int)facets1.GetTopChildren(10, "a").Value); Assert.AreEqual(r.MaxDoc, (int)facets2.GetTopChildren(10, "b").Value); IOUtils.Close(taxoWriter, iw, taxoReader, taxoDir, r, indexDir); }
public virtual void TestCountAndSumScore() { Store.Directory indexDir = NewDirectory(); Store.Directory taxoDir = NewDirectory(); var taxoWriter = new DirectoryTaxonomyWriter(taxoDir); IndexWriter iw = new IndexWriter(indexDir, NewIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random()))); FacetsConfig config = new FacetsConfig(); config.SetIndexFieldName("b", "$b"); for (int i = AtLeast(30); i > 0; --i) { Document doc = new Document(); doc.Add(new StringField("f", "v", Field.Store.NO)); doc.Add(new FacetField("a", "1")); doc.Add(new FacetField("b", "1")); iw.AddDocument(config.Build(taxoWriter, doc)); } DirectoryReader r = DirectoryReader.Open(iw, true); var taxoReader = new DirectoryTaxonomyReader(taxoWriter); FacetsCollector fc = new FacetsCollector(true); FacetsCollector.Search(NewSearcher(r), new MatchAllDocsQuery(), 10, fc); Facets facets1 = GetTaxonomyFacetCounts(taxoReader, config, fc); Facets facets2 = new TaxonomyFacetSumValueSource(new DocValuesOrdinalsReader("$b"), taxoReader, config, fc, new TaxonomyFacetSumValueSource.ScoreValueSource()); Assert.AreEqual(r.MaxDoc, (int)facets1.GetTopChildren(10, "a").Value); Assert.AreEqual(r.MaxDoc, (double)facets2.GetTopChildren(10, "b").Value, 1E-10); IOUtils.Dispose(taxoWriter, iw, taxoReader, taxoDir, r, indexDir); }
public override void BeforeClass() { base.BeforeClass(); dir = NewDirectory(); taxoDir = NewDirectory(); // preparations - index, taxonomy, content var taxoWriter = new DirectoryTaxonomyWriter(taxoDir); // Cannot mix ints & floats in the same indexed field: config = new FacetsConfig(); config.SetIndexFieldName("int", "$facets.int"); config.SetMultiValued("int", true); config.SetIndexFieldName("float", "$facets.float"); config.SetMultiValued("float", true); var writer = new RandomIndexWriter( #if FEATURE_INSTANCE_TESTDATA_INITIALIZATION this, #endif Random, dir); // index documents, 50% have only 'b' and all have 'a' for (int i = 0; i < 110; i++) { Document doc = new Document(); // every 11th document is added empty, this used to cause the association // aggregators to go into an infinite loop if (i % 11 != 0) { doc.Add(new Int32AssociationFacetField(2, "int", "a")); doc.Add(new SingleAssociationFacetField(0.5f, "float", "a")); if (i % 2 == 0) // 50 { doc.Add(new Int32AssociationFacetField(3, "int", "b")); doc.Add(new SingleAssociationFacetField(0.2f, "float", "b")); } } writer.AddDocument(config.Build(taxoWriter, doc)); } taxoWriter.Dispose(); reader = writer.GetReader(); writer.Dispose(); taxoReader = new DirectoryTaxonomyReader(taxoDir); }
/// <summary> /// Add documents. /// </summary> /// <param name="writer">The index writer.</param> /// <param name="facetWriter">The facet index writer.</param> /// <param name="facetData">The complete facet information used to build the index information.</param> public void AddDocuments(Lucene.Net.Index.IndexWriter writer, DirectoryTaxonomyWriter facetWriter, FacetData facetData) { // Build the facet configuration information. FacetsConfig config = new FacetsConfig(); // Builder hierarchicals. if (facetData.Hierarchicals != null && facetData.Hierarchicals.Length > 0) { // Add the config. foreach (FacetData.Hierarchical item in facetData.Hierarchicals) { config.SetHierarchical(item.DimensionName, item.IsHierarchical); } } // Builder index fields. if (facetData.IndexFields != null && facetData.IndexFields.Length > 0) { // Add the config. foreach (FacetData.IndexField item in facetData.IndexFields) { config.SetIndexFieldName(item.DimensionName, item.IndexFieldName); } } // Builder multi values. if (facetData.MultiValues != null && facetData.MultiValues.Length > 0) { // Add the config. foreach (FacetData.MultiValued item in facetData.MultiValues) { config.SetMultiValued(item.DimensionName, item.IsMultiValue); } } // Builder require dimension counts. if (facetData.RequireDimensionCounts != null && facetData.RequireDimensionCounts.Length > 0) { // Add the config. foreach (FacetData.RequireDimensionCount item in facetData.RequireDimensionCounts) { config.SetRequireDimCount(item.DimensionName, item.IsAccurateCountsRequired); } } // Add text data. if (facetData.TextFacetFields.Count > 0) { // Add the text. AddText(writer, facetWriter, facetData.TextFacetFields, config); } // Add file data. if (facetData.FileFacetFields.Count > 0) { // Add the file. AddFile(writer, facetWriter, facetData.FileFacetFields, config); } }
public void BeforeClass() { dir = NewDirectory(); taxoDir = NewDirectory(); // preparations - index, taxonomy, content var taxoWriter = new DirectoryTaxonomyWriter(taxoDir); // Cannot mix ints & floats in the same indexed field: config = new FacetsConfig(); config.SetIndexFieldName("int", "$facets.int"); config.SetMultiValued("int", true); config.SetIndexFieldName("float", "$facets.float"); config.SetMultiValued("float", true); var writer = new RandomIndexWriter(Random(), dir, Similarity, TimeZone); // index documents, 50% have only 'b' and all have 'a' for (int i = 0; i < 110; i++) { Document doc = new Document(); // every 11th document is added empty, this used to cause the association // aggregators to go into an infinite loop if (i % 11 != 0) { doc.Add(new IntAssociationFacetField(2, "int", "a")); doc.Add(new FloatAssociationFacetField(0.5f, "float", "a")); if (i % 2 == 0) // 50 { doc.Add(new IntAssociationFacetField(3, "int", "b")); doc.Add(new FloatAssociationFacetField(0.2f, "float", "b")); } } writer.AddDocument(config.Build(taxoWriter, doc)); } taxoWriter.Dispose(); reader = writer.Reader; writer.Dispose(); taxoReader = new DirectoryTaxonomyReader(taxoDir); }
public static void BeforeClass() { dir = NewDirectory(); taxoDir = NewDirectory(); // preparations - index, taxonomy, content var taxoWriter = new DirectoryTaxonomyWriter(taxoDir); // Cannot mix ints & floats in the same indexed field: config = new FacetsConfig(); config.SetIndexFieldName("int", "$facets.int"); config.SetMultiValued("int", true); config.SetIndexFieldName("float", "$facets.float"); config.SetMultiValued("float", true); var writer = new RandomIndexWriter(Random(), dir); // index documents, 50% have only 'b' and all have 'a' for (int i = 0; i < 110; i++) { Document doc = new Document(); // every 11th document is added empty, this used to cause the association // aggregators to go into an infinite loop if (i % 11 != 0) { doc.Add(new IntAssociationFacetField(2, "int", "a")); doc.Add(new FloatAssociationFacetField(0.5f, "float", "a")); if (i % 2 == 0) // 50 { doc.Add(new IntAssociationFacetField(3, "int", "b")); doc.Add(new FloatAssociationFacetField(0.2f, "float", "b")); } } writer.AddDocument(config.Build(taxoWriter, doc)); } taxoWriter.Dispose(); reader = writer.Reader; writer.Dispose(); taxoReader = new DirectoryTaxonomyReader(taxoDir); }
public virtual void TestWrongIndexFieldName() { Store.Directory dir = NewDirectory(); Store.Directory taxoDir = NewDirectory(); // Writes facet ords to a separate directory from the // main index: DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir, IndexWriterConfig.OpenMode_e.CREATE); FacetsConfig config = new FacetsConfig(); config.SetIndexFieldName("a", "$facets2"); RandomIndexWriter writer = new RandomIndexWriter(Random(), dir, Similarity, TimeZone); Document doc = new Document(); doc.Add(new FacetField("a", "foo1")); writer.AddDocument(config.Build(taxoWriter, doc)); // NRT open IndexSearcher searcher = NewSearcher(writer.Reader); // NRT open var taxoReader = new DirectoryTaxonomyReader(taxoWriter); FacetsCollector c = new FacetsCollector(); searcher.Search(new MatchAllDocsQuery(), c); // Uses default $facets field: Facets facets; if (Random().NextBoolean()) { facets = new FastTaxonomyFacetCounts(taxoReader, config, c); } else { OrdinalsReader ordsReader = new DocValuesOrdinalsReader(); if (Random().NextBoolean()) { ordsReader = new CachedOrdinalsReader(ordsReader); } facets = new TaxonomyFacetCounts(ordsReader, taxoReader, config, c); } // Ask for top 10 labels for any dims that have counts: IList <FacetResult> results = facets.GetAllDims(10); Assert.True(results.Count == 0); try { facets.GetSpecificValue("a"); Fail("should have hit exc"); } catch (System.ArgumentException) { // expected } try { facets.GetTopChildren(10, "a"); Fail("should have hit exc"); } catch (System.ArgumentException) { // expected } IOUtils.Close(writer, taxoWriter, searcher.IndexReader, taxoReader, taxoDir, dir); }
public virtual void TestWrongIndexFieldName() { Store.Directory dir = NewDirectory(); Store.Directory taxoDir = NewDirectory(); // Writes facet ords to a separate directory from the // main index: var taxoWriter = new DirectoryTaxonomyWriter(taxoDir, OpenMode.CREATE); FacetsConfig config = new FacetsConfig(); config.SetIndexFieldName("a", "$facets2"); RandomIndexWriter writer = new RandomIndexWriter(Random(), dir, Similarity, TimeZone); Document doc = new Document(); doc.Add(new Int32Field("num", 10, Field.Store.NO)); doc.Add(new FacetField("a", "foo1")); writer.AddDocument(config.Build(taxoWriter, doc)); // NRT open IndexSearcher searcher = NewSearcher(writer.Reader); writer.Dispose(); // NRT open var taxoReader = new DirectoryTaxonomyReader(taxoWriter); taxoWriter.Dispose(); FacetsCollector c = new FacetsCollector(); searcher.Search(new MatchAllDocsQuery(), c); TaxonomyFacetSumValueSource facets = new TaxonomyFacetSumValueSource(taxoReader, config, c, new Int32FieldSource("num")); // Ask for top 10 labels for any dims that have counts: IList <FacetResult> results = facets.GetAllDims(10); Assert.True(results.Count == 0); try { facets.GetSpecificValue("a"); Fail("should have hit exc"); } catch (System.ArgumentException) { // expected } try { facets.GetTopChildren(10, "a"); Fail("should have hit exc"); } catch (System.ArgumentException) { // expected } IOUtils.Dispose(searcher.IndexReader, taxoReader, dir, taxoDir); }
public virtual void TestWrongIndexFieldName() { Store.Directory dir = NewDirectory(); Store.Directory taxoDir = NewDirectory(); // Writes facet ords to a separate directory from the // main index: DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir, IndexWriterConfig.OpenMode_e.CREATE); FacetsConfig config = new FacetsConfig(); config.SetIndexFieldName("a", "$facets2"); RandomIndexWriter writer = new RandomIndexWriter(Random(), dir); Document doc = new Document(); doc.Add(new FacetField("a", "foo1")); writer.AddDocument(config.Build(taxoWriter, doc)); // NRT open IndexSearcher searcher = NewSearcher(writer.Reader); // NRT open var taxoReader = new DirectoryTaxonomyReader(taxoWriter); FacetsCollector c = new FacetsCollector(); searcher.Search(new MatchAllDocsQuery(), c); // Uses default $facets field: Facets facets; if (Random().NextBoolean()) { facets = new FastTaxonomyFacetCounts(taxoReader, config, c); } else { OrdinalsReader ordsReader = new DocValuesOrdinalsReader(); if (Random().NextBoolean()) { ordsReader = new CachedOrdinalsReader(ordsReader); } facets = new TaxonomyFacetCounts(ordsReader, taxoReader, config, c); } // Ask for top 10 labels for any dims that have counts: IList<FacetResult> results = facets.GetAllDims(10); Assert.True(results.Count == 0); try { facets.GetSpecificValue("a"); Fail("should have hit exc"); } catch (System.ArgumentException) { // expected } try { facets.GetTopChildren(10, "a"); Fail("should have hit exc"); } catch (System.ArgumentException) { // expected } IOUtils.Close(writer, taxoWriter, searcher.IndexReader, taxoReader, taxoDir, dir); }
/// <summary>Creates a new instance and populates the catetory list params mapping.</summary> public MultiCategoryListsFacetsExample() { config.SetIndexFieldName("Author", "author"); config.SetIndexFieldName("Publish Date", "pubdate"); config.SetHierarchical("Publish Date", true); }
public virtual void TestWrongIndexFieldName() { Store.Directory dir = NewDirectory(); Store.Directory taxoDir = NewDirectory(); // Writes facet ords to a separate directory from the // main index: var taxoWriter = new DirectoryTaxonomyWriter(taxoDir, IndexWriterConfig.OpenMode_e.CREATE); FacetsConfig config = new FacetsConfig(); config.SetIndexFieldName("a", "$facets2"); RandomIndexWriter writer = new RandomIndexWriter(Random(), dir, Similarity, TimeZone); Document doc = new Document(); doc.Add(new IntField("num", 10, Field.Store.NO)); doc.Add(new FacetField("a", "foo1")); writer.AddDocument(config.Build(taxoWriter, doc)); // NRT open IndexSearcher searcher = NewSearcher(writer.Reader); writer.Dispose(); // NRT open var taxoReader = new DirectoryTaxonomyReader(taxoWriter); taxoWriter.Dispose(); FacetsCollector c = new FacetsCollector(); searcher.Search(new MatchAllDocsQuery(), c); TaxonomyFacetSumValueSource facets = new TaxonomyFacetSumValueSource(taxoReader, config, c, new IntFieldSource("num")); // Ask for top 10 labels for any dims that have counts: IList<FacetResult> results = facets.GetAllDims(10); Assert.True(results.Count == 0); try { facets.GetSpecificValue("a"); Fail("should have hit exc"); } catch (System.ArgumentException) { // expected } try { facets.GetTopChildren(10, "a"); Fail("should have hit exc"); } catch (System.ArgumentException) { // expected } IOUtils.Close(searcher.IndexReader, taxoReader, dir, taxoDir); }
/// <summary> /// Search facet content in the existing index. /// </summary> /// <param name="text">The text to search for.</param> /// <param name="indexFields">The array of index fields to search in.</param> /// <param name="facetPaths">The array of facet paths to perform a drill down search on.</param> /// <param name="numberToReturn">The maximum number of documents to return.</param> /// <returns>The facet document.</returns> /// <remarks>Use wildcard chars ('*', '?', '\'), logical ('AND', 'OR'), Quoted exact phrase ("search this").</remarks> public Nequeo.Search.Engine.FacetDocument SearchFacetDocument(string text, FacetData.IndexField[] indexFields, FacetPath[] facetPaths, int numberToReturn = Int32.MaxValue) { Nequeo.Search.Engine.FacetDocument documents = new FacetDocument(); documents.TotalHits = 0; try { // If text exists. if (!String.IsNullOrEmpty(text)) { // Load the searcher. Lucene.Net.Search.IndexSearcher searcher = new Lucene.Net.Search.IndexSearcher(_reader); string searchFieldName = "facetcontent"; Query query = null; DrillDownQuery queryFacet = null; TopDocs results = null; // Build the facet configuration information. FacetsConfig config = new FacetsConfig(); // Add the config. foreach (FacetData.IndexField item in indexFields) { config.SetIndexFieldName(item.DimensionName, item.IndexFieldName); } // Get bytes char[] textArray = text.ToCharArray(); // Search logical. if (text.Contains("AND") || text.Contains("OR")) { // Create the query. query = CreateLogicalQuery(text, searchFieldName); } else if (textArray[0].Equals('"') && textArray[textArray.Length - 1].Equals('"')) { // Create the query. query = CreateQuotedQuery(new string(textArray, 1, textArray.Length - 2), searchFieldName); } else { // Create the query. query = CreateBoolenQuery(text, BooleanClause.Occur.SHOULD, searchFieldName); } // Create the facet query. queryFacet = new DrillDownQuery(config, query); foreach (FacetPath facetPath in facetPaths) { // Add the path. queryFacet.Add(facetPath.DimensionName, facetPath.Path); } // The collector. FacetsCollector collector = new FacetsCollector(); // Search. if (queryFacet != null) { results = FacetsCollector.Search(searcher, queryFacet, numberToReturn, collector); } // Get the total number of results that was asked for. int totalResult = ((results.ScoreDocs != null && results.ScoreDocs.Length > 0) ? results.ScoreDocs.Length : 0); // If result found. if (results != null && results.TotalHits > 0) { List <TextDataResult> textDataResults = new List <TextDataResult>(); List <FileDocumentResult> fileDocResults = new List <FileDocumentResult>(); List <FacetPathResult> facetPathResults = new List <FacetPathResult>(); IDictionary <string, Facets> facetsMap = new Dictionary <string, Facets>(); // Add the facet count. foreach (FacetData.IndexField item in indexFields) { // Add the facet for each index field. facetsMap[item.DimensionName] = GetTaxonomyFacetCounts(_facetReader, config, collector, item.IndexFieldName); } // Create the multi facet list. foreach (FacetPath facetPath in facetPaths) { try { // Add the facets. Facets facets = facetsMap.First(u => u.Key.ToLower().Contains(facetPath.DimensionName.ToLower())).Value; float number = facets.GetSpecificValue(facetPath.DimensionName, facetPath.Path); // Add the path. facetPathResults.Add(new FacetPathResult(facetPath.DimensionName, number, facetPath.Path)); } catch { } } // For each document found. for (int i = 0; i < totalResult; i++) { FileDocumentResult fileDocument = null; TextDataResult textData = null; int docID = results.ScoreDocs[i].Doc; Lucene.Net.Documents.Document doc = searcher.Doc(docID); try { // Get the data for each field. IndexableField[] textNameFields = doc.GetFields("textname"); // If this field exists then text data. if (textNameFields.Length > 0) { // Assign the data to the text document. textData = new TextDataResult(); textData.Name = textNameFields.Length > 0 ? textNameFields[0].StringValue : null; textData.Score = results.ScoreDocs[i].Score; textData.Doc = docID; // Do not know if the text was stored. IndexableField[] textValueFields = doc.GetFields("textcomplete"); textData.Text = textValueFields.Length > 0 ? textValueFields[0].StringValue : null; } } catch { } // If text data exists then add. if (textData != null) { textDataResults.Add(textData); } try { // Get the data for each field. IndexableField[] pathNameFields = doc.GetFields("path"); IndexableField[] modifiedNameFields = doc.GetFields("modified"); // If this field exists then file document. if (pathNameFields.Length > 0) { // Assign the data to the path document. fileDocument = new FileDocumentResult(); fileDocument.Path = pathNameFields.Length > 0 ? pathNameFields[0].StringValue : null; fileDocument.Modified = modifiedNameFields.Length > 0 ? modifiedNameFields[0].StringValue : null; fileDocument.Score = results.ScoreDocs[i].Score; fileDocument.Doc = docID; } } catch { } // If file data exists then add. if (fileDocument != null) { fileDocResults.Add(fileDocument); } } // Assign the facet document values. documents.MaxScore = results.MaxScore; documents.TotalHits = results.TotalHits; documents.FacetPathResults = facetPathResults.ToArray(); documents.TextDataResults = textDataResults.ToArray(); documents.FileDocumentResults = fileDocResults.ToArray(); } } // Return the documents. return(documents); } catch (Exception) { throw; } }