solr学习笔记(3)--使用solrj调用solr服务

xiaoxiao2021-02-28  77

solrj是solr的java客户端,用于访问solr索引库。它提供了添加、删除、查询、还有高亮、分页等。 

1.使用的jar包

目录solr-4.7.0/dist/solrj-lib,里面就存放了solrj所用到的jar,再添加dist目录下的一些核心包

2.调用代码

package com.skiff.www.dao; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Map; import org.apache.solr.client.solrj.SolrQuery; import org.apache.solr.client.solrj.SolrServer; 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.apache.solr.common.SolrDocument; import org.apache.solr.common.SolrDocumentList; import org.apache.solr.common.SolrInputDocument; import org.apache.solr.common.params.SolrParams; import org.junit.Test; import com.skiff.www.bean.Article; /** * @ClassName: SolrJ * @Description 类的作用: 使用solrj来调用solr 的服务,前提在本地已启动了solr服务器 * @author 作者:一叶扁舟(skiff) * @date 2017年5月5日 上午9:21:06 * */ public class SolrJ { @Test public void addIndex() throws SolrServerException, IOException{ String urlString = "http://localhost:8983/solr"; SolrServer solr = new HttpSolrServer(urlString); List<Article> list=new ArrayList<Article>(); Article article=null; for(int i=1;i<=12;i++){ article=new Article(); //第二种添加方式... article.setId(i); article.setTitle("solr简介"); String content = "采用Java开发,基于Lucene的全文搜索服务器。同时对其进行了扩展,提供了比Lucene更为丰富的查询语言,同时实现了可配置、可扩展并对查询性能进行了优化,并且提供了一个完善的功能管理界面,是一款非常优秀的全文搜索引擎。" +" Solr是一个独立的企业级搜索应用服务器,它对外提供类似于Web-service的API接口。用户可以通过http请求,向搜索引擎服务器提交一定格式的XML文件,生成索引;也可以通过Http G SolrJ操作提出查找请求,并得到XML格式的返回结果。"; article.setContent(content); article.setPrice(19); article.setName("一叶扁舟"); list.add(article); } //第一种添加方式... /*SolrInputDocument document=new SolrInputDocument(); document.addField("id", "9527"); document.addField("name", "一叶扁舟"); document.addField("xxxxx_ss", "很有含义的一个名字,在IT届,知识就像大海里面的一叶扁舟"); solr.add(document);*/ // solr.addBean(article); solr.addBeans(list); solr.commit(); } @Test public void delete() throws SolrServerException, IOException{ String urlString = "http://localhost:8983/solr"; SolrServer solr = new HttpSolrServer(urlString); solr.deleteById("1"); solr.commit(); } //更新的话如果是id 相同,它会直接更新 @Test public void testFind() throws SolrServerException{ String urlString = "http://localhost:8983/solr"; SolrServer solr = new HttpSolrServer(urlString); //以后参数都是通过这个对象去构造... SolrQuery solrParams=new SolrQuery(); solrParams.setQuery("name:叶"); //分页 // solrParams.setStart(0); // // solrParams.setRows(10); //开启高亮... solrParams.setHighlight(true); //高亮显示的格式... solrParams.setHighlightSimplePre("<font color='red'>"); solrParams.setHighlightSimplePost("</font>"); //我需要那几个字段进行高亮... solrParams.setParam("hl.fl", "name"); QueryResponse queryResponse=solr.query(solrParams); //返回所有的结果... SolrDocumentList documentList=queryResponse.getResults(); Map<String, Map<String, List<String>>> maplist=queryResponse.getHighlighting(); //返回高亮之后的结果.. for(SolrDocument solrDocument:documentList){ Object id=solrDocument.get("id"); // Object name=solrDocument.get("name"); // Object content=solrDocument.get("description"); // System.out.println(id); // System.out.println(name); // System.out.println(content); Map<String, List<String>> fieldMap=maplist.get(id); List<String> stringlist=fieldMap.get("name"); System.out.println(stringlist); } } }

package com.skiff.www.bean; import org.apache.solr.client.solrj.beans.Field; /** * @ClassName: Article * @Description 类的作用: 模拟一个文章的实体类 * @author 作者:一叶扁舟(skiff) * @date 2017年5月5日 上午9:26:33 * */ public class Article { @Field(value="id") private int id; @Field(value="title") private String title; @Field(value="name") private String name; @Field(value="content") private String content; @Field(value="price") private double price; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getTitle() { return this.title; } public void setTitle(String title) { this.title = title; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public String getName() { return name; } public void setName(String name) { this.name = name; } }

3.启动example下面的start.jar,访问:http://localhost:8983/solr/#/collection1/query,效果图:

转载请注明原文地址: https://www.6miu.com/read-33866.html

最新回复(0)