1 package org.apache.lucene.search.spans;
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 import org.apache.lucene.index.IndexReader;
21 import org.apache.lucene.index.Term;
22 import org.apache.lucene.util.ToStringUtils;
24 import java.io.IOException;
27 /** Matches spans containing a term. */
28 public class SpanTermQuery extends SpanQuery {
31 /** Construct a SpanTermQuery matching the named term's spans. */
32 public SpanTermQuery(Term term) { this.term = term; }
34 /** Return the term whose spans are matched. */
35 public Term getTerm() { return term; }
38 public String getField() { return term.field(); }
41 public void extractTerms(Set<Term> terms) {
46 public String toString(String field) {
47 StringBuilder buffer = new StringBuilder();
48 if (term.field().equals(field))
49 buffer.append(term.text());
51 buffer.append(term.toString());
52 buffer.append(ToStringUtils.boost(getBoost()));
53 return buffer.toString();
57 public int hashCode() {
59 int result = super.hashCode();
60 result = prime * result + ((term == null) ? 0 : term.hashCode());
65 public boolean equals(Object obj) {
68 if (!super.equals(obj))
70 if (getClass() != obj.getClass())
72 SpanTermQuery other = (SpanTermQuery) obj;
74 if (other.term != null)
76 } else if (!term.equals(other.term))
82 public Spans getSpans(final IndexReader reader) throws IOException {
83 return new TermSpans(reader.termPositions(term), term);