List of usage examples for org.apache.lucene.spatial.prefix.tree DateRangePrefixTree newCal
public Calendar newCal()
From source file:org.esa.beam.occci.LuceneQueryIndexMain.java
License:Open Source License
public static void main(String[] args) throws Exception { if (args.length != 3) { printUsage();/*from ww w . j av a 2 s.c o m*/ } File indexfile = new File(args[0]); File insituCSVtFile = new File(args[1]); if (!insituCSVtFile.exists()) { System.err.printf("insituList file '%s' does not exits%n", args[2]); printUsage(); } int hours = 0; try { hours = Integer.parseInt(args[2]); } catch (NumberFormatException e) { e.printStackTrace(); System.err.printf("cannot parse hours '%s' %n", args[3]); printUsage(); } long maxTimeDifference = HOURS_IN_MILLIS * hours; final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS", ENGLISH); dateFormat.setCalendar(GregorianCalendar.getInstance(UTC, Locale.ENGLISH)); List<SimpleRecord> insituRecords = ProductDBCheckerMain.readInsituRecords(insituCSVtFile); System.out.println("num insituRecords = " + insituRecords.size()); Directory indexDirectory = FSDirectory.open(indexfile.toPath()); IndexReader indexReader = DirectoryReader.open(indexDirectory); int numProductsInIndex = indexReader.getDocCount("name"); System.out.println("numProductsInIndex = " + numProductsInIndex); IndexSearcher indexSearcher = new IndexSearcher(indexReader); DateRangePrefixTree dateRangePrefixTree = DateRangePrefixTree.INSTANCE; PrefixTreeStrategy strategy = new NumberRangePrefixTreeStrategy(dateRangePrefixTree, "productDateRange"); SpatialOperation operation = SpatialOperation.Intersects; int hits = 0; long t1 = System.currentTimeMillis(); Set<Integer> matches = new HashSet<>(); Calendar calendar = dateRangePrefixTree.newCal(); for (SimpleRecord insituRecord : insituRecords) { final long referenceTime = insituRecord.getTime(); final long windowStartTime = referenceTime - maxTimeDifference; final long windowEndTime = referenceTime + maxTimeDifference; calendar.setTimeInMillis(windowStartTime); NumberRangePrefixTree.UnitNRShape leftShape = dateRangePrefixTree.toShape(calendar); calendar.setTimeInMillis(windowEndTime); NumberRangePrefixTree.UnitNRShape rightShape = dateRangePrefixTree.toShape(calendar); NumberRangePrefixTree.NRShape nrShape = dateRangePrefixTree.toRangeShape(leftShape, rightShape); SpatialArgs sargs = new SpatialArgs(operation, nrShape); Query query = strategy.makeQuery(sargs); TopDocs topDocs = indexSearcher.search(query, 1000); ScoreDoc[] scoreDocs = topDocs.scoreDocs; for (ScoreDoc scoreDoc : scoreDocs) { matches.add(scoreDoc.doc); } // Document doc = indexSearcher.doc(docID); // String productName = doc.get("name"); // matches.add(productName); // } // System.out.println("topDocs.totalHits = " + topDocs.totalHits); // hits += topDocs.totalHits; } long t2 = System.currentTimeMillis(); System.out.println("delta time test insitu = " + ((t2 - t1) / 1000f)); System.out.println("hits = " + hits); System.out.println("matches = " + matches.size()); }