• 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

  • 相关阅读:
    迈瑞医疗招聘-软件测试工程师
    软件自动化测试开发-3期开班啦
    luogu P2744 [USACO5.3]量取牛奶Milk Measuring
    luogu P2515 [HAOI2010]软件安装
    luogu P2423 双塔
    luogu P1651 塔
    luogu P1489 猫狗大战
    luogu P3092 [USACO13NOV]没有找零No Change
    luogu P3800 Power收集
    luogu P2949 [USACO09OPEN]工作调度Work Scheduling
  • 原文地址:https://www.cnblogs.com/mthoutai/p/6920301.html
Copyright © 2020-2023  润新知