陋室铭
永远也不要停下学习的脚步(大道至简至易)

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.request.FieldAnalysisRequest;
import org.apache.solr.client.solrj.response.AnalysisResponseBase.AnalysisPhase;
import org.apache.solr.client.solrj.response.AnalysisResponseBase.TokenInfo;
import org.apache.solr.client.solrj.response.FieldAnalysisResponse;

import base.util.ConfigUtil;

public class SolrService {
    private static Logger log = Logger.getLogger(SolrService.class);

    private static HttpSolrServer solrServer;

    static {
        solrServer = new HttpSolrServer(ConfigUtil.getValue("solr.url"));
        solrServer.setConnectionTimeout(5000);
    }

    /**
     * 给指定的语句分词。
     * 
     * @param sentence 被分词的语句
     * @return 分词结果
     */
    public static List<String> getAnalysis(String sentence) {
        FieldAnalysisRequest request = new FieldAnalysisRequest(
                "/analysis/field");
        request.addFieldName("title");// 字段名,随便指定一个支持中文分词的字段
        request.setFieldValue("");// 字段值,可以为空字符串,但是需要显式指定此参数
        request.setQuery(sentence);

        FieldAnalysisResponse response = null;
        try {
            response = request.process(solrServer);
        } catch (Exception e) {
            log.error("获取查询语句的分词时遇到错误", e);
        }

        List<String> results = new ArrayList<String>();
        Iterator<AnalysisPhase> it = response.getFieldNameAnalysis("title")
                .getQueryPhases().iterator();
        while(it.hasNext()) {
          AnalysisPhase pharse = (AnalysisPhase)it.next();
          List<TokenInfo> list = pharse.getTokens();
          for (TokenInfo info : list) {
              results.add(info.getText());
          }

        }

        return results;
    }

}

 

@Test
public void getAnalysis() {
    List<String> results = SolrService.getAnalysis("DevNote与大家分享开发实践经验");
    for (String word : results) {
        System.out.println(word);
    }
}

运行效果:
devnote
与
大家
分享
开发
实践经验
posted on 2018-09-19 17:09  宏宇  阅读(404)  评论(0编辑  收藏  举报