添加
第一步:把solrJ的jar包添加到工程中
pom文件中添加依赖
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>4.10.3</version>
</dependency>
第二步:创建一个SolrServer,使用HttpSolrServer创建对象。
第三步:创建一个文档对象SolrInputDocument对象。
第四步:向文档中添加域。必须有id域,域的名称必须在schema.xml中定义。
第五步:把文档添加到索引库中。
第六步:提交。
具体代码如下
@Test
public void addDocument() throws Exception{
//创建一个SolrServer,使用HttpSolrServer创建对象。
//baseUrl 为solr服务地址 可以直接写http://192.168.25.202:8080/solr 默认导入collection1
String baseUrl = "http://192.168.25.202:8080/solr/collection1";
SolrServer solrServer = new HttpSolrServer(baseUrl);
//创建一个文档对象SolrInputDocument对象。
SolrInputDocument solrInputDocument = new SolrInputDocument();
//向文档中添加域。必须有id域,域的名称必须在schema.xml中定义。
solrInputDocument.addField("id", "测试doc1");
solrInputDocument.addField("item_title", "测试标题1");
solrInputDocument.addField("item_sell_point", "test01");
solrInputDocument.addField("item_price", 1000);
solrInputDocument.addField("item_image", "测试图片1");
solrInputDocument.addField("item_category_name", "测试类目1");
//把文档添加到索引库中。
solrServer.add(solrInputDocument);
//提交。
solrServer.commit();
}
结果
删除
第一步:创建一个SolrServer对象。
第二步:调用SolrServer对象的根据id删除的方法。
第三步:提交。
@Test
public void delDocument() throws Exception{
//创建一个SolrServer对象。
String baseUrl = "http://192.168.25.202:8080/solr/collection1";
SolrServer solrServer = new HttpSolrServer(baseUrl);
//调用SolrServer对象的根据id删除的方法。
solrServer.deleteById("测试doc1");
//根据条件删除,域field:内容
//solrServer.deleteByQuery("id:测试doc1");
//提交。
solrServer.commit();
}
查询索引库
简单查询--查询步骤:
第一步:创建一个SolrServer对象
第二步:创建一个SolrQuery对象。
第三步:向SolrQuery中添加查询条件、过滤条件。。。
第四步:执行查询。得到一个Response对象。
第五步:取查询结果。
第六步:遍历结果并打印。
@Test
public void queryDocument() throws Exception{
//创建一个SolrServer对象
String baseUrl = "http://192.168.25.202:8080/solr/collection1";
SolrServer solrServer = new HttpSolrServer(baseUrl);
//创建一个SolrQuery对象。
SolrQuery solrQuery = new SolrQuery();
//向SolrQuery中添加查询条件、过滤条件。。。
solrQuery.setQuery("id:测试doc1");
//执行查询。得到一个Response对象。
QueryResponse response = solrServer.query(solrQuery);
//取查询结果。
SolrDocumentList list = response.getResults();
//遍历结果并打印。
System.out.println("查询总条数为"+list.size());
for (SolrDocument solrDocument : list) {
System.out.println(solrDocument.get("id"));
System.out.println(solrDocument.get("item_title"));
System.out.println(solrDocument.get("item_sell_point"));
System.out.println(solrDocument.get("item_price"));
System.out.println(solrDocument.get("item_image"));
}
}
结果:
查询总条数为1
测试doc1
测试标题1
test01
1000
测试图片1
如果将查询条件替换为 *:*将查询所有
//向SolrQuery中添加查询条件、过滤条件。。。
solrQuery.setQuery("*:*");
查询总条数为2
测试doc1
测试标题1
test01
1000
测试图片1
测试doc2
测试标题1
test01
1000
测试图片1
高亮查询
@Test
public void queryhighlightDocument() throws Exception{
//创建一个SolrServer对象
String baseUrl = "http://192.168.25.202:8080/solr/collection1";
SolrServer solrServer = new HttpSolrServer(baseUrl);
//创建一个SolrQuery对象。
SolrQuery solrQuery = new SolrQuery();
//向SolrQuery中添加查询条件、过滤条件。。。
solrQuery.setQuery("测试标题1");
//指定默认搜索域
solrQuery.set("df", "item_title");
//开启高亮显示
solrQuery.setHighlight(true);
//高亮显示的域
solrQuery.addHighlightField("item_title");
solrQuery.setHighlightSimplePre("<em>");
solrQuery.setHighlightSimplePost("</em>");
//执行查询。得到一个Response对象。
QueryResponse response = solrServer.query(solrQuery);
//取查询结果。
SolrDocumentList documentList = response.getResults();
//遍历结果并打印。
System.out.println("查询总条数为"+documentList.size());
for (SolrDocument solrDocument : documentList) {
System.out.println(solrDocument.get("id"));
//取高亮显示
Map<String, Map<String, List<String>>> highlighting = response.getHighlighting();
List<String> list = highlighting.get(solrDocument.get("id")).get("item_title");
String itemTitle = null;
if (list != null && list.size() > 0) {
itemTitle = list.get(0);
} else {
itemTitle = (String) solrDocument.get("item_title");
}
System.out.println(itemTitle);
}
}
结果
查询总条数为2
测试doc1
<em>测试</em><em>标题</em><em>1</em>
测试doc2
<em>测试</em><em>标题</em><em>1</em>