Exemplo n.º 1
0
        /// <summary>
        /// Extends a Content version with text extract.
        /// </summary>
        public static void AddTextExtract(int versionId, string textExtract)
        {
            // 1: load indexDocument.
            var docData  = SearchManager.LoadIndexDocumentByVersionId(versionId);
            var indexDoc = docData.IndexDocument;

            // 2: original and new text extract concatenation.
            textExtract = (indexDoc.GetStringValue(IndexFieldName.AllText) ?? "") + textExtract;

            indexDoc.Add(new IndexField(IndexFieldName.AllText, textExtract, IndexingMode.Analyzed, IndexStoringMode.No,
                                        IndexTermVector.No));

            // 3: save indexDocument.
            docData.IndexDocumentChanged();
            DataProvider.SaveIndexDocument(versionId, docData.SerializedIndexDocument);

            // 4: distributed cache invalidation because of version timestamp.
            DataBackingStore.RemoveNodeDataFromCacheByVersionId(versionId);

            // 5: index update.
            var node = Node.LoadNodeByVersionId(versionId);

            if (node != null)
            {
                SearchManager.GetIndexPopulator().RebuildIndex(node);
            }
        }
Exemplo n.º 2
0
        public static void AddTextExtract(int versionId, string textExtract)
        {
            // 1: load indexDocumentInfo.
            var docData = StorageContext.Search.LoadIndexDocumentByVersionId(versionId);

            var buffer    = docData.IndexDocumentInfoBytes;
            var docStream = new System.IO.MemoryStream(buffer);
            var formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
            var info      = (IndexDocumentInfo)formatter.Deserialize(docStream);

            // 2: original and new text extract concatenation.
            var allTextField = info.Fields.FirstOrDefault(f => f.Name == LucObject.FieldName.AllText);

            if (allTextField != null)
            {
                textExtract = allTextField.Value + textExtract;
                info.Fields.Remove(allTextField);
            }
            info.Fields.Add(
                new IndexFieldInfo(
                    LucObject.FieldName.AllText,
                    textExtract,
                    FieldInfoType.StringField,
                    Lucene.Net.Documents.Field.Store.NO,
                    Lucene.Net.Documents.Field.Index.ANALYZED,
                    Lucene.Net.Documents.Field.TermVector.NO));

            // 3: save indexDocumentInfo.
            using (var docStream2 = new MemoryStream())
            {
                var formatter2 = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                formatter2.Serialize(docStream2, info);
                docStream2.Flush();
                docStream2.Position = 0;
                var bytes = docStream2.GetBuffer();
                DataProvider.SaveIndexDocument(versionId, bytes);
            }

            // 4: distributed cache invalidation because of version timestamp.
            DataBackingStore.RemoveNodeDataFromCacheByVersionId(versionId);

            // 5: distributed lucene index update.
            var node = Node.LoadNodeByVersionId(versionId);

            if (node != null)
            {
                StorageContext.Search.SearchEngine.GetPopulator().RefreshIndex(node, false);
            }
        }