Example usage for org.apache.lucene.document IntPoint newRangeQuery

List of usage examples for org.apache.lucene.document IntPoint newRangeQuery

Introduction

In this page you can find the example usage for org.apache.lucene.document IntPoint newRangeQuery.

Prototype

public static Query newRangeQuery(String field, int[] lowerValue, int[] upperValue) 

Source Link

Document

Create a range query for n-dimensional integer values.

Usage

From source file:IndexAndSearchOpenStreetMaps1D.java

License:Apache License

private static void queryIndex() throws IOException {
    Directory dir = FSDirectory.open(Paths.get("/l/tmp/1dkd" + (USE_NF ? "_nf" : "")));
    System.out.println("DIR: " + dir);
    IndexReader r = DirectoryReader.open(dir);
    System.out.println("maxDoc=" + r.maxDoc());

    IndexSearcher s = new IndexSearcher(r);

    //System.out.println("reader MB heap=" + (reader.ramBytesUsed()/1024/1024.));

    // London, UK:
    int STEPS = 5;
    double MIN_LAT = 51.0919106;
    double MAX_LAT = 51.6542719;
    double MIN_LON = -0.3867282;
    double MAX_LON = 0.8492337;
    byte[] scratch1 = new byte[4];
    byte[] scratch2 = new byte[4];
    for (int iter = 0; iter < 100; iter++) {
        long tStart = System.nanoTime();
        long totHits = 0;
        int queryCount = 0;
        for (int latStep = 0; latStep < STEPS; latStep++) {
            double lat = MIN_LAT + latStep * (MAX_LAT - MIN_LAT) / STEPS;
            for (int lonStep = 0; lonStep < STEPS; lonStep++) {
                double lon = MIN_LON + lonStep * (MAX_LON - MIN_LON) / STEPS;
                for (int latStepEnd = latStep + 1; latStepEnd <= STEPS; latStepEnd++) {
                    double latEnd = MIN_LAT + latStepEnd * (MAX_LAT - MIN_LAT) / STEPS;
                    for (int lonStepEnd = lonStep + 1; lonStepEnd <= STEPS; lonStepEnd++) {
                        double lonEnd = MIN_LON + lonStepEnd * (MAX_LON - MIN_LON) / STEPS;

                        Query q;//w  w w .ja  va 2  s .co m
                        if (USE_NF) {
                            q = LegacyNumericRangeQuery.newIntRange("latnum", (int) (1000000. * lat),
                                    (int) (1000000. * latEnd), true, true);
                        } else {
                            q = IntPoint.newRangeQuery("lat", (int) (1000000. * lat),
                                    (int) (1000000. * latEnd));
                        }

                        TotalHitCountCollector c = new TotalHitCountCollector();
                        //long t0 = System.nanoTime();
                        s.search(q, c);

                        //System.out.println("\nITER: now query lat=" + lat + " latEnd=" + latEnd + " lon=" + lon + " lonEnd=" + lonEnd);
                        //Bits hits = reader.intersect(lat, latEnd, lon, lonEnd);
                        //System.out.println("  total hits: " + hitCount);
                        //totHits += ((FixedBitSet) hits).cardinality();
                        //System.out.println("  add tot " + c.getTotalHits());
                        totHits += c.getTotalHits();
                        queryCount++;
                    }
                }
            }
        }

        long tEnd = System.nanoTime();
        System.out.println("ITER: " + iter + " " + ((tEnd - tStart) / 1000000000.0) + " sec; totHits=" + totHits
                + "; " + queryCount + " queries");

        if (iter == 0) {
            long bytes = 0;
            for (LeafReaderContext ctx : r.leaves()) {
                CodecReader cr = (CodecReader) ctx.reader();
                System.out.println(Accountables.toString(cr));
                bytes += cr.ramBytesUsed();
            }
            System.out.println("READER MB: " + (bytes / 1024. / 1024.));
            System.out.println("RAM: " + Accountables.toString((Accountable) r.leaves().get(0).reader()));
        }
    }

    IOUtils.close(r, dir);
}

From source file:com.epam.catgenome.dao.index.FeatureIndexDao.java

License:Open Source License

