当前位置: 首页 > 工具软件 > SolrJ > 使用案例 >

SolrJ

罗智刚
2023-12-01

SolrJ

solr的安装

(1) 引入相关依赖

<!-- solr客户端 -->
        <dependency>
            <groupId>org.apache.solr</groupId>
            <artifactId>solr-solrj</artifactId>
        </dependency>

(2) 编写配置文件:applicationContext-solr.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" 
    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-4.2.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd 
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
    http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
    
    <!-- 单击版solr -->
    <bean id="solrServer" class="org.apache.solr.client.solrj.impl.HttpSolrServer">
        <constructor-arg name="baseURL" value="http://192.168.204.136:8080/solr/collection1"/>
    </bean>
    <!-- 集群版solr -->
    <!-- <bean id="solrServer" class="org.apache.solr.client.solrj.impl.CloudSolrServer">
        <constructor-arg name="zkHost" value="192.168.204.136:2182,192.168.204.136:2183,192.168.204.136:2184"/>
        <property name="defaultCollection" value="collection2"></property>
    </bean> -->
</beans>

(3) 导入数据到solr索引库

@Service
public class SearchItemServiceImpl implements SearchItemService {
    @Autowired
    private SearchItemMapper searchItemMapper;
    
    @Autowired
    private SolrServer solrServer;
    
    public Result importItemsToIndex() {
        try {
            List<SearchPojo> itemList = searchItemMapper.getItemList();
            for (SearchPojo searchPojo : itemList) {
                SolrInputDocument document = new SolrInputDocument();
                document.addField("id", searchPojo.getId());
                document.addField("item_title", searchPojo.getTitle());
                document.addField("item_sell_point", searchPojo.getSell_point());
                document.addField("item_price", searchPojo.getPrice());
                document.addField("item_image", searchPojo.getImage());
                document.addField("item_desc", searchPojo.getItem_desc());
                document.addField("item_category_name", searchPojo.getCategory_name());

                solrServer.add(document);                
            }
            solrServer.commit();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.build(500, "索引库导入失败!");
        }
        return Result.ok();
    }
}

(4) 搜索solr索引库

@Service
public class SearchServiceImpl implements SearchService {
    @Autowired
    private SearchDao searchDao;
    
    public SearchResult search(String queryString, int page, int rows) throws Exception {
        SolrQuery query = new SolrQuery(queryString);
        if (page < 1) {
            page = 1;
        }
        if (rows < 1) {
            rows = 10;
        }
        query.setStart((page - 1) * rows);
        query.setRows(rows);
        // 设置默认搜索域
        query.set("df", "item_title");
        // 设置高亮
        query.setHighlight(true);    // 开启高亮
        query.addHighlightField("item_title");    // 显示高亮的域
        query.setHighlightSimplePre("<font color='red'>");    //设置高亮颜色
        query.setHighlightSimplePost("</font>");
        
        SearchResult result = searchDao.search(query);
        // 计算总页数
        long recordCount = result.getRecordCount();
        long pages = recordCount / rows;
        if (recordCount % rows > 0) {
            pages++;
        }
        result.setTotalPages(pages);        
        return result;
    }
}

 

转载于:https://www.cnblogs.com/lin-nest/p/10322494.html

 类似资料: