我已经建立了一个Lucene.net书籍索引.一切都运行良好,但我需要添加另一种方式来查询索引,我无法弄清楚如何做到这一点.
基本上每本书都有适合的年龄范围.这由两列表示 – minAge和maxAge.两列都是整数.
我正在索引并将这些字段存储在以下循环中
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.Index.ANALYZED,Field.TermVector.NO)); doc.Add( new Field("Author",strAuthor.Trim().ToLower(),Field.TermVector.NO)); doc.Add( new Field("IssueId",book.IssueId,Field.TermVector.NO)); doc.Add( new Field( "PublicationId",book.PublicationId.Trim().ToLower(),Field.TermVector.NO)); doc.Add( new Field( "MinAge",book.MinAge.ToString("0000"),Field.TermVector.NO)); doc.Add( new Field( "MaxAge",book.MaxAge.ToString("0000"),Field.TermVector.NO)); doc.Add(new NumericField("Price",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.TermVector.NO)); } // add the document to the index indexWriter.AddDocument(doc); } // make lucene fast indexWriter.Optimize(); }
正如您所看到的,我正在填充minAge和maxAge字段,因为我认为对它运行TermRangeQuery最容易.
但是,我需要使用Age查询minAge和maxAge列,以查看Age是否落在minAge和maxAge定义的Age范围内.
sql会的
Select * From books where @age >= minAge and @age <= maxAge
不幸的是我看不到这样做的方法.这在Lucene.Net中甚至可能吗?