import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;
/**
* 测试IndexSearch检索时采用了QueryParser与不采用QueryParser的区别
* 即在检索时使用分析器给检索结果带来的影响
* @author USER
*
*/
public class TestQueryParser4Search {
public static void main(String[] args) throws Exception {
/**
* 建索引
*/
createIndex();
/**
* 检索时没有采用分析器
*/
search4NotAnalyzer();
/**
* 检索时也采用了分析器
*/
search4Analyzer();
}
/**
* 建索引
* @throws Exception
*/
public static void createIndex() throws Exception {
Document doc1 = new Document();
Field field = null;
field = new Field("name", "中国建设银行", Field.Store.YES,
Field.Index.UN_TOKENIZED);
doc1.add(field);
field = new Field("title", "doc1", Field.Store.YES, Field.Index.TOKENIZED);
doc1.add(field);
Document doc2 = new Document();
field = new Field("name", "中国建设银行", Field.Store.YES,
Field.Index.TOKENIZED);
doc2.add(field);
field = new Field("title", "doc1", Field.Store.YES, Field.Index.TOKENIZED);
doc2.add(field);
Document doc3 = new Document();
field = new Field("name", "中国建设银行,深发银行,广东发展银行", Field.Store.YES,
Field.Index.TOKENIZED);
doc3.add(field);
field = new Field("title", "doc3 doc1", Field.Store.YES, Field.Index.TOKENIZED);
doc3.add(field);
IndexWriter writer = new IndexWriter("c://java//index", new StandardAnalyzer(), true);
writer.addDocument(doc1);
writer.addDocument(doc2);
writer.addDocument(doc3);
writer.close();
}
/**
* 检索时也采用了分析器
* QueryParser:会对查询表达式中的每个项及其短语进行分析
* @throws Exception
*
*/
public static void search4Analyzer() throws Exception{
BooleanQuery bquery1 = null;
Hits hits = null;
IndexSearcher searcher = new IndexSearcher("c://java//index");
//查询 +name:"中 国 建 设 银 行" +title:doc1 找到2个结果 doc1 doc3 doc1
/**
* 注意这里只是为了演示而采用了Lucene内置的StandardAnalyzer分析器,当开发时要采用你建索引时的分析器
*/
Analyzer analyzer = new StandardAnalyzer();
QueryParser parser = new QueryParser("name", analyzer);
Query query1 = parser.parse("中国建设银行");
QueryParser parser2 = new QueryParser("title", analyzer);
Query query2 = parser2.parse("doc1");
// 构造布尔查询
bquery1 = new BooleanQuery();
bquery1.add(query1, BooleanClause.Occur.MUST);
bquery1.add(query2, BooleanClause.Occur.MUST);
hits = searcher.search(bquery1);
printResult(hits, bquery1.toString());
}
/**
* 检索时没有采用分析器
* @throws Exception
*
*/
public static void search4NotAnalyzer() throws Exception{
Query query1 = null;
Query query2 = null;
BooleanQuery bquery1 = null;
Hits hits = null;
IndexSearcher searcher = new IndexSearcher("c://java//index");
/* //可以检索到结果
//查询 +name:中 +title:doc1 找到2个结果 doc1 doc3 doc1
query1 = new TermQuery(new Term("name", "中"));
query2 = new TermQuery(new Term("title", "doc1"));*/
// 查询 +name:中国建设银行 +title:doc1 找到1个结果 doc1
query1 = new TermQuery(new Term("name", "中国建设银行"));
query2 = new TermQuery(new Term("title", "doc1"));
// 构造布尔查询
bquery1 = new BooleanQuery();
bquery1.add(query1, BooleanClause.Occur.MUST);
bquery1.add(query2, BooleanClause.Occur.MUST);
hits = searcher.search(bquery1);
printResult(hits, bquery1.toString());
}
/**
* 打印输出检索出的文档,并输出检索的布尔语句
* @param hits
* @param key
* @throws Exception
*/
public static void printResult(Hits hits, String key) throws Exception {
System.out.println("查询 " + key);
if (hits != null) {
if (hits.length() == 0) {
System.out.println("没有找到任何结果");
} else {
System.out.println("找到" + hits.length() + "个结果");
for (int i = 0; i < hits.length(); i++) {
Document d = hits.doc(i);
String dname = d.get("title");
System.out.print(dname + " ");
}
System.out.println();
System.out.println();
}
}
}
}