您的位置:首页 > 其它

全文检索之企业级应用框架solr的使用(下)

2017-11-16 11:52 585 查看
solr是apach组织对全文检索开源项目lucene进行封装后的企业级应用框架,这里仅仅展示最简单的使用,强大的应用,请关注后期博客更新!

package com.ilike.solrj;

import java.util.List;

import java.util.Map;

import org.apache.solr.client.solrj.SolrQuery;

import org.apache.solr.client.solrj.SolrQuery.ORDER;

import org.apache.solr.client.solrj.SolrServer;

import org.apache.solr.client.solrj.impl.HttpSolrServer;

import org.apache.solr.client.solrj.response.QueryResponse;

import org.apache.solr.common.SolrDocument;

import org.apache.solr.common.SolrDocumentList;

import org.apache.solr.common.SolrInputDocument;

import org.junit.Test;

/**

* SolrJ管理: 添加 修改 删除

*

* @author 桑伟东

*

*/

public class SolrJManager {

/*
* 1.添加
*/
@Test
public void testAdd() throws Exception {

// 1.1创建单机版solrServer
// String baseUrl="http://localhost:8080/solr/collection2";
String baseUrl = "http://localhost:8080/solr/collection1";
SolrServer server = new HttpSolrServer(baseUrl);
// 1.2添加
// 1.2.1创建文档对象
SolrInputDocument solrInputDocument = new SolrInputDocument();
// 1.2.2向文档对象中的域添加相应的值
solrInputDocument.setField("id", "101");
solrInputDocument.setField("product_picture", "123.jpg");
solrInputDocument.setField("product_catalog_name", "幽默杂货");
solrInputDocument.setField("product_price", "28.9");
solrInputDocument.setField("product_name", "小台灯");
// 1.3保存文档对象
server.add(solrInputDocument);
// 1.4提交
server.commit();
}

/*
* 2.删除
*/
@Test
public void testDelete() throws Exception {

// 2.1创建单机版solrServer
// String baseUrl="http://localhost:8080/solr/collection2";
String baseUrl = "http://localhost:8080/solr/collection1";
SolrServer server = new HttpSolrServer(baseUrl);
// 2.2删除全部
server.deleteByQuery("*:*", 20);
}

/*
* 3.修改
*/
@Test
public void testUpdate() throws Exception {

// 3.1创建单机版solrServer
// String baseUrl="http://localhost:8080/solr/collection2";
String baseUrl = "http://localhost:8080/solr/collection1";
SolrServer server = new HttpSolrServer(baseUrl);
// 与添加一致,只要id相同,就会执行更新
}

/*
* 4.查询
*/

@Test
public void testSearch() throws Exception {

// 4.1创建单机版solrServer
// String baseUrl="http://localhost:8080/solr/collection2";
String baseUrl = "http://localhost:8080/solr/collection1";
SolrServer server = new HttpSolrServer(baseUrl);
// 4.2创建查询对象
SolrQuery solrQuery = new SolrQuery();
// 4.3设置查询关键词
// solrQuery.set("q", "product_name:台灯");
solrQuery.setQuery("台灯");
// 4.4设置过滤条件
solrQuery.set("fq", "product_catalog_name:幽默杂货");
// 4.5设置价格区间(小于30)
solrQuery.set("fq", "product_price:[* TO 30]");
// 4.6设置排序方式(从高到底)
solrQuery.addSort("product_price", ORDER.asc);
// 4.7设置分页
solrQuery.setStart(0);
solrQuery.setRows(5);
// 4.8设置默认域
solrQuery.set("df", "product_name");
// 4.9指定查询域
solrQuery.set("fl", "id,product_name");
// 4.10设置高亮
// 4.10.1打开高亮开关
solrQuery.setHighlight(true);
// 4.10.2指定高亮域
solrQuery.addHighlightField("product_name");
// 4.10.3设置前缀
solrQuery.setHighlightSimplePre("<span style='color:red'>");

// 4.10.4设置后缀
solrQuery.setHighlightSimplePost("</span>");
// 4.11执行查询
QueryResponse queryResponse = server.query(solrQuery);
// 4.12获取查询的文档结果集
SolrDocumentList docs = queryResponse.getResults();

// 4.12获取查询高亮的结果集,map中第一个k是id,第二个k是域名
Map<String, Map<String, List<String>>> hMap = queryResponse.getHighlighting();
// 4.13获取本次查询的总条数
Long count = docs.getNumFound();
System.out.println(count);
for (SolrDocument doc : docs) {
System.out.println(doc.get("id"));
System.out.println(doc.get("product_picture"));
System.out.println(doc.get("product_catalog_name"));
System.out.println(doc.get("product_price"));
System.out.println(doc.get("product_name"));

System.out.println("-------------");
Map<String, List<String>> map = hMap.get(doc.get("id"));
List<String> list = map.get("product_name");
System.out.println(list.get(0));

}

}


}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  solr 全文检索