📄 inputproxy.java
字号:
package com.microvois.luence;
import org.apache.lucene.analysis.*;
import org.apache.lucene.document.*;
import org.apache.lucene.store.*;
import org.apache.lucene.index.*;
import org.apache.lucene.analysis.cn.*;
import java.io.*;
import org.apache.log4j.Logger;
public class inputproxy
{
/**
* The maximum number of results to return with a search.
*/
private static final Logger log = Logger.getLogger(Config.class);
private static IndexWriter writer;
private static Directory searchDirectory = null;
protected static Analyzer analyzer = new ChineseAnalyzer();
private static int ninputcount = 1;
static
{
if(searchDirectory == null)
{
try
{
searchDirectory= FSDirectory.getDirectory(new File(Config.StrDataDirectroy));
if(searchDirectory.fileExists("write.lock"))
{
searchDirectory.deleteFile("write.lock");
System.out.println("delete luence lock file ...");
}
}catch(Exception se)
{
}
}
}
/**
*
* delete document from searchDB via url .
* @param url: key of url string
* @return
*/
public static synchronized boolean deleteURL(String url)
{
boolean nret = false;
try
{
IndexWriter awriter= getDirectoryWriter();
Term tm = new Term("url",url);
awriter.deleteDocuments(tm);
nret = true;
}catch(Exception se)
{
log.equals(se.getMessage());
se.printStackTrace();
}
return nret;
}
public static synchronized boolean inputData(String strtitle, String strdata, String url)
{
boolean nret = false;
try
{
IndexWriter awriter= getDirectoryWriter();
Document doc = new Document();
doc.add(new Field("title", strtitle, Field.Store.YES,Field.Index.NO));
doc.add(new Field("data", strdata, Field.Store.YES,Field.Index.TOKENIZED));
doc.add(new Field("url", url, Field.Store.YES, Field.Index.UN_TOKENIZED));
awriter.addDocument(doc);
ninputcount++;
if(ninputcount % 100 ==0)
awriter.flush();
nret = true;
}catch(Exception se)
{
log.equals(se.getMessage());
se.printStackTrace();
}
return nret;
}
/**
* if first work , implement create directroy .
*
* @return
*/
private static synchronized IndexWriter getDirectoryWriter() throws Exception
{
if(searchDirectory ==null)
{
searchDirectory = FSDirectory.getDirectory(new File(Config.StrDataDirectroy));
}
if(writer == null)
{
writer= new IndexWriter(searchDirectory, analyzer);
}
return writer;
}
private static boolean indexExists(String indexPath) {
File segments = new File(indexPath+ File.separator + "segments");
return segments.exists();
}
public static void flush()
{
try
{
if(writer!=null)
writer.flush();
}catch(Exception se)
{
}
}
public static synchronized void close()
{
try
{
if(writer != null)
{
writer.optimize();
writer.close();
}
if(searchDirectory !=null)
{
searchDirectory.close();
}
searchDirectory = null;
writer= null;
}catch(Exception see)
{
see.printStackTrace();
}
}
/**
*
*
* @param args
*/
public static void main(String args[])
{
for(int i=0;i<1;i++)
{
boolean bb = inputproxy.inputData("测试标题"+i, "您正abcd在查找的页当前不可用。 网站可能遇到支持问题,或者您需要 调整您的浏览器设置。", "http://www.localhost.com/aa.html");
System.out.println(i+"="+bb);
}
inputproxy.close();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -