Example usage for org.apache.lucene.search SimpleCollector SimpleCollector

List of usage examples for org.apache.lucene.search SimpleCollector SimpleCollector

Introduction

In this page you can find the example usage for org.apache.lucene.search SimpleCollector SimpleCollector.

Prototype

SimpleCollector

Source Link

Usage

From source file:com.o19s.es.ltr.logging.LoggingFetchSubPhaseTests.java

License:Apache License

public SearchHit[] selectRandomHits() throws IOException {
    int minHits = TestUtil.nextInt(random(), 5, 10);
    int maxHits = TestUtil.nextInt(random(), minHits, minHits + 10);
    List<SearchHit> hits = new ArrayList<>(maxHits);
    searcher.search(new MatchAllDocsQuery(), new SimpleCollector() {
        LeafReaderContext context;/*from  w  ww  .  j ava  2s . co m*/

        @Override
        protected void doSetNextReader(LeafReaderContext context) throws IOException {
            super.doSetNextReader(context);
            this.context = context;
        }

        @Override
        public void collect(int doc) throws IOException {
            if (hits.size() < minHits || (random().nextBoolean() && hits.size() < maxHits)) {
                Document d = context.reader().document(doc);
                String id = d.get("id");
                SearchHit hit = new SearchHit(doc + context.docBase, id, new Text("text"),
                        random().nextBoolean() ? new HashMap<>() : null);
                hits.add(hit);
            }
        }

        @Override
        public boolean needsScores() {
            return false;
        }
    });
    assert hits.size() >= minHits;
    Collections.shuffle(hits, random());
    return hits.toArray(new SearchHit[hits.size()]);
}

From source file:com.o19s.es.ltr.query.LtrQueryTests.java

License:Apache License

public Map<String, Map<Integer, Float>> getFeatureScores(List<PrebuiltFeature> features) throws IOException {
    Map<String, Map<Integer, Float>> featuresPerDoc = new HashMap<>();
    PrebuiltFeatureSet set = new PrebuiltFeatureSet("test", features);

    Map<Integer, Float> collectedScores = new HashMap<>();
    LogLtrRanker.LogConsumer logger = new LogLtrRanker.LogConsumer() {
        @Override/* w w  w  . j av a 2 s .c  om*/
        public void accept(int featureOrdinal, float score) {
            collectedScores.put(featureOrdinal, score);
        }

        @Override
        public void reset() {
            collectedScores.clear();
        }
    };
    RankerQuery query = RankerQuery.buildLogQuery(logger, set, null, Collections.emptyMap());

    searcherUnderTest.search(query, new SimpleCollector() {
        private LeafReaderContext context;
        private Scorer scorer;

        @Override
        public boolean needsScores() {
            return true;
        }

        @Override
        public void setScorer(Scorer scorer) throws IOException {
            this.scorer = scorer;
        }

        @Override
        protected void doSetNextReader(LeafReaderContext context) throws IOException {
            this.context = context;
        }

        @Override
        public void collect(int doc) throws IOException {
            scorer.score();
            Document d = context.reader().document(doc);
            featuresPerDoc.put(d.get("id"), new HashMap<>(collectedScores));
        }
    });

    return featuresPerDoc;
}

From source file:org.apache.solr.search.facet.FacetFieldProcessorByHashDV.java

License:Apache License

private void collectDocs() throws IOException {
    if (calc instanceof TermOrdCalc) { // Strings

        // TODO support SortedSetDocValues
        SortedDocValues globalDocValues = FieldUtil.getSortedDocValues(fcontext.qcontext, sf, null);
        ((TermOrdCalc) calc).lookupOrdFunction = ord -> {
            try {
                return globalDocValues.lookupOrd(ord);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }//from  www  .j av  a  2s .  co m
        };

        DocSetUtil.collectSortedDocSet(fcontext.base, fcontext.searcher.getIndexReader(),
                new SimpleCollector() {
                    SortedDocValues docValues = globalDocValues; // this segment/leaf. NN
                    LongValues toGlobal = LongValues.IDENTITY; // this segment to global ordinal. NN

                    @Override
                    public boolean needsScores() {
                        return false;
                    }

                    @Override
                    protected void doSetNextReader(LeafReaderContext ctx) throws IOException {
                        setNextReaderFirstPhase(ctx);
                        if (globalDocValues instanceof MultiDocValues.MultiSortedDocValues) {
                            MultiDocValues.MultiSortedDocValues multiDocValues = (MultiDocValues.MultiSortedDocValues) globalDocValues;
                            docValues = multiDocValues.values[ctx.ord];
                            toGlobal = multiDocValues.mapping.getGlobalOrds(ctx.ord);
                        }
                    }

                    @Override
                    public void collect(int segDoc) throws IOException {
                        if (segDoc > docValues.docID()) {
                            docValues.advance(segDoc);
                        }
                        if (segDoc == docValues.docID()) {
                            long val = toGlobal.get(docValues.ordValue());
                            collectValFirstPhase(segDoc, val);
                        }
                    }
                });

    } else { // Numeric:

        // TODO support SortedNumericDocValues
        DocSetUtil.collectSortedDocSet(fcontext.base, fcontext.searcher.getIndexReader(),
                new SimpleCollector() {
                    NumericDocValues values = null; //NN

                    @Override
                    public boolean needsScores() {
                        return false;
                    }

                    @Override
                    protected void doSetNextReader(LeafReaderContext ctx) throws IOException {
                        setNextReaderFirstPhase(ctx);
                        values = DocValues.getNumeric(ctx.reader(), sf.getName());
                    }

                    @Override
                    public void collect(int segDoc) throws IOException {
                        if (segDoc > values.docID()) {
                            values.advance(segDoc);
                        }
                        if (segDoc == values.docID()) {
                            collectValFirstPhase(segDoc, values.longValue());
                        }
                    }
                });
    }
}

