solr实战-

Posted mthoutai

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了solr实战-相关的知识,希望对你有一定的参考价值。

实现用户数据索引及查询

1. 启动solr
      solr start

2. 创建collection
      solr create -c user

3. schema中加入field
     3.1 solr-5.2.1/server/solr/user/conf/managed-schema中加入
                <!--定义IK分词类型-->
               <fieldType name="text_ik" class="solr.TextField">
                   <!--索引时候的分词器-->
                   <analyzer type="index" isMaxWordLength="false" class="org.wltea.analyzer.lucene.IKAnalyzer"/>
                   <!--查询时候的分词器-->
                   <analyzer type="query" isMaxWordLength="true" class="org.wltea.analyzer.lucene.IKAnalyzer"/>
               </fieldType>

               <field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" /> 
               <field name="username" type="text_ik" indexed="true" stored="true" multiValued="true"/>
               <field name="age" type="text_ik" indexed="true" stored="true"/>
               <field name="keywords" type="text_ik" indexed="true" stored="true"/>


    3.2 加入IK分词库
            filed定义中使用了IKAnalyzer,须要进入相关配置引用分词器
             a. solr-5.2.1/contrib/analysis-extras/lib中加入IKAnalyzer3.2.8.jar  下载地址: http://download.csdn.net/detail/buyaore_wo/8946777
             b. solrconfig.xml (/solr-5.2.1/server/solr/user/conf)中加入库引用配置,例如以下
                    
<lib dir="${solr.install.dir:../../../..}/contrib/analysis-extras/lib" regex=".*\.jar" />

    
 4.使用SolrJ加入索引数据
      
	/**
	 * 加入文档
	 */
	@Test
	public void addDoc() {
		SolrInputDocument doc = new SolrInputDocument();
		doc.addField("id", "12");
		doc.addField("username", "哈哈");
		doc.addField("keywords", "哈哈 你好");
		doc.addField("age", "18");

		UpdateResponse response;
		try {
			response = httpSolrClient.add(/*"user",*/ doc);
			// 提交
			httpSolrClient.commit();

			// logger.info("########## Query Time :" + response.getQTime());

			System.out.println("########## Query Time :" + response.getQTime());
			// logger.info("########## Elapsed Time :" +
			// response.getElapsedTime());
			System.out.println("########## Elapsed Time :"
					+ response.getElapsedTime());
			// logger.info("########## Status :" + response.getStatus());
			System.out.println("########## Status :" + response.getStatus());
		} catch (SolrServerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

 5.查询数据
   
	@Test
	public void testQuery() {
		SolrQuery solrQuery = new SolrQuery("keywords:*好");
		// solrQuery.setFilterQueries("resourcename:*analytics*");
		// SolrQuery solrQuery = new SolrQuery("*:*");
		// solrQuery.setFields("id", "title");
		solrQuery.setStart(0).setRows(5);
		try {
			QueryResponse queryResponse = httpSolrClient.query(/*"user",*/
					solrQuery);
			// logger.info("results:" +
			// queryResponse.getResults().getNumFound());
			System.out.println("results:"
					+ queryResponse.getResults().getNumFound());
			SolrDocumentList solrDocumentList = queryResponse.getResults();
			for (SolrDocument solrDocument : solrDocumentList) {

				Collection<String> fieldNames = solrDocument.getFieldNames();
				// logger.info("==========================================");
				System.out
						.println("==========================================");
				for (String field : fieldNames) {
					// logger.info(field + ":" +
					// solrDocument.getFieldValue(field));
					System.out.println(field + ":"
							+ solrDocument.getFieldValue(field));
				}
			}
		} catch (SolrServerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}


中途可能遇到下面这种异常
org.apache.solr.client.solrj.impl.HttpSolrClient$RemoteSolrException: Error from server at http://192.168.0.12:8983/solr: Expected mime type application/xml but got text/html. <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>Error 404 Not Found</title>
</head>
<body><h2>HTTP ERROR 404</h2>
<p>Problem accessing /solr/update. Reason:
<pre>    Not Found</pre></p><hr><i><small>Powered by Jetty://</small></i><hr/>
</body>
</html>

原因是没有指定 collection_name

以上是关于solr实战-的主要内容,如果未能解决你的问题,请参考以下文章

在 solr 3.4 中放置突出显示片段配置的位置

Solr 高亮是不是还可以指示返回的片段在原始字段中的位置或偏移量?

Solr Highlighting:如何为同一字段请求多个片段长度?

电子书丨《Solr 实战》

Solr之Java实战

solr实战-