solrj之solr基本应用!

来源:互联网 发布:淘宝联盟做什么的 编辑:程序博客网 时间:2024/05/22 15:00

需要打开经过和solr整合的tomcat服务器:

修改solr的conf文件夹下:schema.xml,自定义搜索字段!

<field name="msg_title" type="textComplex" stored="true" indexed="true"/>   <field name="msg_content" type="textComplex" stored="true" indexed="true" multiValued="true"/> <!--该字段用于数组list类型,也可以用于定义copyfile类型-->    <field name="msg_all" type="textComplex" stored="false" indexed="true" multiValued="true" />

copyfile类型,踊跃or类型的的搜索,如想搜索title和content类型中包含某字段,可以指定:msg_all:xxx

<copyField source="msg_title" dest="msg_all"/><copyField source="msg_content" dest="msg_all"/>

public class SolrTest {private final static String URL = "http://localhost:8081/solr";public CommonsHttpSolrServer server = null;@Beforepublic void init() {try {server = new CommonsHttpSolrServer(URL);} catch (MalformedURLException e) {e.printStackTrace();}}/** * 创建索引,添加文档 */@Testpublic void test01() {//1.创建SolrServer对象(CommonsHttpSolrServer(需要启动server),EmbeddedSolrServer(不需要启动http))try {server.deleteByQuery("*:*");//代表搜索所有SolrInputDocument  doc = new SolrInputDocument();doc.addField("id", "1");doc.addField("msg_title", "这是我的第一个solrj的程序");doc.addField("msg_content", "程序能不能跑起来啊");server.add(doc);server.commit();} catch (MalformedURLException e) {e.printStackTrace();} catch (SolrServerException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}/** * 基于列表进行添加 */@Testpublic void test02() {try {List<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();SolrInputDocument doc = new SolrInputDocument();doc.addField("id", "2");doc.addField("msg_title", "很好,solr可以正常工作了");doc.addField("msg_content", "solr总算可以正式工作了");docs.add(doc);doc = new SolrInputDocument();doc.addField("id", "3");doc.addField("msg_title", "测试一下solr的添加");doc.addField("msg_content", "看看添加一个列表信息");docs.add(doc);server.add(docs);//直接添加一个集合server.commit();} catch (MalformedURLException e) {e.printStackTrace();} catch (SolrServerException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}//使用bean的方式添加索引@Testpublic void test03() {try {List<Message> msgs = new ArrayList<Message>();msgs.add(new Message("4","基于java bean的方式添加",new String[]{"javabean","添加成功!"}));msgs.add(new Message("5","基于java bean的方式添加",new String[]{"测试java bean","是否添加成功!"}));server.addBeans(msgs);server.commit();} catch (MalformedURLException e) {e.printStackTrace();} catch (SolrServerException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}@Testpublic void test04() {try {//定义查询字符串SolrQuery query = new SolrQuery("msg_title:测试");//分页query.setStart(0);query.setRows(3);QueryResponse resp = server.query(query);SolrDocumentList sdl = resp.getResults();System.out.println("查询出多少条数据:" + sdl.getNumFound());//查询多少条数据for(SolrDocument sd : sdl) {System.out.println(sd);System.out.println(sd.getFieldValue("msg_title"));}} catch (SolrServerException e) {e.printStackTrace();}}/** * 基于bean的查询,无法获取总数量 */@Testpublic void test05() {try {SolrQuery query = new SolrQuery("msg_title:测试");//分页query.setStart(0);query.setRows(3);QueryResponse resp = server.query(query);//可以直接查询常用的bean对象,但是不常用,查询出来的结果都保存在lsit中List<Message> list = resp.getBeans(Message.class);for(Message msg : list){System.out.println(msg.getTitle());}System.out.println(list.size());} catch (SolrServerException e) {e.printStackTrace();}}@Testpublic void test06() {try {//定义查询字符串SolrQuery query = new SolrQuery("msg_title:测试");//高亮query.setHighlight(true).setHighlightSimplePre("<span class='style'>").setHighlightSimplePost("</span").setRows(5);query.setParam("hl.fl", "msg_title,msg_content");QueryResponse resp = server.query(query);SolrDocumentList sdl = resp.getResults();System.out.println("查询出多少条数据:" + sdl.getNumFound());//查询多少条数据//高亮必须存储for(SolrDocument sd : sdl) {System.out.println(sd);String id = (String)sd.getFieldValue("id");//判断id是否存在并得到高亮titleSystem.out.println(resp.getHighlighting().get(id).get("msg_title"));}} catch (SolrServerException e) {e.printStackTrace();}}}

实体类:

public class Message {private String id;private String title;private String[] content;public Message() {super();// TODO Auto-generated constructor stub}public Message(String id, String title, String[] content) {super();this.id = id;this.title = title;this.content = content;}public String getId() {return id;}@Fieldpublic void setId(String id) {this.id = id;}public String getTitle() {return title;}@Field("msg_title")public void setTitle(String title) {this.title = title;}public String[] getContent() {return content;}//添加是个数组---<field name="msg_content" type="textComplex" stored="true" indexed="true" multiValued="true"/>@Field("msg_content")public void setContent(String[] content) {this.content = content;}}


0 0
原创粉丝点击