public virtual void TestRequireDimCount() { Store.Directory dir = NewDirectory(); Store.Directory taxoDir = NewDirectory(); RandomIndexWriter writer = new RandomIndexWriter( #if FEATURE_INSTANCE_TESTDATA_INITIALIZATION this, #endif Random, dir); DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir, OpenMode.CREATE); FacetsConfig config = new FacetsConfig(); config.SetRequireDimCount("dim", true); config.SetMultiValued("dim2", true); config.SetRequireDimCount("dim2", true); config.SetMultiValued("dim3", true); config.SetHierarchical("dim3", true); config.SetRequireDimCount("dim3", true); Document doc = new Document(); doc.Add(NewTextField("field", "text", Field.Store.NO)); doc.Add(new FacetField("dim", "a")); doc.Add(new FacetField("dim2", "a")); doc.Add(new FacetField("dim2", "b")); doc.Add(new FacetField("dim3", "a", "b")); doc.Add(new FacetField("dim3", "a", "c")); writer.AddDocument(config.Build(taxoWriter, doc)); // NRT open IndexSearcher searcher = NewSearcher(writer.GetReader()); // NRT open var taxoReader = new DirectoryTaxonomyReader(taxoWriter); FacetsCollector c = new FacetsCollector(); searcher.Search(new MatchAllDocsQuery(), c); Facets facets = GetTaxonomyFacetCounts(taxoReader, config, c); Assert.AreEqual(1, facets.GetTopChildren(10, "dim").Value); Assert.AreEqual(1, facets.GetTopChildren(10, "dim2").Value); Assert.AreEqual(1, facets.GetTopChildren(10, "dim3").Value); try { Assert.AreEqual(1, facets.GetSpecificValue("dim")); fail("didn't hit expected exception"); } catch (Exception iae) when(iae.IsIllegalArgumentException()) { // expected } Assert.AreEqual(1, facets.GetSpecificValue("dim2")); Assert.AreEqual(1, facets.GetSpecificValue("dim3")); IOUtils.Dispose(writer, taxoWriter, searcher.IndexReader, taxoReader, dir, taxoDir); }
public virtual void TestRequireDimCount() { Store.Directory dir = NewDirectory(); Store.Directory taxoDir = NewDirectory(); RandomIndexWriter writer = new RandomIndexWriter(Random(), dir, Similarity, TimeZone); DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir, IndexWriterConfig.OpenMode_e.CREATE); FacetsConfig config = new FacetsConfig(); config.SetRequireDimCount("dim", true); config.SetMultiValued("dim2", true); config.SetRequireDimCount("dim2", true); config.SetMultiValued("dim3", true); config.SetHierarchical("dim3", true); config.SetRequireDimCount("dim3", true); Document doc = new Document(); doc.Add(NewTextField("field", "text", Field.Store.NO)); doc.Add(new FacetField("dim", "a")); doc.Add(new FacetField("dim2", "a")); doc.Add(new FacetField("dim2", "b")); doc.Add(new FacetField("dim3", "a", "b")); doc.Add(new FacetField("dim3", "a", "c")); 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); Facets facets = GetTaxonomyFacetCounts(taxoReader, config, c); Assert.AreEqual(1, facets.GetTopChildren(10, "dim").Value); Assert.AreEqual(1, facets.GetTopChildren(10, "dim2").Value); Assert.AreEqual(1, facets.GetTopChildren(10, "dim3").Value); try { Assert.AreEqual(1, facets.GetSpecificValue("dim")); Fail("didn't hit expected exception"); } catch (System.ArgumentException) { // expected } Assert.AreEqual(1, facets.GetSpecificValue("dim2")); Assert.AreEqual(1, facets.GetSpecificValue("dim3")); IOUtils.Close(writer, taxoWriter, searcher.IndexReader, taxoReader, dir, taxoDir); }
public virtual void TestMultiValuedHierarchy() { Store.Directory dir = NewDirectory(); Store.Directory taxoDir = NewDirectory(); DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir, OpenMode.CREATE); FacetsConfig config = new FacetsConfig(); config.SetHierarchical("a", true); config.SetMultiValued("a", true); RandomIndexWriter writer = new RandomIndexWriter( #if FEATURE_INSTANCE_TESTDATA_INITIALIZATION this, #endif Random, dir); Document doc = new Document(); doc.Add(NewTextField("field", "text", Field.Store.NO)); doc.Add(new FacetField("a", "path", "x")); doc.Add(new FacetField("a", "path", "y")); writer.AddDocument(config.Build(taxoWriter, doc)); // NRT open IndexSearcher searcher = NewSearcher(writer.GetReader()); // NRT open var taxoReader = new DirectoryTaxonomyReader(taxoWriter); // Aggregate the facet counts: FacetsCollector c = new FacetsCollector(); // MatchAllDocsQuery is for "browsing" (counts facets // for all non-deleted docs in the index); normally // you'd use a "normal" query, and use MultiCollector to // wrap collecting the "normal" hits and also facets: searcher.Search(new MatchAllDocsQuery(), c); Facets facets = GetTaxonomyFacetCounts(taxoReader, config, c); try { facets.GetSpecificValue("a"); fail("didn't hit expected exception"); } catch (Exception iae) when(iae.IsIllegalArgumentException()) { // expected } FacetResult result = facets.GetTopChildren(10, "a"); Assert.AreEqual(1, result.LabelValues.Length); Assert.AreEqual(1, (int)result.LabelValues[0].Value); IOUtils.Dispose(writer, taxoWriter, searcher.IndexReader, taxoReader, dir, taxoDir); }
public virtual void TestLabelWithDelimiter() { Store.Directory dir = NewDirectory(); Store.Directory taxoDir = NewDirectory(); RandomIndexWriter writer = new RandomIndexWriter( #if FEATURE_INSTANCE_TESTDATA_INITIALIZATION this, #endif Random, dir); var taxoWriter = new DirectoryTaxonomyWriter(taxoDir, OpenMode.CREATE); FacetsConfig config = new FacetsConfig(); config.SetMultiValued("dim", true); Document doc = new Document(); doc.Add(NewTextField("field", "text", Field.Store.NO)); doc.Add(new FacetField("dim", "test\u001Fone")); doc.Add(new FacetField("dim", "test\u001Etwo")); writer.AddDocument(config.Build(taxoWriter, doc)); // NRT open IndexSearcher searcher = NewSearcher(writer.GetReader()); // NRT open var taxoReader = new DirectoryTaxonomyReader(taxoWriter); FacetsCollector c = new FacetsCollector(); searcher.Search(new MatchAllDocsQuery(), c); Facets facets = GetTaxonomyFacetCounts(taxoReader, config, c); Assert.AreEqual(1, facets.GetSpecificValue("dim", "test\u001Fone")); Assert.AreEqual(1, facets.GetSpecificValue("dim", "test\u001Etwo")); FacetResult result = facets.GetTopChildren(10, "dim"); Assert.AreEqual("dim=dim path=[] value=-1 childCount=2\n test\u001Fone (1)\n test\u001Etwo (1)\n", result.ToString()); IOUtils.Dispose(writer, taxoWriter, 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; } }