Is Lucene.Net larger or smaller than TermRangeQuery?

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?

+6
source share
2 answers

You should be able to do this using range queries if memory serves. This is actually the opposite of standard range queries, but you should be able to:

 +minAge:[* TO @age] +maxAge:[@age TO *] 

Or, if you create query objects, RangeQuery (or, even better, NumericRangeQuery) with a null top or bottom number works like an open range.

I used syntax before, but the support seems a little ... trembling. If this does not work, you can always just set an adequately low lower limit (0) and an upper upper limit (for example, 1000), for example:

 +minAge:[0000 TO @age] +maxAge:[@age TO 1000] 

Which should be safe enough, prohibiting any Methuselah.

+10
source

Finished doing this with the femtoRgon answer above.

 var q = new TermRangeQuery("MinAge", "0000",searchTerms.Age.ToString("0000"), true, true); mainQuery.Add(q, BooleanClause.Occur.MUST); q = new TermRangeQuery("MaxAge", searchTerms.Age.ToString("0000"),"9999", true, true); mainQuery.Add(q, BooleanClause.Occur.MUST); 

wings

+4
source

Source: https://habr.com/ru/post/926561/


All Articles