Home » lucene-3.0.1-src » org.apache » lucene » search » spans » [javadoc | source]

    1   package org.apache.lucene.search.spans;
    2   
    3   /**
    4    * Licensed to the Apache Software Foundation (ASF) under one or more
    5    * contributor license agreements.  See the NOTICE file distributed with
    6    * this work for additional information regarding copyright ownership.
    7    * The ASF licenses this file to You under the Apache License, Version 2.0
    8    * (the "License"); you may not use this file except in compliance with
    9    * the License.  You may obtain a copy of the License at
   10    *
   11    *     http://www.apache.org/licenses/LICENSE-2.0
   12    *
   13    * Unless required by applicable law or agreed to in writing, software
   14    * distributed under the License is distributed on an "AS IS" BASIS,
   15    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   16    * See the License for the specific language governing permissions and
   17    * limitations under the License.
   18    */
   19   
   20   import java.io.IOException;
   21   
   22   import java.util.List;
   23   import java.util.Collection;
   24   import java.util.ArrayList;
   25   import java.util.Iterator;
   26   import java.util.Set;
   27   
   28   import org.apache.lucene.index.IndexReader;
   29   import org.apache.lucene.index.Term;
   30   import org.apache.lucene.util.PriorityQueue;
   31   import org.apache.lucene.util.ToStringUtils;
   32   import org.apache.lucene.search.Query;
   33   
   34   /** Matches the union of its clauses.*/
   35   public class SpanOrQuery extends SpanQuery implements Cloneable {
   36     private List<SpanQuery> clauses;
   37     private String field;
   38   
   39     /** Construct a SpanOrQuery merging the provided clauses. */
   40     public SpanOrQuery(SpanQuery... clauses) {
   41   
   42       // copy clauses array into an ArrayList
   43       this.clauses = new ArrayList<SpanQuery>(clauses.length);
   44       for (int i = 0; i < clauses.length; i++) {
   45         SpanQuery clause = clauses[i];
   46         if (i == 0) {                               // check field
   47           field = clause.getField();
   48         } else if (!clause.getField().equals(field)) {
   49           throw new IllegalArgumentException("Clauses must have same field.");
   50         }
   51         this.clauses.add(clause);
   52       }
   53     }
   54   
   55     /** Return the clauses whose spans are matched. */
   56     public SpanQuery[] getClauses() {
   57       return clauses.toArray(new SpanQuery[clauses.size()]);
   58     }
   59   
   60     @Override
   61     public String getField() { return field; }
   62   
   63     @Override
   64     public void extractTerms(Set<Term> terms) {
   65       for(final SpanQuery clause: clauses) {
   66         clause.extractTerms(terms);
   67       }
   68     }
   69     
   70     @Override
   71     public Object clone() {
   72       int sz = clauses.size();
   73       SpanQuery[] newClauses = new SpanQuery[sz];
   74   
   75       for (int i = 0; i < sz; i++) {
   76         newClauses[i] = (SpanQuery) clauses.get(i).clone();
   77       }
   78       SpanOrQuery soq = new SpanOrQuery(newClauses);
   79       soq.setBoost(getBoost());
   80       return soq;
   81     }
   82   
   83     @Override
   84     public Query rewrite(IndexReader reader) throws IOException {
   85       SpanOrQuery clone = null;
   86       for (int i = 0 ; i < clauses.size(); i++) {
   87         SpanQuery c = clauses.get(i);
   88         SpanQuery query = (SpanQuery) c.rewrite(reader);
   89         if (query != c) {                     // clause rewrote: must clone
   90           if (clone == null)
   91             clone = (SpanOrQuery) this.clone();
   92           clone.clauses.set(i,query);
   93         }
   94       }
   95       if (clone != null) {
   96         return clone;                        // some clauses rewrote
   97       } else {
   98         return this;                         // no clauses rewrote
   99       }
  100     }
  101   
  102     @Override
  103     public String toString(String field) {
  104       StringBuilder buffer = new StringBuilder();
  105       buffer.append("spanOr([");
  106       Iterator<SpanQuery> i = clauses.iterator();
  107       while (i.hasNext()) {
  108         SpanQuery clause = i.next();
  109         buffer.append(clause.toString(field));
  110         if (i.hasNext()) {
  111           buffer.append(", ");
  112         }
  113       }
  114       buffer.append("])");
  115       buffer.append(ToStringUtils.boost(getBoost()));
  116       return buffer.toString();
  117     }
  118   
  119     @Override
  120     public boolean equals(Object o) {
  121       if (this == o) return true;
  122       if (o == null || getClass() != o.getClass()) return false;
  123   
  124       final SpanOrQuery that = (SpanOrQuery) o;
  125   
  126       if (!clauses.equals(that.clauses)) return false;
  127       if (!clauses.isEmpty() && !field.equals(that.field)) return false;
  128   
  129       return getBoost() == that.getBoost();
  130     }
  131   
  132     @Override
  133     public int hashCode() {
  134       int h = clauses.hashCode();
  135       h ^= (h << 10) | (h >>> 23);
  136       h ^= Float.floatToRawIntBits(getBoost());
  137       return h;
  138     }
  139   
  140   
  141     private class SpanQueue extends PriorityQueue<Spans> {
  142       public SpanQueue(int size) {
  143         initialize(size);
  144       }
  145   
  146       @Override
  147       protected final boolean lessThan(Spans spans1, Spans spans2) {
  148         if (spans1.doc() == spans2.doc()) {
  149           if (spans1.start() == spans2.start()) {
  150             return spans1.end() < spans2.end();
  151           } else {
  152             return spans1.start() < spans2.start();
  153           }
  154         } else {
  155           return spans1.doc() < spans2.doc();
  156         }
  157       }
  158     }
  159   
  160     @Override
  161     public Spans getSpans(final IndexReader reader) throws IOException {
  162       if (clauses.size() == 1)                      // optimize 1-clause case
  163         return (clauses.get(0)).getSpans(reader);
  164   
  165       return new Spans() {
  166           private SpanQueue queue = null;
  167   
  168           private boolean initSpanQueue(int target) throws IOException {
  169             queue = new SpanQueue(clauses.size());
  170             Iterator<SpanQuery> i = clauses.iterator();
  171             while (i.hasNext()) {
  172               Spans spans = i.next().getSpans(reader);
  173               if (   ((target == -1) && spans.next())
  174                   || ((target != -1) && spans.skipTo(target))) {
  175                 queue.add(spans);
  176               }
  177             }
  178             return queue.size() != 0;
  179           }
  180   
  181           @Override
  182           public boolean next() throws IOException {
  183             if (queue == null) {
  184               return initSpanQueue(-1);
  185             }
  186   
  187             if (queue.size() == 0) { // all done
  188               return false;
  189             }
  190   
  191             if (top().next()) { // move to next
  192               queue.updateTop();
  193               return true;
  194             }
  195   
  196             queue.pop();  // exhausted a clause
  197             return queue.size() != 0;
  198           }
  199   
  200           private Spans top() { return queue.top(); }
  201   
  202           @Override
  203           public boolean skipTo(int target) throws IOException {
  204             if (queue == null) {
  205               return initSpanQueue(target);
  206             }
  207     
  208             boolean skipCalled = false;
  209             while (queue.size() != 0 && top().doc() < target) {
  210               if (top().skipTo(target)) {
  211                 queue.updateTop();
  212               } else {
  213                 queue.pop();
  214               }
  215               skipCalled = true;
  216             }
  217     
  218             if (skipCalled) {
  219               return queue.size() != 0;
  220             }
  221             return next();
  222           }
  223   
  224           @Override
  225           public int doc() { return top().doc(); }
  226           @Override
  227           public int start() { return top().start(); }
  228           @Override
  229           public int end() { return top().end(); }
  230   
  231         @Override
  232         public Collection<byte[]> getPayload() throws IOException {
  233           ArrayList<byte[]> result = null;
  234           Spans theTop = top();
  235           if (theTop != null && theTop.isPayloadAvailable()) {
  236             result = new ArrayList<byte[]>(theTop.getPayload());
  237           }
  238           return result;
  239         }
  240   
  241         @Override
  242         public boolean isPayloadAvailable() {
  243           Spans top = top();
  244           return top != null && top.isPayloadAvailable();
  245         }
  246   
  247         @Override
  248         public String toString() {
  249             return "spans("+SpanOrQuery.this+")@"+
  250               ((queue == null)?"START"
  251                :(queue.size()>0?(doc()+":"+start()+"-"+end()):"END"));
  252           }
  253   
  254         };
  255     }
  256   
  257   }

Home » lucene-3.0.1-src » org.apache » lucene » search » spans » [javadoc | source]