java中使用solr_Java代码里面使用solr

新建一个Maven项目

项目结构

编写代码

(1)导入solr的pom依赖

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.wjx

solr

1.0-SNAPSHOT

org.apache.solr

solr-solrj

7.1.0

junit

junit

4.12

(2)测试Solr类

包括新增,查询,根据Id查询,查询全部,高亮显示

package com.wjx;

import org.apache.solr.client.solrj.SolrQuery;

import org.apache.solr.client.solrj.SolrServerException;

import org.apache.solr.client.solrj.impl.HttpSolrClient;

import org.apache.solr.client.solrj.response.QueryResponse;

import org.apache.solr.common.SolrDocument;

import org.apache.solr.common.SolrDocumentList;

import org.apache.solr.common.SolrInputDocument;

import org.junit.Test;

import java.io.IOException;

import java.util.List;

import java.util.Map;

/**

* @Description: solr测试类

* @Auther: wjx

* @Date: 2019/2/13 14:28

*/

public class SolrTest {

/**

* solr的java客户端

*/

private HttpSolrClient solrClient;

/**

* 获取连接

*

* @return

*/

private HttpSolrClient getSolrClient() {

synchronized (this) {

if (solrClient == null) {

solrClient = new HttpSolrClient.Builder().withBaseSolrUrl("http://114.116.24.32:8080/solr/collection_1").build();

}

}

return solrClient;

}

/**

* 测试提交solr到服务器

*

* @throws IOException

* @throws SolrServerException

*/

@Test

public void testAddDocument() throws IOException, SolrServerException {

//获取solr的服务

getSolrClient();

for (int i = 0; i < 100; i++) {

SolrInputDocument document = new SolrInputDocument();

document.addField("id", i);

document.addField("title_ik", "这是一部手机" + i);

document.addField("content_ik", "手机描述" + i);

solrClient.add(document);

}

//提交

solrClient.commit();

//关闭

solrClient.close();

System.out.println("提交成功...");

}

/**

* 根据Id删除

*

* @throws IOException

* @throws SolrServerException

*/

@Test

public void testDeleteId() throws IOException, SolrServerException {

//获取solr的服务

getSolrClient();

solrClient.deleteById("200");

solrClient.commit();

solrClient.close();

}

/**

* 删除所有

*

* @throws IOException

* @throws SolrServerException

*/

@Test

public void testDeleteAll() throws IOException, SolrServerException {

getSolrClient();

solrClient.deleteByQuery("*:*");

solrClient.commit();

solrClient.close();

}

/**

* 全部查询

*

* @throws IOException

* @throws SolrServerException

*/

@Test

public void testAllQuery() throws IOException, SolrServerException {

getSolrClient();

//设置条件

SolrQuery solrQuery = new SolrQuery();

//查询的前100条记录

solrQuery.setRows(100);

//查询的条件

solrQuery.setQuery("*:*");

//查询返回的对象

QueryResponse response = solrClient.query(solrQuery);

//查询返回的集合

SolrDocumentList results = response.getResults();

for (SolrDocument document : results) {

Object id = document.get("id");

Object title_ik = document.get("title_ik");

Object content_ik = document.get("content_ik");

System.out.println("id:" + id + ",title_ik:" + title_ik + ",content_ik:" + content_ik);

}

solrClient.close();

}

/**

* 分页查询

*

* @throws SolrServerException

* @throws IOException

*/

@Test

public void testQueryByPage() throws SolrServerException, IOException {

getSolrClient();

// 以后参数都是通过这个对象去构造...

SolrQuery solrQuery = new SolrQuery();

//solrParams.setQuery("title_ik:手机"); 注释掉,按照条件查询

solrQuery.setQuery("*:*");

//分页

solrQuery.setStart(10);//当前查询起始页

solrQuery.setRows(10);//每页显示多少条数据

QueryResponse queryResponse = solrClient.query(solrQuery);

// 返回结果,默认只返回10条记录

SolrDocumentList documentList = queryResponse.getResults();

for (SolrDocument solrDocument : documentList) {

Object title_ik = solrDocument.get("title_ik");

System.out.println(title_ik);

}

}

/**

* 高亮显示

*

* @throws IOException

* @throws SolrServerException

*/

@Test

public void testHighlighting() throws IOException, SolrServerException {

getSolrClient();

// 以后参数都是通过这个对象去构造...

SolrQuery solrQuery = new SolrQuery();

solrQuery.setQuery("title_ik:手机");

//分页

solrQuery.setStart(0);//当前查询起始页

solrQuery.setRows(10);//每页显示多少条数据

//开启高亮

solrQuery.setHighlight(true);

//设置需要高亮显示的字段名

solrQuery.setParam("hl.fl", "title_ik");

//设置高亮前置显示

solrQuery.setHighlightSimplePre("");

//设置高亮后缀显示

solrQuery.setHighlightSimplePost("");

QueryResponse response = solrClient.query(solrQuery);

SolrDocumentList documentList = response.getResults();

Map>> highlightingMap = response.getHighlighting();

for (SolrDocument document : documentList) {

//id

Object id = document.get("id");

//根据Id查到高亮显示的结果

Map> stringList = highlightingMap.get(id);

System.out.println(stringList);

}

}

}

高亮显示结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值