教你使用solr搭建你的全文检索
观察这个指定的solr主位置,里面存在两个文件夹:conf和data.其中conf里存放了对solr而言最为重要的两个配置文件schema.xml和solrconfig.xml.data则用于存放索引文件。
schema.xml主要包括types、fields和其他的一些缺省设置。
solrconfig.xml用来配置Solr的一些系统属性,例如与索引和查询处理有关的一些常见的配置选项,以及缓存、扩展等等。
上面的文档对这两个文件有比较详细的说明,非常容易上手。注意到schema.xml里有一个
<uniqueKey>url</uniqueKey>
的配置,这里将url字段作为索引文档的唯一标识符,非常重要。
三、 加入中文分词
对全文检索而言,中文分词非常的重要,这里采用了qieqie庖丁分词(非常不错:))。集成非常的容易,我下载的是2.0.4-alpha2版本,其中它支持最多切分和按最大切分。创建自己的一个中文TokenizerFactory继承自solr的BaseTokenizerFactory.
/**
* Created by IntelliJ IDEA.
* User: ronghao
* Date: 2007-11-3
* Time: 14:40:59
* 中文切词 对庖丁切词的封装
*/
public class ChineseTokenizerFactory extends BaseTokenizerFactory {
? ? /**
? ???* 最多切分? ?默认模式
? ???*/
? ? public static final String MOST_WORDS_MODE = "most-words";
? ? /**
? ???* 按最大切分
? ???*/
? ? public static final String MAX_WORD_LENGTH_MODE = "max-word-length";
? ? private String mode = null;
? ? public void setMode(String mode) {
? ?? ?? ?? ? if (mode==null||MOST_WORDS_MODE.equalsIgnoreCase(mode)
? ?? ?? ?? ?? ?? ?? ? || "default".equalsIgnoreCase(mode)) {
? ?? ?? ?? ?? ?? ?this.mode=MOST_WORDS_MODE;
? ?? ?? ?? ? } else if (MAX_WORD_LENGTH_MODE.equalsIgnoreCase(mode)) {
? ?? ?? ?? ?? ?? ?this.mode=MAX_WORD_LENGTH_MODE;
? ?? ?? ?? ? }
? ?? ?? ?? ? else {
? ?? ?? ?? ?? ?? ?throw new IllegalArgumentException("不合法的分析器Mode? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?参数设置:" + mode);
? ?? ?? ?? ? }
? ?? ???}
? ? @Override
? ? public void init(Map args) {
? ?? ???super.init(args);
? ?? ???setMode(args.get("mode"));
? ? }
? ? public TokenStream create(Reader input) {
? ?? ???return new PaodingTokenizer(input, PaodingMaker.make(),
? ?? ?? ?? ?? ?? ?createTokenCollector());
? ? }
? ? private TokenCollector createTokenCollector() {
? ?? ???if( MOST_WORDS_MODE.equals(mode))
? ?? ?? ?? ? return new MostWordsTokenCollector();
? ?? ???if( MAX_WORD_LENGTH_MODE.equals(mode))
? ?? ?? ?? ? return new MaxWordLengthTokenCollector();
? ?? ???throw new Error("never happened");
? ? }
? ?? ?}
在schema.xml的字段text配置里加入该分词器。
<fieldtype name="text" positionIncrementGap="100">? ?? ???<analyzer type="index">? ?? ?? ? <tokenizer mode="most-words"/>? ?? ?? ? <filter ignoreCase="true"? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???words="stopwords.txt"/>? ?? ? <filter generateWordParts="1"? ?? ?? ?? ???generateNumberParts="1" catenateWords="1" catenateNumbers="1"? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?catenateAll="0"/>? ?? ?? ? <filter mode="most-words"/>? ?? ?? ?? ?<filter synonyms="synonyms.txt"? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?ignoreCase="true" expand="true"/>? ?? ?? ?<filter ignoreCase="true" words="? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? stopwords.txt"/>? ?? ?<filter generateWordParts="1"? ?? ?? ?? ???generateNumberParts="1" catenateWords="0" catenateNumbers="0"? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?catenateAll="0"/>? ?? ?? ?<filter maxHttpHeaderSize="8192" URIEncoding="UTF-8" …/>
另外,向solr Post请求的时候需要转为utf-8编码。对solr 返回的查询结果也需要进行一次utf-8的转码。检索数据时对查询的关键字也需要转码,然后用“+”连接。
String[] array = StringUtils.split(query, null, 0);
? ?? ???for (String str : array) {
? ?? ?? ?? ?result = result + URLEncoder.encode(str, "UTF-8") + "+";
? ?? ???}
页: [1]
查看完整版本: 教你使用solr搭建你的全文检索