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

📄 luceneindexinteger.java

📁 《lucene+nutch搜索引擎开发》源代码
💻 JAVA
字号:
package chapter5;

import java.util.Date;
import java.io.*;

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.TermQuery;
import org.apache.lucene.store.Directory; 
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.analysis.SimpleAnalyzer;
import org.apache.lucene.queryParser.*;

public class LuceneIndexInteger {

	private static String Dest_Index_Path = "D:\\workshop\\TextIndex1";
	private static String Text_File_Path = "D:\\workshop\\ch2\\indexsample.txt";

	public static void main(String[] args) {
	
		try {
			File file = new File(Text_File_Path);  // 原始文件
	        Directory dir = FSDirectory.getDirectory(Dest_Index_Path,false); // 索引目录
	        Analyzer TextAnalyzer = new SimpleAnalyzer();                    // 文档分析器
	        IndexWriter TextIndex = new IndexWriter(dir,TextAnalyzer,true);  // 生成索引器对象
	        TextIndex.setUseCompoundFile(true);
			Document document = new Document();                              // 新建空文档

			Field field_name = new Field("path1", file.getName(), 
					Field.Store.YES,Field.Index.UN_TOKENIZED);
			document.add(field_name);                                        // 添加整体文件名域
			
			Field field_name2 = new Field("path2", file.getName(), 
					Field.Store.YES,Field.Index.TOKENIZED);
			document.add(field_name2);                                      // 添加整体文件名域

			TextIndex.addDocument(document);                                // 添加索引文档
			TextIndex.optimize();
			TextIndex.close();
			
			System.out.println("----------no split-----------------");
			display(file.getName(),true);                                   // 输出替换前结果
			System.out.println("----------  split  ----------------");			
			display(file.getName(),false);                                   // 输出替换前结果

		} catch (IOException e) {
			e.printStackTrace();
		}		
	}
	
	public static void display(String words,boolean bsplit) throws IOException
	{	// 显示结果
		Query query ,query1;
		try {		
			IndexSearcher searcher = new IndexSearcher( Dest_Index_Path ); // 检索器
			if(bsplit)
			{
				Term term = new Term("path2", words);                      // 单词项
				query = new TermQuery(term);                               // 检索单元 
				Term term1 = new Term("path1", words);                      // 单词项
				query1 = new TermQuery(term1);                              // 检索单元 
				
			} else {
			    QueryParser parser = new QueryParser("path2",new StandardAnalyzer()); 
			    query = parser.parse(words);
			    QueryParser parser1 = new QueryParser("path1",new StandardAnalyzer()); 
			    query1 = parser1.parse(words);
			}

			System.out.println("Query  words:");
			System.out.println("  " + query.toString());
			Hits hits = searcher.search(query );                           // 提交检索
			System.out.println("Search result:");
			for(int i=0; i < hits.length(); i++)                           // 输出结果
			{
				System.out.println("  path2: " + hits.doc(i).getField("path2").stringValue());			
				System.out.println("   Path1 : " + hits.doc(i).getField("path1").stringValue());
			}
			
			System.out.println("Query  words:");
			System.out.println("  " + query1.toString());
			hits = searcher.search(query1 );                               // 提交检索
			System.out.println("Search result:");
			for(int i=0; i < hits.length(); i++)                           // 输出结果
			{
				System.out.println("  path2: " + hits.doc(i).getField("path2").stringValue());			
				System.out.println("   Path1 : " + hits.doc(i).getField("path1").stringValue());
			}			
		} catch (ParseException e)
		{
			e.printStackTrace();
		} catch (IOException e)
		{
			e.printStackTrace();
		}
	}
}

⌨️ 快捷键说明

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