/// <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; } }
/// <summary> /// Search text in the existing index. /// </summary> /// <param name="text">The text to search for.</param> /// <param name="numberToReturn">The maximum number of documents to return.</param> /// <returns>The text data.</returns> /// <remarks>Search for text that is stored along with the index data. /// Use wildcard chars ('*', '?', '\'), logical ('AND', 'OR'), Quoted exact phrase ("search this")</remarks> public Nequeo.Search.Engine.TextData SearchText(string text, int numberToReturn = Int32.MaxValue) { Nequeo.Search.Engine.TextData documents = new TextData(); 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 = "text"; Query query = null; TopDocs results = null; // 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); } // Search. if (query != null) { results = searcher.Search(query, numberToReturn); } // 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>(); // For each document found. for (int i = 0; i < totalResult; i++) { TextDataResult document = new TextDataResult(); int docID = results.ScoreDocs[i].Doc; Lucene.Net.Documents.Document doc = searcher.Doc(docID); // Get the data for each field. IndexableField[] textNameFields = doc.GetFields("textname"); // Assign the data to the text document. document.Name = textNameFields.Length > 0 ? textNameFields[0].StringValue : null; document.Score = results.ScoreDocs[i].Score; document.Doc = docID; try { // Do not know if the text was stored. IndexableField[] textValueFields = doc.GetFields("textcomplete"); document.Text = textValueFields.Length > 0 ? textValueFields[0].StringValue : null; } catch { } // Add the document. textDataResults.Add(document); } // Assign documents.TotalHits = results.TotalHits; documents.MaxScore = results.MaxScore; documents.Results = textDataResults.ToArray(); } } // Return the documents. return(documents); } catch (Exception) { throw; } }