标签:
<!--定义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"/>
<lib dir="${solr.install.dir:../../../..}/contrib/analysis-extras/lib" regex=".*\.jar" />
/** * 添加文档 */ @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(); } }
@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(); } }
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/buyaore_wo/article/details/47151695