public IndexSearchResult<FeatureIndexEntry> searchFeaturesInInterval(List<? extends FeatureFile> files,
        int start, int end, Chromosome chromosome) throws IOException {

    List<? extends FeatureFile> indexedFiles = files.stream()
            .filter(f -> fileManager.indexForFeatureFileExists(f)).collect(Collectors.toList());
    if (indexedFiles.isEmpty()) {
        return new IndexSearchResult<>(Collections.emptyList(), false, 0);
    }/*w  w  w .  j av  a  2  s.c o m*/
    SimpleFSDirectory[] indexes = fileManager.getIndexesForFiles(files);

    try (MultiReader reader = openMultiReader(indexes)) {
        if (reader.numDocs() == 0) {
            return new IndexSearchResult<>(Collections.emptyList(), false, 0);
        }
        BooleanQuery.Builder mainBuilder = new BooleanQuery.Builder();
        Query chrQuery = new TermQuery(new Term(FeatureIndexFields.CHROMOSOME_ID.getFieldName(),
                new BytesRef(chromosome.getId().toString())));
        mainBuilder.add(chrQuery, BooleanClause.Occur.MUST);

        BooleanQuery.Builder featureTypeBuilder = new BooleanQuery.Builder();
        featureTypeBuilder.add(new TermQuery(
                new Term(FeatureIndexFields.FEATURE_TYPE.getFieldName(), FeatureType.GENE.getFileValue())),
                BooleanClause.Occur.SHOULD);
        featureTypeBuilder.add(new TermQuery(
                new Term(FeatureIndexFields.FEATURE_TYPE.getFieldName(), FeatureType.EXON.getFileValue())),
                BooleanClause.Occur.SHOULD);

        mainBuilder.add(featureTypeBuilder.build(), BooleanClause.Occur.MUST);

        BooleanQuery.Builder rangeBuilder = new BooleanQuery.Builder();
        //start in interval
        Query startQuery = IntPoint.newRangeQuery(FeatureIndexFields.START_INDEX.getFieldName(), start, end);
        rangeBuilder.add(startQuery, BooleanClause.Occur.SHOULD);
        //end in interval
        Query endQuery = IntPoint.newRangeQuery(FeatureIndexFields.END_INDEX.getFieldName(), start, end);
        rangeBuilder.add(endQuery, BooleanClause.Occur.SHOULD);

        //feature lasts along all the interval (start < range and end > range)
        BooleanQuery.Builder spanQueryBuilder = new BooleanQuery.Builder();
        Query startExtQuery = IntPoint.newRangeQuery(FeatureIndexFields.START_INDEX.getFieldName(), 0,
                start - 1);
        spanQueryBuilder.add(startExtQuery, BooleanClause.Occur.MUST);

        Query endExtQuery = IntPoint.newRangeQuery(FeatureIndexFields.END_INDEX.getFieldName(), end + 1,
                Integer.MAX_VALUE);
        spanQueryBuilder.add(endExtQuery, BooleanClause.Occur.MUST);
        rangeBuilder.add(spanQueryBuilder.build(), BooleanClause.Occur.SHOULD);

        mainBuilder.add(rangeBuilder.build(), BooleanClause.Occur.MUST);

        return searchFileIndexes(files, mainBuilder.build(), null, reader.numDocs(), null);

    } finally {
        for (SimpleFSDirectory index : indexes) {
            IOUtils.closeQuietly(index);
        }
    }
}

From source file:com.epam.catgenome.entity.vcf.VcfFilterForm.java

License:Open Source License

/**
 * Filter variations by positions, using only variation's start index
 * @param builder//from ww  w.  j av  a  2  s . co  m
 */
private void addPositionFilter(BooleanQuery.Builder builder) {
    if (startIndex != null && endIndex != null) {
        builder.add(IntPoint.newRangeQuery(FeatureIndexFields.START_INDEX.getFieldName(), startIndex, endIndex),
                BooleanClause.Occur.MUST);
    } else {
        if (startIndex != null) {
            builder.add(IntPoint.newRangeQuery(FeatureIndexFields.START_INDEX.getFieldName(), startIndex,
                    Integer.MAX_VALUE), BooleanClause.Occur.MUST);
        } else if (endIndex != null) {
            builder.add(IntPoint.newRangeQuery(FeatureIndexFields.START_INDEX.getFieldName(), Integer.MIN_VALUE,
                    endIndex), BooleanClause.Occur.MUST);
        }
    }
}

