The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

threecuptea (1) [Avatar] Offline
I start reading this book and also learn from book source. I have the following test using the BookIndex (total 13 books indexed). I have 4 numeric range query.
Part1: range query pubmonth >= 201001, expected result 2
Part2: range query pubmonth <= 199912, expected result 4
Part3: range query in between pubmonth: <= 200912 and >=200001, expected result 7(13-2-4)
Part4: not in range query: query string printed as
-pubmonth:[200001 TO 200912], expected 6 (2+4)

Everything work fine except for part4. I got 0 instead of 6.
The followings are codes snippet.

public void testMisc() throws Exception {
Directory dir = TestUtil.getBookIndexDirectory();
IndexSearcher searcher = new IndexSearcher(dir);
int totalDoc = searcher.maxDoc();//document id starts with 0
System.out.println("totalDoc= "+totalDoc);
Query greaterThanQuery = NumericRangeQuery.newIntRange("pubmonth", 201001, 999999, true, true);
TopDocs greaterThanMatches =, 20);
assertEquals(2, greaterThanMatches.totalHits);
Query lessThanQuery = NumericRangeQuery.newIntRange("pubmonth", 111111, 199912, true, true);
TopDocs lessThanMatches =, 20);
Query inRangeQuery = NumericRangeQuery.newIntRange("pubmonth", 200001, 200912, true, true);
TopDocs inRangeMatches =, 20);
assertEquals(totalDoc - greaterThanMatches.totalHits - lessThanMatches.totalHits, inRangeMatches.totalHits);
assertEquals(7, inRangeMatches.totalHits);
BooleanQuery notInRangeQuery = new BooleanQuery();
notInRangeQuery.add(inRangeQuery, BooleanClause.Occur.MUST_NOT);

TopDocs notInRangeMatches =, 20);
System.out.println("Not= "+notInRangeQuery);
assertEquals(greaterThanMatches.totalHits + lessThanMatches.totalHits, notInRangeMatches.totalHits);
assertEquals(6, notInRangeMatches.totalHits);

ErikHatcher (211) [Avatar] Offline
Re: Not in numeric range does not work as expected
Purely negative queries don't work as one might expect. To get this to work, AND this negative query to a MatchAllDocsQuery (as a MUST or SHOULD clause, won't matter).