Lucene 5.x 排序

測試數據:
java

鼠標##沃野##沃野牧馬人風格USB有線遊戲鼠標臺式電腦筆記本發光鼠標 lol cf 專業電競競技背光鼠標 沃野S8無聲靜音鼠標黑色##59.00git

鼠標##雙飛燕##雙飛燕(A4TECH) N-810FX 飛梭截圖針光鼠 紳士啞黑##59apache

鼠標##雷柏##雷柏(Rapoo) M218 無線光學鼠標 黑色##34.90測試

鼠標##雷蛇##雷蛇(Razer) Deathadder 煉獄蝰蛇1800 DPI 遊戲鼠標##149.00code

鼠標##羅技##羅技(Logitech)M100r 光電鼠標 (黑色##39.00orm

鼠標##雷蛇##雷蛇(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 :element

DOUBLE_FIELD_TYPE_STORED_SORTED.setDocValuesType(DocValuesType.NUMERIC);
相關文章
相關標籤/搜索