pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / test / org / apache / lucene / search / payloads / TestPayloadExplanations.java
1 package org.apache.lucene.search.payloads;
2
3 /**
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
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  */
19
20 import org.apache.lucene.index.Term;
21 import org.apache.lucene.search.DefaultSimilarity;
22 import org.apache.lucene.search.Similarity;
23 import org.apache.lucene.search.TestExplanations;
24 import org.apache.lucene.search.spans.SpanQuery;
25 import org.apache.lucene.util.BytesRef;
26
27 /**
28  * TestExplanations subclass focusing on payload queries
29  */
30 public class TestPayloadExplanations extends TestExplanations {
31   private PayloadFunction functions[] = new PayloadFunction[] { 
32       new AveragePayloadFunction(),
33       new MinPayloadFunction(),
34       new MaxPayloadFunction(),
35   };
36   
37   @Override
38   public void setUp() throws Exception {
39     super.setUp();
40     searcher.setSimilarity(new PayloadBoostSimilarity());
41   }
42   
43   // must be a static class so it can serialize
44   private static class PayloadBoostSimilarity extends DefaultSimilarity {
45     @Override
46     public float scorePayload(int docId, String fieldName, int start, int end, byte[] payload, int offset, int length) {
47       return 1 + (new BytesRef(payload, offset, length).hashCode() % 10);
48     }
49   }
50
51   /** macro for payloadtermquery */
52   private SpanQuery pt(String s, PayloadFunction fn, boolean includeSpanScore) {
53     return new PayloadTermQuery(new Term(FIELD,s), fn, includeSpanScore);
54   }
55   
56   /* simple PayloadTermQueries */
57   
58   public void testPT1() throws Exception {
59     for (PayloadFunction fn : functions) {
60       qtest(pt("w1", fn, false), new int[] {0,1,2,3});
61       qtest(pt("w1", fn, true), new int[] {0,1,2,3});
62     }
63   }
64
65   public void testPT2() throws Exception {
66     for (PayloadFunction fn : functions) {
67       SpanQuery q = pt("w1", fn, false);
68       q.setBoost(1000);
69       qtest(q, new int[] {0,1,2,3});
70       q = pt("w1", fn, true);
71       q.setBoost(1000);
72       qtest(q, new int[] {0,1,2,3});
73     }
74   }
75
76   public void testPT4() throws Exception {
77     for (PayloadFunction fn : functions) {
78       qtest(pt("xx", fn, false), new int[] {2,3});
79       qtest(pt("xx", fn, true), new int[] {2,3});
80     }
81   }
82
83   public void testPT5() throws Exception {
84     for (PayloadFunction fn : functions) {
85       SpanQuery q = pt("xx", fn, false);
86       q.setBoost(1000);
87       qtest(q, new int[] {2,3});
88       q = pt("xx", fn, true);
89       q.setBoost(1000);
90       qtest(q, new int[] {2,3});
91     }
92   }
93
94   // TODO: test the payloadnear query too!
95 }