⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sortdocid.java

📁 Lucene+nuctch一书的全部源码 测试源码 和几个简单的项目
💻 JAVA
字号:
package chapter7;

import java.io.IOException;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Sort;
import org.apache.lucene.store.RAMDirectory;

public class SortDocID {
	static String[] ContentList = { "Lucene 使用 方便", "使用 Lucene","Lucene 功能 强大", "Lucene 开放 源码" };
	static String[] NumberList = { "No.1", "No.2", "No.3", "No.4"};
	static String[] OrderList = { "3", "2", "3","1"};
	
	public static void main(String[] args) throws IOException{
		searchIndex();
	}
	// 创建索引并通过Sort改变检索结果排序
	private static void searchIndex() throws IOException{   
		try{
			RAMDirectory ramdirectory = new RAMDirectory(); // 内存目录
			IndexWriter writer = new IndexWriter(ramdirectory,new StandardAnalyzer(),true);
			for (int i = 0; i < ContentList.length; i++)
		    {
		        Document document = new Document(); // 创建文档对象
		        // 创建域对象
		        Field fieldContent = new Field("Content", ContentList[i], Field.Store.YES, Field.Index.TOKENIZED);
		        Field fieldNumber  = new Field("Number" , NumberList[i] , Field.Store.YES, Field.Index.TOKENIZED);
		        Field fieldOrder   = new Field("Order"  , OrderList[i]  , Field.Store.YES, Field.Index.TOKENIZED);

		        document.add(fieldContent);         // 添加创建的文本域到当前文档

		        writer.addDocument(document);       // 完成的文档添加到索引
		    }
			writer.close();                         // 关闭索引
		    IndexSearcher searcher = new IndexSearcher(ramdirectory);               // 创建检索器
		    QueryParser parser = new QueryParser("Content",new StandardAnalyzer()); // 创建查询分析器
		    Query  query = parser.parse("Lucene");  // 生成查询对象

		    Hits rstDoc;
		    System.out.println("Lucene默认相关性排序");
		    System.out.println("-----------------------------------");		    
		    rstDoc = searcher.search(query);     				// Lucene默认相关性排序
		    for (int i = 0; i < rstDoc.length(); i++) 			// 遍历获取文档,并读取相关参数
		    {
		        Document doc = rstDoc.doc(i);
		        System.out.println( doc.get("Content") + " Boost: " + doc.getBoost() + ", score : " + rstDoc.score(i));
		    }
		    System.out.println("");
		    System.out.println("Sort静态常量INDEXORDER文档序排序");
		    System.out.println("-----------------------------------");			    
		    rstDoc = searcher.search(query,Sort.INDEXORDER);     // Sort静态常量INDEXORDER文档序排序
		    for (int i = 0; i < rstDoc.length(); i++) 			 // 遍历获取文档,并读取相关参数
		    {
		        Document doc = rstDoc.doc(i);
		        System.out.println( doc.get("Content") + " Boost: " + doc.getBoost() + ", score : " + rstDoc.score(i));
		    }
		    
		    searcher.close();	
		} catch(ParseException e){
			System.out.println("ParseException ");
		} catch(IOException e){
			System.out.println("IOException  ");			
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -