Search code examples
c#lucenesearch-enginelucene.net

Filtering results of Lucene search


Let me explain my problem. I am using Lucene to search and display the results in asp.net web page. When I am searching, the Lucene is displaying all the records associated with my search. For Example I have 5000 records with name John. If I type John it is displaying all this 5000 records. I want to restrict this 5000 records based on some other attribute. I have four attributes namely First Name, Last Name, DOB and ID. Out those 5000 records I want it to display only the ones with the inputted DOB by the user. This means just display the records of john who have DOB as 5/12/1998. This will restrict the result to around 50 records. Once I am done with that I want to search all the fields who have same ID and then display those records. In the end I will have records of John with given DOB and same ID.

Note: Filtering by DOB is for security purpose.

The following is my Code for search.

        List<SearchResults> Searchresults = new List<SearchResults>();


        string indexFileLocation = @"C:\o";
        Lucene.Net.Store.Directory dir =     Lucene.Net.Store.FSDirectory.GetDirectory(indexFileLocation);

        string[] searchfields = new string[] { "fname", "lname", "dob", "id"};
        IndexSearcher indexSearcher = new IndexSearcher(dir);

        var hits = indexSearcher.Search(QueryMaker(searchString, searchfields));

        for (int i = 0; i < hits.Length(); i++)
        {
            SearchResults result = new SearchResults();
            result.fname = hits.Doc(i).GetField("fname").StringValue();
            result.lname = hits.Doc(i).GetField("lname").StringValue();
            result.dob = hits.Doc(i).GetField("dob").StringValue();
            result.id = hits.Doc(i).GetField("id").StringValue();
            Searchresults.Add(result);

        }

Please let me know if you have any questions.


Solution

  • I hate to do this. But I found the answer.

    Its

    Filter fil= new QueryWrapperFilter(new TermQuery( new Term(field, "5/12/1998")));
    var hits = indexSearcher.Search(QueryMaker(searchString, searchfields), fil);
    

    You can filter the results with the above code.

    I don't think its possible to go back after filtering for looking values. Anybody disagree?