List of usage examples for org.apache.lucene.search.spans Spans Spans
Spans
From source file:com.mhs.qsol.spans.SpanWithinQuery.java
License:Apache License
public Spans getSpans(final IndexReader reader) throws IOException { return new Spans() { private Spans includeSpans = include.getSpans(reader); private boolean moreInclude = true; private Spans excludeSpans = exclude.getSpans(reader); private boolean moreExclude = true; public boolean next() throws IOException { if (moreInclude) { // move to next include moreInclude = includeSpans.next(); }/*w w w. j a v a2 s. c om*/ while (moreInclude && moreExclude) { if (includeSpans.doc() > excludeSpans.doc()) { // skip exclude moreExclude = excludeSpans.skipTo(includeSpans.doc()); } int count = 0; while (moreExclude // while exclude is before && (includeSpans.doc() == excludeSpans.doc())) { if ((!(excludeSpans.end() <= includeSpans.start()))) { count += 1; if (count > proximity) { break; } } moreExclude = excludeSpans.next(); // increment exclude } if (!moreExclude // if no intersection || (includeSpans.doc() != excludeSpans.doc()) || (includeSpans.end() <= excludeSpans.start())) { break; // we found a match } moreInclude = includeSpans.next(); // intersected: keep scanning } return moreInclude; } public boolean skipTo(int target) throws IOException { if (moreInclude) { // skip include moreInclude = includeSpans.skipTo(target); } if (!moreInclude) { return false; } if (moreExclude // skip exclude && (includeSpans.doc() > excludeSpans.doc())) { moreExclude = excludeSpans.skipTo(includeSpans.doc()); } int count = 0; while (moreExclude // while exclude is before && (includeSpans.doc() == excludeSpans.doc())) { if ((!(excludeSpans.end() <= includeSpans.start()))) { count += 1; if (count > proximity) { break; } } moreExclude = excludeSpans.next(); // increment exclude } if (!moreExclude // if no intersection || (includeSpans.doc() != excludeSpans.doc()) || (includeSpans.end() <= excludeSpans.start())) { return true; // we found a match } boolean returnboolean = next(); return returnboolean; // scan to next match } public int doc() { return includeSpans.doc(); } public int start() { return includeSpans.start(); } public int end() { return includeSpans.end(); } // TODO: Remove warning after API has been finalized @Override public Collection<byte[]> getPayload() throws IOException { ArrayList<byte[]> result = null; if (includeSpans.isPayloadAvailable()) { result = new ArrayList<byte[]>(includeSpans.getPayload()); } return result; } // TODO: Remove warning after API has been finalized @Override public boolean isPayloadAvailable() { return includeSpans.isPayloadAvailable(); } public String toString() { return "spans(" + SpanWithinQuery.this.toString() + ")"; } }; }
From source file:com.o19s.solr.swan.query.SpanWithinQuery.java
License:Apache License
@Override public Spans getSpans(final AtomicReaderContext context, final Bits acceptDocs, final Map<Term, TermContext> termContexts) throws IOException { return new Spans() { private Spans includeSpans = include.getSpans(context, acceptDocs, termContexts); private boolean moreInclude = true; private Spans excludeSpans = exclude.getSpans(context, acceptDocs, termContexts); private boolean moreExclude = true; public boolean next() throws IOException { if (moreInclude) { // move to next include moreInclude = includeSpans.next(); }//from w w w. ja v a 2 s . c om while (moreInclude && moreExclude) { if (includeSpans.doc() > excludeSpans.doc()) { // skip exclude moreExclude = excludeSpans.skipTo(includeSpans.doc()); } int count = 0; while (moreExclude // while exclude is before && (includeSpans.doc() == excludeSpans.doc())) { if ((excludeSpans.end() - 1) > includeSpans.start() && excludeSpans.start() < (includeSpans.end() - 1)) { count += 1; if (count >= proximity) { break; } } moreExclude = excludeSpans.next(); // increment exclude } if (!moreExclude // if no intersection || (includeSpans.doc() != excludeSpans.doc()) || (includeSpans.end() <= excludeSpans.start())) { break; // we found a match } moreInclude = includeSpans.next(); // intersected: keep scanning } return moreInclude; } public boolean skipTo(int target) throws IOException { if (moreInclude) { // skip include moreInclude = includeSpans.skipTo(target); } if (!moreInclude) { return false; } if (moreExclude // skip exclude && (includeSpans.doc() > excludeSpans.doc())) { moreExclude = excludeSpans.skipTo(includeSpans.doc()); } int count = 0; while (moreExclude // while exclude is before && (includeSpans.doc() == excludeSpans.doc())) { if ((excludeSpans.end() - 1) > includeSpans.start() && excludeSpans.start() < (includeSpans.end() - 1)) { count += 1; if (count >= proximity) { break; } } moreExclude = excludeSpans.next(); // increment exclude } if (!moreExclude // if no intersection || (includeSpans.doc() != excludeSpans.doc()) || (includeSpans.end() <= excludeSpans.start())) { return true; // we found a match } boolean returnboolean = next(); return returnboolean; // scan to next match } public int doc() { return includeSpans.doc(); } public int start() { return includeSpans.start(); } public int end() { return includeSpans.end(); } @Override public Collection<byte[]> getPayload() throws IOException { ArrayList<byte[]> result = null; if (includeSpans.isPayloadAvailable()) { result = new ArrayList<byte[]>(includeSpans.getPayload()); } return result; } @Override public boolean isPayloadAvailable() { try { return includeSpans.isPayloadAvailable(); } catch (IOException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. return false; } } public String toString() { return "spans(" + SpanWithinQuery.this.toString() + ")"; } @Override public long cost() { // TODO Auto-generated method stub return 0; } }; }
From source file:com.shaie.annots.MultiPositionSpanTermQuery.java
License:Apache License
@Override public SpanWeight createWeight(IndexSearcher searcher, boolean needsScores, float boost) throws IOException { final IndexReaderContext topContext = searcher.getTopReaderContext(); final TermContext context; if (termContext == null || termContext.wasBuiltFor(topContext) == false) { context = TermContext.build(topContext, term); } else {/*from w w w . jav a2s.c o m*/ context = termContext; } final Map<Term, TermContext> terms = needsScores ? Collections.singletonMap(term, context) : null; return new SpanTermWeight(context, searcher, terms, boost) { @Override public Spans getSpans(LeafReaderContext context, Postings requiredPostings) throws IOException { final TermSpans spans = (TermSpans) super.getSpans(context, requiredPostings.atLeast(Postings.PAYLOADS)); if (spans == null) { // term is not present in that reader assert context.reader().docFreq(term) == 0 : "no term exists in reader term=" + term; return null; } return new Spans() { private final PositionSpansCollector payloadCollector = new PositionSpansCollector(); private int end = -1; @Override public int advance(int target) throws IOException { end = -1; return spans.advance(target); } @Override public void collect(SpanCollector collector) throws IOException { spans.collect(collector); } @Override public long cost() { return spans.cost(); } @Override public int docID() { return spans.docID(); } @Override public int endPosition() { return end; } @Override public int nextDoc() throws IOException { end = -1; return spans.nextDoc(); } @Override public int nextStartPosition() throws IOException { final int pos = spans.nextStartPosition(); if (pos == NO_MORE_POSITIONS) { end = NO_MORE_POSITIONS; return NO_MORE_POSITIONS; } spans.collect(payloadCollector); end = payloadCollector.payloadValue + pos; return pos; } @Override public float positionsCost() { return spans.positionsCost(); } @Override public int startPosition() { return spans.startPosition(); } @Override public int width() { return spans.width(); } }; } }; }
From source file:com.shaie.annots.SpanAnnotationTermQuery.java
License:Apache License
@Override public Spans getSpans(LeafReaderContext context, Bits acceptDocs, Map<Term, TermContext> termContexts) throws IOException { final Spans spans = super.getSpans(context, acceptDocs, termContexts); return new Spans() { private int start, end; final ByteArrayDataInput in = new ByteArrayDataInput(); @Override//from ww w .jav a 2 s. c o m public int start() { return start; } @Override public boolean skipTo(int target) throws IOException { return spans.skipTo(target); } @Override public boolean next() throws IOException { if (!spans.next()) { return false; } if (!isPayloadAvailable()) { return next(); } byte[] payload = getPayload().iterator().next(); in.reset(payload); start = in.readVInt(); end = in.readVInt() + start - 1; // end is inclusive return true; } @Override public boolean isPayloadAvailable() throws IOException { return spans.isPayloadAvailable(); } @Override public Collection<byte[]> getPayload() throws IOException { return spans.getPayload(); } @Override public int end() { return end; } @Override public int doc() { return spans.doc(); } @Override public long cost() { return spans.cost(); } }; }