Example usage for org.apache.lucene.spatial.prefix NumberRangePrefixTreeStrategy NumberRangePrefixTreeStrategy

List of usage examples for org.apache.lucene.spatial.prefix NumberRangePrefixTreeStrategy NumberRangePrefixTreeStrategy

Introduction

In this page you can find the example usage for org.apache.lucene.spatial.prefix NumberRangePrefixTreeStrategy NumberRangePrefixTreeStrategy.

Prototype

public NumberRangePrefixTreeStrategy(NumberRangePrefixTree prefixTree, String fieldName) 

Source Link

Usage

From source file:com.stratio.cassandra.lucene.schema.mapping.BitemporalMapper.java

License:Apache License

/**
 * Builds a new {@link BitemporalMapper}.
 *
 * @param name    the name of the mapper.
 * @param vtFrom  The column name containing the Start Valid Time.
 * @param vtTo    The column name containing the End Valid Time.
 * @param ttFrom  The column name containing the Start Transaction Time.
 * @param ttTo    The column name containing the End Transaction Time.
 * @param pattern The {@link SimpleDateFormat} pattern to be used.
 *//* w  ww  .j a  v a  2s.  c om*/
public BitemporalMapper(String name, String vtFrom, String vtTo, String ttFrom, String ttTo, String pattern,
        Object nowValue) {

    super(name, true, false, AsciiType.instance, UTF8Type.instance, Int32Type.instance, LongType.instance,
            IntegerType.instance, FloatType.instance, DoubleType.instance, DecimalType.instance,
            TimestampType.instance);

    if (StringUtils.isBlank(vtFrom)) {
        throw new IllegalArgumentException("vtFrom column name is required");
    }

    if (StringUtils.isBlank(vtTo)) {
        throw new IllegalArgumentException("vtTo column name is required");
    }

    if (StringUtils.isBlank(ttFrom)) {
        throw new IllegalArgumentException("ttFrom column name is required");
    }

    if (StringUtils.isBlank(ttTo)) {
        throw new IllegalArgumentException("ttTo column name is required");
    }

    this.pattern = (pattern == null) ? DEFAULT_PATTERN : pattern;
    this.vtFrom = vtFrom;
    this.vtTo = vtTo;
    this.ttFrom = ttFrom;
    this.ttTo = ttTo;

    // Validate pattern
    new SimpleDateFormat(this.pattern);

    // ttTo=now vtTo=now 2 DateRangePrefixTree

    this.tree_t1_V = DateRangePrefixTree.INSTANCE;
    this.strategy_t1_V = new NumberRangePrefixTreeStrategy(tree_t1_V, name + ".t1_v");
    this.tree_t1_T = DateRangePrefixTree.INSTANCE;
    this.strategy_t1_T = new NumberRangePrefixTreeStrategy(tree_t1_T, name + ".t1_t");

    this.tree_t2_V = DateRangePrefixTree.INSTANCE;
    this.strategy_t2_V = new NumberRangePrefixTreeStrategy(tree_t2_V, name + ".t2_v");
    this.tree_t2_T = DateRangePrefixTree.INSTANCE;
    this.strategy_t2_T = new NumberRangePrefixTreeStrategy(tree_t2_T, name + ".t2_t");

    this.tree_t3_V = DateRangePrefixTree.INSTANCE;
    this.strategy_t3_V = new NumberRangePrefixTreeStrategy(tree_t3_V, name + ".t3_v");
    this.tree_t3_T = DateRangePrefixTree.INSTANCE;
    this.strategy_t3_T = new NumberRangePrefixTreeStrategy(tree_t3_T, name + ".t3_t");

    this.tree_t4_V = DateRangePrefixTree.INSTANCE;
    this.strategy_t4_V = new NumberRangePrefixTreeStrategy(tree_t4_V, name + ".t4_v");
    this.tree_t4_T = DateRangePrefixTree.INSTANCE;
    this.strategy_t4_T = new NumberRangePrefixTreeStrategy(tree_t4_T, name + ".t4_t");

    concurrentDateFormat = new ThreadLocal<DateFormat>() {
        @Override
        protected DateFormat initialValue() {
            return new SimpleDateFormat(BitemporalMapper.this.pattern);
        }
    };

    this.nowBitemporalDateTime = nowValue == null ? new BitemporalDateTime(Long.MAX_VALUE)
            : this.parseBiTemporalDate(nowValue);

}

