Home >  SearchBlackBox SDK >  Quick Start Tutorial

Quick Start Tutorial

Summary: The classic Hello World program written in C#. This program demonstrates common SearchBlackBox SDK features and basic steps for adding full-text search to applications.

Contents

Overview

This example illustrates how few lines of code are required to add full-text search to your applications.

To make it simple, let us take three texts with the following contents:

  • "hello document"
  • "world document"
  • "hello world document"

We index them first and then perform a search.

We use the following query as our search query:"he* OR wo*". It means finding all documents that contain worlds beginning with "he" or beginning with "wo". In our case, we are supposed to find all three documents.

Also, we use highlighting to display the results. We highlight the found words by enclosing them in square brackets.

Top of Page

Source Code

Below you can see the program written in C#.

using System;
using SearchBlackBox.SDK;

namespace SearchBlackBox.Samples.HelloWorld {
 class HelloWorldApp {
   static void Main(string[] args) {
     // Index location
     String indexDir = AppDomain.CurrentDomain.BaseDirectory + "index";
     
     using(SearchEngine searchEngine = new SearchEngine(indexDir, true)) {
       // Index
       SearchDocument document = new SearchDocument();
       document.AddField("id","1",FieldAttributes.Stored|FieldAttributes.Indexed);
       document.AddField("content","hello document",
       		FieldAttributes.IndexedAndAnalyzedAndStored);
       searchEngine.Add(document);
       
       document = new SearchDocument();
       document.AddField("id","2",FieldAttributes.Stored|FieldAttributes.Indexed);
       document.AddField("content","world document",
       		FieldAttributes.IndexedAndAnalyzedAndStored);
       searchEngine.Add(document);
       
       document = new SearchDocument();
       document.AddField("id","3",FieldAttributes.Stored|FieldAttributes.Indexed);
       document.AddField("content","hello world document",
       		FieldAttributes.IndexedAndAnalyzedAndStored);
       searchEngine.Add(document);
       
       // Search
       String queryString = "he* OR wo*";
       SearchResults results = searchEngine.ExecuteSearch(queryString,"content");
       
       // Configure Highlighter
       results.HighlightingSettings = new HighlightingSettings("content","[","]");

       // Print Results
       Console.WriteLine("Results for:   " + results.QueryString);
       Console.WriteLine("Total results: " + results.Count);
       Console.WriteLine("Search time:   " + results.SearchTime.Milliseconds + " ms");
       Console.WriteLine("--------------------");
       Console.WriteLine("#\tScore\t\tID\tHighlightedText");
       
       // Print
       foreach(SearchResult result in results) {
         Console.WriteLine(result["ResultIndex"] + "\t" +
                           result["ResultScore"] +"\t" +
                           result["id"] +"\t" +
                           result["ResultHighlight"]);
}}}}}
Top of Page

Building and Running

To build the program, we use build.bat. This file contains a single line that is necessary to build this program:

csc.exe" /target:exe /reference:.\bin\searchblackbox.sdk.dll  
	/o+ /out:.\bin\HelloWorld.exe .\HelloWorld\HelloWorld.cs 
		

As a result, we get the program HelloWorld.exe in the Bin directory.

Top of Page

Program Output

The default output of the C# compiler is an executable file with the same name and running this program generates the following output:

C:\...\Bin\HelloWorld.exe
Results for:   he* OR wo*
Total results: 3
Search time:   20 ms
--------------------
#    Score        ID    HighlightedText
0    0,7071068    3     [hello] [world] document
1    0,2209709    1     [hello] document
2    0,2209709    2     [world] document

The program displays on the screen:

  • line 2 - the original search query
  • lines 3,4 - the number of matches and the time of searching
  • lines 7-9 - the results ordered by relevance with highlighting

Top of Page

Source Code Details

Let us see what happens in detail.

1. Add reference to SearchBlackBox

We add a reference to the assembly to your project.

using SearchBlackBox.SDK;
2. Initialize the SearchEngine

We initialize the SearchEngine object. We specify the path to the index (the directory where the index files will be stored) - it is the indexDir parameter, we use the second parameter as true to specify that a new index will be created.

using(SearchEngine searchEngine = new SearchEngine(indexDir, true)) {
3. Index Documents

We index our documents, to be exact, we present them as a SearchDocument and add them to the index.

In our case, a document has two fields - id and content. As the attributes of the fields, we specify that the value of the id field will be indexed and stored and the value of the content field will be analyzed, indexed and stored in the index.

// Index
SearchDocument document = new SearchDocument();
document.AddField("id","1",FieldAttributes.Stored|FieldAttributes.Indexed);
document.AddField("content","hello document",
   		FieldAttributes.IndexedAndAnalyzedAndStored);
searchEngine.Add(document);

4. Perform a search

Then, we perform a search. To do it, we call ExecuteSearch method passing to it the search string and the default search field name (i.e. fields that will be searched) as parameters.

// Search
String queryString = "he* OR wo*";
SearchResults results = searchEngine.ExecuteSearch(queryString,"content");
5. Configure Highlighting settings

After that we configure Highlighting settings by specifying that the original text should be taken from the "content" field and that square brackets should be used to highlight text. Since the "content" field has the Stored attribute, we can get the original text.

// Configure Highlighter
results.HighlightingSettings = new HighlightingSettings("content","[","]");
6. Display the header

Now we display the header - the original query, the number of matches and the time of searching.

// Print Results
Console.WriteLine("Results for:   " + results.QueryString);
Console.WriteLine("Total results: " + results.Count);
Console.WriteLine("Search time:   " + results.SearchTime.Milliseconds + " ms");
7. Display the results

And finally, we display the results - the number of each result, its score, id and highlighted text.

// Print
foreach(SearchResult result in results) {
  Console.WriteLine(result["ResultIndex"] + "\t" +
                    result["ResultScore"] +"\t" +
                    result["id"] +"\t" +
                    result["ResultHighlight"]);

That's all.

Top of Page

Conclusion

This example shows the essential steps that are required to add search to an application: represent data as a Search Document, implement indexing, implement searching and implement processing the results.

Top of Page

API Reference

SearchDocument, FieldAttributes, SearchEngine, SearchResults, SearchResult, HighlightingSettings

Top of Page

Questions? Need assistance before or after purchasing? Click here for customer support.