01: /*
02: * Copyright 2004-2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.compass.core.lucene.engine;
18:
19: import java.io.IOException;
20: import java.util.Comparator;
21: import java.util.PriorityQueue;
22:
23: import org.apache.lucene.index.Term;
24: import org.apache.lucene.index.TermEnum;
25: import org.compass.core.CompassTermFreq;
26: import org.compass.core.engine.SearchEngineException;
27: import org.compass.core.engine.SearchEngineTermFrequencies;
28: import org.compass.core.impl.DefaultCompassTermFreq;
29:
30: /**
31: * @author kimchy
32: */
33: public class LuceneSearchEngineTermFrequencies implements
34: SearchEngineTermFrequencies {
35:
36: private CompassTermFreq[] termFreqs;
37:
38: public LuceneSearchEngineTermFrequencies(String[] propertyNames,
39: int size, LuceneSearchEngineInternalSearch internalSearch)
40: throws SearchEngineException {
41:
42: if (internalSearch.isEmpty()) {
43: termFreqs = new CompassTermFreq[0];
44: return;
45: }
46:
47: PriorityQueue<CompassTermFreq> queue = new PriorityQueue<CompassTermFreq>(
48: 10, new Comparator<CompassTermFreq>() {
49: public int compare(CompassTermFreq a,
50: CompassTermFreq b) {
51: return (int) (b.getFreq() - a.getFreq());
52: }
53: });
54:
55: for (int i = 0; i < propertyNames.length; i++) {
56: String propertyName = propertyNames[i];
57: TermEnum termEnum = null;
58: try {
59: termEnum = internalSearch.getReader().terms(
60: new Term(propertyName, ""));
61: while (termEnum.term() != null
62: && propertyName.equals(termEnum.term().field())) {
63: queue.add(new DefaultCompassTermFreq(termEnum
64: .term().text(), termEnum.docFreq(),
65: propertyName));
66: if (!termEnum.next()) {
67: break;
68: }
69: }
70: } catch (IOException e) {
71: throw new SearchEngineException(
72: "Failed to get term freq for proeprty ["
73: + propertyName + "]", e);
74: } finally {
75: if (termEnum != null) {
76: try {
77: termEnum.close();
78: } catch (IOException e) {
79: // do nothing here, maybe warn?
80: }
81: }
82: }
83: }
84: int retSize = size;
85: if (queue.size() < size) {
86: retSize = queue.size();
87: }
88: termFreqs = new CompassTermFreq[retSize];
89: for (int i = 0; i < termFreqs.length; i++) {
90: termFreqs[i] = (CompassTermFreq) queue.poll();
91: }
92: }
93:
94: public CompassTermFreq[] getTerms() {
95: return termFreqs;
96: }
97:
98: }
|