📄 simplelucenesearcher.java
字号:
/*
* Copyright 2004 JavaFree.org
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.javabb.lucene.search;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.javabb.infra.Monitor;
import org.springframework.core.io.Resource;
/**
* @author <a href="mailto:jackganzha@dev.java.net">Marcos Silva Pereira</a>
*
* @version $Id: SimpleLuceneSearcher.java,v 1.4.8.3 2007/11/22 03:06:45 daltoncamargo Exp $
*/
public class SimpleLuceneSearcher implements LuceneSearcher {
private Directory path;
private Analyzer analyzer;
private static Object monitor = Monitor.MONITOR;
/**
* @param path
* @param analyzer
*/
public SimpleLuceneSearcher ( Directory path, Analyzer analyzer ) {
this.path = path;
this.analyzer = analyzer;
}
/**
* @param path
* @param analyzer
* @throws IOException
*/
public SimpleLuceneSearcher ( Resource path, Analyzer analyzer ) throws IOException {
this(FSDirectory.getDirectory(path.getFile(), false), analyzer);
}
/**
* Simple search
*/
public List search(String query, String[] fields) {
List result = new ArrayList();
try {
synchronized (monitor) {
IndexSearcher searcher = new IndexSearcher(path);
Query queryObj;
BooleanClause.Occur[] flags = {BooleanClause.Occur.SHOULD,
BooleanClause.Occur.SHOULD};
queryObj = MultiFieldQueryParser.parse(query, fields, flags, analyzer);
Hits hits = searcher.search(queryObj);
for (int i = 0; i < hits.length(); i++) {
Document document = hits.doc(i);
String idValue = document.getField("postId").stringValue();
long postId = Long.parseLong(idValue);
result.add(new Long(postId));
}
searcher.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* @see org.javabb.lucene.search.LuceneSearcher#search(java.lang.String)
*/
public List search(String query[], String[] fields) {
List result = new ArrayList();
try {
synchronized (monitor) {
IndexSearcher searcher = new IndexSearcher(path);
Query queryObj;
BooleanClause.Occur[] flags = {BooleanClause.Occur.SHOULD,
BooleanClause.Occur.SHOULD,
BooleanClause.Occur.SHOULD};
queryObj = MultiFieldQueryParser.parse(query, fields, flags, analyzer);
Hits hits = searcher.search(queryObj);
for (int i = 0; i < hits.length(); i++) {
Document document = hits.doc(i);
String idValue = document.getField("postId").stringValue();
long postId = Long.parseLong(idValue);
long forumId = new Long(document.getField("forumId").stringValue()).longValue();
long forumByParameterId = new Long(query[2]).longValue();
if(forumId == forumByParameterId){
result.add(new Long(postId));
}
}
searcher.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -