1. 排序
1.1. Sort类
public Sort()
public Sort(String field)
public Sort(String field,Boolean reverse) //默认为false,降序排序
public Sort(String[] fields)
public Sort(SortField field)
public Sort(SortField[] fields)
Sort sort=new Sort(“bookname”);按照“bookname“这个Field值进行降序排序
Sort sort=new Sort(“bookname”,true) //升序排序
Sort sort=new Sort(new String[]{“bookNumber”,”bookname”,”publishdate”});按照三个Field进行排序,但无法指定升序排序,所以用SortField
1.2. SortField类
public SortField(String field)
public SortField(String field,Boolean reverse)
public SortField(String field,int type) //type表示当前Field值的类型
public SortField(String field,int type,boolean reverse) //默认为false,升序
Field值的类型:SortField.STRING、SortField.INT、SortField.FLOAT
SortField sf1=new SortField(“bookNumber”,SortField.INT,false);
SortField sf2=new SortField(“bookname”,SortField.STRING,false);
1.3. 指定排序的法则
1.3.1.按照文档的得分降序排序
Hits hits=searcher.search(query,Sort.RELEVANCE);
1.3.2.按文档的内部ID升序排序
Hits hits=searcher.search(query, Sort.INDEXORDER);
1.3.3.按照一个Field来排序
Sort sort=new Sort();
SortField sf=new SortField(“bookNumber”,SortField.INT,false);
sort.setSort(sf);
Hits hits=searcher.search(query,sort);
1.3.4.按照多个Field来排序
Sort sort=new Sort();
SortField sf1=new SortField(“bookNumber”,SortField.INT,false);//升序
SortField sf2=new SortField(“publishdate”,SortField.STRING,true);//降序
sort.setSort(new SortField[]{sf1,sf2});
Hits hits=searcher.search(query,sort);
1.3.5.改变SortField中的Locale信息
String str1=”我”; String str2=”你”;
Collator co1=Collator.getInstance(Locale.CHINA);
Collator co2=Collator.getInstance(Locale.JAPAN);
System.out.println(Locale.CHINA+”:”+co1.compare(str1,str2));
System.out.println(Locale.JAPAN+”:”+co2.compare(str1,str2));
输出结果为:
zh_CN:1
ja_JP:-1
所以
public SortField(String field,Locale locale)
public SortField(String field,Locale locale,boolean reverse)
2. 过滤器
使用public Hits search(Query query,Filter filter)
(1)简单过滤
Hits hits=searcher.search(query,new AdvancedSecurityFilter());//过滤掉securitylevel为0的结果
(2)范围过滤—RangeFilter
只显示中间的
RangeFilter filter=new RangeFilter(“publishdate”,”1970-01-01”,”1998-12-31”,true,true”);
Hits hits=searcher.search(query,filter);
无上边界
public static RangeFilter More(String fieldname,String lowerTerm)
无下边界
public static RangeFilter Less(String fieldname,String upperTerm)
(3)在结果中查询QueryFilter
RangeQuery q=new RangeQuery(new Term(“publicshdate”,”1970-01-01”),
new Term(“publishdate”,”1999-01-01”),true);
QueryFilter filter=new QueryFilter(q);
Hits hits=searcher.search(query,filter);
3. 分析器Analysis
3.1. 自带分析器和过滤器
Ø 标准过滤器:StandardAnalyzer
Ø 大小写转换器:LowerCaseFilter
Ø 忽略词过滤器:StopFilter
public StopFilter(TokenStream input,String [] stopWords)
public StopFilter(TokenStream in,String [] stopWords,boolean ignoreCase)
public StopFilter(TokenStream input,Set stopWords,boolean ignoreCase)
public StopFilter(TokenStream in, Set stopWords)
其中,参数TokenStream代表当前正在进行处理的流;String类型的数组代表一个用数组表示的忽略词集合;Set类型的参数与String一样,是用来表示忽略词集合的;boolean表示当与忽略词集合中的词进行匹配时,是否需要忽略大小写。
Ø 长度过滤器:LengthFilter
Ø PerFieldAnalyzerWrapper
Ø WhitespaceAnalyzer
String str="str1 str2 str3";
StringReader reader=new StringReader(str);
Analyzer anlyzer=new WhitespaceAnalyzer();
TokenStream ts=anlyzer.tokenStream("", reader);
Token t=null;
while( (t=ts.next())!=null ){
System.out.println(t.termText());
}
3.2. 第三方过分析器
Ø 单字分词
Ø 二分法:CJKAnalyzer、中科院ICTCLAS分词、JE分词
Ø 词典分词
3.2.1.JE分词用法
3.2.1.1. 示例
import jeasy.analysis.MMAnalyzer;
IndexWriter writer = new IndexWriter(INDEX_STORE_PATH, new MMAnalyzer()
, true);
String str=" Lucene是一个全文检索引擎的架构,"+
"提供了完整的查询引擎和索引引擎。Lucene以其方便使用、快" +
"速实施以及灵活性受到广泛的关注。它可以方便地嵌入到各种应用" +
"中实现针对应用的全文索引、检索功能,本总结使用lucene--2.3.2。";
MMAnalyzer analyzer=new MMAnalyzer();
try{
System.out.println(analyzer.segment(str, "|"));
}
catch(Exception e)
{
e.printStackTrace();
}
输出结果:lucene|一个|全文|检索|引擎|架构|提供|完整|查询|。。。。
3.2.1.2. 设定正向最大匹配的字数
MMAnalyzer analyzer=new MMAnalyzer(4);
3.2.1.3. 添加新词
MMAnalyzer.addWord(String word);
MMAnalyzer.addDictionary(Reader reader);
MMAnalyzer analyzer=new MMAnalyzer();
MMAnalyzer.addWord("迈克尔雷第");
4. 索引的合并
RAMDirectory RAMDir=new RAMDirectory();
IndexWriter writer = new IndexWriter(RAMDir, new StandardAnalyzer(), true);//删除原有索引
IndexWriter writer2=new IndexWriter(FSDirectory.getDirectory(path,true),
new StandardAnalyzer(), true);
writer.addDocument(doc1);
writer2.addDocument(doc2);
writer.close();
writer2.addIndexes(new Directory[]{RAMDir});
writer2.close();