1. lucene中主要的類
1.1. Document文檔類
1.1.1.常用方法
方法
描述
void add(Field field)
往Document對象中添加字段
void removeField(String name)
刪除字段。若多個字段以同一個字段名存在,則刪除首先添加的字段;若不存在,則Document保持不變
void removeFields(String name)
刪除所有字段。若字段不存在,則Document保持不變
Field getField(String name)
若多個字段以同一個字段名存在,則返回首先添加的字段;若字段不存在,則Document保持不變
Enumeration fields()
返回Document對象的所有字段,以枚舉類型返回
Field [] getFields(String name)
根據名稱得到一個Field的數組
String [] getValues(String name)
根據名稱得到一個Field的值的數組
1.1.2.示例
Document doc1 = new Document();
doc1.add(new Field("name", "word1 word2 word3",
Field.Store.NO,Field.Index.TOKENIZED));
Document doc2 = new Document();
doc2.add(new Field("name", "word1 word2 word3",
Field.Store.NO,Field.Index.TOKENIZED));
1.2. Field字段類
1.2.1.構造方法
1) public Field(String name,String value,Store store,Index index);//直接的字符串方式
2) public Field(String name,String value,Store store,Index index,TermVector termVector);
3) public Field(String name,String value,Reader reader);//使用Reader從外部傳入
4) public Field(String name,String value,Reader reader,TermVector termVector);
5) public Field(String name,byte[] value,Store store)//使用直接的二進制byte傳入
當Field值為二進制時,可以使用Lucene的壓縮功能將其值進行壓縮。
1.2.2.Store類
靜態屬性
描述
Store.NO
表示該Field不需要存儲
Store.YES
表示該Field需要存儲
Store.COMPRESS
表示用壓縮方式來保存這個Field的值
1.2.3.Index類
靜態屬性
描述
Index.NO
不需要索引
Index.TOKENIZED
先被分詞再被索引
Index.UN_TOKENIZED
不對該Field進行分詞,但會對它進行索引
Index.NO_NORMS
對該Field進行索引,但是不使用Analyzer,同時禁止它參加評分,主要是為了減少內存的消耗。
1.2.4.示例
new Field("name", "word1 word2 word3",Field.Store.YES,Field.Index.TOKENIZED)
1.3. IndexWriter類
1.3.1.構造方法
1) public IndexWriter(String path,Analyzer a,Boolean create)
2) public IndexWriter(File path,Analyzer a,Boolean create)
3) public IndexWriter(Directory d,Analyzer a,Boolean create)
第一個參數:索引存放在什么地方
第二個參數:分析器,繼承自org.apache.lucene.analysis.Analyzer類
第三個參數:為true時,IndexWriter不管目錄內是否已經有索引了,一律清空,重新建立;當為false時,則IndexWriter會在原有基礎上增量添加索引。所以在更新的過程中,需要設置該值為false。
1.3.2.添加文檔
public void addDocument(Document doc)
public void addDocument(Document doc,Analyzer analyzer)//使用一個開發者自定義的,而非事先在構建IndexWriter時聲明的Analyzer來進行分析
writer.addDocument(doc1);
1.3.3.性能參數
1) mergeFactor控制Lucene在把索引從內存寫入磁盤上的文件系統時內存中最大的Document數量,同時它還控制內存中最大的Segment數量。默認為10.
writer.setMergeFactor(10);
2) maxMergeDocs限制一個Segment中最大的文檔數量。一個較大的maxMergeDocs適用于對大批量的文檔建立索引,增量式的索引則應使用較小的maxMergeDocs。
writer.setMaxMergeDocs(1000);
3) minMergeDocs用于控制內存中持有的文檔數量的,它對磁盤上的Segment大小沒有任何影響。
1.3.4.限制Field的長度
maxFieldLength限制Field的長度,默認值為10000.最大值100000個。
public void setMaxFieldLength(int maxFieldLength)
writer.addDocument(doc1);
writer.setMaxFieldLength(100000);
writer.addDocument(doc2);
1.3.5.復合索引格式
setUseCompoundFile(Boolean) 默認true
writer.setUseCompoundFile(true);//復合索引
writer.setUseCompoundFile(false);
1.3.6.優化索引
writer.optimize();
將磁盤上的多個segment進行合并,組成一個全新的segment。這種方法并不會增加建索時的速度,反而會降低建索的速度。所以應該在建完索引后在調用這個函數
1.3.7.示例
IndexWriter writer = new IndexWriter(path, new StandardAnalyzer(), true);
writer.addDocument(doc1);
writer.addDocument(doc2);
Sytem.out.println(writer.docCount());
writer.close();
IndexSearcher searcher = new IndexSearcher(path);
Hits hits = null;
Query query = null;
QueryParser parser =new QueryParser("name", new StandardAnalyzer());
query =parser.parse("word1");
hits = searcher.search(query);
System.out.println("查找 word1 共" + hits.length() + "個結果");
1.4. Directory類
Directory:用于索引的存放位置
a) FSDirectory.getDirectory(path, true)第二個參數表示刪除掉目錄內原有內容
IndexWriter writer = new IndexWriter(FSDirectory.getDirectory(path, true), new StandardAnalyzer(), true);//刪除原有索引
或
FSDirectory fsDir=FSDirectory.getDirectory(path,true);
IndexWriter writer = new IndexWriter(fsDir, new StandardAnalyzer(), true);
b) RAMDirectory在內存中存放,讀取速度快,但程序一運行結束,它的內容就不存在了
RAMDirectory ramDir=new RAMDirectory();
IndexWriter writer = new IndexWriter(ramDir, new StandardAnalyzer(), true);
或
IndexWriter writer = new IndexWriter(new RAMDirectory(), new StandardAnalyzer(), true);
1.5. IndexReader類
IndexReader類――索引的讀取工具
1.5.1.刪除文檔
IndexReader reader=IndexReader.open(path);
reader.deleteDocument(0);//刪除第一個
reader.close();
1.5.2.反刪除
reader.undeleteAll();
1.5.3.按字段刪除
reader.deleteDocuments(new Term("name","word1"));
若要真正物理刪除,則只需使用IndexWriter對索引optimize一次即可!
1.5.4.示例
IndexReader reader=IndexReader.open(path);
for(int i=0;i<reader.numDocs();i++){
System.out.println(reader.document(i));
}
System.out.println("版本:"+reader.getVersion());
System.out.println("索引內的文檔數量:"+reader.numDocs());
//reader.deleteDocuments(new Term("name","word1"));
Term term1=new Term("name","word1");
TermDocs docs=reader.termDocs(term1);
while(docs.next())
{
System.out.println("含有所查找的"+term1+"的Document的編號為"+docs.doc());
System.out.println("Term在文檔中的出現次數"+docs.freq());
}
reader.close();
1.6. IndexModifier類
集成了IndexWriter的大部分功能和IndexReader中對索引刪除的功能 ------ Lucene2.0的新類
1.6.1.示例
public static void main(String[] args) throws Exception {
IndexModifier modifier=new IndexModifier("C:\\Q1",new StandardAnalyzer(),true);
Document doc1=new Document();
doc1.add(new Field("bookname","鋼鐵是怎樣煉成的",Field.Store.YES,Field.Index.TOKENIZED));
Document doc2=new Document();
doc2.add(new Field("bookname","山山水水",Field.Store.YES,Field.Index.TOKENIZED));
modifier.addDocument(doc1);
modifier.addDocument(doc2);
System.out.println(modifier.docCount());
modifier.setUseCompoundFile(false);
modifier.close();
IndexModifier mo=new IndexModifier("C:\\Q1",new StandardAnalyzer(),false);
mo.deleteDocument(0);
System.out.println(mo.docCount());
mo.close();
}
1.7. IndexSearcher類
1.7.1.構造方法
IndexSearcher searcher = new IndexSearcher(String path);
IndexSearcher searcher = new IndexSearcher(Directory directory);
IndexSearcher searcher = new IndexSearcher(IndexReader r);
IndexSearcher searcher = new IndexSearcher(IndexReader r,Boolean closeReader);
IndexSearcher searcher = new IndexSearcher(path);
IndexSearcher searcher = new IndexSearcher(FSDirectory.getDirectory(path,false) );
1.7.2.search方法
//返回Hits對象
public Hits search(Query query)
public Hits search(Query query,Filter filter)
public Hits search(Query query,Sort sort)
public Hits search(Query query,Filter filter,Sort sort)
//檢索只返回得分最高的Document
public TopDocs search(Query query,Filter filter,int n)
public TopDocs search(Weight weight,Filter filter,int n)
public TopFieldDocs search(Weight weight,Filter filter,int n,Sort sort)
public TopFieldDocs search(Query query,Filter filter,int n,Sort sort)
//傳入HitCollector,將結果保存在HitCollector中
public void search(Query query,HitCollector results)
public void search(Query query,Filter filter,HitCollector results)
public void search(Weight weight,Filter filter,HitCollector results)
1.7.3.Searcher的explain方法
public Explaination explain(Query query,int doc)throws IOException
for(int i=0;i<hits.length()&&i<10;i++)
{
Document d=hits.doc(i);
System.out.println(i+" "+hits.score(i)+" "+d.get("contents"));
System.out.println(searcher.explain(query,hits.id(i)).toString());
}
1.7.4.示例
IndexSearcher searcher = new IndexSearcher(path);
Hits hits = null;
Query query = null;
QueryParser parser =new QueryParser("contents", new StandardAnalyzer());
query =parser.parse("11");
hits = searcher.search(query);
System.out.println("查找 word1 共" + hits.length() + "個結果");
for(int i=0;i<hits.length()&&i<10;i++)
{
Document d=hits.doc(i);
System.out.println(d+" "+i+" "+hits.score(i)+" "+d.get("contents"));
}
searcher.close();
1.8. Hits類
1.8.1.概述
Hits類――檢索結果
1.8.2.常用方法
方法名
描述
int length()
返回搜索到結果的總數量
Document doc(int i)
返回第i個文檔
int id(int i)
返回第i個文檔的內部ID號
float score(int i)
返回第i個文檔的得分
Iterator iterator()
取得Hits集合的遍歷對象
1.8.3.示例
for(int i=0;i<hits.length()&&i<10;i++)
{
Document d=hits.doc(i);
System.out.println(d+" "+" "+hits.score(i)+" "+d.get("contents"));
System.out.println("文檔的內部ID號:" + hits.id(i));
}
1.9. QueryParser類
1.9.1.改變默認的布爾邏輯
Ø 默認為“或”關系
Query query = null;
QueryParser parser =new QueryParser("contents", new StandardAnalyzer());
query =parser.parse("hello world!");
System.out.println(query.toString());
Ø 改變默認布爾邏輯
Query query = null;
QueryParser parser =new QueryParser("contents", new StandardAnalyzer());
parser.setDefaultOperator(QueryParser.AND_OPERATOR);
query =parser.parse("hello world");//若world后加!會出錯
System.out.println(query.toString());
Ø AND OR NOT – 關鍵字
也可以不用改變默認布爾邏輯,而直接讓用戶在輸入關鍵字時指定不同詞條間的布爾聯系。例如,用戶輸入 hello AND world 必須為大寫
邏輯與:AND (大寫)
邏輯或:OR (大寫)
邏輯非:- 例如: hello - world
也可以是NOT 例如: hello NOT world
1.9.2.不需要分詞
不進行分詞,將其完整的作為一個詞條進行處理,則需要在詞組的外面加上引號
String queryStr="\"God helps those who help themselves\"";
QueryParser parser = new QueryParser("bookname",new StandardAnalyzer());
parser.setDefaultOperator(QueryParser.AND_OPERATOR);
Query query=parser.parse(queryStr);
System.out.println(query.toString());
1.9.3.設置坡度值,支持FuzzyQuery
String queryStr="\"God helps those who help themselves\"~1";//設置坡度為1
QueryParser parser = new QueryParser("bookname",new StandardAnalyzer());
Query query=parser.parse(queryStr);
System.out.println(query.toString());
1.9.4.設置通配符,支持WildcardQuery
String queryStr="wor?"
QueryParser parser = new QueryParser("bookname",new StandardAnalyzer());
parser.setDefaultOperator(QueryParser.AND_OPERATOR);
Query query=parser.parse(queryStr);
System.out.println(query.toString());
1.9.5.查找指定的Field
String queryStr="linux publishdate:2006-09-01";
QueryParser parser = new QueryParser("bookname",new StandardAnalyzer());
parser.setDefaultOperator(QueryParser.AND_OPERATOR);
Query query=parser.parse(queryStr);
System.out.println(query.toString());
例如:要求用戶選擇某一方面的
1.9.6.范圍的查找,支持RangeQuery
String queryStr="[1990-01-01 TO 1998-12-31]";
QueryParser parser=new QueryParser("publishdate",
new StandardAnalyzer());
Query query=parser.parse(queryStr);
System.out.println(query.toString());
輸出結果為publishdate:[081xmghs0 TO 0boeetj3z]
因為建立索引時,如果按照日期表示的字符串來進行索引,實際上比較的是字符串的字典順序。而首先將日期轉為以毫秒計算的時間后,則可以精確地比較兩個日期的大小了。于是,lucene提供DateTools工具,用來完成其內部對時間的轉化和處理,將毫秒級的時間轉化為一個長字符串來進行表示,并進行索引。所以,遇到日期型數據時,最好用DateTools進行轉換,再進行索引!
1.9.7.現在還不支持SpanQuery
1.10. MultiFieldQueryParser類--多域搜索
//在不同的Field上進行不同的查找
public static Query parse(String []queries,String[] fields,Analyzer analyzer)throws ParseException
//在不同的Field上進行同一個查找,指定它們之間的布爾關系
public static Query parse(String query,String[] fields,BooleanClause.Occur[] flags,Analyzer analyzer) throws ParseException
//在不同的Field上進行不同的查找,指定它們之間的布爾關系
public static Query parse(String []queries,String [] fields,BooleanClause.Occur[] flags,Analyzer analyzer)throws ParseException
String [] queries={"鋼", "[10 TO 20]"};
String[] fields={“bookname”,”price”};
BooleanClause.Occur[] clauses={BooleanClause.Occur.MUST,BooleanClause.Occur.MUST};
Query query=MultiFieldQueryParser.parse(queries,fields,clauses,new StandardAnalyzer());
System.out.println(query.toString());
1.11. MultiSearcher類--多個索引搜索
IndexSearcher searcher1=new IndexSearcher(path1);
IndexSearcher searcher2=new IndexSearcher(path2);
IndexSeacher [] searchers={searcher1,seacher2};
MultiSearcher searcher=new MultiSearcher(searchers);
Hits hits=searcher.search(query);
for(int i=0;i<hits.length();i++){
System.out.println(hits.doc(i));
}
1.12. ParalellMultiSearcher類---多線程搜索
IndexSearcher searcher1=new IndexSearcher(path1);
IndexSearcher searcher2=new IndexSearcher(path2);
IndexSearcher [] searchers={searcher1,searcher2};
ParallelMultiSearcher searcher=new ParallelMultiSearcher(searchers);
long start=System.currentTimeMillis();
Hits hits=searcher.search(query);
long end=System.currentTimeMillis();
System.out.println((end-start)+"ms");
本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/xiaoping8411/archive/2010/03/23/5409953.aspx