jeecms系统附件内容Lucene搜索——二次开发

1 原有的针对文章的全文检索方式

1.1文章提交

正常情况下,发表一篇文章时,点击“提交”的时候,如图:

这里写图片描述

点击“提交”的时候是将文章所有字段的信息提交到后台,其实就是将这篇文章对应的“内容模型”中的数据都提交到后台了(“内容模型”的含义可以参考《jeecms系统使用介绍——jeecms中的内容、栏目、模型之间的关系》)。

这个过程比较重要的就是文章内容了,这里是通过百度的ueditor编辑器编辑了内容,然后将其提交的,这部分内容正文的数据其实是保存到了表:

jc_content_txt
  • 1

的“txt”字段之中,如图:

这里写图片描述

而其它几个字段:

 
  1. txt1

  2. txt2

  3. txt3

  • 1
  • 2
  • 3

这些字段中是没有保存内容的,它们是附加字段,默认没有被使用,留由用户在二次开发时自行设置。这也为我们处理对附件文件的全文检索提供了可能。

文章内容的保存是在包:

com.jeecms.cms.action.admin.main
  • 1

里的类:

ContentAct
  • 1
 
  1. @RequiresPermissions("content:o_save")

  2. @RequestMapping("/content/o_save.do")

  3. public String save(Content bean, ContentExt ext, ContentTxt txt,

  4. Boolean copyimg,Integer[] channelIds, Integer[] topicIds,

  5. Integer[] viewGroupIds,

  6. String[] attachmentPaths, String[] attachmentNames,

  7. String[] attachmentFilenames, String[] picPaths, String[] picDescs,

  8. Integer channelId, Integer typeId, String tagStr, Boolean draft,

  9. Integer cid, Integer modelId,Short charge,Double chargeAmount,

  10. Boolean rewardPattern,Double rewardRandomMin,

  11. Double rewardRandomMax,Double[] rewardFix, HttpServletRequest request,HttpServletResponse response, ModelMap model)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

1.2 建立索引

文章在“ContentAct”类的“save”方法中保存过程中,系统会单独开启一个线程,在类:

com.jeecms.cms.staticpage.ContentStatusChangeThread
  • 1

里的方法:

public void run()
  • 1

中。逐层进入到类:

com.jeecms.cms.lucene.LuceneContent
  • 1

里面的方法:

public static Document createDocument(Content c)
  • 1

在这个方法里面,会把文章的相关信息拿出来,包括文章的正文内容,存入Lucene索引里面去。

2 对文章中上传的附件进行全文检索

2.1 实现思路

我们先来说一下实现的思路。有几点我们是可以确认的:

  • 1,在文章中上传的附件是保存在了服务器的磁盘上,有一个磁盘路径。

  • 2,文章提交时,所有信息都会传给后台服务器。

  • 3,后台服务器对文章内容有一个保存的过程。

  • 4,保存过程中还会再单独建一个线程,用来对文章内容建立索引。

那么,根据我们的需求,要达到目的,我们完全可以这样来实现:

  • 1,在文章中上传的附件时,将上传到服务器的附件路径及文件名称先在浏览器的前台页面保存一下。

  • 2,文章提交时,将附件路径及名称也传递到后台服务器。

  • 3,在后台服务器保存文章内容的过程中,将附件信息也保存一下,保存到数据库表“jc_content_txt”的字段“txt1”里面。这里相当于使用了“txt1”这个默认字段。

  • 4,对文章内容创建索引时,除了对文章正文的html内容建立索引,还根据附件路径及名称,找到附件文件并进行读取,对这些附件的内容建立索引。

2.2 上传时保存附件路径及名称到文章页面

编辑文章的页面在:

{jeecms}/WebRoot/WEB-INF/jeecms_sys/content/add.html
  • 1

这个页面,我们在其中加入一个区域,保存上传附件的服务器路径及其名称。注意附件上传并保存到服务器之后其名称就变了,所以需要保存一下。

这里写图片描述

如图所示“input”为新加入部分,“value”值会存放内容。每次上传附件时会将信息保存的这个“input”的“value”里面,那么还要在上传操作中加入保存的方法。

在内容中上传附件,如图:

这里写图片描述

点击附件图标,进入附件上传界面,如图:

这里写图片描述

先点击“点击选择文件”按钮,选择了附件,如图:

这里写图片描述

然后点击“开始上传”,这个过程会将文件上传到服务器的磁盘文件中。点击上传时的操作在:

{jeecms}/WebRoot/thirdparty/ueditor/dialogs/attachment/attachment.js

中的方法:

uploader.on

里面,这是百度“ueditor”编辑器,我们做些修改之后内容如下:

