第五课 使用solrJ操作solr常用方法

2018-08-27  本文已影响48人  Arroganter

既然学的是java那么肯定需要用java代码来进行对solr的操作,如果知道在solr后台管理界面进行增删改查等操作,那么用solrJ操作solr会更好理解。

solrJ介绍
solrJ是一个用来访问solr的java客户端,提供了索引和搜索的方法(将一些常用的命令封装进去了),通过solrJ提供的API 接口来操作solr服务。


image.png

准备工作
创建个maven工程(普通的java工程都可以不过需要自己导包),添加依赖如下:

<dependencies>
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>4.10.2</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
然后启动solr服务器,接下来进行操作。
1、添加文档

/*
 * 测试向索引库中添加文档
 */
@Test
public void testSave() throws Exception{
    //1.创建连接对象
    SolrServer solrServer = new HttpSolrServer("http://192.168.25.128:8080/solr/collection1");
    //2.创建一个文档对象
    SolrInputDocument inputDocument = new SolrInputDocument();
    //向文档中添加域以及对应的值,注意:所有的域必须在schema.xml中定义过,前面已经给出过我定义的域。
    inputDocument.addField("id", "1");
    inputDocument.addField("item_title", "sansung爆炸牌手机");
    inputDocument.addField("item_price", 666);
    inputDocument.addField("item_image", "www.boom.png");
    //3.将文档写入索引库中
    solrServer.add(inputDocument);
    //提交
    solrServer.commit();
}

后台管理界面查询id为1的商品看看

image.png

2、修改文档

/*
 * 测试修改索引库中已存在的文档
 */
@Test
public void testUpdate() throws Exception{
    //1.创建连接对象
    SolrServer solrServer = new HttpSolrServer("http://192.168.25.128:8080/solr/collection1");
    //2.创建一个文档对象
    SolrInputDocument inputDocument = new SolrInputDocument();
    inputDocument.addField("id", "1");
    //修改id为1的商品的信息(如果该商品不存在其实就是添加了)
    inputDocument.addField("item_title", "vivo手机hahaha");
    inputDocument.addField("item_price", 6666);
    inputDocument.addField("item_image", "www.123.png");
    //3.将文档写入索引库中
    solrServer.add(inputDocument);
    //提交
    solrServer.commit();
}

再去后台管理页面查询id为1的商品看看


image.png

3、删除文档

/*
 * 测试删除文档:根据id删除文档 *
 */
@Test
public void testDeleteById() throws Exception{
    SolrServer solrServer = new HttpSolrServer("http://192.168.25.128:8080/solr/collection1");
    //删除文档
    solrServer.deleteById("1");
    //提交
    solrServer.commit();
}
/*
 * 测试删除文档:根据查询结果删除文档(重新添加id为1的文档)
 */
@Test
public void testDeleteByQ() throws Exception{
    SolrServer solrServer = new HttpSolrServer("http://192.168.25.128:8080/solr/collection1");
    //根据查询结果删除文档,注意:用item_image的查询结果来进行删除是不行的
    //因为制定业务域的时候indexed=false,即不被索引,此时是不能根据图片来查询的。
    solrServer.deleteByQuery("item_title:vivo手机hahaha");
    solrServer.commit();
}

不管是根据id删除还是根据查询删除都能达到一样的效果。
4、查询索引

1、简单根据id查询索引

/*
 * 简单查询:查询单个商品信息
 */
@Test
public void testSimpleQ() throws Exception{
    //1.创建连接
    SolrServer solrServer = new HttpSolrServer("http://192.168.25.128:8080/solr/collection1");
    //2.创建查询语句
    SolrQuery query = new SolrQuery();
    //3.设置查询条件
    query.set("q", "id:1");
    //4.执行查询
    QueryResponse queryResponse = solrServer.query(query);
    //5.取文档列表public class SolrDocumentList extends ArrayList<SolrDocument>
    SolrDocumentList documentList = queryResponse.getResults();
    for (SolrDocument solrDocument : documentList) {
        //取各个文档信息
        System.out.println("商品id:"+solrDocument.get("id")+" ");
        System.out.println("商品标题:"+solrDocument.get("item_title")+" ");
        System.out.println("商品价格:"+solrDocument.get("item_price")+" ");
        System.out.println("商品图片:"+solrDocument.get("item_image")+" ");
    }
}

