FieldType的讲解
(1)当type.setStored(false);表示不可存储
(2)当type.setStored(false);而content又想存储
(3)当type.setIndexed(false);表示不建立索引
(4)当type.setIndexed(false);而只想title不建立索引
(5)当type.setTokenized(false);表示不建立分词
总结:
package lucene;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.FieldType;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
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.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.junit.Test;
import java.io.File;
public class lucene {
String path="D:/workforce/lucene/hello"; //创建文件路径
Version version=Version.LUCENE_4_10_4; //创建Lucene的版本
//设置内容
String content1="走好选择的路,别选择好走的路,你才能拥有真正的自己。";
String content2="惟有身处卑微的人,最有机缘看到世态人情的真相。一个人不想攀高就不怕下跌,也不用倾轧排挤,可以保其天真,成其自然,潜心一志完成自己能做的事。";
String content3="我甘心当个“零”,人家不把我当个东西,我正好可以把看不起我的人看个透 ";
//Lucene的录入
@Test
public void testLucene() throws Exception{
//1.定义Lucene存放文件的位置
Directory directory= FSDirectory.open(new File(path));
//2.配置分词对象
Analyzer analyzer=new StandardAnalyzer();
//3.配置对象
IndexWriterConfig config=new IndexWriterConfig(version,analyzer);
IndexWriter writer=new IndexWriter(directory,config);
//4.往库里写入内容
FieldType type=new FieldType();
type.setStored(true);//可存储
type.setIndexed(true);//存储索引
type.setTokenized(true);//设置分词
//创建文档对象
Document doc=new Document();
doc.add(new Field("title","doc1",type));
doc.add(new Field("content",content1,type));
writer.addDocument(doc);
Document doc2=new Document();
doc2.add(new Field("title","doc2",type));
doc2.add(new Field("content",content2,type));
writer.addDocument(doc2);
Document doc3=new Document();
doc3.add(new Field("title","doc3",type));
doc3.add(new Field("content",content3,type));
writer.addDocument(doc3);
//5.提交资源
//6.关闭资源
writer.close();
}
}