一、Solr的简介 二、Java操作solr 1、添加maven依赖 1 2 3 4 5 6 7 8 9 10 <dependency > <groupId > org.apache.solr</groupId > <artifactId > solr-solrj</artifactId > <version > 4.10.4</version > </dependency > <dependency > <groupId > commons-logging</groupId > <artifactId > commons-logging</artifactId > <version > 1.1.1</version > </dependency >
2、代码引包 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 import static org.junit.Assert.*;import java.util.Collection;import java.util.Date;import java.util.Iterator;import java.util.List;import org.apache.solr.client.solrj.SolrQuery;import org.apache.solr.client.solrj.impl.HttpSolrServer;import org.apache.solr.client.solrj.response.QueryResponse;import org.apache.solr.client.solrj.response.SpellCheckResponse;import org.apache.solr.client.solrj.response.SpellCheckResponse.Suggestion;import org.apache.solr.client.solrj.response.UpdateResponse;import org.apache.solr.common.SolrDocument;import org.apache.solr.common.SolrDocumentList;import org.apache.solr.common.SolrInputDocument;import org.junit.Test;
3、获取solr链接 1 2 3 String baseURL = "http://192.168.255.128:8983/solr" ; HttpSolrServer server = new HttpSolrServer(baseURL);
4、查询数据 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 @Test public void test1 () throws Exception { SolrQuery params = new SolrQuery(); params.set("q" , "*:*" ); QueryResponse response = server.query(params); SolrDocumentList results = response.getResults(); long numFound = results.getNumFound(); System.out.println("数据总条数:" +numFound); for (SolrDocument solrDocument : results) { Collection<String> fieldNames = solrDocument.getFieldNames(); for (String field : fieldNames) { System.out.println(field+"--" +solrDocument.get(field)); } System.err.println("=========================================" ); } }
5、添加数据 方式一: 1 2 3 4 5 6 7 8 9 10 @Test public void test2 () throws Exception { SolrInputDocument doc = new SolrInputDocument(); doc.setField("id" , "9527" ); doc.setField("name" , "JJ" ); doc.setField("last_modified" , new Date()); server.add(doc ); server.commit(); }
方式二:
使用HttpSolrServer.addBean(object)这种方式
Student类 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 * Student.java * * Created on: 2016年4月26日 下午3:50:03 * Author: Wayne 13186259527@163.com */ package Solr;import org.apache.solr.client.solrj.beans.Field; * @author Administrator * */ public class Student { @Field private String id; @Field private String name; @Field private String favour; public String getId () { return id; } public void setId (String id) { this .id = id; } public String getName () { return name; } public void setName (String name) { this .name = name; } public String getFavour () { return favour; } public void setFavour (String favour) { this .favour = favour; } }
注:只有被@Field修饰的字段才可以被solr识别 1 2 3 4 5 6 7 8 9 10 11 @Test public void testadd2 () throws Exception { Student student = new Student(); student.setId("224" ); student.setName("集中营" ); student.setFavour("Movie" ); server.addBean(student); server.commit(); System.out.println("添加成功" ); }
但是一执行,报错了! unknown field :favour
纳闷了,我明明在Student类里favour字段前加了@Field注释啊。 为毛还会报这个错。
百度之,原因如下:1 2 3 4 5 6 * 添加数据时,如果添加的字段schema.xml中没有,会报错:unknown filed 解决:可以自行向schema.xml中添加该字段 * 如下例中的favour字段 * * @throws Exception */
添加favour字段信息,搞定!
6、删除数据 1 2 3 4 5 6 7 @Test public void testdel () throws Exception { server.deleteByQuery("name:224集中营" ); server.commit(); System.out.println("删除成功" ); }
7、拼写检查 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 * solr中的拼写检查 * 注意:当把schema文件中的text字段的类型改为text_ik的时候, * 执行这个代码会报错 * 解决方案: * 1:修改schema.xml文件中的name字段的类型,页修改我日text_ik * 2:修改solrconfig.xml文件中的第1315行,吧text_genery,修改为text_ik * @throws Exception */ @Test public void test5 () throws Exception { SolrQuery solrQuery = new SolrQuery(); solrQuery.set("qt" , "/spell" ); solrQuery.set("q" , "text:Samsang" ); QueryResponse response = server.query(solrQuery); SolrDocumentList results = response.getResults(); long numFound = results.getNumFound(); if (numFound==0 ){ System.out.println("拼写错误!" ); SpellCheckResponse spellCheckResponse = response.getSpellCheckResponse(); List<Suggestion> suggestions = spellCheckResponse.getSuggestions(); for (Suggestion suggestion : suggestions) { int numFound2 = suggestion.getNumFound(); System.out.println("推荐的单词个数:" +numFound2); List<String> alternatives = suggestion.getAlternatives(); for (String string : alternatives) { System.out.println(string); } } }else { for (SolrDocument solrDocument : results) { Collection<String> fieldNames = solrDocument.getFieldNames(); for (String field : fieldNames) { System.out.println(field+"--" +solrDocument.get(field)); } System.err.println("=========================================" ); } } }