MtasSpanMatchAllSpans.java

  1. package mtas.search.spans;

  2. import java.io.IOException;

  3. import mtas.codec.util.CodecInfo;
  4. import mtas.codec.util.CodecInfo.IndexDoc;
  5. import mtas.search.spans.util.MtasSpans;

  6. import org.apache.lucene.search.TwoPhaseIterator;
  7. import org.apache.lucene.search.spans.SpanCollector;

  8. /**
  9.  * The Class MtasSpanMatchAllSpans.
  10.  */
  11. public class MtasSpanMatchAllSpans extends MtasSpans {

  12.   /** The query. */
  13.   private MtasSpanMatchAllQuery query;

  14.   /** The field. */
  15.   private String field;

  16.   /** The min position. */
  17.   private int minPosition;

  18.   /** The max position. */
  19.   private int maxPosition;

  20.   /** The current start position. */
  21.   private int currentStartPosition;

  22.   /** The current end position. */
  23.   private int currentEndPosition;

  24.   /** The doc id. */
  25.   private int docId;

  26.   /** The mtas codec info. */
  27.   private CodecInfo mtasCodecInfo;

  28.   /**
  29.    * Instantiates a new mtas span match all spans.
  30.    *
  31.    * @param query the query
  32.    * @param mtasCodecInfo the mtas codec info
  33.    * @param field the field
  34.    */
  35.   public MtasSpanMatchAllSpans(MtasSpanMatchAllQuery query,
  36.       CodecInfo mtasCodecInfo, String field) {
  37.     super();
  38.     this.query = query;
  39.     this.mtasCodecInfo = mtasCodecInfo;
  40.     this.field = field;
  41.     minPosition = NO_MORE_POSITIONS;
  42.     maxPosition = NO_MORE_POSITIONS;
  43.     currentStartPosition = NO_MORE_POSITIONS;
  44.     currentEndPosition = NO_MORE_POSITIONS;
  45.     docId = -1;
  46.   }

  47.   /*
  48.    * (non-Javadoc)
  49.    *
  50.    * @see org.apache.lucene.search.spans.Spans#nextStartPosition()
  51.    */
  52.   @Override
  53.   public int nextStartPosition() throws IOException {
  54.     if (currentStartPosition < minPosition) {
  55.       currentStartPosition = minPosition;
  56.       currentEndPosition = currentStartPosition + 1;
  57.     } else {
  58.       currentStartPosition++;
  59.       currentEndPosition = currentStartPosition + 1;
  60.       if (currentStartPosition > maxPosition) {
  61.         currentStartPosition = NO_MORE_POSITIONS;
  62.         currentEndPosition = NO_MORE_POSITIONS;
  63.       }
  64.     }
  65.     return currentStartPosition;
  66.   }

  67.   /*
  68.    * (non-Javadoc)
  69.    *
  70.    * @see org.apache.lucene.search.spans.Spans#startPosition()
  71.    */
  72.   @Override
  73.   public int startPosition() {
  74.     return currentStartPosition;
  75.   }

  76.   /*
  77.    * (non-Javadoc)
  78.    *
  79.    * @see org.apache.lucene.search.spans.Spans#endPosition()
  80.    */
  81.   @Override
  82.   public int endPosition() {
  83.     return currentEndPosition;
  84.   }

  85.   /*
  86.    * (non-Javadoc)
  87.    *
  88.    * @see org.apache.lucene.search.spans.Spans#width()
  89.    */
  90.   @Override
  91.   public int width() {
  92.     return 0;
  93.   }

  94.   /*
  95.    * (non-Javadoc)
  96.    *
  97.    * @see
  98.    * org.apache.lucene.search.spans.Spans#collect(org.apache.lucene.search.spans
  99.    * .SpanCollector)
  100.    */
  101.   @Override
  102.   public void collect(SpanCollector collector) throws IOException {
  103.     // do nothing
  104.   }

  105.   /*
  106.    * (non-Javadoc)
  107.    *
  108.    * @see org.apache.lucene.search.DocIdSetIterator#docID()
  109.    */
  110.   @Override
  111.   public int docID() {
  112.     return docId;
  113.   }

  114.   /*
  115.    * (non-Javadoc)
  116.    *
  117.    * @see org.apache.lucene.search.DocIdSetIterator#nextDoc()
  118.    */
  119.   @Override
  120.   public int nextDoc() throws IOException {
  121.     IndexDoc indexDoc = mtasCodecInfo.getNextDoc(field, docId);
  122.     if (indexDoc != null) {
  123.       docId = indexDoc.docId;
  124.       minPosition = indexDoc.minPosition;
  125.       maxPosition = indexDoc.maxPosition;
  126.       currentStartPosition = -1;
  127.       currentEndPosition = -1;
  128.     } else {
  129.       docId = NO_MORE_DOCS;
  130.       minPosition = NO_MORE_POSITIONS;
  131.       maxPosition = NO_MORE_POSITIONS;
  132.       currentStartPosition = NO_MORE_POSITIONS;
  133.       currentEndPosition = NO_MORE_POSITIONS;
  134.     }
  135.     return docId;
  136.   }

  137.   /*
  138.    * (non-Javadoc)
  139.    *
  140.    * @see org.apache.lucene.search.DocIdSetIterator#advance(int)
  141.    */
  142.   @Override
  143.   public int advance(int target) throws IOException {
  144.     IndexDoc indexDoc = mtasCodecInfo.getNextDoc(field, (target - 1));
  145.     if (indexDoc != null) {
  146.       docId = indexDoc.docId;
  147.       minPosition = indexDoc.minPosition;
  148.       maxPosition = indexDoc.maxPosition;
  149.       currentStartPosition = -1;
  150.       currentEndPosition = -1;
  151.     } else {
  152.       docId = NO_MORE_DOCS;
  153.       minPosition = NO_MORE_POSITIONS;
  154.       maxPosition = NO_MORE_POSITIONS;
  155.       currentStartPosition = NO_MORE_POSITIONS;
  156.       currentEndPosition = NO_MORE_POSITIONS;
  157.     }
  158.     return docId;
  159.   }

  160.   /*
  161.    * (non-Javadoc)
  162.    *
  163.    * @see org.apache.lucene.search.DocIdSetIterator#cost()
  164.    */
  165.   @Override
  166.   public long cost() {
  167.     return 0;
  168.   }

  169.   /*
  170.    * (non-Javadoc)
  171.    *
  172.    * @see org.apache.lucene.search.spans.Spans#positionsCost()
  173.    */
  174.   @Override
  175.   public float positionsCost() {
  176.     return 0;
  177.   }

  178.   /*
  179.    * (non-Javadoc)
  180.    *
  181.    * @see org.apache.lucene.search.spans.Spans#asTwoPhaseIterator()
  182.    */
  183.   @Override
  184.   public TwoPhaseIterator asTwoPhaseIterator() {
  185.     if (!query.twoPhaseIteratorAllowed()) {
  186.       return null;
  187.     } else {
  188.       // TODO
  189.       return null;
  190.     }
  191.   }

  192. }