1
votes

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.

1
You can specify the fields you want to search against by doing: FirstName: John AND DOB: 5/12/1998. For the ID I'm afraid Lucene doesn't support that type of operation. You can always fetch the records and filter them again...rae1
@rae1n As I said before it is for security purpose. I don't want user to type it. I am looking for something like this. Filter LEA= new QueryWrapperFilter(new TermQuery( new Term("5/12/1998",field)));. However, I dont think I am specifying the filter correctly.Huzaifa
What I meant was that you don't need to specify a filter per say, just add an AND clause to your query with a DOB: 5/12/1998. You can use BooleanQuery to specify the two queries.rae1
@rae1n oops that was the answer. The order of the term was wrong. Its field then value.Huzaifa
Oh! I wonder about that... but having not run the code I'd assume you had it right =)rae1

1 Answers

4
votes

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?