Example usage for org.apache.lucene.index LeafReaderContext reader

List of usage examples for org.apache.lucene.index LeafReaderContext reader

Introduction

In this page you can find the example usage for org.apache.lucene.index LeafReaderContext reader.

Prototype

LeafReader reader

To view the source code for org.apache.lucene.index LeafReaderContext reader.

Click Source Link

Usage

From source file:BM25LSimilarity.java

License:Apache License

@Override
public final SimScorer simScorer(SimWeight stats, LeafReaderContext context) throws IOException {
    BM25Stats bm25stats = (BM25Stats) stats;
    return new BM25DocScorer(bm25stats, context.reader().getNormValues(bm25stats.field));
}

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;//from   ww w . j a va 2 s .  c  om
                        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:alba.solr.core.PseudoFieldsDynamicValueSource.java

License:Apache License

public FunctionValues getValues(Map context, LeafReaderContext readerContext) throws IOException {

    pseudoField.setLeafReader(readerContext.reader(), this.args);

    //TODO. params are positional, make them named!
    pseudoField.setArgs(this.args);

    pseudoField.setSolrQueryRequest(this.solrQueryRequest);

    pseudoField.init();// w w  w .j av a2s.c om

    return pseudoField;

}

From source file:br.pucminas.ri.jsearch.queryexpansion.RocchioQueryExpansion.java

License:Open Source License

private float getScore(Directory directory, String term) throws CorruptIndexException, IOException {

    try (IndexReader idxReader = DirectoryReader.open(directory)) {

        ConcreteTFIDFSimilarity sim = new ConcreteTFIDFSimilarity();

        for (LeafReaderContext context : idxReader.leaves()) {
            LeafReader reader = context.reader();

            try {
                Terms terms = reader.terms(Constants.DOC_CONTENT);
                TermsEnum termsEnum = terms.iterator();
                PostingsEnum postings = null;

                BytesRef text;/* w  w  w  .j  a  v a  2  s  .  co m*/
                while ((text = termsEnum.next()) != null) {
                    postings = termsEnum.postings(postings);
                    if (text.utf8ToString().equalsIgnoreCase(term)) {

                        while (postings.nextDoc() != PostingsEnum.NO_MORE_DOCS) {
                            int freq = postings.freq();
                            float tf = sim.tf(freq);
                            float idf = sim.idf(termsEnum.docFreq(), indexReader.numDocs());
                            return tf * idf;
                        }
                    }
                }

            } catch (IOException ex) {
                Logger.getLogger(RocchioQueryExpansion.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

    }

    return 0;
}

From source file:com.b2international.index.lucene.AbstractDocsOutOfOrderCollector.java

License:Apache License

@Override
public final void doSetNextReader(final LeafReaderContext context) throws IOException {
    initDocValues(context.reader());

    if (!isLeafCollectible()) {
        throw new CollectionTerminatedException();
    }//w w  w. jav  a  2s.c om
}

From source file:com.basistech.lucene.tools.LuceneQueryTool.java

License:Apache License

LuceneQueryTool(IndexReader reader, PrintStream out) throws IOException {
    this.indexReader = reader;
    this.outputLimit = Integer.MAX_VALUE;
    this.analyzer = new KeywordAnalyzer();
    this.fieldNames = Lists.newArrayList();
    this.defaultOut = out;
    allFieldNames = Sets.newTreeSet();//from  w  w  w . ja v  a2  s.c o  m
    for (LeafReaderContext leaf : reader.leaves()) {
        for (FieldInfo fieldInfo : leaf.reader().getFieldInfos()) {
            allFieldNames.add(fieldInfo.name);
        }
    }
    this.formatter = Formatter.newInstance(Formatter.Format.MULTILINE, false);
}

From source file:com.basistech.lucene.tools.LuceneQueryTool.java

License:Apache License

private void enumerateTerms(String field) throws IOException {
    if (!allFieldNames.contains(field)) {
        throw new RuntimeException("Invalid field name: " + field);
    }/*ww w  . j  av a  2s .  c  o m*/
    List<LeafReaderContext> leaves = indexReader.leaves();
    TermsEnum termsEnum;
    boolean unindexedField = true;
    Map<String, Integer> termCountMap = new TreeMap<>();
    for (LeafReaderContext leaf : leaves) {
        Terms terms = leaf.reader().terms(field);
        if (terms == null) {
            continue;
        }
        unindexedField = false;
        termsEnum = terms.iterator();
        BytesRef bytesRef;
        while ((bytesRef = termsEnum.next()) != null) {
            String term = bytesRef.utf8ToString();
            if (termCountMap.containsKey(term)) {
                termCountMap.put(term, termsEnum.docFreq() + termCountMap.get(term));
            } else {
                termCountMap.put(term, termsEnum.docFreq());
            }
        }
    }
    if (unindexedField) {
        throw new RuntimeException("Unindexed field: " + field);
    }
    for (Map.Entry<String, Integer> entry : termCountMap.entrySet()) {
        defaultOut.println(entry.getKey() + " (" + entry.getValue() + ")");
    }
}

From source file:com.basistech.lucene.tools.LuceneQueryTool.java

License:Apache License

private void countFields() throws IOException {
    for (String field : allFieldNames) {
        List<LeafReaderContext> leaves = indexReader.leaves();
        Map<String, Integer> fieldCounts = new TreeMap<>();
        int count = 0;
        for (LeafReaderContext leaf : leaves) {
            Terms terms = leaf.reader().terms(field);
            if (terms == null) {
                continue;
            }//from  ww w .jav  a2  s .  c  om
            count += terms.getDocCount();
        }
        fieldCounts.put(field, count);
        for (Map.Entry<String, Integer> entry : fieldCounts.entrySet()) {
            defaultOut.println(entry.getKey() + ": " + entry.getValue());
        }
    }
}

From source file:com.bdaum.zoom.lal.internal.LireActivator.java

License:Open Source License

public void closeIndexReader(IndexReader reader) throws IOException {
    searcherMap.remove(reader);/*  w ww  .  jav  a2 s . c om*/
    try {
        if (reader instanceof CompositeReader)
            for (LeafReaderContext context : ((CompositeReader) reader).leaves())
                context.reader().close();
        reader.close();
    } catch (AlreadyClosedException e) {
        // do nothing
    }
}

From source file:com.core.nlp.similarity.TFIDFSimilarity.java

License:Apache License

@Override
public final SimScorer simScorer(SimWeight stats, LeafReaderContext context) throws IOException {
    IDFStats idfstats = (IDFStats) stats;
    return new TFIDFSimScorer(idfstats, context.reader().getNormValues(idfstats.field));
}