之前的文章有创建索引的方法,这里不贴了
先上代码,注释还算清楚
为了减少代码量,写一个公用的增删改公用的获取IndexWriter对象的方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 1 /**
2 * 增删改公用的获取IndexWriter对象
3 * @return
4 * @throws Exception
5 */
6 public IndexWriter getIndexWriter() throws Exception{
7 //获得索引存放的位置
8 Directory directory = FSDirectory.open(new File("./index"));
9 //获得分词器
10 Analyzer analyzer=new IKAnalyzer();
11 //获得IndexWriterConfig对象
12 IndexWriterConfig indexWriterConfig=new IndexWriterConfig(Version.LUCENE_4_10_2,analyzer);
13 //获得indexWeiter对象
14 IndexWriter indexWriter =new IndexWriter(directory,indexWriterConfig);
15 return indexWriter;
16 }
17
18
删除,修改索引
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 1/**
2 * 删除索引——全部删除
3 * @throws Exception
4 */
5 @Test
6 public void deleteAll() throws Exception{
7 //获得indexWeiter对象
8 IndexWriter indexWriter= this.getIndexWriter();
9 //删除所有的索引
10 indexWriter.deleteAll();
11 indexWriter.close();
12 }
13 /**
14 * 删除索引-按条件删除
15 */
16 @Test
17 public void delete() throws Exception{
18 //获得indexWeiter对象
19 IndexWriter indexWriter= this.getIndexWriter();
20
21 Term t=new Term("fileName","全文");
22 TermQuery query=new TermQuery(t);
23 //删除指定条件
24 indexWriter.deleteDocuments(query);
25 indexWriter.close();
26 }
27
28 /**
29 * 更新索引
30 * @throws Exception
31 */
32 @Test
33 public void update() throws Exception{
34 //获得indexWeiter对象
35 IndexWriter indexWriter= this.getIndexWriter();
36 Document document =new Document();
37 document.add(new TextField("filen","测试文件名", Field.Store.YES));
38 document.add(new TextField("fileC","测试文件内容", Field.Store.YES));
39
40 indexWriter.updateDocument(new Term("fileName","全文"),document,new IKAnalyzer());
41 indexWriter.close();
42 }
43
44
公用的查询对象获取,结果输出方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 1/**
2 * 公用的查询方法获取indexSearcher
3 * @return
4 * @throws Exception
5 */
6 public IndexSearcher getIndexSearcher() throws Exception{
7
8 //1:创建一个Directory对象,也就是索引库存放的位置
9 Directory directory = FSDirectory.open(new File("./index"));
10 //2:创建一个indexReader对象,需要制定Directory对象。
11 IndexReader indexReader =DirectoryReader.open(directory);
12 //3:创建一个indexSearcher对象,需要制定IndexReader对象
13 return new IndexSearcher(indexReader);
14 }
15
16 /**
17 * 公用的返回结果方法
18 * @param indexSearcher
19 * @param query
20 * @throws Exception
21 */
22 public void printResult(IndexSearcher indexSearcher, Query query) throws Exception{
23 //5:执行查询
24 TopDocs topDocs =indexSearcher.search(query,10);
25 //6:返回查询结果,便利查询结果且输出
26 ScoreDoc[] scoreDocs=topDocs.scoreDocs;
27 for(ScoreDoc scoreDoc: scoreDocs){
28 int doc=scoreDoc.doc;
29 Document document =indexSearcher.doc(doc);
30 //文件名
31 String fileName=document.get("fileName");
32 System.out.println("fileName:"+fileName);
33 // 文件大小
34 String fileSize = document.get("fileSize");
35 System.out.println("fileSize:::"+fileSize);
36 //文件路径
37 String filePath = document.get("filePath");
38 System.out.println("filePath:::"+filePath);
39 //文件内容
40 // fileContent=document.get("fileContent");
41 //System.out.println(fileContent);
42 System.out.println("===============================");
43 }
44 }
45
46
查询所有,数字方位查询,组合,解析查询
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58 1 /**
2 * 查询所有
3 * @throws Exception
4 */
5 @Test
6 public void searchAll() throws Exception{
7 IndexSearcher indexSearcher =getIndexSearcher();
8
9 Query query=new MatchAllDocsQuery();
10 System.out.println("query::::"+query);
11 printResult(indexSearcher,query);
12 indexSearcher.getIndexReader().close();
13 }
14 /**
15 * 数值范围查询
16 */
17 @Test
18 public void NumRangeSearch() throws Exception{
19 IndexSearcher indexSearcher = getIndexSearcher();
20
21 //查询条件:文件大小在1000到2500的,不包括1000,包含2500,设置参数可修改
22 Query query=NumericRangeQuery.newLongRange("fileSize",1000L,2500L,false,true);
23
24 System.out.println(query);
25 printResult(indexSearcher,query);
26 indexSearcher.getIndexReader().close();
27 }
28 /**
29 * 组合查询
30 */
31 @Test
32 public void searchMore() throws Exception{
33 IndexSearcher indexSearcher=getIndexSearcher();
34 //查询名字中有bbb,"全文可有可无"
35 BooleanQuery bc=new BooleanQuery();
36 Query query1=new TermQuery(new Term("fileName","bbb"));
37 Query query2=new TermQuery(new Term("fileName","全文"));
38 bc.add(query1, BooleanClause.Occur.MUST);
39 bc.add(query2, BooleanClause.Occur.SHOULD);
40
41 System.out.println(bc);
42 printResult(indexSearcher,bc);
43 indexSearcher.getIndexReader().close();
44 }
45 /**
46 *解析查询
47 */
48 @Test
49 public void QueryParser() throws Exception{
50 IndexSearcher indexSearcher=getIndexSearcher();
51 //参数1:默认查询的域,参数2:采用的分析器
52 QueryParser queryParser=new QueryParser("fileName",new IKAnalyzer());
53 Query query=queryParser.parse("fielName:aaabbb OR fileContent:what");
54 printResult(indexSearcher,query);
55 indexSearcher.getIndexReader().close();
56 }
57
58
Occur.MUST 是必须的条件
Occur.MUST_NOT 是否定条件
Occur.SHOULD 是可以满足的条件
下一阶段:Slor