lucene教程详解.docx

上传人:b****8 文档编号:9601217 上传时间:2023-05-20 格式:DOCX 页数:50 大小:131.06KB
下载 相关 举报
lucene教程详解.docx_第1页
第1页 / 共50页
lucene教程详解.docx_第2页
第2页 / 共50页
lucene教程详解.docx_第3页
第3页 / 共50页
lucene教程详解.docx_第4页
第4页 / 共50页
lucene教程详解.docx_第5页
第5页 / 共50页
lucene教程详解.docx_第6页
第6页 / 共50页
lucene教程详解.docx_第7页
第7页 / 共50页
lucene教程详解.docx_第8页
第8页 / 共50页
lucene教程详解.docx_第9页
第9页 / 共50页
lucene教程详解.docx_第10页
第10页 / 共50页
lucene教程详解.docx_第11页
第11页 / 共50页
lucene教程详解.docx_第12页
第12页 / 共50页
lucene教程详解.docx_第13页
第13页 / 共50页
lucene教程详解.docx_第14页
第14页 / 共50页
lucene教程详解.docx_第15页
第15页 / 共50页
lucene教程详解.docx_第16页
第16页 / 共50页
lucene教程详解.docx_第17页
第17页 / 共50页
lucene教程详解.docx_第18页
第18页 / 共50页
lucene教程详解.docx_第19页
第19页 / 共50页
lucene教程详解.docx_第20页
第20页 / 共50页
亲,该文档总共50页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

lucene教程详解.docx

《lucene教程详解.docx》由会员分享,可在线阅读,更多相关《lucene教程详解.docx(50页珍藏版)》请在冰点文库上搜索。

lucene教程详解.docx

lucene教程详解

Lucene使用代码实例之搜索文档

1,Query类:

这是一个抽象类,用于将用户输入的查询字符串封装成Lucene能够识别的Query,它具有TermQuery,BooleanQuery,PrefixQuery等多种实现。

2,Term类:

用于描述搜索的基本单位,其构造函数是Term(“fieldName”,”queryWord”),其中第一个参数代表要在文档的哪一个Field上进行搜索,第二个参数代表要搜索的关键词。

3,TermQuery类:

TermQuery是抽象类Query的一个具体实现,也是Lucene支持的最为基本的一个查询类。

TermQuery的构造函数是TermQuery(newTerm(“fieldName”,”queryWord”)),唯一的参数是一个Term对象。

4,IndexSearcher类:

用于在建立好的索引上进行搜索的句柄类,其打开索引方式被设置为只读,因此允许多个IndexSearcher实例操作同一个索引。

5,Hits类:

搜索结果类。

代码:

利用索引搜索文档

packageTestLucene;

importjava.io.File;

importorg.apache.lucene.document.Document;

importorg.apache.lucene.index.Term;

importorg.apache.lucene.search.Hits;

importorg.apache.lucene.search.IndexSearcher;

importorg.apache.lucene.search.TermQuery;

importorg.apache.lucene.store.FSDirectory;

/**

*Thisclassisusedtodemonstratethe

*processofsearchingonanexisting

*Luceneindex

*

*/

