public virtual List <SearchFacet> GetSearchFacets(IQueryExpression expression, string[] groupByFields, Directory directory = null) { if (directory == null) { directory = LuceneContext.Directory; } groupByFields = groupByFields.Select(x => ContentIndexHelpers.GetIndexFieldName(x)).ToArray(); var result = new List <SearchFacet>(); using (IndexReader indexReader = IndexReader.Open(directory, true)) { var queryParser = new MultiFieldQueryParser(LuceneConfiguration.LuceneVersion, expression.GetFieldName() , LuceneConfiguration.Analyzer); queryParser.AllowLeadingWildcard = true; var query = queryParser.Parse(expression.GetExpression()); SimpleFacetedSearch facetSearch = new SimpleFacetedSearch(indexReader, groupByFields); SimpleFacetedSearch.Hits hits = facetSearch.Search(query, int.MaxValue); long totalHits = hits.TotalHitCount; foreach (SimpleFacetedSearch.HitsPerFacet hitPerGroup in hits.HitsPerFacet) { long hitCountPerGroup = hitPerGroup.HitCount; result.Add(new SearchFacet() { Count = hitPerGroup.HitCount, Term = hitPerGroup.Name.ToString() }); } } return(result); }
/// <summary> /// /// </summary> /// <remarks></remarks> /// <seealso cref=""/> /// <param name="query"></param> /// <param name="facets"></param> /// <returns></returns> public static IEnumerable<Facet> facetSearch(Query query, IEnumerable<Facet> facets) { List<Facet> l = new List<Facet>(); foreach (Facet f in facets) { Facet c = new Facet(); c.Name = f.Name; c.Text = f.Text; c.Value = f.Value; c.DisplayName = f.DisplayName; c.Childrens = new List<Facet>(); using (SimpleFacetedSearch sfs = new SimpleFacetedSearch(_Reader, new string[] { "facet_" + f.Name })) { SimpleFacetedSearch.Hits hits = sfs.Search(query); int cCount = 0; foreach (SimpleFacetedSearch.HitsPerFacet hpg in hits.HitsPerFacet) { if (!hpg.Name.ToString().Equals("")) { Facet cc = new Facet(); cc.Name = hpg.Name.ToString(); cc.Text = hpg.Name.ToString(); cc.Value = hpg.Name.ToString(); cc.Count = (int)hpg.HitCount; if (cc.Count > 0) cCount++; c.Childrens.Add(cc); } } c.Count = cCount; l.Add(c); } } return l; }
/// <summary> /// /// </summary> /// <remarks></remarks> /// <seealso cref=""/> /// <param name="query"></param> /// <param name="facets"></param> /// <returns></returns> public static IEnumerable<Property> propertySearch(Query query, IEnumerable<Property> properties) { foreach (Property p in properties) { using (SimpleFacetedSearch sfs = new SimpleFacetedSearch(_Reader, new string[] { "property_" + p.Name })) { SimpleFacetedSearch.Hits hits = sfs.Search(query); int cCount = 0; List<string> tmp = new List<string>(); foreach (SimpleFacetedSearch.HitsPerFacet hpg in hits.HitsPerFacet) { if (!String.IsNullOrEmpty(hpg.Name.ToString())) { if ((int)hpg.HitCount > 0) tmp.Add(hpg.Name.ToString()); } } p.Values = tmp; } } return properties; }
public IDictionary <IDictionary <string, string>, long> SearchGetFacets(string term, string prefix, bool usePrefixQuery, DateTime?start, DateTime?end, int numResults, bool canViewAndSearchAll, bool includeRestrictedSources, string uploadedByUserId, IList <string> owners) { IDictionary <IDictionary <string, string>, long> facetCount = new Dictionary <IDictionary <string, string>, long>(); try { string[] fieldNames = new string[] { //"IsRestricted", //"IsReadOnly", "UploadedBy" //"IsPublic" }; using (SimpleFacetedSearch sfs = new SimpleFacetedSearch(SourceIndexWriterSingleton.Instance.GetReader(), fieldNames)) { Query query = this.BuildQuery(term, prefix, usePrefixQuery, start, end, canViewAndSearchAll, includeRestrictedSources, uploadedByUserId, owners); SimpleFacetedSearch.Hits hits = sfs.Search(query, numResults); //long totalHits = hits.TotalHitCount; foreach (SimpleFacetedSearch.HitsPerFacet hpg in hits.HitsPerFacet) { // HitsPerFacet.Name can be treated like an array whose values match the fieldNames array we fed into SimpleFacetedSearch() // constructor above. IDictionary <string, string> key = new Dictionary <string, string>(); for (int i = 0; i < hpg.Name.Length; i++) { key.Add(fieldNames[i], hpg.Name[i]); } facetCount.Add(key, hpg.HitCount); } } } catch (NullReferenceException e) { log.Error("SimpleFacetedSearch constructor fails when source index is empty: could this be the case?", e); } return(facetCount); }
/// <summary> /// /// </summary> /// <remarks></remarks> /// <seealso cref=""/> private static void Load() { configXML = new XmlDocument(); configXML.Load(FileHelper.ConfigFilePath); XmlNodeList fieldProperties = configXML.GetElementsByTagName("field"); Category categoryDefault = new Category(); categoryDefault.Name = "All"; categoryDefault.Value = "All"; categoryDefault.DisplayName = "All"; categoryDefault.DefaultValue = "nothing"; AllCategoriesDefault.Add(categoryDefault); foreach (XmlNode fieldProperty in fieldProperties) { if (!fieldProperty.Attributes.GetNamedItem("type").Value.ToLower().Equals("primary_data_field")) { String headerItem = fieldProperty.Attributes.GetNamedItem("header_item").Value; String storeItem = fieldProperty.Attributes.GetNamedItem("store").Value; if (headerItem.ToLower().Equals("yes") && storeItem.ToLower().Equals("yes")) { headerItemXmlNodeList.Add(fieldProperty); } String fieldType = fieldProperty.Attributes.GetNamedItem("type").Value; String fieldName = fieldProperty.Attributes.GetNamedItem("lucene_name").Value; String primitiveType = fieldProperty.Attributes.GetNamedItem("primitive_type").Value; if (!primitiveType.ToLower().Equals("string")) { numericProperties.Add(fieldName.ToLower()); } if (fieldType.ToLower().Equals("facet_field")) { facetXmlNodeList.Add(fieldProperty); Facet cDefault = new Facet(); cDefault.Name = fieldName; cDefault.Text = fieldName; cDefault.Value = fieldName; cDefault.DisplayName = fieldProperty.Attributes.GetNamedItem("display_name").Value; cDefault.Childrens = new List <Facet>(); List <Facet> lcDefault = new List <Facet>(); try { Query query = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, "id", new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30)).Parse("*:*"); SimpleFacetedSearch sfs = new SimpleFacetedSearch(_Reader, new string[] { "facet_" + fieldName }); SimpleFacetedSearch.Hits hits = sfs.Search(query); int cCount = 0; foreach (SimpleFacetedSearch.HitsPerFacet hpg in hits.HitsPerFacet) { if (!hpg.Name.ToString().Equals("")) { Facet ccDefault = new Facet(); ccDefault.Name = hpg.Name.ToString(); ccDefault.Text = hpg.Name.ToString(); ccDefault.Value = hpg.Name.ToString(); ccDefault.Count = (int)hpg.HitCount; if (ccDefault.Count > 0) { cCount++; } cDefault.Childrens.Add(ccDefault); } } cDefault.Count = cCount; AllFacetsDefault.Add(cDefault); } catch { } } else if (fieldType.ToLower().Equals("property_field")) { propertyXmlNodeList.Add(fieldProperty); Property cDefault = new Property(); //c.Id = x.Attributes[Property.ID].InnerText; cDefault.Name = fieldProperty.Attributes.GetNamedItem("lucene_name").Value; cDefault.DisplayName = fieldProperty.Attributes.GetNamedItem("display_name").Value; cDefault.DisplayTitle = fieldProperty.Attributes.GetNamedItem("display_name").Value; cDefault.DataSourceKey = fieldProperty.Attributes.GetNamedItem("metadata_name").Value; cDefault.UIComponent = fieldProperty.Attributes.GetNamedItem("uiComponent").Value;; cDefault.AggregationType = "distinct"; cDefault.DefaultValue = "All"; cDefault.DataType = fieldProperty.Attributes.GetNamedItem("primitive_type").Value; if (cDefault.UIComponent.ToLower().Equals("range") && cDefault.DataType.ToLower().Equals("date")) { String direction = fieldProperty.Attributes.GetNamedItem("direction").Value; if (direction.ToLower().Equals("increase")) { cDefault.Direction = Direction.increase; } else { cDefault.Direction = Direction.decrease; } } Query query = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, "id", new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29)).Parse("*:*"); try { SimpleFacetedSearch sfs = new SimpleFacetedSearch(_Reader, new string[] { "property_" + fieldName }); SimpleFacetedSearch.Hits hits = sfs.Search(query); List <string> laDefault = new List <string>(); foreach (SimpleFacetedSearch.HitsPerFacet hpg in hits.HitsPerFacet) { laDefault.Add(hpg.Name.ToString()); } if (!cDefault.UIComponent.ToLower().Equals("range")) { laDefault.Add("All"); } ; laDefault.Sort(); cDefault.Values = laDefault; AllPropertiesDefault.Add(cDefault); } catch { } } else if (fieldType.ToLower().Equals("category_field")) { categoryXmlNodeList.Add(fieldProperty); Category cDefault = new Category(); cDefault.Name = fieldProperty.Attributes.GetNamedItem("lucene_name").Value; cDefault.DisplayName = fieldProperty.Attributes.GetNamedItem("display_name").Value; cDefault.Value = fieldProperty.Attributes.GetNamedItem("lucene_name").Value;; cDefault.DefaultValue = "nothing"; AllCategoriesDefault.Add(cDefault); } else if (fieldType.ToLower().Equals("general_field")) { generalXmlNodeList.Add(fieldProperty); } } else if (fieldProperty.Attributes.GetNamedItem("type").Value.ToLower().Equals("primary_data_field")) { categoryXmlNodeList.Add(fieldProperty); Category cDefault = new Category(); cDefault.Name = fieldProperty.Attributes.GetNamedItem("lucene_name").Value; cDefault.DisplayName = fieldProperty.Attributes.GetNamedItem("display_name").Value; cDefault.Value = fieldProperty.Attributes.GetNamedItem("lucene_name").Value;; cDefault.DefaultValue = "nothing"; AllCategoriesDefault.Add(cDefault); } } }