lucene-4.8.0版本SSM框架配置

账号ID:83D4A16A28561E54
appkey= SG55aK340TAhz80y1j52FuOUan8xd7sX
categoryId=B3BC163013F66083

<lucene.version>4.8.0</lucene.version>

<!-- lucene  全文搜索  -->
<dependency>
  <groupId>org.apache.lucene</groupId>
  <artifactId>lucene-core</artifactId>
  <version>${lucene.version}</version>
</dependency>
<dependency>
  <groupId>org.apache.lucene</groupId>
  <artifactId>lucene-highlighter</artifactId>
  <version>${lucene.version}</version>
</dependency>
<dependency>
  <groupId>org.apache.lucene</groupId>
  <artifactId>lucene-memory</artifactId>
  <version>${lucene.version}</version>
</dependency>
<dependency>
  <groupId>org.apache.lucene</groupId>
  <artifactId>lucene-queries</artifactId>
  <version>${lucene.version}</version>
</dependency>
<dependency>
  <groupId>org.apache.lucene</groupId>
  <artifactId>lucene-queryparser</artifactId>
  <version>${lucene.version}</version>
</dependency>
<dependency>
  <groupId>org.apache.lucene</groupId>
  <artifactId>lucene-analyzers-common</artifactId>
  <version>${lucene.version}</version>
</dependency>

手动导入IKAnalyzer-5.0.1.jar (中文分词词,实现一些自定义的类 ) src - WEBINF 下
conf lucene-config.properties spring_luncene.xml
web.xml 中导入classpath:conf/spring_lucene.xml
$取数据 在spring.xml中placeholder加

<context:property-placeholder location="classpath:conf/mysql.properties,classpath:conf/rabbitmq-global.properties,classpath:conf/redis-config.properties,classpath:conf/lucene-config.properties"/> 

封装的工具包- - - 自己做视频查询封装–

-仅供参考

package org.lx.util;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
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.TopDocs;
import org.apache.lucene.util.Version;
import org.springframework.stereotype.Component;
import org.wltea.analyzer.lucene.IKAnalyzer;

import org.lx.entity.Movie;
import org.lx.service.MovieService;


