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 java.io.IOException;
23
import org.apache.lucene.index.IndexReader;
24
import org.apache.lucene.index.Term;
25
import org.apache.lucene.search.Query;
26
import org.apache.lucene.search.Weight;
27
import org.apache.lucene.search.Searcher;
28
import org.apache.lucene.search.Similarity;
29
import org.apache.lucene.util.ToStringUtils;
32
* <p>Wrapper to allow {@link SpanQuery} objects participate in composite
33
* single-field SpanQueries by 'lying' about their search field. That is,
34
* the masked SpanQuery will function as normal,
35
* but {@link SpanQuery#getField()} simply hands back the value supplied
36
* in this class's constructor.</p>
38
* <p>This can be used to support Queries like {@link SpanNearQuery} or
39
* {@link SpanOrQuery} across different fields, which is not ordinarily
42
* <p>This can be useful for denormalized relational data: for example, when
43
* indexing a document with conceptually many 'children': </p>
47
* studentfirstname: james
48
* studentsurname: jones
51
* studenfirstname: james
52
* studentsurname: smith
53
* studentfirstname: sally
54
* studentsurname: jones
57
* <p>a SpanNearQuery with a slop of 0 can be applied across two
58
* {@link SpanTermQuery} objects as follows:
60
* SpanQuery q1 = new SpanTermQuery(new Term("studentfirstname", "james"));
61
* SpanQuery q2 = new SpanTermQuery(new Term("studentsurname", "jones"));
62
* SpanQuery q2m = new FieldMaskingSpanQuery(q2, "studentfirstname");
63
* Query q = new SpanNearQuery(new SpanQuery[]{q1, q2m}, -1, false);
65
* to search for 'studentfirstname:james studentsurname:jones' and find
66
* teacherid 1 without matching teacherid 2 (which has a 'james' in position 0
67
* and 'jones' in position 1). </p>
69
* <p>Note: as {@link #getField()} returns the masked field, scoring will be
70
* done using the norms of the field name supplied. This may lead to unexpected
71
* scoring behaviour.</p>
73
public class FieldMaskingSpanQuery extends SpanQuery {
74
private SpanQuery maskedQuery;
77
public FieldMaskingSpanQuery(SpanQuery maskedQuery, String maskedField) {
78
this.maskedQuery = maskedQuery;
79
this.field = maskedField;
83
public String getField() {
87
public SpanQuery getMaskedQuery() {
91
// :NOTE: getBoost and setBoost are not proxied to the maskedQuery
92
// ...this is done to be more consistent with things like SpanFirstQuery
95
public Spans getSpans(IndexReader reader) throws IOException {
96
return maskedQuery.getSpans(reader);
100
public void extractTerms(Set<Term> terms) {
101
maskedQuery.extractTerms(terms);
105
public Weight createWeight(Searcher searcher) throws IOException {
106
return maskedQuery.createWeight(searcher);
110
public Query rewrite(IndexReader reader) throws IOException {
111
FieldMaskingSpanQuery clone = null;
113
SpanQuery rewritten = (SpanQuery) maskedQuery.rewrite(reader);
114
if (rewritten != maskedQuery) {
115
clone = (FieldMaskingSpanQuery) this.clone();
116
clone.maskedQuery = rewritten;
127
public String toString(String field) {
128
StringBuilder buffer = new StringBuilder();
129
buffer.append("mask(");
130
buffer.append(maskedQuery.toString(field));
132
buffer.append(ToStringUtils.boost(getBoost()));
133
buffer.append(" as ");
134
buffer.append(this.field);
135
return buffer.toString();
139
public boolean equals(Object o) {
140
if (!(o instanceof FieldMaskingSpanQuery))
142
FieldMaskingSpanQuery other = (FieldMaskingSpanQuery) o;
143
return (this.getField().equals(other.getField())
144
&& (this.getBoost() == other.getBoost())
145
&& this.getMaskedQuery().equals(other.getMaskedQuery()));
150
public int hashCode() {
151
return getMaskedQuery().hashCode()
152
^ getField().hashCode()
153
^ Float.floatToRawIntBits(getBoost());