1.添加依賴
~~~
<dependencies>
<!-- Junit單元測試 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>4.10.2</version>
</dependency>
<!-- Solr底層會使用到slf4j日志系統 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.22</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
~~~
2.Book類
~~~
package cn.li;
import org.apache.solr.client.solrj.beans.Field;
public class Book {
@Field
private String id;
@Field
private String name;
@Field//需要將該字段添加到索引庫
private String author;
@Field
private String description;
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getAuthor() {
return author;
}
public String getDescription() {
return description;
}
public void setId(String id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setAuthor(String author) {
this.author = author;
}
public void setDescription(String description) {
this.description = description;
}
public Book() {
}
public Book(String id, String name, String author, String description) {
this.id = id;
this.name = name;
this.author = author;
this.description = description;
}
@Override
public String toString() {
return "Book{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", author='" + author + '\'' +
", description='" + description + '\'' +
'}';
}
}
~~~
3.demo1
~~~
package cn.li;
import static org.junit.Assert.assertTrue;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
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.apache.solr.common.params.SolrParams;
import org.junit.Test;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
/**
* Unit test for simple App.
*/
public class SolrDemo
{
//新增索引
@Test
public void createIndex(){
//創建solr服務對象 參數:solr服務的地址 并且需要指定到索引庫位置
HttpSolrServer server=new HttpSolrServer("http://localhost:8080/solr/collection1");
//創建solr文檔對象
SolrInputDocument document=new SolrInputDocument();
document.addField("id","7");
document.addField("name","西游記");
document.addField("author","吳承恩");
document.addField("description","這本書講的是一個猴子和兩個和尚西天取經的故事");
//添加文檔
try {
server.add(document);
//提交
server.commit();
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void createIndex_Bean(){
//創建solr服務對象 參數:solr服務的地址 并且需要指定到索引庫位置
HttpSolrServer server=new HttpSolrServer("http://localhost:8080/solr/collection1");
//創建solr文檔對象
Book book=new Book("8","紅樓夢","曹雪芹","賈寶玉和林黛玉");
try {
server.addBean(book);
server.commit();
} catch (Exception e) {
e.printStackTrace();
}
}
//刪除
@Test
public void deleteIndex(){
HttpSolrServer server=new HttpSolrServer("http://localhost:8080/solr/collection1");
try {
//根據單個id刪除
//server.deleteById("8");
//根據id集合刪除
//server.deleteById(Arrays.asList("7","8"));
//根據查詢語句進行刪除
server.deleteByQuery("id:8");
server.commit();
} catch (Exception e) {
e.printStackTrace();
}
}
//查詢
@Test
public void selectIndex(){
HttpSolrServer server=new HttpSolrServer("http://localhost:8080/solr/collection1");
//創建solr查詢參數 參數:查詢條件
SolrParams params=new SolrQuery("description:著作");
try {
//執行查詢 并返回查詢結果
QueryResponse response = server.query(params);
//解析查詢結果 獲取文檔列表
SolrDocumentList results = response.getResults();
System.out.println("共找到條數:"+results.getNumFound());
for (SolrDocument result:results
) {
System.out.println("id"+result.get("id"));
System.out.println("name"+result.get("name"));
System.out.println("author"+result.get("author"));
System.out.println("description"+result.get("description"));
}
} catch (SolrServerException e) {
e.printStackTrace();
}
}
@Test
public void selectIndex_Bean(){
HttpSolrServer server=new HttpSolrServer("http://localhost:8080/solr/collection1");
//創建solr查詢參數 參數:查詢條件
SolrParams params=new SolrQuery("*:*");
try {
//執行查詢 并返回查詢結果
QueryResponse response = server.query(params);
//解析查詢結果 獲取文檔列表
SolrDocumentList results = response.getResults();
System.out.println("共找到條數:"+results.getNumFound());
List<Book> books = response.getBeans(Book.class);
for (Book boook:books){
System.out.println(boook);
}
} catch (SolrServerException e) {
e.printStackTrace();
}
}
}
~~~
4.demo2
~~~
package cn.li;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.params.SolrParams;
import org.junit.Test;
import java.util.List;
import java.util.Map;
public class SolrDemo2 {
//分頁
//每頁顯示3條
@Test
public void page(){
int pageNum=1;//當前頁
int rows=3;//每頁顯示條數
HttpSolrServer server=new HttpSolrServer("http://localhost:8080/solr/collection1");
SolrQuery params=new SolrQuery("*:*");
params.setStart((pageNum-1)*rows);//設置起始條數
params.setRows(rows);//設置每頁數量
try {
QueryResponse query = server.query(params);
List<Book> books = query.getBeans(Book.class);
System.out.println(("查詢條數:" + books.size()));
for(Book book:books){
System.out.println(book);
}
} catch (SolrServerException e) {
e.printStackTrace();
}
}
//排序
@Test
public void sort(){
HttpSolrServer server=new HttpSolrServer("http://localhost:8080/solr/collection1");
SolrQuery params=new SolrQuery("*:*");
//設置排序字段 升序還是降序 參數:1.排序字段 2.asc升序 默認是升序 desc降序
params.setSort("id", SolrQuery.ORDER.desc);
try {
QueryResponse query = server.query(params);
List<Book> books = query.getBeans(Book.class);
System.out.println(("查詢條數:" + books.size()));
for(Book book:books){
System.out.println(book);
}
} catch (SolrServerException e) {
e.printStackTrace();
}
}
//高亮
@Test
public void highLighter(){
HttpSolrServer server=new HttpSolrServer("http://localhost:8080/solr/collection1");
SolrQuery params=new SolrQuery("id:[2 TO 5]");
//添加高亮字段
params.addHighlightField("description");
//設置高亮前綴和后綴
params.setHighlightSimplePre("<em>");
params.setHighlightSimplePost("</em>");
try {
QueryResponse query = server.query(params);
//獲取高亮數據
Map<String, Map<String, List<String>>> highlighting = query.getHighlighting();
/*
* 外層map的key是當前文檔的id
* 內層map:key是高亮字段名
* value:集合?【要考慮到多值字段】高亮字段的值
* */
List<Book> books = query.getBeans(Book.class);
System.out.println(("查詢條數:" + books.size()));
for(Book book:books){
String bookId = book.getId();
Map<String, List<String>> stringListMap = highlighting.get(bookId);
List<String> description = stringListMap.get("description");
if(description!=null&&description.size()>0){
book.setDescription(description.get(0));
}
System.out.println(book);
}
} catch (SolrServerException e) {
e.printStackTrace();
}
}
}
~~~
- spring
- 1.spring第一天
- 1.1 安裝spring插件(spring tool suite)
- 1.2 spring概述
- 1.3 控制反轉&依賴注入
- 1.4 springIOC容器
- 1.5 依賴注入的四種方式
- 1.6 配置bean的細節
- 1.7 bean之間的關系
- 1.8 bean作用域
- 1.9 補充:創建對象的幾種方法
- 1源代碼位置
- 2.spring第二天
- 2.1 使用外部屬性文件
- 2.2 spEL
- 2.3 bean的生命周期
- 2.4 通過工廠方式配置bean
- 2.5 基于注解的方式配置bean
- 2.6 組件裝配
- 2.7 靜態代理
- 2.8 動態代理
- 2.9 Cglib代理
- 2源代碼位置
- 3. spring第三天
- 3.1 springAOP
- 3.1.1 AOP簡介
- 3.1.2 為什么使用AOP
- 3.1.3 AOP關鍵術語
- 3.1.4 AOP圖解
- 3.1.5 springAOP實現步驟
- 3.1.6 SpringAOP實現原理:
- 3.1.7 AOP的好處
- 3.1.8 AOP在實際項目中的主要應用
- 3代碼地址
- 3.1.9 純注解版配置aop的方式
- 3.2 maven環境搭建
- 附IDEA激活碼
- 4. spring第四天
- 4.1 c3p0事務
- 4.2 命令窗口事務
- 4.3 c3p0連接池設置
- 4.4 事務中的一些基本概念
- 4.5 事務的傳播行為
- 4.6 自定義異常
- 4.7 spring整合Junit單元測試
- 4.8 JdbcTemplate(附源代碼)
- 事務源代碼
- 4.9 純注解tx
- 4.10 基于xml配置事務
- 0. jsp頁面修改編碼方式
- 0.1 eclipse配置tomcat
- 0.單例模式-飽漢模式
- 0.單例模式-饑漢模式
- springMVC
- 1. springmvc第一天
- 1.1 springMVC概述
- 1.2 springmvc框架搭建及第一個應用程序
- 1.3 @RequestMapping
- 1.4 RequestMapping修飾類
- 1.5 RequestMapping精準化映射
- 1.6 Ant風格URL
- 1.7 帶有占位符的url映射
- 1.8 REST風格
- 1.9 RequerstParam獲取請求正文
- 2. springmvc第二天
- 2.1 優化
- 2.2 POJO綁定請求參數
- 2.3 RequestHeader獲取請求報頭信息
- 2.4 CookieValue獲取Cookie信息
- 2.5 獲取原生ServletAPI
- 2.6 ModelAndView處理模型數據
- 2.7 Map、Model、ModelMap處理模型數據
- 2.8 @SessionAttributes注解
- 2.9 @ModelAttribute無返回值方法及方法入參
- 2.10 @ModelAttribute修飾有返回值類型的方法
- 代碼地址
- 3. springmvc補充
- 3-1 springmvc工作原理
- 3-2 springmvc form表單提交中文亂碼
- 3-3 數據的格式化
- 3-4 自定義類型轉換器
- 3-5 其他知識點
- 3-6 crud代碼
- 3-7 @DateTimeFormat日期格式化
- 3-8 數據驗證的概念及JSR303驗證
- 3-9 Hibernate-Validator驗證框架
- 3-10 Controller捕獲錯誤消息
- 3-11 errors標簽在頁面中獲取錯誤消息
- 3-12 錯誤消息的定制及國際化
- 3-13 自定義攔截器
- 3-14 Java代碼中獲取國際化信息
- 3-15 超級鏈接設置國際化
- 3-16 AJAX支持之@RequestBody
- mybatis
- 1. mybatis第一天
- 1. 為什么使用mybatis
- 2. 下載地址
- 3. hello
- 4. mybatis三種開發模式
- 5. 全局配屬屬性內容
- 6. DTD設置
- 7. Mapper中的CRUD
- 8. 8.mybatis使用主鍵自增
- 9. #{}中的參數處理
- 10. #{}與${}區別
- 11. 集合數據的查詢
- 12 動態sql
- 12.1 if
- 12.2 choose, when, otherwise
- 12.3 trim, where, set
- 12.4 foreach
- 代碼位置
- 2. mybatis第二天
- 1.封裝map類型的數據
- 2. resultMap自定義封裝規則
- 0代碼位置
- 3. mybatis緩存機制
- ssm整合
- 1.maven
- 2.ssm基礎環境搭建
- 2-1 引入項目依賴的jar包
- 2-2 引入bootstrap,jquery
- 2-3 創建項目包結構
- 2-4 編寫web.xml配置文件
- 2-5 編寫sping,springmvc,mybatis配置文件
- 2-6 逆向工程mbg.xml
- shiro安全框架
- 1.shiro簡介
- 易購Buy商城
- 第一天
- 1.課程計劃
- 2.電商行業背景
- 3.易購Buy介紹
- 4.易購Buy架構
- 5.工程搭建
- 6.工程啟動和測試
- 7.ssm框架整合
- 8.整合測試
- 9.svn
- 9.1 svn服務端
- 9.2 svn客戶端
- 第二天
- 1.SOA架構分析
- 2.dubbo使用方法
- 3.注冊中心
- 4.工程改造
- 5.easyUI
- maven
- 1.maven介紹
- 2.idea配置maven和服務器
- 3.創建web工程
- 4.分模塊構建工程
- 5. 代碼位置
- 6. nexus
- Luence搜索
- 1.了解搜索技術
- 2.Lucene的基本使用
- solr
- SolrCloud