Click here to Skip to main content
6,292,811 members and growing! (11,487 online)
Email Password   helpLost your password?
Database » Database » Other databases     Intermediate

Fulltext Search for Firebird SQL

By Dan Letecky

How to search data stored in a Firebird SQL database using a fulltext index.
C#, SQL, Windows, .NET, Visual Studio, ADO.NET, DBA, Dev
Posted:9 Mar 2005
Views:68,799
Bookmarked:66 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
16 votes for this article.
Popularity: 5.56 Rating: 4.62 out of 5

1

2
1 vote, 6.3%
3
2 votes, 12.5%
4
13 votes, 81.3%
5

Introduction

In this article, we will talk about searching the data in a Firebird database using DotLucene full-text search engine. We will focus on storing the index directly in the database (the source code of the solution is attached).

Firebird SQL

Firebird SQL (excellent embedded database with a good .NET support) has no built-in fulltext search support so far. Instead, you need to rely on third party tools. Fortunately, there is a great search engine library available: DotLucene. It is an open-source .NET library (ported from Java) that can index any data (structured or unstructured) that you are able to convert to raw text. Using an additional library for fulltext doesn't look too elegant at first sight. However, it has some advantages. Let's compare it quickly with MySQL integrated fulltext search:

MySQL Fulltext Search

MySQL fulltext search has these drawbacks (compared to DotLucene):

  • You can use it only in MyISAM tables (i.e. no transactions)
  • You can't browse the index (see Luke)
  • You need to store transformed text in the DB (i.e. for indexing HTML, you need to store another copy of the text with stripped HTML tags)
  • It doesn't support highlighting of the query words in the result
  • You will hardly modify the sources to do custom changes
  • The license doesn't allow to use it in commercial application for free
  • It is reported to be slow on large data sets

How to Index the Data?

For basics about using DotLucene to index your data, I recommend reading:

The following applies for indexing the database:

  • You are using a different source of data (obviously ;-). Instead of reading from the disk, you need to load it from the database.
  • When indexing texts, you don't need to it in the index in full, just keep them in the database.
  • Create an additional Field that will contain the primary key of the indexed document (so you can later load it from the database).
  • When indexing HTML, you need to strip the HTML tags (you need to supply raw text to DotLucene).

Where to Store the Index

On a server, it's no problem to store the index in a separate directory (you can also load it to RAM to make your searches super fast - if you have enough RAM, of course). In a desktop application, it might be useful to store the index in a Firebird database.

DotLucene supports a mechanism for adding custom index storages. All storage types (file system and RAM are built-in) are implemented as a class derived from Lucene.Net.Store.Directory abstract class. I have created a Directory implementation that stores the index directly in a Firebird SQL database.

FbDirectory class

All index reading/writing operations in DotLucene are done using a Directory class. The new FbDirectory class is based on FSDirectory. The filesystem operations are replaced with database operations. Here you can see what we need to implement:

using System;

namespace Lucene.Net.Store
{
    public abstract class Directory 
    {
        public abstract String[] List();
        public abstract bool FileExists(String name);
        public abstract long FileModified(String name);
        public abstract void TouchFile(String name);
        public abstract void DeleteFile(String name);
        public abstract void RenameFile(String from, String to);
        public abstract long FileLength(String name);
        public abstract OutputStream CreateFile(String name);
        public abstract InputStream OpenFile(String name);
        public abstract Lock MakeLock(String name);
        public abstract void Close();
    }
}

Performance Tips

  1. If performance is your main concern, use the standard FSDirectory instead to store the index on disk. My tests show that database storage is twice slower than filesystem. Use the database storage only when you have no other choice.
  2. Use compound index format (IndexWriter.SetUseCompoundFile(true);). This is default in DotLucene 1.4 but in 1.3 you have to do it manually.
  3. Create the index in memory, optimize, then save it on disk using FbDirectory.Copy(); This will only help you if you are rebuilding the whole index from scratch.
  4. If you are adding a document to the index from a desktop application, do it in background (in a separate thread). You are still able to search while you are adding a new document.

Useful Resources

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Dan Letecky


Member
My open-source ASP.NET 2.0 controls:

DayPilot - Outlook-like calendar/scheduling control
DayPilot MonthPicker - Light-weight month picker
MenuPilot - Hover context menu
Location: Czech Republic Czech Republic

Other popular Database articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 12 of 12 (Total in Forum: 12) (Refresh)FirstPrevNext
GeneralLucene.Net - 2.0 Pinmemberalisson_abreu8:17 3 Nov '08  
GeneralThe code is released for businesses too? PinmemberAdriano Alves de Lima8:36 23 Oct '08  
Questionnew IndexWriter syntax ? Pinmembervaclav13544:02 3 Aug '06  
GeneralHow to index data stored in a database using a fulltext index? PinmemberMichael Freidgeim15:54 18 Jul '06  
GeneralRe: How to index data stored in a database using a fulltext index? PinmemberMichael Freidgeim22:20 8 Aug '06  
GeneralProblem .NET 2.0 Lucene 1.4.3 [modified] PinmemberJonas Ljunggren12:04 26 May '06  
GeneralRe: Problem .NET 2.0 Lucene 1.4.3 PinmemberArkar Myo2:11 30 Oct '06  
GeneralNice Article Pinmembercomputerguru9238215:27 1 Mar '06  
GeneralIncrease performance PinmemberSamuel Chen21:41 22 Mar '05  
GeneralRe: Increase performance PinmemberSamuel Chen23:39 22 Mar '05  
GeneralSome questions... PinsupporterPaul Selormey14:35 9 Mar '05  
GeneralRe: Some questions... PinmemberDan Letecky6:40 11 Mar '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 9 Mar 2005
Editor: Nishant Sivakumar
Copyright 2005 by Dan Letecky
Everything else Copyright © CodeProject, 1999-2009
Web13 | Advertise on the Code Project