| 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; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 4 | import java.util.Map; |
| 5 | |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 6 | import org.apache.lucene.index.AtomicReaderContext; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 7 | import org.apache.lucene.index.IndexReader; |
| margaretha | 50c7633 | 2015-03-19 10:10:39 +0100 | [diff] [blame] | 8 | import org.apache.lucene.index.Term; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 9 | import org.apache.lucene.index.TermContext; |
| margaretha | 50c7633 | 2015-03-19 10:10:39 +0100 | [diff] [blame] | 10 | import org.apache.lucene.search.Query; |
| 11 | import org.apache.lucene.search.spans.SpanQuery; |
| 12 | import org.apache.lucene.search.spans.Spans; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 13 | import org.apache.lucene.util.Bits; |
| 14 | import org.apache.lucene.util.ToStringUtils; |
| 15 | |
| 16 | import de.ids_mannheim.korap.query.spans.ClassSpans; |
| 17 | |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 18 | /** |
| 19 | * Marks spans with a special class payload. |
| 20 | */ |
| margaretha | 50c7633 | 2015-03-19 10:10:39 +0100 | [diff] [blame] | 21 | public class SpanClassQuery extends SimpleSpanQuery { |
| 22 | protected byte number = 1; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 23 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 24 | |
| Nils Diewald | 56dc258 | 2014-11-04 21:33:46 +0000 | [diff] [blame] | 25 | public SpanClassQuery (SpanQuery operand) { |
| margaretha | 50c7633 | 2015-03-19 10:10:39 +0100 | [diff] [blame] | 26 | super(operand, false); |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 27 | }; |
| 28 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 29 | |
| margaretha | 50c7633 | 2015-03-19 10:10:39 +0100 | [diff] [blame] | 30 | public SpanClassQuery (SpanQuery operand, byte number) { |
| 31 | super(operand, false); |
| 32 | this.number = number; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 33 | }; |
| 34 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 35 | |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 36 | @Override |
| 37 | public String toString (String field) { |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 38 | StringBuffer buffer = new StringBuffer("{"); |
| 39 | short classNr = (short) this.number; |
| 40 | buffer.append(classNr & 0xFF).append(": "); |
| margaretha | 50c7633 | 2015-03-19 10:10:39 +0100 | [diff] [blame] | 41 | buffer.append(this.firstClause.toString()).append('}'); |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 42 | buffer.append(ToStringUtils.boost(getBoost())); |
| 43 | return buffer.toString(); |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 44 | }; |
| 45 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 46 | |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 47 | @Override |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 48 | public Spans getSpans (final AtomicReaderContext context, Bits acceptDocs, |
| 49 | Map<Term, TermContext> termContexts) throws IOException { |
| margaretha | 50c7633 | 2015-03-19 10:10:39 +0100 | [diff] [blame] | 50 | return (Spans) new ClassSpans(this.firstClause, context, acceptDocs, |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 51 | termContexts, number); |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 52 | }; |
| 53 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 54 | |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 55 | @Override |
| 56 | public Query rewrite (IndexReader reader) throws IOException { |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 57 | SpanClassQuery clone = null; |
| margaretha | 50c7633 | 2015-03-19 10:10:39 +0100 | [diff] [blame] | 58 | SpanQuery query = (SpanQuery) this.firstClause.rewrite(reader); |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 59 | |
| margaretha | 50c7633 | 2015-03-19 10:10:39 +0100 | [diff] [blame] | 60 | if (query != this.firstClause) { |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 61 | if (clone == null) |
| 62 | clone = this.clone(); |
| margaretha | 50c7633 | 2015-03-19 10:10:39 +0100 | [diff] [blame] | 63 | clone.firstClause = query; |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 64 | }; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 65 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 66 | if (clone != null) |
| 67 | return clone; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 68 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 69 | return this; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 70 | }; |
| 71 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 72 | |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 73 | @Override |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 74 | public SpanClassQuery clone () { |
| 75 | SpanClassQuery spanClassQuery = new SpanClassQuery( |
| margaretha | 50c7633 | 2015-03-19 10:10:39 +0100 | [diff] [blame] | 76 | (SpanQuery) this.firstClause.clone(), this.number); |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 77 | spanClassQuery.setBoost(getBoost()); |
| 78 | return spanClassQuery; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | |
| 82 | /** Returns true iff <code>o</code> is equal to this. */ |
| 83 | @Override |
| Nils Diewald | c025a23 | 2014-02-28 19:01:14 +0000 | [diff] [blame] | 84 | public boolean equals (Object o) { |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 85 | if (this == o) |
| 86 | return true; |
| 87 | if (!(o instanceof SpanClassQuery)) |
| 88 | return false; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 89 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 90 | final SpanClassQuery spanClassQuery = (SpanClassQuery) o; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 91 | |
| margaretha | 50c7633 | 2015-03-19 10:10:39 +0100 | [diff] [blame] | 92 | if (!this.firstClause.equals(spanClassQuery.firstClause)) |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 93 | return false; |
| 94 | |
| 95 | if (this.number != spanClassQuery.number) |
| 96 | return false; |
| 97 | |
| 98 | return getBoost() == spanClassQuery.getBoost(); |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | |
| 102 | // I don't know what I am doing here |
| 103 | @Override |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 104 | public int hashCode () { |
| 105 | int result = 1; |
| margaretha | 50c7633 | 2015-03-19 10:10:39 +0100 | [diff] [blame] | 106 | result = firstClause.hashCode(); |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 107 | result += (int) number; |
| 108 | result ^= (result << 15) | (result >>> 18); |
| 109 | result += Float.floatToRawIntBits(getBoost()); |
| 110 | return result; |
| margaretha | 50c7633 | 2015-03-19 10:10:39 +0100 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | |
| 114 | public byte getNumber () { |
| 115 | return number; |
| 116 | } |
| 117 | |
| 118 | |
| 119 | public void setNumber (byte number) { |
| 120 | this.number = number; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 121 | }; |
| 122 | }; |