SearcherFile.java
上传用户:zhihansy
上传日期:2014-12-04
资源大小:7241k
文件大小:2k
- package com.lucene;
- import java.io.IOException;
- import org.apache.lucene.analysis.Analyzer;
- import org.apache.lucene.analysis.standard.StandardAnalyzer;
- import org.apache.lucene.document.Document;
- import org.apache.lucene.queryParser.MultiFieldQueryParser;
- import org.apache.lucene.queryParser.ParseException;
- import org.apache.lucene.search.Hits;
- import org.apache.lucene.search.Query;
- import org.apache.lucene.search.Searcher;
- import java.util.*;
- import jeasy.analysis.MMAnalyzer;;
- /*
- * @CopyRigth(R) 城市通
- * @author 申华锋 E-mail:leonshine@qq.com
- * @version 创建时间:Mar 9, 2008 12:12:45 AM
- * @description
- */
- public class SearcherFile {
- private static int hitslength = 0;
-
- public static List search(Searcher searcher, String[] q, int startIndex,int endIndex)
- throws IOException, ParseException {
- //Analyzer analyzer = new StandardAnalyzer();
- Analyzer analyzer = new MMAnalyzer();//实例化jeasy分词器
- String[] fields = { "title", "description", "tag", "userName" };
- Query query = MultiFieldQueryParser.parse(q, fields, analyzer);
- Hits hits = searcher.search(query);
- hitslength = hits.length();
- System.out.println("Hits总长度"+hits.length());
- List lst = new ArrayList();
- /*for (int i = 0; i < hits.length(); i++) {
- Document doc = hits.doc(i);
- //取出ID,存入List
- lst.add(doc.get("productId"));
- }*/
-
- if(endIndex>=hits.length())
- endIndex=hits.length()-1;
- List docs=new ArrayList();
- for(int i=startIndex;i<=endIndex;i++){
- Document doc=hits.doc(i);
- lst.add(doc.get("productId"));
- }
- return lst;
- }
- public static int getHitsLength(){
- return hitslength;
- }
- }