首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网络技术 > 网络基础 >

solr配备搜索引擎

2012-06-28 
solr配置搜索引擎1.下载apache-solr-3.5.0.zip包,解压2.修改example/solr/conf/schema.xml文件,增加fieldt

solr配置搜索引擎

1.下载apache-solr-3.5.0.zip包,解压

2.修改example/solr/conf/schema.xml文件,增加fieldtype节点:

??? <fieldType name="text_cn" protected="protwords.txt"/>
??? ??<filter />
??????? <filter synonyms="synonyms_cn.txt" ignoreCase="true" expand="true"/>
??????? <filter protected="protwords.txt" />
????? </analyzer>
??? </fieldType>

?

3.增加field节点:

?? <field name="text_cn" type="text_cn" indexed="true" stored="true" multiValued="true" />

4.修改为defaultSearchField:

?<defaultSearchField>text_cn</defaultSearchField>

5.修改example/solr/conf/solrconfig.xml,增加lib包:

? <lib dir="../../contrib/analysis-extras/lucene-libs" />
? <lib dir="../../contrib/analysis-extras/lib" />
? <lib dir="../../dist/" regex="apache-solr-analysis-extras-\d.*\.jar" />

6.到example下执行命令启动jetty: java -jar start.jar

?

7.在spring中配置定时任务执行索引:

@Service
public class IndexTask {
?private Constant constant = new Constant();
?
?@Scheduled(cron = "20 13 20 ? * *")
?public void createIndex() {
??String appPath = IndexTask.class.getResource("/").toString();
??appPath = StringUtils.substringAfter(
????StringUtils.substringBefore(appPath, "WEB-INF"), "/");
??String docPath = appPath + "upload/";

??try {
???File file = new File(docPath);
???String urlString = constant.getUrl();
???StreamingUpdateSolrServer solr = new StreamingUpdateSolrServer(
?????urlString, 1, 1);

???solr.deleteByQuery("*:*");
???solr.blockUntilFinished();
???indexFilesSolrCell(file.listFiles(), solr);
??} catch (Exception e) {
???e.printStackTrace();
??}
?}

?private void indexFilesSolrCell(File[] fileList,
???StreamingUpdateSolrServer solr) throws IOException,
???SolrServerException, InterruptedException {
??for (File file : fileList) {
???if (file.isFile()) {
????ContentStreamUpdateRequest up = new ContentStreamUpdateRequest(
??????"/update/extract");
????up.addFile(file);
????up.setParam("uprefix", "attr_");
????up.setParam("fmap.content", "text_cn");
????up.setParam("literal.id", file.getPath());
????up.setAction(AbstractUpdateRequest.ACTION.COMMIT, false, false);
????solr.request(up);
????solr.blockUntilFinished();
????System.out.println("Analysis file:" + file.getPath());
???} else if (file.isDirectory()) {
????indexFilesSolrCell(file.listFiles(), solr);
???}
??}
?}
}

?

8.在struts的action中增加全文检索方法:

?@Action(value = "/listSolrAction", results = { @Result(name = "list", location = "/lucene/listLucene.jsp") })
?public String list() {
??String urlString ="http://localhost:8983/solr";
??System.out.println(urlString);
??try {
???StreamingUpdateSolrServer solr = new StreamingUpdateSolrServer(
?????urlString, 1, 1);

???if (StringUtils.isNotBlank(search)) {
????SolrQuery solrQuery = new SolrQuery();
????solrQuery.set("q", "text_cn:" + search);

????QueryResponse rsp = solr.query(solrQuery);
????SolrDocumentList solrDocumentList = rsp.getResults();
????for (SolrDocument solrDocument : solrDocumentList) {
?????SearchItem searchItem = new SearchItem();

?????searchItem.setHref(solrDocument.getFieldValue("id")
???????.toString());
?????String content = removeSpecial(solrDocument.getFieldValue(
???????"text_cn").toString());
?????content = StringUtils.left(content, 30);
?????searchItem.setContent(content);

?????String title = "";
?????Object titleO = solrDocument.getFieldValue("title");
?????if (titleO == null) {
??????title = StringUtils.left(content, 10);
?????} else {
??????title = removeSpecial(titleO.toString());
?????}
?????searchItem.setTitle(title);

?????searchItemList.add(searchItem);
????}
???}
??} catch (Exception e) {
???e.printStackTrace();
??}
??return "list";
?}

?

9.jsp页面内容:

<s:form action="listSolrAction">
??<s:textfield name="search"></s:textfield>
??<s:submit></s:submit>
?</s:form>
?<table>
??<s:iterator var="searchItem" value="searchItemList">
???<tr>
????<td><a href='<s:property value="href"/>'><s:property
???????value="title" /> </a></td>
???</tr>
???<tr>
????<td><s:property value="content"/></td>
???</tr>
??</s:iterator>
?</table>

热点排行