1.luence 创建步骤 3.0
1.创建luence
Directory 类似数据库
---------------------------------------------------------------------------------------------------------------------------------
/*
Directory
dir = FSDirectory. open(
new
File(
INDEX_PATH
));
fs是创建本地文件
ram是创建在内存
Analyzer writerAnalyzer =
new
StandardAnalyzer(Version.
LUCENE_30
);
*/
2.创建
IndexWriter 类似数据表
writer =
new
IndexWriter(dir, writerAnalyzer, isFirstIndex, IndexWriter.MaxFieldLength.
UNLIMITED
);
/*
dir 是上面创建 luence目录
writerAnalyzer 分词器
isFirstIndex boolean 类型 首次创建 true 其余创建 false
*/
3.创建document 类似是数据库中的数据
将document add到writer里
Document doc =new Document();
doc.add(
new
Field(
"name"
,name,Field.Store.
YES
,Field.Index.
ANALYZED
));
field 只是其中的一个字段 可以创建多个
"name" 类似是字段名
Field.Store.
YES
//Field.Store.YES:存储字段值(未分词前的字段值)
//Field.Store.NO:不存储,存储与索引没有关系
//Field.Store.COMPRESS:压缩存储,用于长文本或二进制,但性能受损
Field.Index.
ANALYZED
//Field.Index.ANALYZED:分词建索引
//Field.Index.ANALYZED_NO_NORMS:分词建索引,但是Field的值不像通常那样被保存,而是只取一个byte,这样节约存储空间
//Field.Index.NOT_ANALYZED:不分词且索引
//Field.Index.NOT_ANALYZED_NO_NORMS:不分词建索引,Field的值去一个byte保存
2.luence 删除
删除luence
---------------------------------------------------------------------------------------------------------------------------------
1.删除单条数据luence
2.
IndexReader ir =
null
;
Directory dir =
null
;
dir = FSDirectory. open(
new
File(
INDEX_PATH
));
ir = IndexReader. open(dir,
false
);
//第一个参数是索引的目录 第二个参数 是否以只读的形式打开
//删除类似与sql删除 根据字段删除
ir.deleteDocuments(
new
Term(
"productId"
,productId));
2.删除整个luence
2.1 删除程序luence 只需将索引文件删除即可
3. luence 索引的更新,优化
对于索引的更新 只能先进行删除 之后重新添加所以
对于重新创建的所以 需要new writer
问题 会出现索引文件增加 解决办法
1.先判断是否创建过
2.如果创建过 需要优化luence
writer.setUseCompoundFile(
true
);
isFirstIndex = false;
writer =
new
IndexWriter(dir, writerAnalyzer, isFirstIndex, IndexWriter.MaxFieldLength.
UNLIMITED
);
//简便优化
writer.setUseCompoundFile (
true
);
优化
IndexWriter writer =
null
;
Directory dir =
null
;
Analyzer writerAnalyzer =
new
StandardAnalyzer(Version.
LUCENE_30
);
dir = FSDirectory. open(
new
File(
INDEX_PATH
));
writer =
new
IndexWriter(dir,writerAnalyzer,
false
,IndexWriter.MaxFieldLength.
UNLIMITED
);
//合并到一起
writer.optimize();
---------------------------------------------------------------------------------------------------------------------------------
4.luence 查询
1.获取luence仓库
Directory
2.创建
IndexReader
3.创建索引条件
IndexSearcher
4.排序
5.返回查询结果
Directory dir =
null
;
IndexReader reader =
null
;
IndexSearcher searcher =
null
;
SortField[] sfs =
null
;
sfs=
new
SortField[] {
new
SortField(
null
, SortField.
DOC
,
true
) };
Sort sort =
new
Sort(sfs);
dir = FSDirectory.open(
new
File(
INDEX_PATH
));
reader = IndexReader. open(dir);
searcher =
new
IndexSearcher(reader);
BooleanQuery bquery=
new
BooleanQuery();
bquery.add(
new
TermQuery(
new
Term(
"state"
,
"2"
)),BooleanClause.Occur.
MUST
)
TopDocs topDocs = searcher.search(bquery,
null
,1000,sort);
ScoreDoc[] hits = topDocs.
scoreDocs
;
CopyOnWriteArrayList<Product> resultlist =
new
CopyOnWriteArrayList<Product>();
for
(
int
i=0;i<hits.
length
;i++){
Document doc = searcher.doc(hits[i].
doc
);
//从数据库进行二次查询
Product sp = (Product)Tools.getManager(Product.
class
).get(doc.get(
"productId"
));
resultlist.add(sp);
}
//排序规则
SortField. SCORE 按积分排序
SortField. DOC 按文档排序
SortField. AUTO 域的值为int、long、float都有效
SortField.STRING 域按STRING排序
SortField..FLOAT
SortField.LONG
SortField.DOUBLE
SortField.SHORT
SortField.CUSTOM 通过比较器排序
SortField.BYTE
SortField. DOC 按文档排序
SortField. AUTO 域的值为int、long、float都有效
SortField.STRING 域按STRING排序
SortField..FLOAT
SortField.LONG
SortField.DOUBLE
SortField.SHORT
SortField.CUSTOM 通过比较器排序
SortField.BYTE