1 package org.apache.lucene.search;
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 java.util.concurrent.ExecutorService;
21 import java.io.IOException;
23 import org.apache.lucene.index.IndexReader;
26 * Helper class that adds some extra checks to ensure correct
27 * usage of {@code IndexSearcher} and {@code Weight}.
28 * TODO: Extend this by more checks, that's just a start.
30 public class AssertingIndexSearcher extends IndexSearcher {
31 public AssertingIndexSearcher(IndexReader r) {
35 public AssertingIndexSearcher(IndexReader r, ExecutorService ex) {
39 // not anonymous because else not serializable (compare trunk)
40 private static final class UnmodifiableWeight extends Weight {
41 private final Weight w;
43 UnmodifiableWeight(Weight w) {
48 public Explanation explain(IndexReader reader, int doc) throws IOException {
49 return w.explain(reader, doc);
53 public Query getQuery() {
58 public float getValue() {
63 public void normalize(float norm) {
64 throw new IllegalStateException("Weight already normalized.");
68 public Scorer scorer(IndexReader reader, boolean scoreDocsInOrder, boolean topScorer) throws IOException {
69 Scorer scorer = w.scorer(reader, scoreDocsInOrder, topScorer);
71 // check that scorer obeys disi contract for docID() before next()/advance
73 int docid = scorer.docID();
74 assert docid == -1 || docid == DocIdSetIterator.NO_MORE_DOCS;
75 } catch (UnsupportedOperationException ignored) {
76 // from a top-level BS1
84 public float sumOfSquaredWeights() throws IOException {
85 throw new IllegalStateException("Weight already normalized.");
89 public boolean scoresDocsOutOfOrder() {
90 return w.scoresDocsOutOfOrder();
94 /** Ensures, that the returned {@code Weight} is not normalized again, which may produce wrong scores. */
96 public Weight createNormalizedWeight(Query query) throws IOException {
97 final Weight w = super.createNormalizedWeight(query);
98 return new UnmodifiableWeight(w);