lucene.net partial word search

This article show you how to do Partial word search in Lucene, Lucene is powerful search engine but many time we need partial word search for example I have to upload a document with word “ConsoleApplication”. Now on search of “Console” it should search all words which starts with Console.

Approach : Lucene provide multiple ways to compose “Query” to search in this case we are using WildcardQuery which supporting partial word search

First we index the some field and then search those.

This is console application
First include ddls required
Lucene.Net.dll
Lucene.Net.Contrib.Highlighter.dll

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
using Lucene.Net.Analysis;
using Lucene.Net.Documents;
using Lucene.Net.QueryParsers;
using Lucene.Net.Search;
using Lucene.Net.Search.Highlight;
using Lucene.Net.Store;
using Version = Lucene.Net.Util.Version;
using Lucene.Net.Search.Spans;
using Lucene.Net.Analysis.Standard;
using Lucene.Net.Index;
using Lucene.Net.Util;

namespace PartialLU
{
    class Program
    {
        static void Main(string[] args)
        {
            IndexWriter iw = new IndexWriter(FSDirectory.Open(@"D:\tem_Search"), new StandardAnalyzer(Version.LUCENE_30), true, IndexWriter.MaxFieldLength.LIMITED);
            
            Document doc = new Document();
            Field loc = new Field("location", "", Field.Store.YES, Field.Index.NOT_ANALYZED);
            doc.Add(loc);

            loc.SetValue("chicago heights");
            iw.AddDocument(doc);

            loc.SetValue("new-york");
            iw.AddDocument(doc);

            loc.SetValue("chicago low");
            iw.AddDocument(doc);

            loc.SetValue("montreal");
            iw.AddDocument(doc);

            loc.SetValue("paris");
            iw.AddDocument(doc);

            iw.Commit();

            IndexSearcher ins = new IndexSearcher(iw.GetReader());

            WildcardQuery query = new WildcardQuery(new Term("location", "chicago he*"));

            TopDocs hits = ins.Search(query, 10);
            for (int i = 0; i < hits.TotalHits; i++)
            {
                Document doctemp = ins.Doc(hits.ScoreDocs[i].Doc);
                Console.WriteLine(doctemp.Get("location").ToString());
            }
            Console.WriteLine("---");

            query = new WildcardQuery(new Term("location", "chic*"));
            hits = ins.Search(query, 10);
            for (int i = 0; i < hits.TotalHits; i++)
            {
                Document doctemp = ins.Doc(hits.ScoreDocs[i].Doc);
                Console.WriteLine(doctemp.Get("location").ToString());
            }

            iw.Close();
            Console.ReadLine();
        }
    }
}

Download Source code

lucene.net partial word search

lucene.net partial word search

Advertisements
This entry was posted in lucene, MVC and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s