/// <summary> /// index all supplied nodes (and their detached content) /// </summary> /// <param name="nodes">collection of nodes (and any detached content they may have) to be indexed</param> internal void Index(IPublishedContent[] nodes) { var indexWriter = this.GetIndexWriter(); foreach (var node in nodes) { var indexingContext = new IndexingContext(null, node, this.Name); var document = new Document(); LookService.Index(indexingContext, document); indexWriter.AddDocument(document); foreach (var detachedContent in node.GetFlatDetachedDescendants()) { indexingContext = new IndexingContext(node, detachedContent, this.Name); document = new Document(); LookService.Index(indexingContext, document); indexWriter.AddDocument(document); // index each detached item } indexWriter.Commit(); //indexWriter.Optimize(); } }
//protected override void AddDocument(Dictionary<string, string> fields, IndexWriter writer, int nodeId, string type) //{ // base.AddDocument(fields, writer, nodeId, type); //} //protected override void AddSingleNodeToIndex(XElement node, string type) //{ // base.AddSingleNodeToIndex(node, type); //} //protected override IndexWriter CreateIndexWriter() //{ // var debug = base.CreateIndexWriter(); // return debug; //} //public override void DeleteFromIndex(string nodeId) //{ // base.DeleteFromIndex(nodeId); //} //protected override Dictionary<string, string> GetDataToIndex(XElement node, string type) //{ // var debug = base.GetDataToIndex(node, type); // return debug; //} //protected override IIndexCriteria GetIndexerData(IndexSet indexSet) //{ // var debug = base.GetIndexerData(indexSet); // return debug; //} //public override Directory GetLuceneDirectory() //{ // var debug = base.GetLuceneDirectory(); // return debug; //} //protected override FieldIndexTypes GetPolicy(string fieldName) //{ // var debug = base.GetPolicy(fieldName); // return debug; //} //protected override Dictionary<string, string> GetSpecialFieldsToIndex(Dictionary<string, string> allValuesForIndexing) //{ // var debug = base.GetSpecialFieldsToIndex(allValuesForIndexing); // return debug; //} //public override void IndexAll(string type) //{ // base.IndexAll(type); //} //public override bool IndexExists() //{ // var debug = base.IndexExists(); // return debug; //} //protected override void OnDocumentWriting(DocumentWritingEventArgs docArgs) //{ // base.OnDocumentWriting(docArgs); //} //protected override void OnDuplicateFieldWarning(int nodeId, string indexSetName, string fieldName) //{ // base.OnDuplicateFieldWarning(nodeId, indexSetName, fieldName); //} //protected override void OnGatheringFieldData(IndexingFieldDataEventArgs e) //{ // base.OnGatheringFieldData(e); //} //protected override void OnGatheringNodeData(IndexingNodeDataEventArgs e) //{ // base.OnGatheringNodeData(e); //} //protected override void OnIgnoringNode(IndexingNodeDataEventArgs e) //{ // base.OnIgnoringNode(e); //} //protected override void OnIndexDeleted(DeleteIndexEventArgs e) //{ // base.OnIndexDeleted(e); //} //protected override void OnIndexingError(IndexingErrorEventArgs e) //{ // base.OnIndexingError(e); //} //protected override void OnIndexOperationComplete(EventArgs e) //{ // base.OnIndexOperationComplete(e); //} //protected override void OnIndexOptimized(EventArgs e) //{ // base.OnIndexOptimized(e); //} //protected override void OnIndexOptimizing(EventArgs e) //{ // base.OnIndexOptimizing(e); //} //protected override void OnNodeIndexed(IndexedNodeEventArgs e) //{ // base.OnNodeIndexed(e); //} //protected override void OnNodeIndexing(IndexingNodeEventArgs e) //{ // base.OnNodeIndexing(e); //} //protected override void OnNodesIndexed(IndexedNodesEventArgs e) //{ // base.OnNodesIndexed(e); //} //protected override void OnNodesIndexing(IndexingNodesEventArgs e) //{ // base.OnNodesIndexing(e); //} //public override void ReIndexNode(XElement node, string type) //{ // base.ReIndexNode(node, type); //} //protected override bool ValidateDocument(XElement node) //{ // return base.ValidateDocument(node); //} /// <summary> /// index all supplied nodes (and their detached content) /// </summary> /// <param name="nodes">collection of nodes (and any detached content they may have) to be indexed</param> internal void Index(IPublishedContent[] nodes) { #if DEBUG var stopwatch = Stopwatch.StartNew(); #endif var indexWriter = this.GetIndexWriter(); foreach (var node in nodes) { var indexingContext = new IndexingContext( hostNode: null, node: node, indexerName: this.Name); var document = new Document(); LookService.Index(indexingContext, document); indexWriter.AddDocument(document); foreach (var detachedNode in node.GetDetachedDescendants()) { indexingContext = new IndexingContext( hostNode: node, node: detachedNode, indexerName: this.Name); document = new Document(); LookService.Index(indexingContext, document); indexWriter.AddDocument(document); // index each detached item } } indexWriter.Commit(); #if DEBUG stopwatch.Stop(); LogHelper.Debug(typeof(LookService), $"Indexing { nodes.Length } Item(s) Took { stopwatch.ElapsedMilliseconds }ms"); #endif }
/// <summary> /// Do the indexing and set the field values onto the Lucene document /// </summary> /// <param name="indexingContext"></param> /// <param name="document"></param> internal static void Index(IndexingContext indexingContext, Document document) { if (indexingContext.Item != null) { var publishedItemType = indexingContext?.HostItem?.ItemType ?? indexingContext.Item.ItemType; var hasNodeField = new Field( LookConstants.HasNodeField, "1", Field.Store.NO, Field.Index.NOT_ANALYZED); var nodeIdField = new Field( LookConstants.NodeIdField, indexingContext.Item.Id.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED); var nodeKeyField = new Field( LookConstants.NodeKeyField, indexingContext.Item.GetGuidKey().ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED); var nodeTypeField = new Field( LookConstants.NodeTypeField, publishedItemType.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED, Field.TermVector.NO); var nodeAliasField = new Field( LookConstants.NodeAliasField, indexingContext.Item.DocumentTypeAlias, Field.Store.NO, Field.Index.NOT_ANALYZED, Field.TermVector.NO); document.Add(hasNodeField); document.Add(nodeIdField); document.Add(nodeKeyField); document.Add(nodeTypeField); document.Add(nodeAliasField); if (publishedItemType == PublishedItemType.Content) { var culture = indexingContext?.HostItem?.GetCulture() ?? indexingContext.Item.GetCulture(); if (culture != null) { var cultureField = new Field( LookConstants.CultureField, culture.LCID.ToString(), Field.Store.NO, Field.Index.NOT_ANALYZED, Field.TermVector.NO); document.Add(cultureField); } } if (indexingContext.HostItem != null) { var isDetachedField = new Field( LookConstants.IsDetachedField, "1", Field.Store.NO, Field.Index.NOT_ANALYZED); // indexing detached item, so store the host context id so we can return the detached item var hostIdField = new Field( LookConstants.HostIdField, indexingContext.HostItem.Id.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED); document.Add(isDetachedField); document.Add(hostIdField); } } if (LookService.Instance.NameIndexer != null) { string name = null; try { name = LookService.Instance.NameIndexer(indexingContext); } catch (Exception exception) { LogHelper.WarnWithException(typeof(LookService), "Error in name indexer", exception); } if (name != null) { var hasNameField = new Field( LookConstants.HasNameField, "1", Field.Store.NO, Field.Index.NOT_ANALYZED); var nameField = new Field( LookConstants.NameField, name, Field.Store.YES, Field.Index.NOT_ANALYZED, Field.TermVector.YES); // field for lower case searching var nameFieldLowered = new Field( LookConstants.NameField + "_Lowered", name.ToLower(), Field.Store.NO, Field.Index.NOT_ANALYZED, Field.TermVector.YES); var nameSortedField = new Field( LuceneIndexer.SortedFieldNamePrefix + LookConstants.NameField, name.ToLower(), // force case insentive sorting Field.Store.NO, Field.Index.NOT_ANALYZED, Field.TermVector.NO); document.Add(hasNameField); document.Add(nameField); document.Add(nameFieldLowered); document.Add(nameSortedField); } } if (LookService.Instance.DateIndexer != null) { DateTime?date = null; try { date = LookService.Instance.DateIndexer(indexingContext); } catch (Exception exception) { LogHelper.WarnWithException(typeof(LookService), "Error in date indexer", exception); } if (date != null) { var hasDateField = new Field( LookConstants.HasDateField, "1", Field.Store.NO, Field.Index.NOT_ANALYZED); var dateValue = DateTools.DateToString(date.Value, DateTools.Resolution.SECOND); var dateField = new Field( LookConstants.DateField, dateValue, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.YES); var dateSortedField = new Field( LuceneIndexer.SortedFieldNamePrefix + LookConstants.DateField, dateValue, Field.Store.NO, Field.Index.NOT_ANALYZED, Field.TermVector.NO); document.Add(hasDateField); document.Add(dateField); document.Add(dateSortedField); } } if (LookService.Instance.TextIndexer != null) { string text = null; try { text = LookService.Instance.TextIndexer(indexingContext); } catch (Exception exception) { LogHelper.WarnWithException(typeof(LookService), "Error in text indexer", exception); } if (text != null) { var hasTextField = new Field( LookConstants.HasTextField, "1", Field.Store.NO, Field.Index.NOT_ANALYZED); var textField = new Field( LookConstants.TextField, text, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.YES); document.Add(hasTextField); document.Add(textField); } } if (LookService.Instance.TagIndexer != null) { LookTag[] tags = null; try { tags = LookService.Instance.TagIndexer(indexingContext); } catch (Exception exception) { LogHelper.WarnWithException(typeof(LookService), "Error in tag indexer", exception); } if (tags != null) { foreach (var tag in tags) { var hasTagsField = new Field( LookConstants.HasTagsField, "1", Field.Store.NO, Field.Index.NOT_ANALYZED); // add all tags to a common field (serialized such that Tag objects can be restored from this) var allTagsField = new Field( LookConstants.AllTagsField, tag.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED); // add the tag value to a specific field - this is used for searching on var tagField = new Field( LookConstants.TagsField + tag.Group, tag.Name, Field.Store.YES, Field.Index.NOT_ANALYZED); document.Add(hasTagsField); document.Add(allTagsField); document.Add(tagField); } } } if (LookService.Instance.LocationIndexer != null) { Location location = null; try { location = LookService.Instance.LocationIndexer(indexingContext); } catch (Exception exception) { LogHelper.WarnWithException(typeof(LookService), "Error in location indexer", exception); } if (location != null) { var hasLocationField = new Field( LookConstants.HasLocationField, "1", Field.Store.NO, Field.Index.NOT_ANALYZED); var locationField = new Field( LookConstants.LocationField, location.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED); var locationLatitudeField = new Field( LookConstants.LocationField + "_Latitude", NumericUtils.DoubleToPrefixCoded(location.Latitude), Field.Store.YES, Field.Index.NOT_ANALYZED); var locationLongitudeField = new Field( LookConstants.LocationField + "_Longitude", NumericUtils.DoubleToPrefixCoded(location.Longitude), Field.Store.YES, Field.Index.NOT_ANALYZED); document.Add(hasLocationField); document.Add(locationField); document.Add(locationLatitudeField); document.Add(locationLongitudeField); foreach (var cartesianTierPlotter in LookService.Instance.CartesianTierPlotters) { var boxId = cartesianTierPlotter.GetTierBoxId(location.Latitude, location.Longitude); var tierField = new Field( cartesianTierPlotter.GetTierFieldName(), NumericUtils.DoubleToPrefixCoded(boxId), Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS); document.Add(tierField); } } } }
/// <summary> /// index all supplied nodes (and their detached content) /// </summary> /// <param name="nodes">collection of nodes conent/media/member nodes to be indexed</param> /// <param name="indexItem">when true, indicates the IPublishedContent nodes should be indexed</param> /// <param name="indexDetached">when true, indicates the detached items for each node should be indexed</param> internal void Index(IEnumerable <IPublishedContent> nodes, bool indexItem, bool indexDetached) { if (!nodes.Any()) { return; } if (!indexItem && !indexDetached) { return; // possible } var stopwatch = Stopwatch.StartNew(); var counter = 0; var indexWriter = this.GetIndexWriter(); foreach (var node in nodes) { IndexingContext indexingContext; Document document; if (indexItem) { indexingContext = new IndexingContext(null, node, this.Name); document = new Document(); LookService.Index(indexingContext, document); if (!indexingContext.Cancelled) { counter++; indexWriter.AddDocument(document); } } if (indexDetached) { IPublishedContent[] detachedNodes = null; try { // SEOChecker prior to 2.2 doesn't handle IPublishedContent without an ID detachedNodes = node.GetDetachedDescendants(); } catch (Exception exception) { LogHelper.WarnWithException(typeof(LookIndexer), "Error handling Detached items", exception); } finally { if (detachedNodes != null) { foreach (var detachedNode in detachedNodes) { indexingContext = new IndexingContext(node, detachedNode, this.Name); document = new Document(); LookService.Index(indexingContext, document); if (!indexingContext.Cancelled) { counter++; indexWriter.AddDocument(document); // index each detached item } } } } } } indexWriter.Commit(); stopwatch.Stop(); if (counter > 0) { LogHelper.Debug(typeof(LookIndexer), $"Indexing { counter } Item(s) Took { stopwatch.ElapsedMilliseconds }ms"); } }