From source file:com.epam.catgenome.entity.vcf.VcfFilterForm.java

License:Open Source License

private void tryAddIntegralKeyValueFilter(BooleanQuery.Builder builder, Map.Entry<String, Object> entry,
        String key, List list, Object val) {
    if (val instanceof Integer || entry.getValue() instanceof Long) {
        builder.add(//from w w w. ja  v  a  2 s  . com
                IntPoint.newRangeQuery(key, list.get(0) != null ? (Integer) list.get(0) : Integer.MIN_VALUE,
                        list.get(1) != null ? (Integer) list.get(1) : Integer.MAX_VALUE),
                BooleanClause.Occur.MUST);
    }
}

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

License:Apache License

@Override
public Query getPointRangeQuery(QParser parser, SchemaField field, String min, String max, boolean minInclusive,
        boolean maxInclusive) {
    int actualMin, actualMax;
    if (min == null) {
        actualMin = Integer.MIN_VALUE;
    } else {//  w  w  w.j a v  a2  s  . c  o  m
        actualMin = Integer.parseInt(min);
        if (!minInclusive) {
            actualMin++;
        }
    }
    if (max == null) {
        actualMax = Integer.MAX_VALUE;
    } else {
        actualMax = Integer.parseInt(max);
        if (!maxInclusive) {
            actualMax--;
        }
    }
    return IntPoint.newRangeQuery(field.getName(), actualMin, actualMax);
}

From source file:org.elasticsearch.index.query.RangeQueryBuilderTests.java

License:Apache License

@Override
protected void doAssertLuceneQuery(RangeQueryBuilder queryBuilder, Query query, QueryShardContext context)
        throws IOException {
    if (getCurrentTypes().length == 0 || (queryBuilder.fieldName().equals(DATE_FIELD_NAME) == false
            && queryBuilder.fieldName().equals(INT_FIELD_NAME) == false)) {
        assertThat(query, instanceOf(TermRangeQuery.class));
        TermRangeQuery termRangeQuery = (TermRangeQuery) query;
        assertThat(termRangeQuery.getField(), equalTo(queryBuilder.fieldName()));
        assertThat(termRangeQuery.getLowerTerm(), equalTo(BytesRefs.toBytesRef(queryBuilder.from())));
        assertThat(termRangeQuery.getUpperTerm(), equalTo(BytesRefs.toBytesRef(queryBuilder.to())));
        assertThat(termRangeQuery.includesLower(), equalTo(queryBuilder.includeLower()));
        assertThat(termRangeQuery.includesUpper(), equalTo(queryBuilder.includeUpper()));
    } else if (queryBuilder.fieldName().equals(DATE_FIELD_NAME)) {
        //we can't properly test unmapped dates because LateParsingQuery is package private
    } else if (queryBuilder.fieldName().equals(INT_FIELD_NAME)) {
        assertThat(query,/*  ww w  .  j  a va  2s.c  o m*/
                either(instanceOf(LegacyNumericRangeQuery.class)).or(instanceOf(PointRangeQuery.class)));
        if (query instanceof LegacyNumericRangeQuery) {
            LegacyNumericRangeQuery numericRangeQuery = (LegacyNumericRangeQuery) query;
            assertThat(numericRangeQuery.getField(), equalTo(queryBuilder.fieldName()));
            assertThat(numericRangeQuery.getMin(), equalTo(queryBuilder.from()));
            assertThat(numericRangeQuery.getMax(), equalTo(queryBuilder.to()));
            assertThat(numericRangeQuery.includesMin(), equalTo(queryBuilder.includeLower()));
            assertThat(numericRangeQuery.includesMax(), equalTo(queryBuilder.includeUpper()));
        } else {
            Integer min = (Integer) queryBuilder.from();
            Integer max = (Integer) queryBuilder.to();
            int minInt, maxInt;
            if (min == null) {
                minInt = Integer.MIN_VALUE;
            } else {
                minInt = min.intValue();
                if (queryBuilder.includeLower() == false && minInt != Integer.MAX_VALUE) {
                    minInt++;
                }
            }
            if (max == null) {
                maxInt = Integer.MAX_VALUE;
            } else {
                maxInt = max.intValue();
                if (queryBuilder.includeUpper() == false && maxInt != Integer.MIN_VALUE) {
                    maxInt--;
                }
            }
            try {
                assertEquals(IntPoint.newRangeQuery(INT_FIELD_NAME, minInt, maxInt), query);
            } catch (AssertionError e) {
                throw e;
            }
        }
    } else {
        throw new UnsupportedOperationException();
    }
}

