Knowledgebase Home Page  >  SearchUnit  >  Version 3 Articles
Search the Knowledge Base
How to add/remove rows in a DataSet to the index. (Version 3)
https://keyoti.com/kb/Default.aspx?ToDo=view&questId=203&catId=67

Options

Print this page
Email this to a friend
This article is for version 3
 
The following code demonstrates how to add and remove DataTable rows to the index, one at a time.  This is useful when reimporting/indexing the complete DataSet from scratch is not desirable.
 
This example can be used alone, or inside the "Programmatic" demo project that is supplied with Search.  To use it with the "Programmatic" demo project, it is recommended that 2 buttons are added to the "DB_Import_Form", one for adding and one for removing.
 
1. Adding a row to the index.
 
This code is fairly lengthy, but it does include a test search at the end.  The DataTable is setup like the data in our test DB that is included with the demo project.
 

private void button1_Click(object sender, EventArgs e)

{

//-----------------------------------We want to index a new row in a table with 5 columns

// "col1", "col2", "col3", "col4", "col5"

// jj 66 fff jj

//

string newRowKey = "jj";

string uniqueColumn = "col4";

//Create dataset holding the new row of data

DataSet ds = new DataSet();

DataTable table = new DataTable();

ds.Tables.Add(table);

table.Columns.Add("col1");

table.Columns.Add("col2");

table.Columns.Add("col3");

table.Columns.Add("col4");

table.Columns.Add("col5");

table.Rows.Add(new object[] { "jj", 666, "fff", newRowKey, DateTime.Now });

//-----------------------------------------------------------------------------------------

 

//Create an indexable source record for this added data

IndexableSourceRecord indexableSourceRecord = IndexableSourceRecordFactory.CreateRecord(

SourceType.PresetDataSetProvider, //this is a preset DataSet

null, //not applicable

null, //not applicable

uniqueColumn, //the 'primary key'/'identifying' field name

"http://localhost/r.aspx?key={0}&keyField={1}" //the format to use for the result URLs

);

//Assign any number not currently assigned to an existing source

indexableSourceRecord.ID = 900001;

//Create the adapter around any DataSet

PresetDataSetAdapter presetDataSetAdapter = new PresetDataSetAdapter(ds);

//Create the IndexableSource around the adapter

DataSetBasedSource dataSetBasedSource = new DataSetBasedSource(Configuration, indexableSourceRecord, presetDataSetAdapter);

//Create the DocumentIndex, which will index the new data

DocumentIndex idx = new DocumentIndex(Configuration);

idx.RegisterDataSetBasedSource(indexableSourceRecord.ID, dataSetBasedSource);

idx.Open();

//Add the 'document' (new row), and signify that it should be indexed now.

idx.AddDocument(new Document(new Keyoti.SearchEngine.DataAccess.DocumentRecord(

"idxsrc://" + indexableSourceRecord.ID + "/?pk=" + newRowKey + "&uniqueColumn=" + uniqueColumn), Configuration), true, true);

idx.Close();

//-----------------------------------------------------------------------------------------

 

//Run a test search

string searchQuery = "fff";

Keyoti.SearchEngine.Search.SearchAgent searchAgent = new Keyoti.SearchEngine.Search.SearchAgent("5853685F655B6760695A6B6A625B67243B423E3F393F463C433E4443444E434A514D4E4", Configuration);

//Register the IndexableSource for URL mapping

searchAgent.RegisterDataSetBasedSource(indexableSourceRecord.ID, dataSetBasedSource);

Keyoti.SearchEngine.Search.SearchResult result = searchAgent.Search(searchQuery, 1, 10);

MessageBox.Show("1st search result: "+(result[0] as Keyoti.SearchEngine.Search.ResultItem).UriString);

 

}

 

 

2. Removing a row from the index.

To remove a row, we only need to identify it by it's 'URL', which is based on the indexable source ID and unique field value for the row.  Since we added a row identified by 'jj' in the first method, this method will then remove it.

 

private void button2_Click(object sender, EventArgs e)

{

//-----------------------------------We want to remove a row

string delRowKey = "jj";

string uniqueColumn = "col4";

int indexableSourceRecordID = 900001;

//Create the DocumentIndex, which will index the new data

DocumentIndex idx = new DocumentIndex(Configuration);

idx.Open();

//Add the 'document' (new row), and signify that it should be indexed now.

idx.RemoveDocument("idxsrc://" + indexableSourceRecordID + "/?pk=" + delRowKey + "&uniqueColumn=" + uniqueColumn);

idx.Close();

//-----------------------------------------------------------------------------------------

 

//Run a test search

string searchQuery = "fff";

Keyoti.SearchEngine.Search.SearchAgent searchAgent = new Keyoti.SearchEngine.Search.SearchAgent("5853685F655B6760695A6B6A625B67243B423E3F393F463C433E4443444E434A514D4E4", Configuration);

Keyoti.SearchEngine.Search.SearchResult result = searchAgent.Search(searchQuery, 1, 10);

MessageBox.Show("Number of results: " + result.Count);

}

 

3. Using the demo project after these changes.

i) Run the project

ii) Click the "Launch DB Import Form", click "Import" to import the regular DB data

iii) Close the "DB Import Form"

iv) Click "Build Index"

v) At this point, the index is searchable.

vi) Click the "Launch DB Import Form", click "button1", and see that it was able to search for the term that was newly added.

vii) Click "button2" and see that no results exist now that the row was removed.

 

Note.

Due to the way the new rows are added, it is not possible to use the "Build" index operation on the index.  This is because the newly added indexable source does not actually 'exist' and cannot be accessed by the indexer.  Should it be necessary to rebuild the index, then the index should be deleted first and freshly imported/indexed. 


Related Questions:

Attachments:

No attachments were found.