Example usage for org.apache.lucene.search.spans SpanCollector SpanCollector

List of usage examples for org.apache.lucene.search.spans SpanCollector SpanCollector

Introduction

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

Prototype

SpanCollector

Source Link

Usage

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

public List<Integer[]> query_for_offsets(String term, String field, boolean useNearQuery) throws IOException {
    if (term.length() == 0) {
        return null;
    }/*from  w  w w  .j  a  va  2  s .c  o  m*/

    SpanQuery sq = null;
    if (!useNearQuery) {
        sq = new SpanTermQuery(new Term(field, term));
    } else {

        SpanNearQuery.Builder builder = new SpanNearQuery.Builder(field, true);
        for (int i = 0; i < term.length(); ++i) {
            builder.addClause(new SpanTermQuery(new Term(field, term.substring(i, i + 1))));
        }
        sq = builder.build();
    }

    IndexSearcher searcher = new IndexSearcher(idx_reader);
    List<Integer[]> offs = new ArrayList<>();
    for (LeafReaderContext ctx : idx_reader.leaves()) {

        SpanWeight weights = sq.createWeight(searcher, false);
        if (weights == null) {
            continue;
        }
        Spans spans = weights.getSpans(ctx, Postings.OFFSETS);
        if (spans == null) {
            System.out.printf("Nothing found for %s%n", term);
            continue;
        }

        int nxtDoc = -1;
        while ((nxtDoc = spans.nextDoc()) != Spans.NO_MORE_DOCS) {
            final int doc_id = nxtDoc;
            while (spans.nextStartPosition() != Spans.NO_MORE_POSITIONS) {
                final int start_pos = spans.startPosition();
                final int end_pos = spans.endPosition();
                Integer[] off_x = new Integer[] { doc_id, -1, -1 };
                spans.collect(new SpanCollector() {
                    @Override
                    public void collectLeaf(PostingsEnum pe, int i, Term term) throws IOException {
                        int s_off = pe.startOffset();
                        int e_off = pe.endOffset();
                        if (i == start_pos)
                            off_x[1] = s_off;
                        if (i + 1 == end_pos)
                            off_x[2] = e_off;
                    }

                    @Override
                    public void reset() {
                        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
                    }
                });
                offs.add(off_x);
            }

        }

    }

    return offs;
}