...省略索引创建的步骤,如果不懂请看http://my.oschina.net/kkrgwbj/blog/513329
lucene提供的查询方式有很多,api中列出的几种查询方式如下图:
在这里,我只简简单单的举例以下几种查询
创建Directory,IndexSearcher
package com.kkrgwbj.index; import java.io.File; import java.io.IOException; import java.util.Calendar; import org.apache.lucene.document.Document; import org.apache.lucene.index.DirectoryReader; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.Term; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.Query; import org.apache.lucene.search.ScoreDoc; import org.apache.lucene.search.TermQuery; import org.apache.lucene.search.TermRangeQuery; import org.apache.lucene.search.TopDocs; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; import org.apache.lucene.util.BytesRef; public class SearchUtil { private Directory directory; private IndexReader indexReader; public SearchUtil() { try { directory = FSDirectory.open(new File("D:/workspace/lucene/index02").toPath()); } catch (IOException e) { e.printStackTrace(); } } /** * 创建IndexSearcher * * @return */ public IndexSearcher getSearcher() { if (indexReader == null) { try { indexReader = DirectoryReader.open(directory); } catch (IOException e) { e.printStackTrace(); } } else { try { IndexReader tr = DirectoryReader.openIfChanged((DirectoryReader) indexReader); if (tr != null) { indexReader.close(); indexReader = tr; } } catch (IOException e) { e.printStackTrace(); } } return new IndexSearcher(indexReader); }
/** * 根据固定的term查询 * * @param field * @param name */ public void searchByTerm(String field, String name) { // 创建IndexSearch IndexSearcher indexSearcher = getSearcher(); Query query = new TermQuery(new Term(field, name)); try { TopDocs tds = indexSearcher.search(query, 10); System.out.println("一共查询了:" + tds.totalHits); for (ScoreDoc item : tds.scoreDocs) { Document doc = indexSearcher.doc(item.doc); Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(Long.valueOf(doc.get("date"))); System.out.println("名字:" + doc.get("name") + "邮箱:" + doc.get("email") + "id:" + doc.get("id") + "附件个数:" + doc.get("attach") + "日期:" + cal.getTime().toString()); } } catch (IOException e) { e.printStackTrace(); } finally { if (indexReader != null) try { indexReader.close(); } catch (IOException e) { e.printStackTrace(); } } }
/** * 范围查询 * * @param field * 查询的域 * @param start * 查询的开始 * @param end * 查询的结束 * @param num * 返回的行数 */ public void searchByTermRange(String field, String start, String end, int num) { IndexSearcher indexSearcher = getSearcher(); Query query = new TermRangeQuery(field, new BytesRef(start), new BytesRef(end), true, true); try { TopDocs tds = indexSearcher.search(query, 10); System.out.println("一共查询了:" + tds.totalHits); for (ScoreDoc item : tds.scoreDocs) { Document doc = indexSearcher.doc(item.doc); Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(Long.valueOf(doc.get("date"))); System.out.println("名字:" + doc.get("name") + "邮箱:" + doc.get("email") + "id:" + doc.get("id") + "附件个数:" + doc.get("attach") + "日期:" + cal.getTime().toString()); } } catch (Exception e) { e.printStackTrace(); } finally { if (indexReader != null) try { indexReader.close(); } catch (IOException e) { e.printStackTrace(); } } }
/** * 数字范围的查询 * @param field * @param start * @param end * @param num */ public void searchByNumericRange(String field,int start,int end,int num){ IndexSearcher indexSearcher = getSearcher(); Query query = NumericRangeQuery.newIntRange(field, start, end,true,true); try { TopDocs tds = indexSearcher.search(query, 10); System.out.println("一共查询了:" + tds.totalHits); for (ScoreDoc item : tds.scoreDocs) { Document doc = indexSearcher.doc(item.doc); Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(Long.valueOf(doc.get("date"))); System.out.println("名字:" + doc.get("name") + "邮箱:" + doc.get("email") + "id:" + doc.get("id") + "附件个数:" + doc.get("attach") + "日期:" + cal.getTime().toString()); } } catch (Exception e) { e.printStackTrace(); } finally { if (indexReader != null) try { indexReader.close(); } catch (IOException e) { e.printStackTrace(); } } }
/** * 前缀搜索 * @param field * @param value * @param num */ public void searchByPrefix(String field,String value,int num){ IndexSearcher indexSearcher = getSearcher(); Query query = new PrefixQuery(new Term(field, value)); try { TopDocs tds = indexSearcher.search(query, 10); System.out.println("一共查询了:" + tds.totalHits); for (ScoreDoc item : tds.scoreDocs) { Document doc = indexSearcher.doc(item.doc); Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(Long.valueOf(doc.get("date"))); System.out.println("名字:" + doc.get("name") + "邮箱:" + doc.get("email") + "id:" + doc.get("id") + "附件个数:" + doc.get("attach") + "日期:" + cal.getTime().toString()); } } catch (Exception e) { e.printStackTrace(); } finally { if (indexReader != null) try { indexReader.close(); } catch (IOException e) { e.printStackTrace(); } } }
public void searchByBoolean(int num) { IndexSearcher indexSearcher = getSearcher(); BooleanQuery.Builder booleanBuild = new BooleanQuery.Builder(); // 名字中含有mick,并且内容含有game的 /** * BooleanQuery可以连接多个子查询 * Occur.MUST:必须出现(相当于数据库的and) * Occur.SHOULD:表示可以出现(相当于数据库的or) * Occur.MUSTNOT:不能出现(相当于数据库的not) */ booleanBuild.add(new TermQuery(new Term("name", "mick")), Occur.MUST); booleanBuild.add(new TermQuery(new Term("content", "game")), Occur.MUST); BooleanQuery booleanQuery = booleanBuild.build(); try { TopDocs tds = indexSearcher.search(booleanQuery, 10); System.out.println("一共查询了:" + tds.totalHits); for (ScoreDoc item : tds.scoreDocs) { Document doc = indexSearcher.doc(item.doc); Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(Long.valueOf(doc.get("date"))); System.out.println("名字:" + doc.get("name") + "邮箱:" + doc.get("email") + "id:" + doc.get("id") + "附件个数:" + doc.get("attach") + "日期:" + cal.getTime().toString()); } } catch (Exception e) { e.printStackTrace(); } finally { if (indexReader != null) try { indexReader.close(); } catch (IOException e) { e.printStackTrace(); } } }