在使用solr可能会需要使用solr来索引文件,如txt,xml,json,word,xlsx,pdf格式的文件。
solr可以读取文件夹中的文件,并为文件的内容创建索引。这里使用tika来读取文件。在(solr目录)\example\example-DIH\solr\tika\conf中,可以使用tika-data-config.xml的配置文件为模板配置,并将配置文件放在core下面的conf文件夹下: <dataConfig> <dataSource type="BinFileDataSource"/> <document> <entity name="file" processor="FileListEntityProcessor" dataSource="null" baseDir="${solr.install.dir}/example/exampledocs" fileName=".*pdf" rootEntity="false"> <field column="file" name="id"/> <entity name="pdf" processor="TikaEntityProcessor" url="${file.fileAbsolutePath}" format="text"> <field column="Author" name="author" meta="true"/> <!-- in the original PDF, the Author meta-field name is upper-cased,but in Solr schema it is lower-cased --> <field column="title" name="title" meta="true"/> <field column="dc:format" name="format" meta="true"/> <field column="text" name="text"/> </entity> </entity> </document> </dataConfig> 在entity file下面还可以添加field: <field column="fileAbsolutePath" name="filePath" /> <field column="fileSize" name="size" /> <field column="fileLastModified" name="lastModified" /> 其中字段‘file’表示文件的名称,字段'fileAbsolutePath'是文件的绝对路径,字段'fileSize'是文件的大小,字段'fileLastModified'是文件的最近修改的时间。在文件的内容部分可以有文档的作者,内容文本,以及标题。在solrconfig.xml中导入配置文件: <requestHandler name="/dataimport" class="solr.DataImportHandler"> <lst name="defaults"> <str name="config">这里输入文件名</str> </lst> </requestHandler> 另外还需要在managed-schema文件中修改字段 <field name="text" type="text_hmm_chinese" indexed="true" stored="true" omitNorms="true" multiValued="false"/> <field name="author" type="string" indexed="true" stored="true" /> <field name="title" type="text_hmm_chinese" indexed="true" stored="true"/> <field name="fileName" type="string" indexed="true" stored="true" /> <field name="filePath" type="string" indexed="true" stored="true" multiValued="false" /> <field name="size" type="plong" indexed="true" stored="true" /> <field name="lastModified" type="pdate" indexed="true" stored="true" /> 在这里text使用的是'text_hmm_chinese'这个是使用lucene自带的中文分词器,需要在配置分词器,配置字段类型: <fieldType name="text_hmm_chinese" class="solr.TextField" positionIncrementGap="100"> <analyzer type="index"> <tokenizer class="org.apache.lucene.analysis.cn.smart.HMMChineseTokenizerFactory"/> </analyzer> <analyzer type="query"> <tokenizer class="org.apache.lucene.analysis.cn.smart.HMMChineseTokenizerFactory"/> </analyzer> </fieldType> 这里的配置文件就可以了,另外在将jar包引入,启动tomcat就可以使用了。