测试数据:
鼠标##沃野##沃野牧马人风格USB有线游戏鼠标台式电脑笔记本发光鼠标 lol cf 专业电竞竞技背光鼠标 沃野S8无声静音鼠标黑色##59.00
鼠标##双飞燕##双飞燕(A4TECH) N-810FX 飞梭截图针光鼠 绅士哑黑##59
鼠标##雷柏##雷柏(Rapoo) M218 无线光学鼠标 黑色##34.90
鼠标##雷蛇##雷蛇(Razer) Deathadder 炼狱蝰蛇1800 DPI 游戏鼠标##149.00
鼠标##罗技##罗技(Logitech)M100r 光电鼠标 (黑色##39.00
鼠标##雷蛇##雷蛇(Razer) Abyssus 地狱狂蛇2014 游戏鼠标##179.00
一下代码分别是针对价格排序和先评分再价格排序
package com.lucene5.dream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.ansj.lucene5.AnsjAnalyzer;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.DoubleField;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.FieldType;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.DocValuesType;
import org.apache.lucene.index.IndexOptions;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.SortField;
import org.apache.lucene.search.SortField.Type;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import com.lucene5.demo.LuceneQueryTest1;
public class LuceneSortTest {
static Analyzer analyzer;
static Directory d;
static IndexWriterConfig conf;
static IndexWriter indexWriter;
final static String queryKeyWord1 = "华美";
private static final FieldType DOUBLE_FIELD_TYPE_STORED_SORTED = new FieldType();
static {
DOUBLE_FIELD_TYPE_STORED_SORTED.setTokenized(true);
DOUBLE_FIELD_TYPE_STORED_SORTED.setOmitNorms(true);
DOUBLE_FIELD_TYPE_STORED_SORTED.setIndexOptions(IndexOptions.DOCS);
DOUBLE_FIELD_TYPE_STORED_SORTED.setNumericType(FieldType.NumericType.DOUBLE);
DOUBLE_FIELD_TYPE_STORED_SORTED.setStored(true);
DOUBLE_FIELD_TYPE_STORED_SORTED.setDocValuesType(DocValuesType.NUMERIC);
DOUBLE_FIELD_TYPE_STORED_SORTED.freeze();
}
@BeforeClass
public static void setup() throws Exception {
analyzer = new AnsjAnalyzer("index");
d = new RAMDirectory();
conf = new IndexWriterConfig(analyzer);
indexWriter = new IndexWriter(d, conf);
InputStream is = LuceneQueryTest1.class.getResourceAsStream("/data/data");
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line = null;
while ((line = br.readLine()) != null) {
String[] elements = line.split("##");
Document document = new Document();
StringField category = new StringField("category", elements[0], Store.YES);
TextField brandName = new TextField("brandName", elements[1], Store.YES);
TextField productName = new TextField("productName", elements[2], Store.YES);
DoubleField price = new DoubleField("price", Double.valueOf(elements[3]), DOUBLE_FIELD_TYPE_STORED_SORTED);
document.add(category);
document.add(brandName);
document.add(productName);
document.add(price);
indexWriter.addDocument(document);
}
indexWriter.commit();
indexWriter.close();
br.close();
is.close();
}
@AfterClass
public static void teardown() {
try {
indexWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// @Test
public void testSearchBySort() {
try {
IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(d));
Sort sort = new Sort(new SortField("price", Type.INT));
QueryParser parser = new QueryParser("productName", analyzer);
Query query2 = parser.parse("狂蛇鼠标");
TopDocs hits = searcher.search(query2, 10, sort, true, true);
ScoreDoc[] scoreDocs = hits.scoreDocs;
for (ScoreDoc scoreDoc : scoreDocs) {
Document document = searcher.doc(scoreDoc.doc);
System.err.println("productName:" + document.get("productName") + "price:" + document.get("price")
+ "score:" + scoreDoc.score);
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void testSearchBySort1() {
try {
IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(d));
Sort sort = new Sort(new SortField("productName", Type.SCORE), new SortField("price", Type.INT));
QueryParser parser = new QueryParser("productName", analyzer);
Query query2 = parser.parse("狂蛇鼠标");
TopDocs hits = searcher.search(query2, 10, sort, true, true);
ScoreDoc[] scoreDocs = hits.scoreDocs;
for (ScoreDoc scoreDoc : scoreDocs) {
Document document = searcher.doc(scoreDoc.doc);
System.err.println("productName:" + document.get("productName") + "price:" + document.get("price")
+ "score:" + scoreDoc.score);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
核心在于自定义一个FieldType :
DOUBLE_FIELD_TYPE_STORED_SORTED.setDocValuesType(DocValuesType.NUMERIC);