代码语言:javascript复制
package com.lucene.entity;
public class Ans {
public final static String LUCENE_PATH="/WEB-INF/lucene";
private String name;
private String content;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
package com.lucene;
import javax.servlet.http.HttpServletRequest;
import com.lucene.entity.Ans;
public interface Content {
public boolean saveConten(Ans ans,String path);
public String getConet(String name,String path);
}
package com.lucene.impl;
import java.io.File;
import java.io.IOException;
import java.io.Reader;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.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.store.Directory;
import org.apache.lucene.store.SimpleFSDirectory;
import org.apache.lucene.util.Version;
import org.wltea.analyzer.lucene.IKAnalyzer;
import org.wltea.analyzer.lucene.IKTokenizer;
import com.lucene.Content;
import com.lucene.entity.Ans;
public class ContentImpl implements Content{
@Override
public boolean saveConten(Ans ans,String path) {
try {
Directory dir=new SimpleFSDirectory(new File(path));
boolean exits=IndexReader.indexExists(dir);
IKAnalyzer analyzer=new IKAnalyzer(true);
IndexWriter writer=new IndexWriter(dir, new StandardAnalyzer(Version.LUCENE_30), !exits, IndexWriter.MaxFieldLength.LIMITED);
//IndexWriter writer=new IndexWriter(dir, analyzer, !exits, IndexWriter.MaxFieldLength.LIMITED);
Document doc=new Document();
/**
* Field.Store.YES表示存储field
* Field.Index.ANALYZED 进行分词
*/
doc.add(new Field("name",ans.getName(),Field.Store.YES,Field.Index.ANALYZED));
doc.add(new Field("content",ans.getContent(),Field.Store.YES,Field.Index.ANALYZED));
writer.addDocument(doc);
System.out.println(writer.numDocs());
writer.optimize();
writer.close();
return true;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
@Override
public String getConet(String name,String path) {
try {
Directory dir=new SimpleFSDirectory(new File(path));
IndexReader reader=IndexReader.open(dir);
IndexSearcher searcher=new IndexSearcher(reader);
IKAnalyzer analyzer=new IKAnalyzer(true);
//searcher.setSimilarity(new IKTokenizer(null, false));
QueryParser query=new QueryParser(Version.LUCENE_30, "name", new StandardAnalyzer(Version.LUCENE_30));
//QueryParser query=new QueryParser(Version.LUCENE_30, "name", analyzer);
Query q=query.parse(name);
TopDocs docs=searcher.search(q, 10);
ScoreDoc[] hits = docs.scoreDocs;
System.out.println(hits.length);
/*for(ScoreDoc doc:hits){
Document document=searcher.doc(doc.doc);
System.out.println(document);
System.out.println(document.getField("name") "==========" document.getField("content"));
}*/
for(int i=0;i
Document document=searcher.doc(hits[i].doc);
//System.out.println(document.getValues("name") "" document.ge);
System.out.println(document.getField("name").stringValue() "==========" document.getField("content").stringValue());
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
import javax.servlet.http.HttpServletRequest;
import com.lucene.Content;
import com.lucene.entity.Ans;
import com.lucene.impl.ContentImpl;
public class LuceneTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
Ans ans=new Ans();
ans.setName("上星期我在哪里?");
ans.setContent("上个星期我在广东...");
//ans.setName("hello");
//ans.setContent("welcome to here");
Content content=new ContentImpl();
LuceneTest test=new LuceneTest();
String path=test.getLucenePath();
//content.saveConten(ans,path);
content.getConet("我在哪里啊???", path);
}
public String getLucenePath(){
//String path=request.getSession().getServletContext().getRealPath(Ans.LUCENE_PATH);
String root=this.getClass().getResource("").getPath();
String path=root.substring(0, root.indexOf("classes")) "luceneData";
System.out.println("The lucene's file save path is: " path);
return path;
}
}