Skip to content

Commit 4f7bf07

Browse files
committed
lucene
1 parent 800cb69 commit 4f7bf07

6 files changed

Lines changed: 32 additions & 22 deletions

File tree

-341 Bytes
Binary file not shown.
-1.59 KB
Binary file not shown.
-376 Bytes
Binary file not shown.
-136 Bytes
Binary file not shown.

‎spring-boot-lucene-demo/pom.xml‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,28 +60,28 @@
6060
<version>7.1.0</version>
6161
</dependency>
6262

63-
<!-- 高亮 -->
63+
<!--高亮 -->
6464
<dependency>
6565
<groupId>org.apache.lucene</groupId>
6666
<artifactId>lucene-highlighter</artifactId>
6767
<version>7.1.0</version>
6868
</dependency>
6969

70-
<!-- smartcn 中文分词器 SmartChineseAnalyzer smartcn分词器 需要lucene依赖 且和lucene版本同步-->
70+
<!--smartcn 中文分词器 SmartChineseAnalyzer smartcn分词器 需要lucene依赖 且和lucene版本同步-->
7171
<dependency>
7272
<groupId>org.apache.lucene</groupId>
7373
<artifactId>lucene-analyzers-smartcn</artifactId>
7474
<version>7.1.0</version>
7575
</dependency>
7676

77-
<!-- ik-analyzer 中文分词器-->
77+
<!--ik-analyzer 中文分词器-->
7878
<dependency>
7979
<groupId>cn.bestwu</groupId>
8080
<artifactId>ik-analyzers</artifactId>
8181
<version>5.1.0</version>
8282
</dependency>
8383

84-
<!-- MMSeg4j 分词器 -->
84+
<!--MMSeg4j 分词器-->
8585
<dependency>
8686
<groupId>com.chenlb.mmseg4j</groupId>
8787
<artifactId>mmseg4j-solr</artifactId>

‎spring-boot-lucene-demo/src/test/java/io/ymq/lucene/BaseTest.java‎

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@
2525

2626
import java.io.IOException;
2727
import java.io.StringReader;
28+
import java.nio.file.Path;
2829
import java.nio.file.Paths;
2930

3031
/**
31-
* 描述:
32+
* 描述: 单元测试
3233
*
3334
* @author yanpenglei
3435
* @create 2017-11-07 17:59
@@ -43,9 +44,9 @@ public class BaseTest {
4344

4445
@Before
4546
public void setUp() throws IOException {
47+
4648
//索引存放的位置,设置在当前目录中
4749
directory = FSDirectory.open(Paths.get("indexDir/"));
48-
4950
//创建索引的读取器
5051
indexReader = DirectoryReader.open(directory);
5152

@@ -55,12 +56,14 @@ public void setUp() throws IOException {
5556

5657
@After
5758
public void tearDown() throws Exception {
58-
indexReader.close();
59+
if (indexReader != null) {
60+
indexReader.close();
61+
}
5962
}
6063

6164

6265
/**
63-
* 写索引
66+
* 创建索引
6467
*
6568
* @throws IOException
6669
*/
@@ -146,7 +149,7 @@ public void deleteDocumentsTest() throws IOException {
146149
indexWriter.commit();
147150
indexWriter.close();
148151

149-
System.out.println("删除完成" + count);
152+
System.out.println("删除完成:" + count);
150153
}
151154

152155

@@ -177,18 +180,18 @@ public void updateDocumentTest() throws IOException {
177180

178181
doc.add(new IntPoint("id", id));
179182
doc.add(new StringField("title", "Spark", Field.Store.YES));
180-
doc.add(new TextField("content", "Apache Spark 是专为大规模数据处理而设计的快速通用的计算引擎", Field.Store.YES));
183+
doc.add(new TextField("content", "Apache Spark 是专为大规模数据处理而设计的快速通用的计算引擎!", Field.Store.YES));
181184
doc.add(new StoredField("id", id));
182185

183186
long count = indexWriter.updateDocument(new Term("id", "1"), doc);
184-
System.out.println(count);
187+
System.out.println("更新文档:" + count);
185188
indexWriter.close();
186189
}
187190

188191

