schema.xml位于solr/collection1/conf/目录下,是Solr中用户定义字段类型及字段的配置文件.
Solr版本: 4.6.0
第一步: Schema.xml说明
实例schema.xml结构同solr/collection1/conf/schema.xml结构一致,具体配置通过注解已注明.
<?xml version="1.0" encoding="UTF-8" ?>
<schema name="example" version="1.5">
<fields><!--field属性说明:filed字段用于定义数据源字段所使用的搜索类型与相关设置.name:数据源字段名,搜索使用到.type:搜索类型名例如中文ika搜索名text_ika,对应于fieldType中的name.不需要分词的字符串类型,string即可,如果需要分词,types中配置好的分词type。indexed:是否被索引,只有设置为true的字段才能进行搜索排序分片(earchable, sortable, facetable)。stored:是否存储内容,如果不需要存储字段值,尽量设置为false以提高效率。multiValued:是否为多值类型,SOLR允许配置多个数据源字段存储到一个搜索字段中。多个值必须为true,否则有可能抛出异常。--><field name="id" type="string" indexed="true" stored="true" required="true" /> <field name="name" type="text_ik" indexed="true" stored="true" multiValued="false"/> <field name="phone" type="string" indexed="false" stored="true" /> <field name="email" type="string" indexed="false" stored="true" multiValued="true" /> <field name="city_id" type="int" indexed="true" stored="true" /> <field name="address" type="text_ik" index="true" stored="true" /> <field name="all" type="string" index="true" stored="true" /><!-- 动态字段定义通过*来定义 --><dynamicField name="*_i" type="int" indexed="true" stored="true"/><dynamicField name="*_s" type="string" indexed="true" stored="true"/></fields><!--uniqueKey节点设置主键,solr必须有一个主键,一般为id也可以自行定义.这个字段决定和增强文档的唯一性-->
<uniqueKey>id</uniqueKey><!--defaultSearchField节点默认搜索的字段,默认值为text, 如果我们已经将需要搜索的字段拷贝至all字段了,在这里设为all即可-->
<defaultSearchField>text</defaultSearchField> <!--solrQueryParser节点默认搜索操作符参数,及搜索短语间的逻辑,用AND增加准确率,用OR增加覆盖面,建议用AND,也可在搜索语句中定义。例如搜索"Java 多线程",使用AND默认搜索为"Java AND 多线程"-->
<solrQueryParser defaultOperator="OR"/><!--copyField节点如果我们的搜索需要搜索多个字段该怎么办呢?这时候,我们就可以使用copyField节点,我们将所有的中文分词字段全部拷贝至all中,当我们进行全文检索是,只用搜索all字段就OK了.
-->
<copyField source="name" dest="all" />
<copyField source="phone" dest="all" />
<copyField source="email" dest="all" /><types><!--定义字段处理类型 --><fieldType name="int" class="solr.TrieIntField" precisionStep="0" omitNorms="true" positionIncrementGap="0" /> <fieldType name="float" class="solr.TrieFloatField" precisionStep="0" omitNorms="true" positionIncrementGap="0" /> <fieldType name="double" class="solr.TrieDoubleField" precisionStep="0" omitNorms="true" positionIncrementGap="0" /> <fieldType name="string" class="solr.StrField" sortMissingLast="true" omitNorms="true" /> <fieldType name="text" class="solr.TextField" positionIncrementGap="100"> <analyzer> <tokenizer class="solr.WhitespaceTokenizerFactory" /> </analyzer> </fieldType> <!-- 定义常规分词 类型--><fieldType name="text_general" class="solr.TextField" positionIncrementGap="100"><!-- 建立索引时的分词器配置 --><analyzer type="index"><!-- 建立索引时使用标准分词器 --><tokenizer class="solr.StandardTokenizerFactory"/><!-- 停用词过滤器, 用于索引文档中的停用词去掉 --><filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" /><!-- 大写转小写过滤器 --><filter class="solr.LowerCaseFilterFactory"/></analyzer><!-- 查询的时候使用的分词器 --><analyzer type="query"><!-- 查询索引时使用标准分词器 --><tokenizer class="solr.StandardTokenizerFactory"/><!-- 停用词过滤器, 用于索引文档中的停用词去掉 --><filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" /><!-- 定义查询的时使用同义词过滤器 --><filter class="solr.SynonymFilterFactory" synonyms="synonyms.txt" ignoreCase="true" expand="true"/><!-- 大写转小写过滤器 --><filter class="solr.LowerCaseFilterFactory"/></analyzer></fieldType><!--定义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></types></schema>
在默认的solr/collection1/conf/schema.xml文件中,有如下field设定,其中title配置为允许多个值,所以我们抽象类中可以用集合标示.
<field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" />
<field name="title" type="text_general" indexed="true" stored="true" multiValued="true"/>
<field name="author" type="text_general" indexed="true" stored="true"/>
第二步: 创建相应的抽象类
package com.test.model;
import java.io.Serializable;
import java.util.List;
import org.apache.commons.lang.builder.ToStringBuilder;public class Article implements Serializable{/*** */private static final long serialVersionUID = 4017316764889231758L;private String id;private List<String> title;private String author;public String getId() {return id;}public void setId(String id) {this.id = id;}public String getAuthor() {return author;}public void setAuthor(String author) {this.author = author;}public List<String> getTitle() {return title;}public void setTitle(List<String> title) {this.title = title;}@Overridepublic String toString() {return ToStringBuilder.reflectionToString(this);}}
第三步: 创建solr客户端
package com.plugin.solr.client;import java.util.Collections;
import java.util.HashMap;
import java.util.Map;import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;public class SolrClient {private static final Logger LOG = LoggerFactory.getLogger(SolrClient.class);private static Map<String, SolrServer> solrServerMap = Collections.synchronizedMap(new HashMap<String, SolrServer>());/*** 获取HttpSolrServer* * @param SOLR_URL* * @return SolrServer*/public static SolrServer getHttpSolrServer(final String solrURL) {SolrServer solrServer = null;if (!solrServerMap.containsKey(solrURL)) {try {solrServer = new HttpSolrServer(solrURL);if (solrServer != null) {solrServerMap.put(solrURL, solrServer);LOG.info("Load " + solrURL + " finish.");}} catch (Exception e) {LOG.warn("sorlURL error ," + solrURL);e.printStackTrace();}}return solrServerMap.get(solrURL);}
}
第四步: 编写客户端连接检测方法并测试
/*** ping检测solr是否down掉 [测试通过]* @param server* @return*/public static String ping(SolrServer server){try {return server.ping().getResponse().toString();} catch (SolrServerException e) {LOG.error("Solr system ping error " + e.getMessage(), e);} catch (IOException e) {LOG.error("Solr system ping error " + e.getMessage(), e);}return null;}
启动tomcat服务器(前提是solr与tomcat已集成),本地tomcat端口为8888,编写junit测试
package com.test.search;import java.util.ArrayList;
import java.util.List;
import java.util.UUID;import org.apache.solr.client.solrj.SolrServer;
import org.junit.Before;
import org.junit.Test;import com.plugin.page.Page;
import com.plugin.solr.client.SolrClient;
import com.plugin.solr.engine.SolrEngineHandler;
import com.test.model.Article;public class SolrTest {private SolrServer server;@Beforepublic void init(){String solrURL = "http://localhost:8888/solr"; server = SolrClient.getHttpSolrServer(solrURL);}@Testpublic void pingSolr(){System.out.println("ping solr result: " +SolrEngineHandler.ping(server));}
}
运行结果
ping solr result: {responseHeader={status=0,QTime=656,params={df=text,echoParams=all,rows=10,echoParams=all,wt=javabin,version=2,q=solrpingquery,distrib=false}},status=OK}
连接成功.
转载请注明出处:[http://www.cnblogs.com/dennisit/p/3620597.html]