【数值型】
1 /**
2 * 创建索引
3 */
4 @Test
5 public void testIndex(){
6 //存储3篇文章的信息。包括:id、标题、作者、字数、发布日期、内容
7 int[] ids = {1,2,3};
8 String[] titles = {"Hello","I love you","morning"};
9 String[] authors = {"Hello Mike","HanMeimei","Tom"};
10 //正文的字数(数值类型数据)
11 int[] sizes = {7000,98092,500};
12 //发布日期(日期类型数据)
13 Date[] publishDate={fmt("2010-2-23"),fmt("2015-8-06"),fmt("2017-3-12")};
14
15 String[] contents= {"Hello,My Name Is Mike; good","Tome,I Love You; good,good,good","Good Moring,I'm so sorry; good,,good,good,good,good"};
16
17 IndexWriter writer = null;
18
19 try {
20 //1、创建Directory
21 Directory directory = FSDirectory.open(new File("E:\lucene\index2"));
22
23 ......
24 //3、创建Document
25 Document doc = null;
26
27 //4、设置Field
28 for(int i = 0;i<ids.length;i++){
29 doc = new Document();
30 //为文档添加域(属性)
31 String id = Integer.toString(ids[i]);
32 doc.add(new Field("id",id,Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS));
33 doc.add(new Field("title",titles[i],Field.Store.YES,Field.Index.NOT_ANALYZED));
34 doc.add(new Field("author",authors[i],Field.Store.YES,Field.Index.NOT_ANALYZED));
35 doc.add(new Field("content", contents[i],Field.Store.NO,Field.Index.ANALYZED));
36
37 //数值型索引
38 doc.add(new NumericField("size",Field.Store.YES,false).setIntValue(sizes[i]));
39
40 //日期类型数据索引
41 doc.add(new NumericField("date", Field.Store.YES, false).setLongValue(publishDate[i].getTime()));
42
43 //修改索引的权值
44 //默认权值为1.0
45 //将Id为1的数据权值调整到5.0
46 if(ids[i] == 1)
47 doc.setBoost(5.0f);
48
49 writer.addDocument(doc);
50 }
51 } catch (IOException e) {
52 e.printStackTrace();
53 }
54 finally{
55 ......
56 }
57 }