publicclassTxtFileSearcher{

publicstaticvoidmain(String[]args)throwsException{

StringqueryStr="lucene";

//ThisisthedirectorythathoststheLuceneindex

FileindexDir=newFile("D:

\\luceneIndex");

FSDirectorydirectory=FSDirectory.getDirectory(indexDir,false);

IndexSearchersearcher=newIndexSearcher(directory);

if(!

indexDir.exists()){

System.out.println("TheLuceneindexisnotexist");

return;

}

Termterm=newTerm("contents",queryStr.toLowerCase());

TermQueryluceneQuery=newTermQuery(term);

Hitshits=searcher.search(luceneQuery);

for(inti=0;i

Documentdocument=hits.doc(i);

System.out.println("File:

"+document.get("path"));

}

}

}

在代码中,类IndexSearcher的构造函数接受一个类型为Directory的对象,传入的FSDirctory对象代表索引存储在磁盘上的位置,IndexSearcher实例化后,其以只读方式打开了这个索引。

然后程序构造了一个Term对象,指定要在文档内容中搜索包含关键词“lucene”的文档,程序利用这个Term对象构造出TermQuery对象,并把其传入到IndexSearcher的search方法中进行查询,返回的结果保存在Hits对象中。

最后程序利用循环代码将搜索到的文档路径全部打印出来。

本文来源于金色坐标,原文地址:

构建各种LuceneQuery

(一)

2009-11-2517:

41

1搜索流程中的第二步就是构建一个Query。

下面就来介绍Query及其构建。

2

3当用户输入一个关键字,搜索引擎接收到后,并不是立刻就将它放入后台开始进行关键字的检索,而应当首先对这个关键字进行一定的分析和处理,使之成为一种后台可以理解的形式,只有这样,才能提高检索的效率,同时检索出更加有效的结果。

那么,在Lucene中,这种处理,其实就是构建一个Query对象。

4

5就Query对象本身言,它只是Lucene的search包中的一个抽象类,这个抽象类有许多子类,代表了不同类型的检索。

如常见的TermQuery就是将一个简单的关键字进行封装后的对象,类似的还有BooleanQuery,即布尔型的查找。

6

7IndexSearcher对象的search方法中总是需要一个Query对象(或是Query子类的对象),本节就来介绍各种Query类。

8

911.4.1按词条搜索—TermQuery

10TermQuery是最简单、也是最常用的Query。

TermQuery可以理解成为“词条搜索”,在搜索引擎中最基本的搜索就是在索引中搜索某一词条,而TermQuery就是用来完成这项工作的。

11

12在Lucene中词条是最基本的搜索单位,从本质上来讲一个词条其实就是一个名/值对。

只不过这个“名”是字段名,而“值”则表示字段中所包含的某个关键字。

13

14要使用TermQuery进行搜索首先需要构造一个Term对象,示例代码如下:

15

16TermaTerm=newTerm("contents","java");

17

18然后使用aTerm对象为参数来构造一个TermQuery对象,代码设置如下:

19

20Queryquery=newTermQuery(aTerm);

21

22这样所有在“contents”字段中包含有“java”的文档都会在使用TermQuery进行查询时作为符合查询条件的结果返回。

23

24下面就通过代码11.4来介绍TermQuery的具体实现过程。

25

26代码11.4TermQueryTest.java

27

28packagech11;

29

30importorg.apache.lucene.analysis.standard.StandardAnalyzer;

31

32importorg.apache.lucene.document.Document;

33

34importorg.apache.lucene.document.Field;

35

36importorg.apache.lucene.index.IndexWriter;

37

38importorg.apache.lucene.index.Term;

39

40importorg.apache.lucene.search.Hits;

41

42importorg.apache.lucene.search.IndexSearcher;

43

44importorg.apache.lucene.search.Query;

45

46importorg.apache.lucene.search.TermQuery;

47

48

49

50publicclassTermQueryTest

51

52{

53

54publicstaticvoidmain(String[]args)throwsException

55

56{

57

58//生成Document对象

59

60Documentdoc1=newDocument();

61

62//添加“name”字段的内容

63

64doc1.add(Field.Text("name","word1word2word3"));

65

66//添加“title”字段的内容

67

68doc1.add(Field.Keyword("title","doc1"));

69

70//生成索引书写器

71

72IndexWriterwriter=newIndexWriter("c:

\\index",newStandardAnalyzer(),true);

73

74

75

76//将文档添加到索引中

77

78writer.addDocument(doc1);

79

80//关闭索引

81

82writer.close();

83

84

85

86//生成查询对象query

87

88Queryquery=null;

89

90

91

92//生成hits结果对象,保存返回的检索结果

93

94Hitshits=null;

95

96

97

98//生成检索器

99

100IndexSearchersearcher=newIndexSearcher("c:

\\index");

101

102

103

104//构造一个TermQuery对象

105

106query=newTermQuery(newTerm("name","word1"));

107

108//开始检索,并返回检索结果到hits中

109

110hits=searcher.search(query);

111

112//输出检索结果中的相关信息

113

114printResult(hits,"word1");

115

116

117

118//再次构造一个TermQuery对象,只不过查询的字段变成了"title"

119

120query=newTermQuery(newTerm("title","doc1"));

121

122//开始第二次检索,并返回检索结果到hits中

123

124hits=searcher.search(query);

125

126//输出检索结果中的相关信息

127

128printResult(hits,"doc1");

129

130

131

132}

133

134

135

136publicstaticvoidprintResult(Hitshits,Stringkey)throwsException

137

138{

139

140System.out.println("查找\""+key+"\":

");

141

142if(hits!

=null)

143

144{

145

146if(hits.length()==0)

147

148{

149

150System.out.println("没有找到任何结果");

151

152}

153

154else

155

156{

157

158System.out.println("找到"+hits.length()+"个结果");

159

160for(inti=0;i

161

162{

163

164Documentd=hits.doc(i);

165

166Stringdname=d.get("title");

167

168System.out.print(dname+"");

169

170}

171

172System.out.println();

173

174System.out.println();

175

176}

177

178}

179

180}

181

182}

183

184在代码11.4中使用TermQuery进行检索的运行结果如图11-8所示。

185

186注意:

字段值是区分大小写的,因此在查询时必须注意大小写的匹配。

187

188从图11-8中可以看出,代码11.4两次分别以“word1”和“doc1”为关键字进行检索,并且都只得到了一个检索结果。

189

190在代码11.4中通过构建TermQuery的对象,两次完成了对关键字的查找。

两次查找过程中不同的是,第一次构建的TermQuery是查找“name”这个字段,而第二次构建的TermQuery则查找的是“title”这个字段。

191

19211.4.2“与或”搜索—BooleanQuery

193BooleanQuery也是实际开发过程中经常使用的一种Query。

它其实是一个组合的Query,在使用时可以把各种Query对象添加进去并标明它们之间的逻辑关系。

在本节中所讨论的所有查询类型都可以使用BooleanQuery综合起来。

BooleanQuery本身来讲是一个布尔子句的容器,它提供了专门的API方法往其中添加子句,并标明它们之间的关系,以下代码为BooleanQuery提供的用于添加子句的API接口:

194

195publicvoidadd(Queryquery,booleanrequired,booleanprohibited);

196

197注意:

BooleanQuery是可以嵌套的,一个BooleanQuery可以成为另一个BooleanQuery的条件子句。

198

199下面以11.5为例来介绍进行“与”操作的布尔型查询。

200

201代码11.5BooleanQueryTest1.java

202

203packagech11;

204

205importorg.apache.lucene.analysis.standard.StandardAnalyzer;

206

207importorg.apache.lucene.document.Document;

208

209importorg.apache.lucene.document.Field;

210

211importorg.apache.lucene.index.IndexWriter;

212

213importorg.apache.lucene.index.Term;

214

215importorg.apache.lucene.search.BooleanQuery;

216

217importorg.apache.lucene.search.Hits;

218

219importorg.apache.lucene.search.IndexSearcher;

220

221importorg.apache.lucene.search.Query;

222

223importorg.apache.lucene.search.TermQuery;

224

225

226

227publicclassBooleanQueryTest1

228

229{

230

231publicstaticvoidmain(String[]args)throwsException{

232

233//生成新的Document对象

234

235Documentdoc1=newDocument();

236

237doc1.add(Field.Text("name","word1word2word3"));

238

239doc1.add(Field.Keyword("title","doc1"));

240

241

242

243Documentdoc2=newDocument();

244

245doc2.add(Field.Text("name","word1word4word5"));

246

247doc2.add(Field.Keyword("title","doc2"));

248

249

250

251Documentdoc3=newDocument();

252

253doc3.add(Field.Text("name","word1word2word6"));

254

255doc3.add(Field.Keyword("title","doc3"));

256

257

258

259//生成索引书写器

260

261IndexWriterwriter=newIndexWriter("c:

\\index",newStandardAnalyzer(),true);

262

263//添加到索引中

264

265writer.addDocument(doc1);

266

267writer.addDocument(doc2);

268

269writer.addDocument(doc3);

270

271writer.close();

272

273

274

275Queryquery1=null;

276

277Queryquery2=null;

278

279BooleanQueryquery=null;

280

281Hitshits=null;

282

283

284

285//生成IndexSearcher对象

286

287IndexSearchersearcher=newIndexSearcher("c:

\\index");

288

289

290

291query1=newTermQuery(newTerm("name","word1"));

292

293query2=newTermQuery(newTerm("name","word2"));

294

295

296

297//构造一个布尔查询

298

299query=newBooleanQuery();

300

301

302

303//添加两个子查询

304

305query.add(query1,true,false);

306

307query.add(query2,true,false);

308

309

310

311hits=searcher.search(query);

312

313printResult(hits,"word1和word2");

314

315

316

317}

318

319

320

321publicstaticvoidprintResult(Hitshits,Stringkey)throwsException

322

323{

324

325System.out.println("查找\""+key+"\":

");

326

327if(hits!

=null)

328

329{

330

331if(hits.length()==0)

332

333{

334

335System.out.println("没有找到任何结果");

336

337}

338

339else

340

341{

342

343System.out.println("找到"+hits.length()+"个结果");

344

345for(inti=0;i

346

347{

348

349Documentd=hits.doc(i);

350

351Stringdname=d.get("title");

352

353System.out.print(dname+"");

354

355}

356

357System.out.println();

358

359System.out.println();

360

361}

362

363}

364

365}

366

367}

368

369代码11.5首先构造了两个TermQuery,然后构造了一个BooleanQuery的对象,并将两个TermQuery当成它的查询子句加入Boolean查询中。

370

371再来看一下BooleanQuery的add方法,除了它的第一个参数外,它还有另外两个布尔型的参数。

第1个参数的意思是当前所加入的查询子句是否必须满足,第2个参数的意思是当前所加入的查询子句是否不需要满足。

这样,当这两个参数分别选择true和false时,会有4种不同的组合。

372

373true&false:

表明当前加入的子句是必须要满足的。

374

37

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > PPT模板 > 中国风

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2