From source file:org.elasticsearch.index.query.RangeQueryBuilderTests.java

License:Apache License

public void testToQueryNumericField() throws IOException {
    assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0);
    Query parsedQuery = rangeQuery(INT_FIELD_NAME).from(23).to(54).includeLower(true).includeUpper(false)
            .toQuery(createShardContext());
    // since age is automatically registered in data, we encode it as numeric
    assertThat(parsedQuery,//ww w.  j  a  v  a  2  s .  c om
            either(instanceOf(LegacyNumericRangeQuery.class)).or(instanceOf(PointRangeQuery.class)));
    if (parsedQuery instanceof LegacyNumericRangeQuery) {
        LegacyNumericRangeQuery rangeQuery = (LegacyNumericRangeQuery) parsedQuery;
        assertThat(rangeQuery.getField(), equalTo(INT_FIELD_NAME));
        assertThat(rangeQuery.getMin().intValue(), equalTo(23));
        assertThat(rangeQuery.getMax().intValue(), equalTo(54));
        assertThat(rangeQuery.includesMin(), equalTo(true));
        assertThat(rangeQuery.includesMax(), equalTo(false));
    } else {
        assertEquals(IntPoint.newRangeQuery(INT_FIELD_NAME, 23, 53), parsedQuery);
    }
}

From source file:org.elasticsearch.search.aggregations.metrics.avg.AvgAggregatorTests.java

License:Apache License

public void testQueryFiltering() throws IOException {
    testCase(IntPoint.newRangeQuery("number", 0, 3), iw -> {
        iw.addDocument(Arrays.asList(new IntPoint("number", 7), new SortedNumericDocValuesField("number", 7)));
        iw.addDocument(Arrays.asList(new IntPoint("number", 1), new SortedNumericDocValuesField("number", 2)));
        iw.addDocument(Arrays.asList(new IntPoint("number", 3), new SortedNumericDocValuesField("number", 3)));
    }, avg -> {//ww w  .  j  av a 2  s.  c  om
        assertEquals(2.5, avg.getValue(), 0);
    });
}

From source file:org.elasticsearch.search.aggregations.metrics.avg.AvgAggregatorTests.java

License:Apache License

public void testQueryFiltersAll() throws IOException {
    testCase(IntPoint.newRangeQuery("number", -1, 0), iw -> {
        iw.addDocument(Arrays.asList(new IntPoint("number", 7), new SortedNumericDocValuesField("number", 7)));
        iw.addDocument(Arrays.asList(new IntPoint("number", 1), new SortedNumericDocValuesField("number", 2)));
        iw.addDocument(Arrays.asList(new IntPoint("number", 3), new SortedNumericDocValuesField("number", 7)));
    }, avg -> {/*from w  ww .  j av  a 2s  . c  o  m*/
        assertEquals(Double.NaN, avg.getValue(), 0);
    });
}

From source file:org.elasticsearch.search.aggregations.metrics.CardinalityAggregatorTests.java

License:Apache License

public void testQueryFiltering() throws IOException {
    testCase(IntPoint.newRangeQuery("number", 0, 5), iw -> {
        iw.addDocument(Arrays.asList(new IntPoint("number", 7), new SortedNumericDocValuesField("number", 7)));
        iw.addDocument(Arrays.asList(new IntPoint("number", 1), new SortedNumericDocValuesField("number", 1)));
    }, card -> {//from   w  ww . java 2 s .c  om
        assertEquals(1, card.getValue(), 0);
    });
}