需要打开经过和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;
@Before
public void init() {
try {
server = new CommonsHttpSolrServer(URL);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
/**
* 创建索引,添加文档
*/
@Test
public 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();
}
}
/**
* 基于列表进行添加
*/
@Test
public 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的方式添加索引
@Test
public 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();
}
}
@Test
public 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的查询,无法获取总数量
*/
@Test
public 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();
}
}
@Test
public 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是否存在并得到高亮title
System.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;
}
@Field
public 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;
}
}