lucene之sort

package cn.zqh.lucene.sort;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.NumberTools;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Filter;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.RangeFilter;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.SortField;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.highlight.Formatter;
import org.apache.lucene.search.highlight.Fragmenter;
import org.apache.lucene.search.highlight.Highlighter;
import org.apache.lucene.search.highlight.QueryScorer;
import org.apache.lucene.search.highlight.Scorer;
import org.apache.lucene.search.highlight.SimpleFragmenter;
import org.apache.lucene.search.highlight.SimpleHTMLFormatter;

import cn.zqh.lucene.highlight.QueryResult;

/**
zqh
*/
public class TestSort {
String indexPath ="D:\\p\\luceneDemo\\luceneIndex";
Analyzer analyzer = new StandardAnalyzer();

//相关度排序
public QueryResult search(String queryString,int firstResult,int maxResult) throws ParseException, IOException{
IndexSearcher indexSearcher = null;


//方法1.在搜索的时候指定Field的相关值 方法2.创建索引时指定Document的相关值 doc.setBoost();
String[] fields={"name","content"};
Map<String,Float> boots = new HashMap<String,Float>();
boots.put("name", 3f);//boots.put("name", 1f);默认
QueryParser queryParser = new MultiFieldQueryParser(fields,analyzer,boots);
Query query =queryParser.parse(queryString);


//2、进行查询
indexSearcher = new IndexSearcher(indexPath);
Filter filter = null;
TopDocs top =indexSearcher.search(query, filter, 10000);

int recordCount = top.totalHits;
List<Document> recordList = new ArrayList<Document>();

//准备高亮
Formatter formatter = new SimpleHTMLFormatter("<font color='red'>","</font>");
Scorer scorer = new QueryScorer(query);
Highlighter hg = new Highlighter(formatter,scorer);
Fragmenter fragmenter = new SimpleFragmenter(50);
hg.setTextFragmenter(fragmenter);



//3取出当前页的数据
int end = Math.min(firstResult+maxResult,top.totalHits);
for(int i=firstResult;i< end;i++){
ScoreDoc scoreDoc =top.scoreDocs[i];
int docSn =scoreDoc.doc;
Document doc = indexSearcher.doc(docSn);

//返回高亮后的结果,如果当前属性值中没有出现关键字,会返回null
String hc =hg.getBestFragment(analyzer,"content",doc.get("content"));

if(hc ==null){
String content =doc.get("content");
int endIndex = Math.min(50,content.length());
hc = content.substring(0,50);
}
doc.getField("content").setValue(hc);

recordList.add(doc);
}
//返回结果
return new QueryResult(recordCount,recordList);
//indexSearcher.close();
}

//自定义排序
public QueryResult search(Query query,int firstResult,int maxResult) throws ParseException, IOException{
IndexSearcher indexSearcher = null;

indexSearcher = new IndexSearcher(indexPath);

//使用过滤器
Filter filter = new RangeFilter("size", NumberTools.longToString(200), NumberTools.longToString(500), true, true);

//自定义排序
Sort sort = new Sort();
sort.setSort(new SortField("size"));//默认升序
TopDocs top =indexSearcher.search(query, filter, 10000);

int recordCount = top.totalHits;
List<Document> recordList = new ArrayList<Document>();

//准备高亮
Formatter formatter = new SimpleHTMLFormatter("<font color='red'>","</font>");
Scorer scorer = new QueryScorer(query);
Highlighter hg = new Highlighter(formatter,scorer);
Fragmenter fragmenter = new SimpleFragmenter(50);
hg.setTextFragmenter(fragmenter);



//3取出当前页的数据
int end = Math.min(firstResult+maxResult,top.totalHits);
for(int i=firstResult;i< end;i++){
ScoreDoc scoreDoc =top.scoreDocs[i];
int docSn =scoreDoc.doc;
Document doc = indexSearcher.doc(docSn);

//返回高亮后的结果,如果当前属性值中没有出现关键字,会返回null
String hc =hg.getBestFragment(analyzer,"content",doc.get("content"));

if(hc ==null){
String content =doc.get("content");
int endIndex = Math.min(50,content.length());
hc = content.substring(0,50);
}
doc.getField("content").setValue(hc);

recordList.add(doc);
}
//返回结果
return new QueryResult(recordCount,recordList);
//indexSearcher.close();
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值