1、windows10下配置solr环境,将solr官网下载好的压缩包解压到指定目录,solr的项目结构如下
cmd进入server/solr目录下启动solr服务:
浏览器访问:http://localhost:8983/solr
创建核心文档
2、项目集成:
pom.xml文件添加依赖
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-solr</artifactId>
</dependency>
application.yml配置
data:
solr:
host: http://192.168.168.224:8983/solr/searchList
业务代码:
solr工具类
@Component
public class SolrUtils {
private SolrClient solrClient;
private SolrQuery solrQuery;
@Autowired
public SolrUtils(SolrClient solrClient) {
this.solrClient = solrClient;
solrQuery = new SolrQuery();
}
/**
* 向solr文档中添加对象
* @param data
* @throws IOException
* @throws SolrServerException
*/
public void addSolrPO(SolrSearchPO data) throws IOException, SolrServerException {
solrClient.addBean(data);
solrClient.commit();
}
/**
* solr查询
* @param key 关键字
* @param page
* @param row
* @return
* @throws IOException
* @throws SolrServerException
*/
public List<SolrSearchPO> queryList(String key,int page,int row) throws IOException, SolrServerException {
List<SolrSearchPO> list=new ArrayList<>();
solrQuery.setQuery(key);
solrQuery.setStart(page);
solrQuery.setRows(row);
//设置高亮字段
solrQuery.setHighlight(true);
solrQuery.addHighlightField("name,title");
solrQuery.setHighlightSimplePre("<font color='red'>");
solrQuery.setHighlightSimplePost("</font>");
QueryResponse query = solrClient.query(solrQuery);
// 文档结果集
SolrDocumentList docs = query.getResults();
SolrSearchPO solrSearchPO;
for (SolrDocument s : docs) {
solrSearchPO = new SolrSearchPO();
solrSearchPO.setId(s.getFirstValue("id").toString());
solrSearchPO.setName(s.getFirstValue("name").toString());
solrSearchPO.setTitle(s.getFirstValue("title").toString());
solrSearchPO.setContent(s.getFirstValue("content").toString());
list.add(solrSearchPO);
}
return list;
}
/**
* 删除
* @throws IOException
* @throws SolrServerException
*/
public void delDoc() throws IOException, SolrServerException {
solrClient.deleteByQuery("*:*");
solrClient.commit();
}
}
测试
@RunWith(SpringRunner.class)
@SpringBootTest
public class SolrTest {
private Logger logger = LoggerFactory.getLogger(SolrTest.class);
@Autowired
private SolrUtils solrUtils;
@Autowired
private ITestService testService;
@Test
public void addSolr() {
SolrSearchPO solrSearchPO;
List<TestPO> allTestPo;
try {
allTestPo = testService.findAllTestPo(3);
for (TestPO t : allTestPo) {
solrSearchPO = new SolrSearchPO();
solrSearchPO.setId(String.valueOf(t.getId()));
solrSearchPO.setName(t.getName());
solrSearchPO.setContent(t.getContent());
solrSearchPO.setTitle(t.getTitle());
solrUtils.addSolrPO(solrSearchPO);
}
} catch (Exception e) {
e.printStackTrace();
logger.error("添加错误--->"+e.toString());
}
}
@Test
public void queryList() {
try {
List<SolrSearchPO> solrSearchPOS = solrUtils.queryList("*:*", 0, 10);
logger.info("搜索结果集长度---------->size=" + solrSearchPOS.size());
for (SolrSearchPO s : solrSearchPOS) {
logger.info(s.toString());
}
} catch (IOException | SolrServerException e) {
e.printStackTrace();
logger.info("搜索失败------>e=" + e.toString());
}
}
@Test
public void del() {
try {
solrUtils.delDoc();
logger.info("删除solr索引成功");
} catch (IOException | SolrServerException e) {
e.printStackTrace();
logger.info("删除solr索引失败");
}
}
}