Programming     Travel Logs     Life Is Good     Surfing Online     About Me
Set and enforce an aspirational personal hourly rate. If fixing a problem will save less than your hourly rate, ignore it. If outsourcing a task will cost less than your hourly rate, outsource it.
-Naval Ravikant
2018-05-03 20:46:22

Copy this link when reproducing:
http://www.casperlee.com/en/y/blog/54

Now, let's review 3 key features of this Personal Document Management System.

1. Support handling huge files.
2. Support encrypting different documents with different passwords. Every time user accesses this system, he or she must offer a password. Then the system will only try to perform the searching in the documents which are encrypted with the same password.
3. For the text documents, support full text searching.

We have implemented the first 2 features. Now it is time to implement the 3rd feature: full text searching.

Just like before, let's take it easy and enjoy a music video first. This video was shot by me at the sea world, and it is about a song performed by a beautiful lady.

The size of this video is about 50 MB. So better not to play it without WiFi.

To support full text searching, I chose 2 3rd-party tools: Lucene.Net & Pangu. Let's get these 2 tools first.

1. Right click on the solution and choose "Add -> New Project..." on the popped menu to create a new Class Library project. We'll call it "the full text search library project" later.

/Images/20170116/01.jpg

2. Remove the file "Class1.cs".

3. Right click on the newly created project and choose "Properties" on the popped menu. Change the option "Output Path" to "..\Bin".

/Images/20170116/02.jpg

4. Right click on the full text search library project and choose "Manage NuGet Packages..." on the popped menu.

5. Search with "Lucene.Net" and install the tool.

/Images/20170116/03.jpg

6. Search with "PanGu" and install the tool "Lucene.Net.Analysis.PanGu".

/Images/20170116/04.jpg

Now, let's make a little change to the Add Resource form.

7. Open the file frmAddResource.cs in the main project.

8. Add a Text Box, a Check Box, and a Button to the form.

/Images/20170116/05.jpg

9. Here are some properties of these controls.

 Control Name   Property Name   Property Value 
 Check Box   Name  cbFulltextSearch
 Text  Enable Full text searching
 Raw Text Edit    Name  tbRawTextFile
 ReadOnly  True
 TabStop  False
 SelectRawTextFile Button   Name  btnSelectRawTextFile

 

The two check boxes on the form ("Batch Import" and "Enable Full text searching") are mutually exclusive, which means only one of them can be ticked.

10. Modify cbBatch_CheckedChanged function in the frmAddResource.cs file.

        private void cbBatch_CheckedChanged(object sender, EventArgs e)
{
tbName.ReadOnly = cbBatch.Checked;
if (cbFulltextSearch.Checked == cbBatch.Checked)
{
cbFulltextSearch.Checked = !cbBatch.Checked;
}
}

11. Add a OnCheckedChanged event handler for the newly added check box.

        private void cbFulltextSearch_CheckedChanged(object sender, EventArgs e)
{
btnSelectRawTextFile.Enabled = cbFulltextSearch.Checked;
if (cbBatch.Checked == cbFulltextSearch.Checked)
{
cbBatch.Checked = !cbFulltextSearch.Checked;
}
}

According to its document, "Apache Lucene.Net is a C# full-text search engine. Apache Lucene.Net is not a complete application,
but rather a code library and API that can easily be used to add search capabilities to applications."

"Lucene.Net is limited to creating an index and searching that index. It's up to the application to handle opening files and extracting their contents for the index." http://stackoverflow.com/questions/1275722/how-might-i-index-pdf-files-using-lucene-net

For now, I don't want to extract the text for a document automatically. Instead, I added a Raw Text Edit to the form which allows me extracting text from a document manually to a .txt file and then specify the full name of the .txt file on the form.

12. Add a OnClick event handler for the newly added button.

        private void btnSelectRawTextFile_Click(object sender, EventArgs e)
{
OpenFileDialog fd = new OpenFileDialog();
fd.Filter = "*.txt|*.txt";
if (fd.ShowDialog(this) == DialogResult.OK)
{
tbRawTextFile.Text = fd.FileName;
}
}

13. Modify the function ValidateInput and add the validation for the newly added controls.

        private bool ValidateInput()
{
if (cbBatch.Checked)
{
...
}
else
{
if ((!File.Exists(tbFile.Text)
|| (string.IsNullOrEmpty(tbName.Text))))
{
MessageBox.Show(this, "Please offer at least the Name and specify a file!");
return false;
}

if ((cbFulltextSearch.Checked)
&& (!File.Exists(tbRawTextFile.Text)))
{
MessageBox.Show(this, "The Raw Text file must be specified when full text searching is enabled!");
return false;
}
}

return true;
}

14. Modify the function btnOk_Click, and enable the full text searching flag.

        private void btnOk_Click(object sender, EventArgs e)
{
if (!ValidateInput())
{
return;
}

if (cbBatch.Checked)
{
...
}
else
{
ResourceEx r = ReadFromUI();
ResourceBll.Instance.AddResource(
r,
cbFulltextSearch.Checked,
ResourceBll_ResourceHandle);
}

MessageBox.Show(this, "Done!");
Close();
}

15. Open the file ResourceEx.cs in the Bll project, and add 2 properties to the class.

        public bool FullTextSearchingEnabled { get; set; }
public string RawTextFileName { get; set; }

16. Rebuild the Bll project, and then switch back to the frmAddResource.cs in the main project.

17. Modify the function ReadFromUI which has no parameters, and read more information from UI.

        private ResourceEx ReadFromUI()
{
ResourceEx r = new ResourceEx();
r.FileName = tbFile.Text;
r.Keywords = tbKeywords.Text;
r.Name = tbName.Text;
r.Password = tbPassword.Text;
r.FullTextSearchingEnabled = cbFulltextSearch.Checked;
if (r.FullTextSearchingEnabled)
{
r.RawTextFileName = tbRawTextFile.Text;
}

r.ResourceTypeID = ((ResourceTypeEx)cbType.SelectedItem).ID;
r.EntityType = ResourceEntityType.retRaw;
return r;
}

18. To be continued...