码迷,mamicode.com
首页 > Web开发 > 详细

Lucene搜索引擎入门写法

时间:2014-11-21 12:39:09      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:lencene   java   搜索引擎   


需要的jar包:

<dependency>
  		<groupId>org.apache.lucene</groupId>
  		<artifactId>lucene-core</artifactId>
  		<version>${lucene-version}</version>
  	</dependency>
  	
  	<dependency>
		<groupId>org.apache.lucene</groupId>
		<artifactId>lucene-analyzers-common</artifactId>
		<version>${lucene-version}</version>
	</dependency>


	<dependency>
		<groupId>org.apache.lucene</groupId>
		<artifactId>lucene-queryparser</artifactId>
		<version>${lucene-version}</version>
	</dependency>


其中: <lucene-version>4.10.2</lucene-version>


入门代码:

package com.power.lencene;

import java.io.File;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.FieldType;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryparser.classic.MultiFieldQueryParser;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

/* 作者Gary Huang 博客地址 :http://blog.csdn.net/hfmbook 邮箱:834865081@qq.com**/
public class LenceneDemo {
	
	static  String path = "d:/lencene" ; 
	
	public static void main(String[] args) {
		try {
			String keyword = "文字Score" ;  
			Analyzer analyzer = new StandardAnalyzer(); 
			Directory directory = FSDirectory.open(new File(path)) ; 
			IndexReader indexReader = DirectoryReader.open(directory) ;
			IndexSearcher indexSearcher = new IndexSearcher(indexReader);
			QueryParser queryParser = new MultiFieldQueryParser(new String[] {  "content"  },analyzer);  
			Query query = queryParser.parse(keyword); 
			Sort sort = new Sort() ;   
			TopDocs topDocs = indexSearcher.search(query, 5 , sort); 
			ScoreDoc[] scoreDoces = topDocs.scoreDocs;
			for(ScoreDoc scoreDoc : scoreDoces){
				int docID = scoreDoc.doc ;
				Document document = indexSearcher.doc(docID);
				System.out.println( document.get("id") ) ;  
				System.out.println( document.get("content") ) ;  
				
			}
			indexReader.close(); 
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void writeIndex(){
		try {
			Analyzer analyzer = new StandardAnalyzer(); 
			IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LATEST, analyzer);
		    Directory directory = FSDirectory.open(new File(path)) ; 
			IndexWriter iwriter = new IndexWriter(directory, indexWriterConfig) ; 
			Document doc = new Document();
			FieldType fieldType = new FieldType(); 
			fieldType.setIndexed(true);
			fieldType.setStored(true); 
			doc.add(new Field("id", "1", fieldType ) ) ;  
			doc.add(new Field("content", "Lucene文字这个相关度排序是基于内部的Score和DocID,Score又基于关键词的内部评分和做索引时的boost。默认Score高的排前面,如果Score一样,.”", fieldType)); 
			iwriter.addDocument(doc);
			iwriter.close() ;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}


Lucene搜索引擎入门写法

标签:lencene   java   搜索引擎   

原文地址:http://blog.csdn.net/hfmbook/article/details/41346859

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!