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
#1
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);
//part1
Query greaterThanQuery = NumericRangeQuery.newIntRange("pubmonth", 201001, 999999, true, true);
TopDocs greaterThanMatches = searcher.search(greaterThanQuery, 20);
assertEquals(2, greaterThanMatches.totalHits);
:
//part2
Query lessThanQuery = NumericRangeQuery.newIntRange("pubmonth", 111111, 199912, true, true);
TopDocs lessThanMatches = searcher.search(lessThanQuery, 20);
:
//part3
Query inRangeQuery = NumericRangeQuery.newIntRange("pubmonth", 200001, 200912, true, true);
TopDocs inRangeMatches = searcher.search(inRangeQuery, 20);
assertEquals(totalDoc - greaterThanMatches.totalHits - lessThanMatches.totalHits, inRangeMatches.totalHits);
assertEquals(7, inRangeMatches.totalHits);
//part4
BooleanQuery notInRangeQuery = new BooleanQuery();
notInRangeQuery.add(inRangeQuery, BooleanClause.Occur.MUST_NOT);


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

searcher.close();
dir.close();
}
ErikHatcher (211) [Avatar] Offline
#2
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).