~slub.team/goobi-indexserver/3.x

« back to all changes in this revision

Viewing changes to solr/core/src/java/org/apache/solr/search/LuceneQueryOptimizer.java

  • Committer: Sebastian Meyer
  • Date: 2012-08-03 09:12:40 UTC
  • Revision ID: sebastian.meyer@slub-dresden.de-20120803091240-x6861b0vabq1xror
Remove Lucene and Solr source code and add patches instead
Fix Bug #985487: Auto-suggestion for the search interface

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
 
18
 
package org.apache.solr.search;
19
 
 
20
 
/* Copyright (c) 2003 The Nutch Organization.  All rights reserved.   */
21
 
/* Use subject to the conditions in http://www.nutch.org/LICENSE.txt. */
22
 
 
23
 
 
24
 
import org.apache.lucene.search.*;
25
 
 
26
 
import java.util.LinkedHashMap;
27
 
import java.util.Map;
28
 
import java.io.IOException;
29
 
 
30
 
/** Utility which converts certain query clauses into {@link QueryFilter}s and
31
 
 * caches these.  Only required {@link TermQuery}s whose boost is zero and
32
 
 * whose term occurs in at least a certain fraction of documents are converted
33
 
 * to cached filters.  This accelerates query constraints like language,
34
 
 * document format, etc., which do not affect ranking but might otherwise slow
35
 
 * search considerably. */
36
 
// Taken from Nutch and modified - YCS
37
 
class LuceneQueryOptimizer {
38
 
  private LinkedHashMap cache;                   // an LRU cache of QueryFilter
39
 
 
40
 
  private float threshold;
41
 
 
42
 
  /** Construct an optimizer that caches and uses filters for required {@link
43
 
   * TermQuery}s whose boost is zero.
44
 
   * @param cacheSize the number of QueryFilters to cache
45
 
   * @param threshold the fraction of documents which must contain term
46
 
   */
47
 
  public LuceneQueryOptimizer(final int cacheSize, float threshold) {
48
 
    this.cache = new LinkedHashMap(cacheSize, 0.75f, true) {
49
 
        @Override
50
 
        protected boolean removeEldestEntry(Map.Entry eldest) {
51
 
          return size() > cacheSize;              // limit size of cache
52
 
        }
53
 
      };
54
 
    this.threshold = threshold;
55
 
  }
56
 
 
57
 
  public TopDocs optimize(BooleanQuery original,
58
 
                          Searcher searcher,
59
 
                          int numHits,
60
 
                          Query[] queryOut,
61
 
                          Filter[] filterOut
62
 
                          )
63
 
    throws IOException {
64
 
 
65
 
    BooleanQuery query = new BooleanQuery();
66
 
    BooleanQuery filterQuery = null;
67
 
 
68
 
    for (BooleanClause c : original.clauses()) {
69
 
 
70
 
/***
71
 
System.out.println("required="+c.required);
72
 
System.out.println("boost="+c.query.getBoost());
73
 
System.out.println("isTermQuery="+(c.query instanceof TermQuery));
74
 
if (c.query instanceof TermQuery) {
75
 
 System.out.println("term="+((TermQuery)c.query).getTerm());
76
 
 System.out.println("docFreq="+searcher.docFreq(((TermQuery)c.query).getTerm()));
77
 
}
78
 
***/
79
 
      Query q = c.getQuery();
80
 
      if (c.isRequired()                              // required
81
 
          && q.getBoost() == 0.0f           // boost is zero
82
 
          && q instanceof TermQuery         // TermQuery
83
 
          && (searcher.docFreq(((TermQuery)q).getTerm())
84
 
              / (float)searcher.maxDoc()) >= threshold) { // check threshold
85
 
        if (filterQuery == null)
86
 
          filterQuery = new BooleanQuery();
87
 
        filterQuery.add(q, BooleanClause.Occur.MUST);    // filter it
88
 
//System.out.println("WooHoo... qualified to be hoisted to a filter!");
89
 
      } else {
90
 
        query.add(c);                             // query it
91
 
      }
92
 
    }
93
 
 
94
 
    Filter filter = null;
95
 
    if (filterQuery != null) {
96
 
      synchronized (cache) {                      // check cache
97
 
        filter = (Filter)cache.get(filterQuery);
98
 
      }
99
 
      if (filter == null) {                       // miss
100
 
        filter = new CachingWrapperFilter(new QueryWrapperFilter(filterQuery)); // construct new entry
101
 
        synchronized (cache) {
102
 
          cache.put(filterQuery, filter);         // cache it
103
 
        }
104
 
      }        
105
 
    }
106
 
 
107
 
    // YCS: added code to pass out optimized query and filter
108
 
    // so they can be used with Hits
109
 
    if (queryOut != null && filterOut != null) {
110
 
      queryOut[0] = query; filterOut[0] = filter;
111
 
      return null;
112
 
    } else {
113
 
      return searcher.search(query, filter, numHits);
114
 
    }
115
 
 
116
 
  }
117
 
}