Note that there are some explanatory texts on larger screens.

plurals
  1. POLucene.Net greater than/less than TermRangeQuery?
    text
    copied!<p>I have built a Lucene.net index of books. All is working well but I need to add another way to query the index and I cant figure out how to do it.</p> <p>Basically each book has an age range that it is suitable for. This is expressed by two columns namely - minAge and maxAge. Both columns are integers.</p> <p>I am indexing and storing these fields in the following loop</p> <pre><code>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(); } </code></pre> <p>As you can see I am padding out the minAge and maxAge fields as I thought it would be easiest to run a TermRangeQuery against it.</p> <p>However I need to query both the minAge and maxAge columns with an Age to see if that Age falls with in the Age range defined by minAge and maxAge.</p> <p>Sql would be </p> <pre class="lang-sql prettyprint-override"><code>Select * From books where @age &gt;= minAge and @age &lt;= maxAge </code></pre> <p>Unfortunately I cannot see a way to do this. Is this even possible in Lucene.Net?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload