》
使用solr,那么solr是什么呢?
solr是基于Lucene开发的一个企业级搜索应用服务器(即搜索系统)。
应用服务器:就是把开发出的系统war包运行在Tomcat中,运行该系统的Tomcat就是应用服务器。
可以通过Solr系统管理索引库(core)。
1、在solr 服务器中添加field 2种方式
- 1、添加 document
- 2、添加 JavaBean
代码思想步骤
1、连接jetty服务器
2、创建文档对象,在document添加字段
3、commit 提交
以下测试源码分享:
https://github.com/medoo-Ai/solrj
》
2、添加JavaBean的方式长见的错误:
pojo中属性没有注解@Field,导致solrJ并不知道哪个属性要对应到索引库中:
创建pojo
public class Item implements Serializable {
private static final long serialVersionUID=1L;
@Field("id")
private String id;
@Field("title")
private String title;
@Field("price")
private Float price;
// 提供 getter 、setter 重写toString()
}
1、 添加solr索引库的 2种方式:
1、通过 document 的方式
2、通过 javabean 的方式
添加 javaBean单元测试:
//通过 document 的方式
@Test
public void test() throws IOException, SolrServerException {
HttpSolrServer server = new HttpSolrServer("http://localhost:8080/solr/core1");
SolrInputDocument document = new SolrInputDocument();
document.addField("id", "4");
document.addField("title", "五菱宏光,神车");
document.addField("price", 30000F);
//
server.add(document);
//提交
server.commit();
}
//pojo中属性没有注解@Field,导致solrJ并不知道哪个属性要对应到索引库中
@Test
public void testCreateIndexBean() throws IOException, SolrServerException {
HttpSolrServer server = new HttpSolrServer("http://localhost:8080/solr/core1");
//
Item item = new Item();
item.setId("9");
item.setPrice(3000F);
item.setTitle("