@Component("luceneUtil")
public class LuceneUtil {
@Resource(name="indexWriter")
private IndexWriter indexWriter;
@Resource(name="analyzer")
private IKAnalyzer analyzer;

@Resource(name="movieService")
private MovieService movieService;
@PostConstruct
public  void init(){
	//在当前类实例化完后,会自动调用此方法,做初始化工作
	System.out.println("init lucene index ");
	List<Movie> movies=this.movieService.findAllMoviesByLucene();
	System.out.println(movies);
	this.createIndex(movies);
}

/**
 * 基于查询数据库的视频结果,做lucene索引
 * @param videos  ,数据库的视频数据
 */
public void createIndex(List<Movie> movies){
	Document document=null;
	try{
		//创建新索引前,必须先删除原有的索引,否则索引会叠加
		this.indexWriter.deleteAll();
		
		//创建索引
		for(Movie movie : movies){
			document=new Document();
			if(movie.getId()!=null){
				Field id=new Field("id",movie.getId(),Field.Store.YES,Field.Index.NO);
				document.add(id);
			}
			if(movie.getMname()!=null){
				Field mname=new Field("mname",movie.getMname(),Field.Store.YES,Field.Index.ANALYZED);
				document.add(mname);
			}
			if(movie.getCountry()!=null){
				Field country=new Field("country",movie.getCountry(),Field.Store.YES,Field.Index.ANALYZED);
				document.add(country);
			}
			if(movie.getDate()!=null){
				Field date=new Field("date",movie.getDate(),Field.Store.YES,Field.Index.NO);
				document.add(date);
			}
			
			if(movie.getCategory()!=null){
				Field category=new Field("category",movie.getCategory(),Field.Store.YES,Field.Index.ANALYZED);
				document.add(category);
			}
			if(movie.getDirector()!=null){
				Field director=new Field("director",movie.getDirector(),Field.Store.YES,Field.Index.ANALYZED);
				document.add(director);
			}
			if(movie.getActor()!=null){
				Field actor=new Field("actor",movie.getActor(),Field.Store.YES,Field.Index.ANALYZED);
				document.add(actor);
			}
			if(movie.getPlot()!=null){
				Field plot=new Field("plot",movie.getPlot(),Field.Store.YES,Field.Index.NO);
				document.add(plot);
			}
			
			if(movie.getPrice()!=null){
				Field price=new Field("price",movie.getPrice(),Field.Store.YES,Field.Index.NO);
				document.add(price);
			}
			
			if(movie.getImage1()!=null){
				Field image1=new Field("image1",movie.getImage1(),Field.Store.YES,Field.Index.NO);
				document.add(image1);
			}
			if(movie.getImage2()!=null){
				Field image2=new Field("image2",movie.getImage2(),Field.Store.YES,Field.Index.NO);
				document.add(image2);
			}
			if(movie.getImage3()!=null){
				Field image3=new Field("image3",movie.getImage3(),Field.Store.YES,Field.Index.NO);
				document.add(image3);
			}
			if(movie.getImage4()!=null){
				Field image4=new Field("image4",movie.getImage4(),Field.Store.YES,Field.Index.NO);
				document.add(image4);
			}
			
			
			
		/*	if(movie.getTitle()!=null){
				Field title=new Field("title",video.getTitle(),Field.Store.YES,Field.Index.ANALYZED);
				document.add(title);
			}
			if(video.getCourse()!=null && video.getCourse().getName()!=null){
				Field courseName=new Field("courseName",video.getCourse().getName(),Field.Store.YES,Field.Index.ANALYZED);
				document.add(courseName);
			}
			if(video.getUser()!=null && video.getUser().getNickName()!=null){
				Field nickName=new Field("nickName",video.getUser().getNickName(),Field.Store.YES,Field.Index.ANALYZED);
				document.add(nickName);
			}
			if(video.getCount()>=0){
				Field clickCount=new Field("clickCount",video.getCount().toString(),Field.Store.YES,Field.Index.NO);
				document.add(clickCount);
			}
			if(video.getPicture()!=null){
				Field picture=new Field("picture",video.getPicture(),Field.Store.YES,Field.Index.NO);
				document.add(picture);
			}
			if(video.getFileName()!=null){
				Field fileName=new Field("fileName",video.getFileName(),Field.Store.YES,Field.Index.NO);
				document.add(fileName);
			}
			if(video.getPicturecc()!=null){
				Field picturecc=new Field("picturecc",video.getPicturecc(),Field.Store.YES,Field.Index.NO);
				document.add(picturecc);
			}
			if(video.getVideoUrlcc()!=null){
				Field videoUrlcc=new Field("videoUrlcc",video.getVideoUrlcc(),Field.Store.YES,Field.Index.NO);
				document.add(videoUrlcc);
			}
			if(video.getIntroduction()!=null){
				Field introduction=new Field("introduction",video.getIntroduction(),Field.Store.YES,Field.Index.NO);
				document.add(introduction);
			}*/
			this.indexWriter.addDocument(document);
		}
		this.indexWriter.commit();
	}catch(Exception e){
		e.printStackTrace();
	}		
}
//搜索索引
public List<Movie> search(String queryString){
	List<Movie> movies=new ArrayList<Movie>();
	try{
		//获取索引查询器
		IndexSearcher searcher=this.getSearcher();
		//创建查询转换器
		QueryParser parser=new MultiFieldQueryParser(Version.LUCENE_48,
				new String[]{
						"mname",
						"country",
						"category",
						"director",
						"actor"
				},analyzer);
		//创建查询,queryString,要基于索引查询的字符串
		Query query=parser.parse(queryString);
		TopDocs td=searcher.search(query, 100);
		ScoreDoc[] sd=td.scoreDocs;
		
		Movie movie=null;
		Document doc=null;
		for(int i=0;i<sd.length;i++){
			movie=new Movie();
			int docId=sd[i].doc;
			doc=searcher.doc(docId);
			
			/*Course course=new Course();
			course.setName(doc.get("courseName"));
			
			User user=new User();
			user.setNickName(doc.get("nickName"));*/
			
			
			movie.setId(doc.get("id"));
			movie.setMname(doc.get("mname"));
			movie.setCountry(doc.get("country"));
			movie.setDate(doc.get("date"));
			movie.setCategory(doc.get("category"));
			movie.setDirector(doc.get("director"));
			movie.setActor(doc.get("actor"));
			movie.setPlot(doc.get("plot"));
			movie.setPrice(doc.get("price"));
			movie.setImage1(doc.get("image1"));
			movie.setImage2(doc.get("image2"));
			movie.setImage3(doc.get("image3"));
			movie.setImage4(doc.get("image4"));
			
			/*video.setTitle(doc.get("title"));
			video.setCourse(course);
			video.setUser(user);
			video.setCount(Long.parseLong(doc.get("clickCount")));
			video.setPicture(doc.get("picture"));*/
			//如果从doucment中读取的数据为null,则报异常
		/*	if(doc.get("picturecc")!=null){
				video.setPicturecc(doc.get("picturecc"));
			}
			if(doc.get("fileName")!=null){
				video.setFileName(doc.get("fileName"));
			}
			if(doc.get("videoUrlcc")!=null){
				video.setVideoUrlcc(doc.get("videoUrlcc"));
			}
			video.setIntroduction(doc.get("introduction"));*/
			movies.add(movie);
		}

	}catch(Exception e){
		e.printStackTrace();
	}
	return movies;
}
private IndexSearcher getSearcher()throws Exception{
	return new IndexSearcher(IndexReader.open(this.indexWriter.getDirectory()));
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值