From source file:stroom.index.server.BenchmarkIndex.java

License:Apache License

private void doSearchOnField(final IndexReader[] readers, final String arg) throws IOException {
    long timeSearching = 0;
    long searchesDone = 0;
    long matchesFound = 0;

    for (int i = 0; i < 10000; i++) {
        final long startTime = System.currentTimeMillis();
        final Query query = new TermQuery(new Term(arg, "user" + getRandomSkewed()));

        for (final IndexReader reader : readers) {
            final List<Integer> documentIdList = new ArrayList<>();
            final IndexSearcher searcher = new IndexSearcher(reader);
            searcher.search(query, new SimpleCollector() {
                private int docBase;

                @Override/*from   w  w w . j a  v a 2 s.  c  o  m*/
                protected void doSetNextReader(final LeafReaderContext context) throws IOException {
                    super.doSetNextReader(context);
                    docBase = context.docBase;
                }

                @Override
                public void collect(final int doc) throws IOException {
                    documentIdList.add(docBase + doc);
                }

                @Override
                public boolean needsScores() {
                    return false;
                }
            });

            for (final Integer docId : documentIdList) {
                final Document doc = reader.document(docId);
                final String streamId = doc.get(IndexConstants.STREAM_ID);
                final String eventId = doc.get(IndexConstants.EVENT_ID);
            }
            matchesFound += documentIdList.size();
        }

        timeSearching += System.currentTimeMillis() - startTime;
        searchesDone++;

    }
    LOGGER.info("Performed " + ModelStringUtil.formatCsv(searchesDone) + " searches on arg " + arg + " in "
            + ModelStringUtil.formatDurationString(timeSearching) + " and found "
            + ModelStringUtil.formatCsv(matchesFound) + " matches");
}

From source file:tw.com.kyle.luminance.LumReader.java

public List<Long> getAnnotations(long ref_uuid) throws IOException {
    Document ref_doc = GetDocument(ref_uuid);
    if (ref_doc == null) {
        return null;
    }/*from w  w  w  . j  a v  a  2 s. c om*/
    if (!ref_doc.get("class").equals(LumIndexer.DOC_DISCOURSE)) {
        return null;
    }

    List<Long> uuid_list = new ArrayList<>();
    BytesRef bref = LumUtils.LongToBytesRef(ref_uuid);
    TermQuery tquery = new TermQuery(new Term("base_ref", bref));
    //TermQuery aquery = new TermQuery(new Term("class", LumDocument.ANNO));
    BooleanQuery.Builder bquery_builder = new BooleanQuery.Builder();
    bquery_builder.add(tquery, Occur.SHOULD);
    // bquery_builder.add(aquery, Occur.SHOULD);

    searcher.search(bquery_builder.build(), new SimpleCollector() {
        int docBase = 0;

        @Override
        protected void doSetNextReader(LeafReaderContext context) {
            docBase = context.docBase;
        }

        @Override
        public void collect(int i) throws IOException {
            BytesRef uuid_x = searcher.doc(i + docBase).getBinaryValue("uuid");
            uuid_list.add(LumUtils.BytesRefToLong(uuid_x));
            // System.out.printf("%016x%n", uuid_list.get(uuid_list.size() - 1));
        }

        @Override
        public boolean needsScores() {
            return false;
        }

    });

    return uuid_list;
}

From source file:uk.co.flax.luwak.matchers.HighlightingMatcher.java

License:Apache License

/** Find the highlights for a specific query */
protected HighlightsMatch findHighlights(String queryId, Query query) throws IOException {

    final HighlightCollector collector = new HighlightCollector(queryId);

    docs.getSearcher().search(rewriter.rewrite(query), new SimpleCollector() {

        Scorer scorer;/* ww w  .  ja  v  a  2 s . c om*/

        @Override
        public void collect(int i) throws IOException {
            try {
                collector.setMatch(i);
                SpanExtractor.collect(scorer, collector, true);
            } catch (Exception e) {
                collector.match.error = e.getMessage();
            }
        }

        @Override
        public void setScorer(Scorer scorer) throws IOException {
            this.scorer = scorer;
        }

        @Override
        public boolean needsScores() {
            return true;
        }
    });

    return collector.match;
}