输出结果:
商品id:1
商品标题:vivo手机hahaha
商品价格:6666
商品图片:www.123.png
2、设置条件进行查询

@Test
public void testSimpleQ2 () throws Exception{
//1.创建连接
SolrServer solrServer = new HttpSolrServer("http://192.168.25.128:8080/solr/collection1");
//2.创建查询语句
SolrQuery query = new SolrQuery();
//3.设置查询条件
query.set("q", "夏普");//设置查询关键字
query.setSort("id", ORDER.desc);//按照id降序排列
query.setStart(0);
query.setRows(2);//分页条件
query.set("df", "item_title");//默认在商品标题域进行查询
//4、执行查询
QueryResponse queryResponse = solrServer.query(query);
//5.获取文档列表
SolrDocumentList documentList = queryResponse.getResults();
//获取总记录数
long numFound = documentList.getNumFound();
System.out.println("总记录数:" + numFound);
for (SolrDocument solrDocument : documentList) {
//取各个文档信息
System.out.print("商品id:"+solrDocument.get("id")+" ");
System.out.print("商品标题:"+solrDocument.get("item_title")+" ");
System.out.print("商品价格:"+solrDocument.get("item_price")+" ");
System.out.println();
}
}
注:如果设置了默认查询域的话,自己又不想在这个域内查那么在查询关键字前要注明要在哪个域查,比如:”item_price:600”

输出结果:
总记录数:19
商品id:936920 商品标题:夏普(SHARP)LCD-52DS70A 52英寸 日本原装液晶面板 3D Android操作系统智能液晶电视 商品价格:699900
商品id:816753 商品标题:夏普(SHARP)LCD-46DS40A 46英寸 日本原装液晶面板 智能全高清液晶电视 商品价格:379900
商品id:1356054 商品标题:夏普(SHARP)LCD-50DS72A 50英寸 无线网络 安卓智能 4K超高清液晶电视 商品价格:549900
3、带高亮的复杂查询

@Test
public void testSimpleQuery() throws IOException, SolrServerException {
SolrServer solrServer = new HttpSolrServer("http://192.168.238.131:8080/solr/collection1");

    SolrQuery solrQuery = new SolrQuery();
    solrQuery.set("q","手机");
    solrQuery.set("df","item_keywords");//df=>default field
    solrQuery.setSort("id", SolrQuery.ORDER.asc);

// solrQuery.setStart(2);
// solrQuery.setRows(2);

    //开启高亮显示
    solrQuery.setHighlight(true);
    solrQuery.addHighlightField("item_title");
    solrQuery.addHighlightField("item_sell_point");
    solrQuery.setHighlightSimplePre("<em>");
    solrQuery.setHighlightSimplePost("</em>");

    QueryResponse queryResponse = solrServer.query(solrQuery);
    SolrDocumentList solrDocumentList = queryResponse.getResults();
    Map<String, Map<String, List<String>>> mapMapHighlighting = queryResponse.getHighlighting();

    for(SolrDocument solrDocument : solrDocumentList)
    {
        String id = (String)solrDocument.get("id");

        Map<String, List<String>> map = mapMapHighlighting.get(id);//一个商品,一个json对象

// {
// "id": "988",
// "item_title": "neusoft爆炸牌手机neusoft爆炸牌手机",
// "item_price": 666,
// "item_image": "www.boom.png",
// "item_category_name": "东软股份",
// "version": 1608444819718275000
// }
for(Map.Entry<String, List<String>> entry : map.entrySet())
{
System.out.print(entry.getKey() +":"+ entry.getValue().get(0));
System.out.println();
}
System.out.println("----------------");
}

}

输出结果:
item_title neusoft爆炸牌<em>手机</em>neusoft爆炸牌<em>手机</em>
item_sell_point 东软股份<em>手机</em>


item_title neusoft爆炸牌<em>手机</em>

item_title neusoft爆炸牌<em>手机</em>

item_title neusoft爆炸牌<em>手机</em>neusoft爆炸牌<em>手机</em>

如果看了关于在solr后台管理页面进行的相关操作后,再看这个solrJ应该就瞬间会了。

上一篇下一篇

猜你喜欢

热点阅读