|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.lucene.search; |
| 18 | + |
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.List; |
| 21 | +import org.apache.lucene.document.Document; |
| 22 | +import org.apache.lucene.document.Field; |
| 23 | +import org.apache.lucene.document.NumericDocValuesField; |
| 24 | +import org.apache.lucene.index.DirectoryReader; |
| 25 | +import org.apache.lucene.index.IndexReader; |
| 26 | +import org.apache.lucene.index.Term; |
| 27 | +import org.apache.lucene.store.Directory; |
| 28 | +import org.apache.lucene.tests.index.RandomIndexWriter; |
| 29 | +import org.apache.lucene.tests.util.LuceneTestCase; |
| 30 | + |
| 31 | +public class TestDoubleValuesSourceRescorer extends LuceneTestCase { |
| 32 | + |
| 33 | + private static final String ID_FIELD = "id"; |
| 34 | + private static final String DOC_VAL_FIELD = "docVal"; |
| 35 | + private static final String DOC_VAL_STORED_FIELD = "storedDocVal"; |
| 36 | + |
| 37 | + private final DoubleValuesSource doubleValuesSource = |
| 38 | + DoubleValuesSource.fromIntField(DOC_VAL_FIELD); |
| 39 | + |
| 40 | + private final DoubleValuesSourceRescorer rescorer = |
| 41 | + new DoubleValuesSourceRescorer(doubleValuesSource) { |
| 42 | + @Override |
| 43 | + protected float combine(float firstPassScore, boolean valuePresent, double sourceValue) { |
| 44 | + return valuePresent ? (float) sourceValue : 0f; |
| 45 | + } |
| 46 | + }; |
| 47 | + |
| 48 | + private static final List<String> dictionary = |
| 49 | + Arrays.asList( |
| 50 | + "river", "quick", "brown", "fox", "jumped", "lazy", "fence", "wizard", "of", "a", "an", |
| 51 | + "the", "cookie", "golf", "golden", "tennis", "boy", "plays", "likes", "wants"); |
| 52 | + |
| 53 | + String randomSentence() { |
| 54 | + final int length = random().nextInt(3, 10); |
| 55 | + StringBuilder sentence = new StringBuilder(); |
| 56 | + for (int i = 0; i < length; i++) { |
| 57 | + sentence.append(dictionary.get(random().nextInt(dictionary.size() - 1)) + " "); |
| 58 | + } |
| 59 | + return sentence.toString(); |
| 60 | + } |
| 61 | + |
| 62 | + private void publishDocs(int numDocs, String fieldName, boolean indexDocValues, Directory dir) |
| 63 | + throws Exception { |
| 64 | + RandomIndexWriter w = new RandomIndexWriter(random(), dir, newIndexWriterConfig()); |
| 65 | + for (int i = 0; i < numDocs; i++) { |
| 66 | + Document d = new Document(); |
| 67 | + d.add(newStringField(ID_FIELD, Integer.toString(i), Field.Store.YES)); |
| 68 | + d.add(newTextField(fieldName, randomSentence(), Field.Store.NO)); |
| 69 | + if (indexDocValues) { |
| 70 | + int val = i + 100; |
| 71 | + d.add(new NumericDocValuesField(DOC_VAL_FIELD, val)); |
| 72 | + d.add(newStringField(DOC_VAL_STORED_FIELD, Integer.toString(val), Field.Store.YES)); |
| 73 | + } |
| 74 | + w.addDocument(d); |
| 75 | + } |
| 76 | + w.close(); |
| 77 | + } |
| 78 | + |
| 79 | + public void testBasic() throws Exception { |
| 80 | + try (Directory dir = newDirectory()) { |
| 81 | + publishDocs(random().nextInt(100), "title", true, dir); |
| 82 | + try (IndexReader r = DirectoryReader.open(dir)) { |
| 83 | + IndexSearcher s = new IndexSearcher(r); |
| 84 | + TermQuery query = |
| 85 | + new TermQuery( |
| 86 | + new Term("title", dictionary.get(random().nextInt(dictionary.size() - 1)))); |
| 87 | + TopDocs queryHits = s.search(query, 50); |
| 88 | + TopDocs rescoredHits = rescorer.rescore(s, queryHits, 15); |
| 89 | + assertTrue(rescoredHits.scoreDocs.length <= 15); |
| 90 | + assertEquals(queryHits.totalHits, rescoredHits.totalHits); |
| 91 | + for (int i = 1; i < rescoredHits.scoreDocs.length; i++) { |
| 92 | + assertTrue(rescoredHits.scoreDocs[i - 1].score > rescoredHits.scoreDocs[i].score); |
| 93 | + } |
| 94 | + for (ScoreDoc hit : rescoredHits.scoreDocs) { |
| 95 | + assertEquals( |
| 96 | + s.storedFields().document(hit.doc).get(DOC_VAL_STORED_FIELD), |
| 97 | + Integer.toString((int) hit.score)); |
| 98 | + } |
| 99 | + int doc = rescoredHits.scoreDocs[0].doc; |
| 100 | + Explanation e = rescorer.explain(s, s.explain(query, doc), doc); |
| 101 | + String msg = e.toString(); |
| 102 | + assertTrue(msg.contains("combined score from firstPass and DoubleValuesSource")); |
| 103 | + assertTrue(msg.contains(getClass().toString())); |
| 104 | + assertTrue(msg.contains("first pass score")); |
| 105 | + assertTrue(msg.contains("value from DoubleValuesSource")); |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + public void testSubsetAndIdempotency() throws Exception { |
| 111 | + try (Directory dir = newDirectory()) { |
| 112 | + publishDocs(random().nextInt(60, 200), "title", true, dir); |
| 113 | + try (IndexReader r = DirectoryReader.open(dir)) { |
| 114 | + IndexSearcher s = new IndexSearcher(r); |
| 115 | + TermQuery query = |
| 116 | + new TermQuery( |
| 117 | + new Term("title", dictionary.get(random().nextInt(dictionary.size() - 1)))); |
| 118 | + TopDocs queryHits = s.search(query, 50); |
| 119 | + TopDocs rescoredHits1 = rescorer.rescore(s, queryHits, 15); |
| 120 | + |
| 121 | + int hits1Len = rescoredHits1.scoreDocs.length; |
| 122 | + int hit2N = Math.max(hits1Len / 2, 1); |
| 123 | + TopDocs rescoredHits2 = rescorer.rescore(s, queryHits, hit2N); |
| 124 | + assertEquals(hit2N, rescoredHits2.scoreDocs.length); |
| 125 | + for (int i = 0; i < hit2N; i++) { |
| 126 | + assertEquals(rescoredHits1.scoreDocs[i].doc, rescoredHits2.scoreDocs[i].doc); |
| 127 | + assertEquals(rescoredHits1.scoreDocs[i].score, rescoredHits2.scoreDocs[i].score, 1e-5); |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + public void testMissingValues() throws Exception { |
| 134 | + try (Directory dir = newDirectory()) { |
| 135 | + publishDocs(random().nextInt(60, 200), "title", false, dir); |
| 136 | + try (IndexReader r = DirectoryReader.open(dir)) { |
| 137 | + IndexSearcher s = new IndexSearcher(r); |
| 138 | + TermQuery query = |
| 139 | + new TermQuery( |
| 140 | + new Term("title", dictionary.get(random().nextInt(dictionary.size() - 1)))); |
| 141 | + TopDocs queryHits = s.search(query, 50); |
| 142 | + TopDocs rescoredHits = rescorer.rescore(s, queryHits, 15); |
| 143 | + assertTrue(rescoredHits.scoreDocs.length <= 15); |
| 144 | + assertEquals(queryHits.totalHits, rescoredHits.totalHits); |
| 145 | + for (int i = 0; i < rescoredHits.scoreDocs.length; i++) { |
| 146 | + assertEquals(rescoredHits.scoreDocs[i].score, 0f, 1e-5); |
| 147 | + if (i > 0) { |
| 148 | + assertTrue(rescoredHits.scoreDocs[i - 1].doc < rescoredHits.scoreDocs[i].doc); |
| 149 | + } |
| 150 | + } |
| 151 | + int doc = rescoredHits.scoreDocs[0].doc; |
| 152 | + Explanation e = rescorer.explain(s, s.explain(query, doc), doc); |
| 153 | + String msg = e.toString(); |
| 154 | + assertTrue(msg.contains("combined score from firstPass and DoubleValuesSource")); |
| 155 | + assertTrue(msg.contains(getClass().toString())); |
| 156 | + assertTrue(msg.contains("first pass score")); |
| 157 | + assertTrue(msg.contains("no value in DoubleValuesSource")); |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | +} |
0 commit comments