Filter documents to be searched using .NET

Hi,i wish to pass Specific Folder or few documents inside the Main folder need to be indexed and Search should perform.any Idea?

image.png (319.9 KB)

Instead of Sending whole Folder path,i need to specify some files,that should be indexed and search should happen

string documentsFolder = Utils.DocumentsPath; // Specify the path to a folder containing documents to search

        Index index = new Index(indexFolder);
        index.Events.ErrorOccurred += (sender, args) =>
        {
            Console.WriteLine(args.Message);
        };

        index.Add(documentsFolder);
1 Like

@bharathiGK,

Please go through document filtering during indexing article. You can filter documents for indexing purpose. Let us know if it doesn’t help.

I gone through the filter,but my requirement is,
For Example,i stored all files in one folder(common for storing all files),from that ,i used to share files to others,if user or DataRoom has access to that particular file means,i need to search that particular file only

1 Like

i have an another doubt in indexing.
For Example- initially i have 5 files inside the storage folder.When i do searching,it will create an index for those 5 files.After that i’m adding another file into that storage folder,so in this case how index will update?..it will update or create as a new index?

1 Like

@bharathiGK,

If you have let’s say 50 files in a folder and you search for a file “ABC.docx” and it is available in the storage. API will surely return it. Have a look at this article and you can see what could be done using search results.

You can update existing index. Calling the Update method starts re-indexing of changed, new and deleted documents. Please go through this update index article to learn more.

Thank you for you information

1 Like

@bharathiGK,

You’re welcome.

string indexFolder = @"c:\MyIndex\" ;

string documentFolder = @"c:\MyDocuments\" ;

// Creating an index in the specified folder

Index index = new Index(indexFolder);

// Indexing documents from the specified folder

index.Add(documentFolder);

// Change, delete, add documents in the document folder

// ...

UpdateOptions options = new UpdateOptions();

options.Threads = 2; // Setting the number of indexing threads

index.Update(options); // Updating the index

I have a doubt in this way…First time i added Index

index.Add(documentFolder);

from next time on wards i need to update the index only…

if(indexdone)
{
UpdateOptions options = new UpdateOptions();

options.Threads = 2; // Setting the number of indexing threads

index.Update(options); // Updating the index
}
else
{
index.Add(documentFolder);
}

Like this will it work

because update index not working for me.after adding a document into document folder

1 Like

@bharathiGK,

Can you please share a sample application (using your use-case) where update index is not working? We’ll then look into this scenario.

HI.Refer the following code

        //path settings
        string indexFolder = "C:\\Store\\IndexStore\\";
        string documentsFolder = Path.Combine(Properties.Settings.Default.DocumentStorageFolderPath);
       
        if (Directory.Exists(indexFolder))
        {}
        else
        {
            Directory.CreateDirectory(indexFolder);
        }
      
        //index settings AutoDetectEncoding
        IndexSettings settings = new IndexSettings();
        settings.AutoDetectEncoding = true;

      
        List<MODEL.AdvancedSearchResult> getDocuments = new List<MODEL.AdvancedSearchResult>();
        List<MODEL.AdvancedSearchResult> getDocumentsAll = new List<MODEL.AdvancedSearchResult>();


        //setting DocumentFilter for Indexing
        List<DocumentFilter> filterSet = new List<DocumentFilter>();

        //Get All Documents Under ProjectId
        getDocumentsAll = searchRepo.GetDocumentListItems(projectId, 0, 0, userId, fileUrlBase, findText, fromDate, toDate);

        //Filter Documents based on ProjectId from Document Storage

        if (getDocumentsAll.Count > 1)
        {
            foreach (var item in getDocumentsAll)
            {
                DocumentFilter filter = DocumentFilter.CreateFilePathRegularExpression(item.Location, RegexOptions.IgnoreCase);
                filterSet.Add(filter);
            }
            DocumentFilter finalFilter = DocumentFilter.CreateOr(filterSet.ToArray());
            settings.DocumentFilter = finalFilter;
        }
        else
        {
            DocumentFilter filter = DocumentFilter.CreateFilePathRegularExpression(getDocumentsAll.Select(x => x.Location).Single(), RegexOptions.IgnoreCase);
            settings.DocumentFilter = filter;
        }


        //index class with async option

        Index index = new Index(indexFolder, settings);
        IndexingOptions options = new IndexingOptions();
        options.IsAsync = true;

        // Adding Additional Data
        if (getDocumentsAll.Count > 1)
        {
            foreach (var item in getDocumentsAll)
            {
                index.Events.FileIndexing += (sender, args) =>
                {
                    if (args.DocumentFullPath.EndsWith(item.Location))
                    {
                        // Adding two attributes
                        args.AdditionalFields = new DocumentField[] // Setting additional fields for the current document
                         {
                            new DocumentField("ProjectName", item.ProjectName),
                            new DocumentField("DataRoomName", item.DataRoomName),
                            new DocumentField("FolderName", item.FolderName),
                            new DocumentField("UserName", model.DisplayUserName)
                         };
                    }
                };
            }
        }

        //Creating an Indexing

        string[] files = System.IO.Directory.GetFiles(indexFolder , "*.body");
        if (files.Length == 0)
        {
            index.Add(documentsFolder);
        }
        else
        {
          
            UpdateOptions updateoptions = new UpdateOptions();
            updateoptions.Threads = 2; // Setting the number of indexing threads
            index.Update(updateoptions);

            MergeOptions mergeoptions = new MergeOptions();
            mergeoptions.Cancellation = new Cancellation(); // Creating cancellation object to be able to cancel the operation
            mergeoptions.Cancellation.CancelAfter(1200000); // Setting maximum duration of the operation to 30 seconds

            index.Optimize(mergeoptions);
          
        }

Update index not working…can you check and update me

once i stopped application,after rebuild,its updating sometime and sometime not updating

updateoptions.Threads = 2; what it means…2 is atleast 2 documents or something else

1 Like

@bharathiGK,

We’re investigating this scenario. Your investigation ticket ID is SEARCHNET-2269. As there’s any update, you’ll be notified.

@bharathiGK,

Please note that the updating process when calling the update method is performed only for those documents that have been changed (the update date of the document is changed). The rest of the indexed documents will not be updated.

If any new file added into the document storage folder means,it should get updated right?

@bharathiGK,

Yes, if the update index function is processed/invoked.