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 org.apache.lucene.index.IndexReader; 21 import org.apache.lucene.index.Term; 22 import org.apache.lucene.util.ToStringUtils; 23 24 import java.io.IOException; 25 import java.util.Set; 26 27 /** Matches spans containing a term. */ 28 public class SpanTermQuery extends SpanQuery { 29 protected Term term; 30 31 /** Construct a SpanTermQuery matching the named term's spans. */ 32 public SpanTermQuery(Term term) { this.term = term; } 33 34 /** Return the term whose spans are matched. */ 35 public Term getTerm() { return term; } 36 37 @Override 38 public String getField() { return term.field(); } 39 40 @Override 41 public void extractTerms(Set<Term> terms) { 42 terms.add(term); 43 } 44 45 @Override 46 public String toString(String field) { 47 StringBuilder buffer = new StringBuilder(); 48 if (term.field().equals(field)) 49 buffer.append(term.text()); 50 else 51 buffer.append(term.toString()); 52 buffer.append(ToStringUtils.boost(getBoost())); 53 return buffer.toString(); 54 } 55 56 @Override 57 public int hashCode() { 58 final int prime = 31; 59 int result = super.hashCode(); 60 result = prime * result + ((term == null) ? 0 : term.hashCode()); 61 return result; 62 } 63 64 @Override 65 public boolean equals(Object obj) { 66 if (this == obj) 67 return true; 68 if (!super.equals(obj)) 69 return false; 70 if (getClass() != obj.getClass()) 71 return false; 72 SpanTermQuery other = (SpanTermQuery) obj; 73 if (term == null) { 74 if (other.term != null) 75 return false; 76 } else if (!term.equals(other.term)) 77 return false; 78 return true; 79 } 80 81 @Override 82 public Spans getSpans(final IndexReader reader) throws IOException { 83 return new TermSpans(reader.termPositions(term), term); 84 } 85 86 }