I created the Lucene.net book index. Everything works fine, but I need to add another way to query the index, and I cannot figure out how to do this.
Basically, each book has an age range to which it fits. This is expressed in two columns, namely minAge and maxAge. Both columns are integers.
I will index and save these fields in the next loop
foreach (var catalogueBook in books) { var book = new Book(catalogueBook.CatalogueBookNo,catalogueBook.IssueId); var strTitle = book.FullTitle ?? ""; var strAuthor = book.Author ?? ""; // create a Lucene document for this book var doc = new Document(); // add the ID as stored but not indexed field, not used to query on doc.Add( new Field( "BookId", book.CatalogueBookNo.ToString(System.Globalization.CultureInfo.InvariantCulture), Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS, Field.TermVector.NO)); // add the title and author as stored and tokenized fields, the analyzer processes the content doc.Add( new Field("FullTitle", strTitle.Trim().ToLower(), Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.NO)); doc.Add( new Field("Author", strAuthor.Trim().ToLower(), Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.NO)); doc.Add( new Field("IssueId", book.IssueId, Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS, Field.TermVector.NO)); doc.Add( new Field( "PublicationId", book.PublicationId.Trim().ToLower(), Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS, Field.TermVector.NO)); doc.Add( new Field( "MinAge", book.MinAge.ToString("0000"), Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS, Field.TermVector.NO)); doc.Add( new Field( "MaxAge", book.MaxAge.ToString("0000"), Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS, Field.TermVector.NO)); doc.Add(new NumericField("Price",Field.Store.YES,true).SetDoubleValue(Convert.ToDouble(book.Price))); //Now we can loop through categories foreach(var bc in book.GetBookCategories()) { doc.Add( new Field("CategoryId", bc.CategoryId.Trim().ToLower(), Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS, Field.TermVector.NO)); } // add the document to the index indexWriter.AddDocument(doc); } // make lucene fast indexWriter.Optimize(); }
As you can see, I fill in the minAge and maxAge fields, as I thought it would be easier to run TermRangeQuery against it.
However, I need to query both the minAge and maxAge columns with age to see if this age falls in the age range defined by minAge and maxAge.
Sql will be
Select * From books where @age >= minAge and @age <= maxAge
Unfortunately, I see no way to do this. Is this possible in Lucene.Net?