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
9
* http://www.apache.org/licenses/LICENSE-2.0
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.
18
package org.apache.solr.search;
20
/* Copyright (c) 2003 The Nutch Organization. All rights reserved. */
21
/* Use subject to the conditions in http://www.nutch.org/LICENSE.txt. */
24
import org.apache.lucene.search.*;
26
import java.util.LinkedHashMap;
28
import java.io.IOException;
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
40
private float threshold;
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
47
public LuceneQueryOptimizer(final int cacheSize, float threshold) {
48
this.cache = new LinkedHashMap(cacheSize, 0.75f, true) {
50
protected boolean removeEldestEntry(Map.Entry eldest) {
51
return size() > cacheSize; // limit size of cache
54
this.threshold = threshold;
57
public TopDocs optimize(BooleanQuery original,
65
BooleanQuery query = new BooleanQuery();
66
BooleanQuery filterQuery = null;
68
for (BooleanClause c : original.clauses()) {
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()));
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!");
90
query.add(c); // query it
95
if (filterQuery != null) {
96
synchronized (cache) { // check cache
97
filter = (Filter)cache.get(filterQuery);
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
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;
113
return searcher.search(query, filter, numHits);