From source file:com.stratio.cassandra.lucene.schema.mapping.DateRangeMapper.java

License:Apache License

/**
 * Builds a new {@link DateRangeMapper}.
 *
 * @param name    The name of the mapper.
 * @param start   The column containing the start {@link Date}.
 * @param stop    The column containing the stop {@link Date}.
 * @param pattern The {@link SimpleDateFormat} pattern to be used.
 *//*www.  j  a  v a 2s.  c o  m*/
public DateRangeMapper(String name, String start, String stop, String pattern) {
    super(name, true, false, AsciiType.instance, UTF8Type.instance, Int32Type.instance, LongType.instance,
            IntegerType.instance, FloatType.instance, DoubleType.instance, DecimalType.instance,
            TimestampType.instance);

    if (StringUtils.isBlank(start)) {
        throw new IllegalArgumentException("start column name is required");
    }

    if (StringUtils.isBlank(stop)) {
        throw new IllegalArgumentException("stop column name is required");
    }

    this.start = start;
    this.stop = stop;
    this.tree = DateRangePrefixTree.INSTANCE;
    this.strategy = new NumberRangePrefixTreeStrategy(tree, name);
    this.pattern = pattern == null ? DEFAULT_PATTERN : pattern;

    concurrentDateFormat = new ThreadLocal<DateFormat>() {
        @Override
        protected DateFormat initialValue() {
            return new SimpleDateFormat(DateRangeMapper.this.pattern);
        }
    };
}

From source file:org.apache.solr.schema.DateRangeField.java

License:Apache License

@Override
protected NumberRangePrefixTreeStrategy newPrefixTreeStrategy(String fieldName) {
    return new NumberRangePrefixTreeStrategy(tree, fieldName);
}

From source file:org.esa.beam.occci.LuceneCreateIndexMain.java

License:Open Source License

public static void main(String[] args) throws IOException, ParseException {
    if (args.length != 2) {
        printUsage();//ww  w .j a  v a2 s  . co m
    }
    File productListFile = new File(args[0]);
    File indexfile = new File(args[1]);
    if (!productListFile.exists()) {
        System.err.printf("productList file '%s' does not exits%n", args[0]);
        printUsage();
    }
    List<EoProduct> eoProductList = ProductDB.readProducts("s2", productListFile);

    Directory indexDirectory = FSDirectory.open(indexfile.toPath());
    IndexWriterConfig config = new IndexWriterConfig(new SimpleAnalyzer());
    config.setRAMBufferSizeMB(100);

    DateRangePrefixTree dateRangePrefixTree = DateRangePrefixTree.INSTANCE;
    PrefixTreeStrategy strategy = new NumberRangePrefixTreeStrategy(dateRangePrefixTree, "productDateRange");

    final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS", ENGLISH);
    dateFormat.setCalendar(GregorianCalendar.getInstance(UTC, Locale.ENGLISH));

    int indexCount = 0;
    try (IndexWriter indexWriter = new IndexWriter(indexDirectory, config)) {
        for (EoProduct eoProduct : eoProductList) {
            Document doc = new Document();
            doc.add(new StringField("name", eoProduct.getName(), Field.Store.YES));
            String start = dateFormat.format(new Date(eoProduct.getStartTime()));
            String end = dateFormat.format(new Date(eoProduct.getEndTime()));
            String range = "[" + start + " TO " + end + "]";

            NumberRangePrefixTree.NRShape nrShape = dateRangePrefixTree.parseShape(range);
            for (IndexableField f : strategy.createIndexableFields(nrShape)) {
                doc.add(f);
            }
            indexWriter.addDocument(doc);

            indexCount++;
            if (indexCount % 10_000 == 0) {
                System.out.println("indexCount = " + indexCount);
            }
        }

    }

}

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();/*www  .  jav a  2s  .  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());

}