lucene:索引 -不分詞


package com.capinfotech.faq.classifier;
import java.util.*;
import java.io.File;
import java.io.IOException;

import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.util.Version;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;

import com.capinfotech.faq.data.Query;
import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException;
public class luceneindex {
    
	 private static String dest_path="E:/gongzuo/FAQdata/kb/kuaiindex";
     static protected String[] keywords={"4001"};
     static protected String[] contents={"在嗎","在不在","有人嗎","在線","在沒","在線嗎","有不有人"," 有人沒"," 有人不 在?"};

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
//		File file=new File(dest_path);
//	    FSDirectory directory=FSDirectory.open(file);
//		Analyzer textanalyzer=new StandardAnalyzer(Version.LUCENE_36);
//		IndexWriterConfig cfg = new IndexWriterConfig(Version.LUCENE_36, textanalyzer);	
//		IndexWriter indexa=new IndexWriter(directory,cfg);
//	for(int i=0;i<9;i++){
//			Document doc=new Document();
//			Field fieldnum=new Field("id",keywords[0],Field.Store.YES,Field.Index.NOT_ANALYZED);
//			doc.add(fieldnum);
//			Field fieldcontent=new Field("content",contents[i],Field.Store.YES,Field.Index.NOT_ANALYZED);
//			doc.add(fieldcontent);
//			indexa.addDocument(doc);
//	}
//		//indexa.optimize();
//		indexa.close();
	
	
	
		luceneindex querytest=new luceneindex();
		querytest.TemqueryTest();
	}
  public  void TemqueryTest() throws IOException{
	//    File file=new File("E:/gongzuo/FAQdata/kb/kuaiindex");
	  File file=new File(dest_path);
	    FSDirectory directory=FSDirectory.open(file);
		  IndexReader reader=IndexReader.open(directory);
		  IndexSearcher search=new IndexSearcher(reader);
//		  Analyzer textanalyzer=new StandardAnalyzer(Version.LUCENE_36);
//		 QueryParser parser=new QueryParser(Version.LUCENE_36,"content",textanalyzer);
//	     Query query=parser.parse("在");
		  Term t =new Term("content","有人");
		  TermQuery query=new TermQuery(t);
		//  ScoreDoc[] docs=search.search(query,10).scoreDocs;
		  ScoreDoc[] docs=search.search(query,12).scoreDocs;
		for(int i=0;i<docs.length;i++){   	
	
			  String querycontent=search.doc(docs[i].doc).get("content");
			  String queryid=search.doc(docs[i].doc).get("id");
			  System.out.println("查詢id: "+queryid+"\t查詢內容: "+querycontent);
			  System.out.println(docs[i].score);
			  
		}
		  search.close();
  }

}


以上是建立索引以及查詢索引的簡單例子,當不分詞時,檢索關鍵字根據完全匹配搜索


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM