SortExtendSearch.java
上传用户:cctqzzy
上传日期:2022-03-14
资源大小:12198k
文件大小:4k
- package chapter7;
- import org.apache.lucene.index.Term;
- import org.apache.lucene.index.TermEnum;
- import org.apache.lucene.index.TermDocs;
- import org.apache.lucene.search.ScoreDoc;
- import org.apache.lucene.search.SortField;
- import org.apache.lucene.index.IndexReader;
- import org.apache.lucene.search.SortComparatorSource;
- import org.apache.lucene.search.ScoreDocComparator;
- import java.io.IOException;
- // SortExtendSearch 实现SortComparatorSource接口,用来测试自定义排序,
- // 根据索引中的不同水果历史价格变化情况,按照价格波动的大小进行排序
- public class SortExtendSearch implements SortComparatorSource {
- private float last; // 历史价格
- private float stat; // 当前价格
- private float next; // 预测价格
- // 默认构造函数
- public SortExtendSearch(){
- this.last = 0;
- this.next = 0;
- this.stat = 0;
- }
-
- // 带输入参数的构造函数
- public SortExtendSearch(float last,float stat, float next) {
- this.last = last;
- this.stat = stat;
- this.next = next;
- }
-
- /*================================================================
- * 返回ScoreDocComparator 用来实现排序功能
- *================================================================*/
- public ScoreDocComparator newComparator(IndexReader reader, String sortname)
- throws IOException {
- return new PriceScoreDocComparator(reader, sortname, last,stat, next);
- }
- /*================================================================
- * PriceScoreDocComparator 实现了ScoreDocComparator 用来对价格浮动情况进行排序
- *================================================================*/
- private static class PriceScoreDocComparator implements ScoreDocComparator {
-
- private float[] markup; // 保存每个商品历史价格波动幅度
-
- // 实现了ScoreDocComparator接口的构造函数
- public PriceScoreDocComparator(IndexReader reader,String sortname, float last,float stat, float next)
- throws IOException {
- final TermEnum enumerator = reader.terms(new Term(sortname, "")); // 索引项枚举器
-
- markup = new float[reader.maxDoc()]; // 初始化markup数组
-
- if (markup.length > 0) {
- TermDocs termDocs = reader.termDocs();
- try {
- if (enumerator.term() == null) {
- throw new RuntimeException("no terms in field " + sortname);
- }
-
- int i = 0,j = 0;
- do {
- Term term = enumerator.term(); // 取出每一个Term
- if (term.field() != sortname) // 与给定的域不符合则停止比较
- break;
- termDocs.seek(enumerator); // 定位枚举器
- while (termDocs.next()) { // 循环取枚举器内每一个TermDocs
- String[] pricedis = term.text().split(",");
- float price1 = Float.parseFloat( pricedis[0] ) - last;
- float price2 = Float.parseFloat( pricedis[1] ) - stat;
- float price3 = Float.parseFloat( pricedis[2] ) - next;
-
- // 计算价格变动幅度
- markup[termDocs.doc()] = (price3 - price2) - (price2 - price1) ;
- }
- } while (enumerator.next());
-
- } finally {
- termDocs.close();
- }
- }
- }
-
- // 定义排序的比较函数
- public int compare(ScoreDoc i, ScoreDoc j) {
- if (markup[i.doc] < markup[j.doc])
- return -1;
- if (markup[i.doc] > markup[j.doc])
- return 1;
- return 0;
- }
-
- // 返回排序的结果
- public Comparable sortValue(ScoreDoc data) {
- return new Float(markup[data.doc]);
- }
- // 指定接口的排序类型
- public int sortType() {
- return SortField.FLOAT;
- }
- }
- // 输出对象的内部变量
- public String toString() {
- return "Price makeup (" + last + "," + stat+ "," + next + ")";
- }
- // 测试方法
- public static void main(String[] args) throws IOException{
- SortExtendSearch sortext = new SortExtendSearch();
- System.out.println(sortext.toString());
- }
- }