1 package org.apache.lucene.search.payloads;
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 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;
28 * TestExplanations subclass focusing on payload queries
30 public class TestPayloadExplanations extends TestExplanations {
31 private PayloadFunction functions[] = new PayloadFunction[] {
32 new AveragePayloadFunction(),
33 new MinPayloadFunction(),
34 new MaxPayloadFunction(),
38 public void setUp() throws Exception {
40 searcher.setSimilarity(new PayloadBoostSimilarity());
43 // must be a static class so it can serialize
44 private static class PayloadBoostSimilarity extends DefaultSimilarity {
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);
51 /** macro for payloadtermquery */
52 private SpanQuery pt(String s, PayloadFunction fn, boolean includeSpanScore) {
53 return new PayloadTermQuery(new Term(FIELD,s), fn, includeSpanScore);
56 /* simple PayloadTermQueries */
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});
65 public void testPT2() throws Exception {
66 for (PayloadFunction fn : functions) {
67 SpanQuery q = pt("w1", fn, false);
69 qtest(q, new int[] {0,1,2,3});
70 q = pt("w1", fn, true);
72 qtest(q, new int[] {0,1,2,3});
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});
83 public void testPT5() throws Exception {
84 for (PayloadFunction fn : functions) {
85 SpanQuery q = pt("xx", fn, false);
87 qtest(q, new int[] {2,3});
88 q = pt("xx", fn, true);
90 qtest(q, new int[] {2,3});
94 // TODO: test the payloadnear query too!