1 package org.apache.lucene.search.spans; 2 3 18 19 import java.io.IOException ; 20 21 import java.util.Collection ; 22 import java.util.List ; 23 import java.util.ArrayList ; 24 import java.util.Iterator ; 25 26 27 import org.apache.lucene.index.IndexReader; 28 import org.apache.lucene.search.Query; 29 import org.apache.lucene.util.ToStringUtils; 30 31 34 public class SpanNearQuery extends SpanQuery { 35 private List clauses; 36 private int slop; 37 private boolean inOrder; 38 39 private String field; 40 41 45 public SpanNearQuery(SpanQuery[] clauses, int slop, boolean inOrder) { 46 47 this.clauses = new ArrayList (clauses.length); 49 for (int i = 0; i < clauses.length; i++) { 50 SpanQuery clause = clauses[i]; 51 if (i == 0) { field = clause.getField(); 53 } else if (!clause.getField().equals(field)) { 54 throw new IllegalArgumentException ("Clauses must have same field."); 55 } 56 this.clauses.add(clause); 57 } 58 59 this.slop = slop; 60 this.inOrder = inOrder; 61 } 62 63 64 public SpanQuery[] getClauses() { 65 return (SpanQuery[])clauses.toArray(new SpanQuery[clauses.size()]); 66 } 67 68 69 public int getSlop() { return slop; } 70 71 72 public boolean isInOrder() { return inOrder; } 73 74 public String getField() { return field; } 75 76 public Collection getTerms() { 77 Collection terms = new ArrayList (); 78 Iterator i = clauses.iterator(); 79 while (i.hasNext()) { 80 SpanQuery clause = (SpanQuery)i.next(); 81 terms.addAll(clause.getTerms()); 82 } 83 return terms; 84 } 85 86 public String toString(String field) { 87 StringBuffer buffer = new StringBuffer (); 88 buffer.append("spanNear(["); 89 Iterator i = clauses.iterator(); 90 while (i.hasNext()) { 91 SpanQuery clause = (SpanQuery)i.next(); 92 buffer.append(clause.toString(field)); 93 if (i.hasNext()) { 94 buffer.append(", "); 95 } 96 } 97 buffer.append("], "); 98 buffer.append(slop); 99 buffer.append(", "); 100 buffer.append(inOrder); 101 buffer.append(")"); 102 buffer.append(ToStringUtils.boost(getBoost())); 103 return buffer.toString(); 104 } 105 106 public Spans getSpans(final IndexReader reader) throws IOException { 107 if (clauses.size() == 0) return new SpanOrQuery(getClauses()).getSpans(reader); 109 110 if (clauses.size() == 1) return ((SpanQuery)clauses.get(0)).getSpans(reader); 112 113 return new NearSpans(this, reader); 114 } 115 116 public Query rewrite(IndexReader reader) throws IOException { 117 SpanNearQuery clone = null; 118 for (int i = 0 ; i < clauses.size(); i++) { 119 SpanQuery c = (SpanQuery)clauses.get(i); 120 SpanQuery query = (SpanQuery) c.rewrite(reader); 121 if (query != c) { if (clone == null) 123 clone = (SpanNearQuery) this.clone(); 124 clone.clauses.set(i,query); 125 } 126 } 127 if (clone != null) { 128 return clone; } else { 130 return this; } 132 } 133 134 135 public boolean equals(Object o) { 136 if (this == o) return true; 137 if (!(o instanceof SpanNearQuery)) return false; 138 139 final SpanNearQuery spanNearQuery = (SpanNearQuery) o; 140 141 if (inOrder != spanNearQuery.inOrder) return false; 142 if (slop != spanNearQuery.slop) return false; 143 if (!clauses.equals(spanNearQuery.clauses)) return false; 144 145 return getBoost() == spanNearQuery.getBoost(); 146 } 147 148 public int hashCode() { 149 int result; 150 result = clauses.hashCode(); 151 result ^= (result << 14) | (result >>> 19); result += Float.floatToRawIntBits(getBoost()); 156 result += slop; 157 result ^= (inOrder ? 0x99AFD3BD : 0); 158 return result; 159 } 160 } 161 | Popular Tags |