189192
/**
190-
* 对特定项搜索
191-
* 按词条搜索—TermQuery
193+
* 按词条搜索
194+
* <p>
192195
* TermQuery是最简单、也是最常用的Query。TermQuery可以理解成为“词条搜索”,
193196
* 在搜索引擎中最基本的搜索就是在索引中搜索某一词条,而TermQuery就是用来完成这项工作的。
194197
* 在Lucene中词条是最基本的搜索单位,从本质上来讲一个词条其实就是一个名/值对。
@@ -209,7 +212,8 @@ public void termQueryTest() throws IOException {
209212

210213

211214
/**
212-
* “多条件查询”搜索—BooleanQuery
215+
* 多条件查询
216+
* <p>
213217
* BooleanQuery也是实际开发过程中经常使用的一种Query。
214218
* 它其实是一个组合的Query,在使用时可以把各种Query对象添加进去并标明它们之间的逻辑关系。
215219
* BooleanQuery本身来讲是一个布尔子句的容器,它提供了专门的API方法往其中添加子句,
@@ -251,6 +255,8 @@ public void BooleanQueryTest() throws IOException {
251255

252256

253257
/**
258+
* 匹配前缀
259+
* <p>
254260
* PrefixQuery用于匹配其索引开始以指定的字符串的文档。就是文档中存在xxx%
255261
* <p>
256262
*
@@ -268,6 +274,8 @@ public void prefixQueryTest() throws IOException {
268274

269275

270276
/**
277+
* 短语搜索
278+
* <p>
271279
* 所谓PhraseQuery,就是通过短语来检索,比如我想查“big car”这个短语,
272280
* 那么如果待匹配的document的指定项里包含了"big car"这个短语,
273281
* 这个document就算匹配成功。可如果待匹配的句子里包含的是“big black car”,
@@ -294,7 +302,8 @@ public void phraseQueryTest() throws IOException {
294302
}
295303

296304
/**
297-
* 相近词语的搜索—FuzzyQuery
305+
* 相近词语搜索
306+
* <p>
298307
* FuzzyQuery是一种模糊查询,它可以简单地识别两个相近的词语。
299308
*
300309
* @throws IOException
@@ -312,7 +321,8 @@ public void fuzzyQueryTest() throws IOException {
312321

313322

314323
/**
315-
* 使用通配符搜索—WildcardQuery
324+
* 通配符搜索
325+
* <p>
316326
* Lucene也提供了通配符的查询,这就是WildcardQuery。
317327
* 通配符“?”代表1个字符,而“*”则代表0至多个字符。
318328
*
@@ -357,7 +367,7 @@ public void queryParserTest() throws IOException, ParseException {
357367
}
358368

359369
/**
360-
* 多个 Field 查询
370+
* 多个 Field 分词查询
361371
*
362372
* @throws IOException
363373
* @throws ParseException
@@ -400,21 +410,21 @@ public void AnalyzerTest() throws IOException {
400410
Analyzer analyzer = null;
401411
String text = "Apache Spark 是专为大规模数据处理而设计的快速通用的计算引擎";
402412

403-
analyzer = new IKAnalyzer();//中文分词
413+
analyzer = new IKAnalyzer();//IKAnalyzer 中文分词
404414
printAnalyzerDoc(analyzer, text);
405415
System.out.println();
406416

407-
analyzer = new ComplexAnalyzer();//中文分词
417+
analyzer = new ComplexAnalyzer();//MMSeg4j 中文分词
408418
printAnalyzerDoc(analyzer, text);
409419
System.out.println();
410420

411-
analyzer = new SmartChineseAnalyzer();//中文分词
421+
analyzer = new SmartChineseAnalyzer();//Lucene 中文分词器
412422
printAnalyzerDoc(analyzer, text);
413423
}
414424

415425

416426
/**
417-
* 数据高亮查询
427+
* 高亮处理
418428
*
419429
* @throws IOException
420430
*/
@@ -428,7 +438,7 @@ public void HighlighterTest() throws IOException, ParseException, InvalidTokenOf
428438
Analyzer analyzer = new IKAnalyzer();//中文分词
429439

430440
String searchField = "content";
431-
String text = "Apache Spark 是专为大规模数据处理而设计的快速通用的计算引擎";
441+
String text = "Apache Spark 大规模数据处理";
432442

433443
//指定搜索字段和分析器
434444
QueryParser parser = new QueryParser(searchField, analyzer);

0 commit comments

Comments
 (0)