| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 1 | package de.ids_mannheim.korap.query; |
| 2 | |
| 3 | import java.io.IOException; |
| 4 | |
| 5 | import java.util.Set; |
| 6 | import java.util.Map; |
| 7 | |
| 8 | import org.apache.lucene.search.spans.Spans; |
| 9 | import org.apache.lucene.search.spans.SpanQuery; |
| 10 | import org.apache.lucene.search.Query; |
| 11 | import org.apache.lucene.index.AtomicReaderContext; |
| 12 | import org.apache.lucene.index.Term; |
| 13 | import org.apache.lucene.index.IndexReader; |
| 14 | import org.apache.lucene.index.TermContext; |
| 15 | import org.apache.lucene.util.Bits; |
| 16 | import org.apache.lucene.util.ToStringUtils; |
| 17 | |
| 18 | import de.ids_mannheim.korap.query.spans.ClassSpans; |
| 19 | |
| 20 | |
| 21 | /** |
| 22 | * Marks spans with a special class payload. |
| 23 | */ |
| 24 | public 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 Diewald | 3caa00d | 2013-12-13 02:24:04 +0000 | [diff] [blame] | 32 | if (number <= 15) { |
| 33 | this.number = number; |
| 34 | } else{ |
| 35 | this.number = (byte) 0; |
| 36 | }; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 37 | }; |
| 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 Diewald | c025a23 | 2014-02-28 19:01:14 +0000 | [diff] [blame] | 68 | Bits acceptDocs, |
| 69 | Map<Term,TermContext> termContexts) throws IOException { |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 70 | 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 Diewald | c025a23 | 2014-02-28 19:01:14 +0000 | [diff] [blame] | 109 | public boolean equals (Object o) { |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 110 | 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 | }; |