这里写图片描述

在原有内容里,加入了每次上传时先将所上传的附件文件的路径及名称保存到前台页面的功能。详细代码如下:

 
  1. uploader.on('uploadSuccess', function (file, ret) {

  2. var $file = $('#' + file.id);

  3. try {

  4. var responseText = (ret._raw || ret),

  5. json = utils.str2json(responseText);

  6. if (json.state == 'SUCCESS') {

  7. _this.fileList.push(json);

  8. $file.append('<span class="success"></span>');

  9.  
  10. //alert(file + "----" + ret);

  11.  
  12. var attachTemp = $("input[name='attachmentsName']", window.parent.document).val();

  13. if( (attachTemp == null) || (attachTemp == "")) {

  14. $("input[name='attachmentsName']", window.parent.document).val(ret.url);

  15. } else {

  16. $("input[name='attachmentsName']", window.parent.document).val(attachTemp + ',' + ret.url);

  17. }

  18.  
  19. //alert('attachmentsName=' + $("input[name='attachmentsName']", window.parent.document).val());

  20.  
  21. } else {

  22. $file.find('.error').text(json.state).show();

  23. }

  24. } catch (e) {

  25. $file.find('.error').text(lang.errorServerUpload).show();

  26. }

  27. });

  • 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

可以把对alert的注释删除掉,然后看一下每次记录的内容。这里上传三个文件,看一下效果:

这里写图片描述

一个doc,一个docx,一个pdf,这3个上传文件的服务器相对路径及新名称都得到了。

3 提交文章时将附件路径传到后台

之前已经说了,点击了“提交”按钮后,后台对文章内容的保存是在类:

com.jeecms.cms.action.admin.main.ContentAct
  • 1

的方法:

public String save
  • 1

中进行的,然后在对其建立索引。为了能够对前台传过来的附件内容也能建立索引,要对“save”方法加点内容,如下:

这里写图片描述

4 对附件创建索引

万事俱备,只欠东风。该得到的信息都得到了,最后我就需要对上传的附件创建索引了。之前讲过创建索引是在类:

com.jeecms.cms.lucene.LuceneContent
  • 1

里面的方法:

public static Document createDocument(Content c)
  • 1

进行的,这里我们还在这里面进行。

这里面有一个默认处理模型项“txt1”的地方,如下:

 
  1. if (!StringUtils.isBlank(c.getTxt1())) {

  2. doc.add(new Field(CONTENT1, c.getTxt1(), Field.Store.NO,

  3. Field.Index.ANALYZED));

  4. }

  • 1
  • 2
  • 3
  • 4

