全文检索之企业级应用框架solr的使用(下)
来源:互联网 发布:php 防止xss函数 编辑:程序博客网 时间:2024/05/27 00:41
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.添加 */@Testpublic 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.删除 */@Testpublic 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.修改 */@Testpublic 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.查询 */@Testpublic 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)); }}
}
阅读全文
0 0
- 全文检索之企业级应用框架solr的使用(下)
- 使用Solr构建企业级的全文检索(一)---------开篇
- 使用Solr构建企业级的全文检索(二)---------管理界面简介
- 使用Solr构建企业级的全文检索(三)---------Schema定义
- 使用Solr构建企业级的全文检索(四)---------写入文档
- 使用solr搭建你的全文检索
- 使用solr搭建你的全文检索
- 使用solr搭建你的全文检索
- 使用solr搭建你的全文检索
- 使用solr搭建你的全文检索
- 使用solr搭建你的全文检索
- 使用solr搭建你的全文检索
- 使用solr搭建你的全文检索
- 使用solr搭建你的全文检索
- 使用solr搭建你的全文检索
- java中solr全文检索的使用
- 图解全文检索SOLR的酷应用[Ajax Solr]
- solr全文检索应用实例
- linux 常用命令
- 如何使用thinkphp 5内置验证功能及自定义验证方法
- HDU 2830(动态规划)
- Android--过滤EditText的输入
- android.graphics(二)drawable
- 全文检索之企业级应用框架solr的使用(下)
- MacDown的使用规范总结 96 卞泽 2016.04.08 15:05* 字数 1273 阅读 4295评论 10喜欢 34 MacDown是什么? 请点击here MacDown下载地址,请点
- java 中的锁 -- 偏向锁、轻量级锁、自旋锁、重量级锁
- EXCEPTION:ERROR DruidStatService:368
- Ubuntu16.04下配置php+vscode+xdebug开发环境
- openwrt下搭建lighttpd+mysql+php5环境(二)
- MVC页面重定向,改变地址栏地址的方法
- 记录ogre中gpu程序在compositor中使用问题
- Android 多语言翻译的自动拷贝