如何实现从slor里拿数据

1.先写一个配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
     <bean id="httpSolrServer" class="org.apache.solr.client.solrj.impl.HttpSolrServer">
     <constructor-arg index="0" value="http://localhost:8983/taotao"/>
     <property name="parser">
     <bean class="org.apache.solr.client.solrj.impl.XMLResponseParser"/>
     </property>
     <property name="maxRetries" value="1"/>
     <property name="connectionTimeout" value="500"/>
     
     </bean>


</beans>

2.写一个方法

package com.mytaotao.search.service;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mytaotao.search.bean.Item;
import com.mytaotao.search.bean.ItemResult;

@Service
public class SearchService {
    @Autowired
    private HttpSolrServer httpSolrServer;

    public ItemResult doGet(String keywords,Integer page,Integer rows) {
        
        //构造搜索条件
        SolrQuery solrQuery=new SolrQuery();
        //搜索关键词
        solrQuery.setQuery("title:"+keywords+" AND status:1");
        //设置分页
        solrQuery.setStart((Math.max(page, 1)-1)*rows);
        solrQuery.setRows(rows);
        //是否需要高亮
        Boolean isHighlighting=!StringUtils.equals("*", keywords)&&StringUtils.isNotBlank(keywords);
        if(isHighlighting) {
            //设置高亮
            solrQuery.setHighlight(true);//开启高亮组件
            solrQuery.addHighlightField("title");//高亮字段
            solrQuery.setHighlightSimplePre("<em>");//标记,高亮关键字前缀
            solrQuery.setHighlightSimplePost("</em>");//后缀
        }
        //执行查询
        try {
            QueryResponse queryResponse =httpSolrServer.query(solrQuery);
            System.out.println(queryResponse);
            long totals = queryResponse.getResults().getNumFound();
            List<Item> items = queryResponse.getBeans(Item.class);
            if(isHighlighting) {
                Map<String, Map<String, List<String>>> map = queryResponse.getHighlighting();
                for(Map.Entry<String, Map<String, List<String>>> highlighting:map.entrySet()) {
                    for(Item item:items) {
                        if(!highlighting.getKey().equals(item.getId().toString())) {
                            continue;
                        }
                        item.setTitle(StringUtils.join(highlighting.getValue().get("title"),""));
                        break;
                    }
                }
            }
            
            return new ItemResult(totals, items);
        
        } catch (SolrServerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        return null;
    }
    
    

}

3.添加jar包

    <dependency>
            <groupId>org.apache.solr</groupId>
            <artifactId>solr-solrj</artifactId>
            <version>4.10.2</version>
        </dependency>

 

上一篇:相关性搜索:利用Solr与Elasticsearch创建智能应用2


下一篇:03-Java基础01