我们对它进行修改,在这个区域里面,将上传的附件按照类型的不同分别创建索引,内容如下:

 
  1. if (!StringUtils.isBlank(c.getTxt1())) {

  2.  
  3. InputStream in = null;

  4. InputStreamReader reader = null;

  5.  
  6. try {

  7. //读取txt1中保存的json串

  8. JSONObject json = new JSONObject(c.getContentTxt().getTxt1());

  9. //读取json串中保存的应用服务器在操作系统上的绝对路径

  10. String realPath = json.getString("realPath");

  11. //读取json串中保村的上传附件的相对路径及名称

  12. String attachmentsNameArrays = json.getString("attachmentsNameArrays");

  13. //得到索引文件数组

  14. String []arr = attachmentsNameArrays.split(",");

  15.  
  16. for (int j = 0; j < arr.length; j++) {

  17. //将要搜索word文件的地方

  18. String dateDir = realPath + "\\" + arr[j];

  19. //读取当前这个附件文件

  20. File file = new File(dateDir);

  21.  
  22. //输出这个文件是第几个附件

  23. System.out.println("j=" + j);

  24. //输出这个文件的路径及名称

  25. System.out.println("file=" + file);

  26.  
  27. //获取文件名称

  28. String fileName = file.getName();

  29. //获取文件后缀名

  30. String fileType = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()).toLowerCase();

  31.  
  32. //输出文件名称

  33. System.out.println("fileName=" + fileName);

  34. //输出文件名称

  35. System.out.println("fileType=" + fileType);

  36.  
  37. //创建当前附件文件的输入流

  38. in = new FileInputStream(file);

  39.  
  40. //在当前附件文件的后缀名不为空的情况下进行操作,创建索引的文件包括doc、docx、pdf、txt

  41. if (fileType != null && !fileType.equals("")) {

  42.  
  43. if (fileType.equals("doc")) {

  44. //创建斌保存doc文件索引

  45.  
  46. //读取doc文件

  47. WordExtractor wordData = new WordExtractor(in);

  48.  
  49. //创建Field对象,并放入lucene的document对象doc中

  50. doc.add(new Field(CONTENT1, wordData.getText(), Field.Store.NO,

  51. Field.Index.ANALYZED));

  52. //输出当前操作的文件名称

  53. System.out.println("注意:已为文件“" + fileName + "”创建了索引");

  54. wordData.close();

  55.  
  56. } else if ( fileType.equals("docx")) {

  57. //创建斌保存docx文件索引

  58.  
  59. //读取docx文件

  60. XWPFWordExtractor wordData = new XWPFWordExtractor(new XWPFDocument(in));

  61.  
  62. //创建Field对象,并放入lucene的document对象doc中

  63. doc.add(new Field(CONTENT1, wordData.getText(), Field.Store.NO,

  64. Field.Index.ANALYZED));

  65. //输出当前操作的文件名称

  66. System.out.println("注意:已为文件“" + fileName + "”创建了索引");

  67. wordData.close();

  68.  
  69. }else if ( fileType.equals("pdf")) {

  70. //创建斌保存pdf文件索引

  71.  
  72. //读取pdf文件

  73. PDFParser parser = new PDFParser(in);

  74. parser.parse();

  75. PDDocument pdDocument = parser.getPDDocument();

  76. PDFTextStripper stripper = new PDFTextStripper();

  77. // String result = stripper.getText(pdDocument);

  78.  
  79. //创建Field对象,并放入lucene的document对象doc中

  80. doc.add(new Field(CONTENT1, stripper.getText(pdDocument), Field.Store.NO,

  81. Field.Index.ANALYZED));

  82. //输出当前操作的文件名称

  83. System.out.println("注意:已为文件“" + fileName + "”创建了索引");

  84. pdDocument.close();

  85.  
  86. } else if ( fileType.equals("txt") ) {

  87. //创建斌保存txt文件索引

  88.  
  89. //读取txt文件

  90. //建立一个输入流对象reader

  91. reader = new InputStreamReader(in);

  92. //建立一个对象,它把文件内容转成计算机能读懂的语言

  93. BufferedReader br = new BufferedReader(reader);

  94. String wordData = "";

  95. String line = null;

  96.  
  97. //一次读入一行数据

  98. while ((line = br.readLine()) != null) {

  99. wordData += line;

  100. }

  101.  
  102. //创建Field对象,并放入lucene的document对象doc中

  103. doc.add(new Field(CONTENT1, wordData, Field.Store.NO,

  104. Field.Index.ANALYZED));

  105. //输出当前操作的文件名称

  106. System.out.println("注意:已为文件“" + fileName + "”创建了索引");

  107.  
  108.  
  109. } else {

  110.  
  111.  
  112. }

  113.  
  114. }

  115.  
  116. }

  117.  
  118. } catch (JSONException e) {

  119.  
  120. e.printStackTrace();

  121.  
  122. } catch (FileNotFoundException e) {

  123.  
  124. e.printStackTrace();

  125.  
  126. } catch (IOException e) {

  127.  
  128. e.printStackTrace();

  129.  
  130. } finally {

  131.  
  132. if (in != null) {

  133. try {

  134.  
  135. in.close();

  136.  
  137. } catch (IOException e) {

  138.  
  139. e.printStackTrace();

  140.  
  141. }

  142. }

  143. if (reader != null) {

  144.  
  145. try {

  146.  
  147. reader.close();

  148.  
  149. } catch (IOException e) {

  150.  
  151. e.printStackTrace();

  152.  
  153. }

  154.  
  155. }

  156.  
  157. }

  158.  
  159.  
  160. }

  • 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
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160

这里面对doc、docx、pdf、txt几种类型的文件分别创建了索引,具体的调用方式可以参考《使用Lucene对doc、docx、pdf、txt文档进行全文检索功能的实现》这篇文章,此处就不赘述了。

5 搜索

至此,功能事先就完成了。我们新建一篇文章,上传一个附件进去。然后搜索一个只在附件中才出现过的内容,进行搜索。比如新建一篇文章,如图:

这里写图片描述

里面上传了一个附件,内容为:

这里写图片描述

里面的内容很简单。现在我们在前台页面上搜索“上传附件”这几个汉字,比如输入“使用介绍”,如图:

这里写图片描述

然后点击键盘回车或鼠标点击右侧的搜索按钮,得到搜索结果如下:

这里写图片描述

能够看到,文章的正文内容里直接看是没有“使用介绍”这几个字的,但搜索这几个字还是把这篇文章搜了出来,说明我们建立的索引生效了。

猜你喜欢

转载自blog.csdn.net/qq_37634294/article/details/86415691