blob: 16730ceb7725fac08565aafe6ddf87ce146308c6 [file] [log] [blame]
Nils Diewaldf399a672013-11-18 17:55:22 +00001package de.ids_mannheim.korap.query;
2
3import java.io.IOException;
4
5import java.util.Set;
6import java.util.Map;
7
8import org.apache.lucene.search.spans.Spans;
9import org.apache.lucene.search.spans.SpanQuery;
10import org.apache.lucene.search.Query;
11import org.apache.lucene.index.AtomicReaderContext;
12import org.apache.lucene.index.Term;
13import org.apache.lucene.index.IndexReader;
14import org.apache.lucene.index.TermContext;
15import org.apache.lucene.util.Bits;
16import org.apache.lucene.util.ToStringUtils;
17
18import de.ids_mannheim.korap.query.spans.ClassSpans;
19
20
21/**
22 * Marks spans with a special class payload.
23 */
24public class SpanClassQuery extends SpanQuery {
25 public String field;
26 protected byte number;
27 protected SpanQuery highlight;
28
29 public SpanClassQuery (SpanQuery highlight, byte number) {
30 this.field = highlight.getField();
31 this.highlight = highlight;
Nils Diewald3caa00d2013-12-13 02:24:04 +000032 if (number <= 15) {
33 this.number = number;
34 } else{
35 this.number = (byte) 0;
36 };
Nils Diewaldf399a672013-11-18 17:55:22 +000037 };
38
39 public SpanClassQuery (SpanQuery highlight) {
40 this.field = highlight.getField();
41 this.highlight = highlight;
42 this.number = (byte) 0;
43 };
44
45 public byte number () {
46 return this.number;
47 };
48
49 @Override
50 public String getField () { return field; }
51
52 @Override
53 public void extractTerms (Set<Term> terms) {
54 this.highlight.extractTerms(terms);
55 };
56
57 @Override
58 public String toString (String field) {
59 StringBuffer buffer = new StringBuffer("{");
60 buffer.append((int) this.number).append(": ");
61 buffer.append(this.highlight.toString()).append('}');
62 buffer.append(ToStringUtils.boost(getBoost()));
63 return buffer.toString();
64 };
65
66 @Override
67 public Spans getSpans (final AtomicReaderContext context,
Nils Diewaldc025a232014-02-28 19:01:14 +000068 Bits acceptDocs,
69 Map<Term,TermContext> termContexts) throws IOException {
Nils Diewaldf399a672013-11-18 17:55:22 +000070 return (Spans) new ClassSpans(
71 this.highlight,
72 context,
73 acceptDocs,
74 termContexts,
75 number
76 );
77 };
78
79 @Override
80 public Query rewrite (IndexReader reader) throws IOException {
81 SpanClassQuery clone = null;
82 SpanQuery query = (SpanQuery) this.highlight.rewrite(reader);
83
84 if (query != this.highlight) {
85 if (clone == null)
86 clone = this.clone();
87 clone.highlight = query;
88 };
89
90 if (clone != null)
91 return clone;
92
93 return this;
94 };
95
96 @Override
97 public SpanClassQuery clone() {
98 SpanClassQuery spanClassQuery = new SpanClassQuery(
99 (SpanQuery) this.highlight.clone(),
100 this.number
101 );
102 spanClassQuery.setBoost(getBoost());
103 return spanClassQuery;
104 };
105
106
107 /** Returns true iff <code>o</code> is equal to this. */
108 @Override
Nils Diewaldc025a232014-02-28 19:01:14 +0000109 public boolean equals (Object o) {
Nils Diewaldf399a672013-11-18 17:55:22 +0000110 if (this == o) return true;
111 if (!(o instanceof SpanClassQuery)) return false;
112
113 final SpanClassQuery spanClassQuery = (SpanClassQuery) o;
114
115 if (!highlight.equals(spanClassQuery.highlight)) return false;
116
117 if (this.number != spanClassQuery.number) return false;
118
119 return getBoost() == spanClassQuery.getBoost();
120 };
121
122
123 // I don't know what I am doing here
124 @Override
125 public int hashCode() {
126 int result = 1;
127 result = highlight.hashCode();
128 result += (int) number;
129 result ^= (result << 15) | (result >>> 18);
130 result += Float.floatToRawIntBits(getBoost());
131 return result;
132 };
133};