前言
在Solr使用過程中,對Solr版本進行了升級,升級後發現API也變更了,所以這兒通過查閱相關的API情況,整理這篇文章!
使用SolrJ8.2.0最新版連接solr服務實現新增/查詢/刪除
1、使用solrj需要導入依賴包:
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>8.2.0</version>
</dependency>
2、新增文檔:
/**
*
* @author HuaZai
* @contact [email protected]
* @title addDocument
* <ul>
* @description 添加文檔
* </ul>
* @createdTime 2017年06月15日
* @return void
*
* @version : V1.0.0
*/
@Test
public void addDocument()
{
// 通過HttpSolrClient.Builder一個SolrClient對象
SolrClient solrClient = new HttpSolrClient.Builder()
.withBaseSolrUrl(SOLR_URL)
.withConnectionTimeout(10000)
.withSocketTimeout(60000)
.build();
// 創建SolrInputDocument文檔流對象
SolrInputDocument doc = new SolrInputDocument();
doc.addField("id", "1001");
doc.addField("title", "測試數據");
doc.addField("node", "1919");
doc.addField("desc", "這是一條測試數據");
try
{
// 添加文檔
solrClient.add(doc);
// 提交文檔
solrClient.commit();
} catch (SolrServerException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
} catch (Exception e)
{
e.printStackTrace();
} finally
{
// 關閉連接
try
{
solrClient.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
2、查詢文檔
/**
*
* @author HuaZai
* @contact [email protected]
* @title queryDocumentById
* <ul>
* @description 根據文檔ID查詢文檔
* </ul>
* @createdTime 2017年06月15日
* @return void
*
* @version : V1.0.0
*/
@Test
public void queryDocumentById()
{
// 通過HttpSolrClient.Builder一個SolrClient對象
SolrClient solrClient = new HttpSolrClient.Builder()
.withBaseSolrUrl(SOLR_URL)
.withConnectionTimeout(10000)
.withSocketTimeout(60000)
.build();
// 設置查詢參數
// 第一種查詢方式():MapSolrParams來實現
HashMap<String, String> map = new HashMap<>();
// 設置查詢參數
map.put("q", "id:1001");
// 創建MapSolrParams查詢對象
MapSolrParams solrParams = new MapSolrParams(map);
// 第二種查詢方式:SolrParams 有一個 SolrQuery 子類來實現
SolrQuery solrQuery = new SolrQuery("id:1001");
// 指定需要回顯的內容
solrQuery.addField("id");
solrQuery.addField("title");
solrQuery.addField("node");
try
{
// 根據參數查詢指定文檔
QueryResponse query = solrClient.query(solrParams);
// 提交查詢請求
solrClient.commit();
// 輸出查詢內容
SolrDocumentList list = query.getResults();
for (SolrDocument document : list)
{
System.err.println("問題ID:" + document.get("id"));
System.err.println("問題標題:" + document.get("title"));
System.err.println("副標題:" + document.get("node"));
}
} catch (SolrServerException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
} catch (Exception e)
{
e.printStackTrace();
} finally
{
// 關閉連接
try
{
solrClient.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
3-1、刪除文檔
/**
*
* @author HuaZai
* @contact [email protected]
* @title deleteDocumentById
* <ul>
* @description 根據文檔ID刪除文檔
* </ul>
* @createdTime 2017年06月15日
* @throws SolrServerException
* @throws IOException
* @return void
*
* @version : V1.0.0
*/
@Test
public void deleteDocumentById()
{
// 通過HttpSolrClient.Builder一個SolrClient對象
SolrClient solrClient = new HttpSolrClient.Builder()
.withBaseSolrUrl(SOLR_URL)
.withConnectionTimeout(10000)
.withSocketTimeout(60000)
.build();
try
{
// 刪除指定ID的文檔
solrClient.deleteById("1001");
// 提交請求
solrClient.commit();
} catch (SolrServerException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
} catch (Exception e)
{
e.printStackTrace();
} finally
{
// 關閉連接
try
{
solrClient.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
3-2、查詢刪除
/**
*
* @author HuaZai
* @contact [email protected]
* @title deleteDocumentByQuery
* <ul>
* @description 查詢刪除
* </ul>
* @createdTime 2017年06月15日
* @return void
*
* @version : V1.0.0
*/
@Test
public void deleteDocumentByQuery()
{
// 通過HttpSolrClient.Builder一個SolrClient對象
SolrClient solrClient = new HttpSolrClient.Builder()
.withBaseSolrUrl(SOLR_URL)
.withConnectionTimeout(10000)
.withSocketTimeout(60000)
.build();
try
{
// 刪除指定ID的文檔
solrClient.deleteByQuery("id:1001");
// 提交請求
solrClient.commit();
} catch (SolrServerException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
} catch (Exception e)
{
e.printStackTrace();
} finally
{
// 關閉連接
try
{
solrClient.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
4、查詢文檔
/**
*
* @author HuaZai
* @contact [email protected]
* @title queryDocumentById
* <ul>
* @description 根據文檔ID查詢文檔
* </ul>
* @createdTime 2017年06月15日
* @return void
*
* @version : V1.0.0
*/
@Test
public void queryDocumentById()
{
// 通過HttpSolrClient.Builder一個SolrClient對象
SolrClient solrClient = new HttpSolrClient.Builder()
.withBaseSolrUrl(SOLR_URL)
.withConnectionTimeout(10000)
.withSocketTimeout(60000)
.build();
// 設置查詢參數
// 第一種查詢方式():MapSolrParams來實現
HashMap<String, String> map = new HashMap<>();
// 設置查詢參數
map.put("q", "id:1001");
// 創建MapSolrParams查詢對象
MapSolrParams solrParams = new MapSolrParams(map);
// 第二種查詢方式:SolrParams 有一個 SolrQuery 子類來實現
SolrQuery solrQuery = new SolrQuery("id:1001");
// 指定需要回顯的內容
solrQuery.addField("id");
solrQuery.addField("title");
solrQuery.addField("node");
try
{
// 根據參數查詢指定文檔
QueryResponse query = solrClient.query(solrParams);
// 提交查詢請求
solrClient.commit();
// 輸出查詢內容
SolrDocumentList list = query.getResults();
for (SolrDocument document : list)
{
System.err.println("問題ID:" + document.get("id"));
System.err.println("問題標題:" + document.get("title"));
System.err.println("副標題:" + document.get("node"));
}
} catch (SolrServerException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
} catch (Exception e)
{
e.printStackTrace();
} finally
{
// 關閉連接
try
{
solrClient.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
5、完整示例代碼:
package com.huazai.b2c.aiyou.test.solrj;
import java.io.IOException;
import java.util.HashMap;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
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.MapSolrParams;
import org.junit.Test;
/**
*
* @author HuaZai
* @contact [email protected]
* <ul>
* @description 測試solrj客戶端
* </ul>
* @className TSolrjClient
* @package com.huazai.b2c.aiyou.test.solrj
* @createdTime 2017年06月14日
*
* @version V1.0.0
*/
public class TSolrjClient
{
public static final String SOLR_URL = "http://***.***.***.***:****/solr/core_name";
/**
*
* @author HuaZai
* @contact [email protected]
* @title addDocument
* <ul>
* @description 添加文檔
* </ul>
* @createdTime 2017年06月15日
* @return void
*
* @version : V1.0.0
*/
@Test
public void addDocument()
{
// 通過HttpSolrClient.Builder一個SolrClient對象
SolrClient solrClient = new HttpSolrClient.Builder()
.withBaseSolrUrl(SOLR_URL)
.withConnectionTimeout(10000)
.withSocketTimeout(60000)
.build();
// 創建SolrInputDocument文檔流對象
SolrInputDocument doc = new SolrInputDocument();
doc.addField("id", "1001");
doc.addField("title", "測試數據");
doc.addField("node", "1919");
doc.addField("desc", "這是一條測試數據");
try
{
// 添加文檔
solrClient.add(doc);
// 提交文檔
solrClient.commit();
} catch (SolrServerException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
} catch (Exception e)
{
e.printStackTrace();
} finally
{
// 關閉連接
try
{
solrClient.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
/**
*
* @author HuaZai
* @contact [email protected]
* @title deleteDocumentById
* <ul>
* @description 根據文檔ID刪除文檔
* </ul>
* @createdTime 2017年06月15日
* @throws SolrServerException
* @throws IOException
* @return void
*
* @version : V1.0.0
*/
@Test
public void deleteDocumentById()
{
// 通過HttpSolrClient.Builder一個SolrClient對象
SolrClient solrClient = new HttpSolrClient.Builder()
.withBaseSolrUrl(SOLR_URL)
.withConnectionTimeout(10000)
.withSocketTimeout(60000)
.build();
try
{
// 刪除指定ID的文檔
solrClient.deleteById("1001");
// 提交請求
solrClient.commit();
} catch (SolrServerException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
} catch (Exception e)
{
e.printStackTrace();
} finally
{
// 關閉連接
try
{
solrClient.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
/**
*
* @author HuaZai
* @contact [email protected]
* @title deleteDocumentByQuery
* <ul>
* @description 查詢刪除
* </ul>
* @createdTime 2017年06月15日
* @return void
*
* @version : V1.0.0
*/
@Test
public void deleteDocumentByQuery()
{
// 通過HttpSolrClient.Builder一個SolrClient對象
SolrClient solrClient = new HttpSolrClient.Builder()
.withBaseSolrUrl(SOLR_URL)
.withConnectionTimeout(10000)
.withSocketTimeout(60000)
.build();
try
{
// 刪除指定ID的文檔
solrClient.deleteByQuery("id:1001");
// 提交請求
solrClient.commit();
} catch (SolrServerException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
} catch (Exception e)
{
e.printStackTrace();
} finally
{
// 關閉連接
try
{
solrClient.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
/**
*
* @author HuaZai
* @contact [email protected]
* @title queryDocumentById
* <ul>
* @description 根據文檔ID查詢文檔
* </ul>
* @createdTime 2017年06月15日
* @return void
*
* @version : V1.0.0
*/
@Test
public void queryDocumentById()
{
// 通過HttpSolrClient.Builder一個SolrClient對象
SolrClient solrClient = new HttpSolrClient.Builder()
.withBaseSolrUrl(SOLR_URL)
.withConnectionTimeout(10000)
.withSocketTimeout(60000)
.build();
// 設置查詢參數
// 第一種查詢方式():MapSolrParams來實現
HashMap<String, String> map = new HashMap<>();
// 設置查詢參數
map.put("q", "id:1001");
// 創建MapSolrParams查詢對象
MapSolrParams solrParams = new MapSolrParams(map);
// 第二種查詢方式:SolrParams 有一個 SolrQuery 子類來實現
SolrQuery solrQuery = new SolrQuery("id:1001");
// 指定需要回顯的內容
solrQuery.addField("id");
solrQuery.addField("title");
solrQuery.addField("node");
try
{
// 根據參數查詢指定文檔
QueryResponse query = solrClient.query(solrParams);
// 提交查詢請求
solrClient.commit();
// 輸出查詢內容
SolrDocumentList list = query.getResults();
for (SolrDocument document : list)
{
System.err.println("問題ID:" + document.get("id"));
System.err.println("問題標題:" + document.get("title"));
System.err.println("副標題:" + document.get("node"));
}
} catch (SolrServerException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
} catch (Exception e)
{
e.printStackTrace();
} finally
{
// 關閉連接
try
{
solrClient.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
}
好了,關於 【SolrJ8.2.0】使用SolrJ8.2.0最新版連接solr服務實現新增/查詢/刪除 就寫到這兒了,如果還有什麼疑問或遇到什麼問題歡迎掃碼提問,也可以給我留言哦,我會一一詳細的解答的。
歇後語:“ 共同學習,共同進步 ”,也希望大家多多關注CSND的IT社區。
作 者: | 華 仔 |
聯繫作者: | [email protected] |
來 源: | CSDN (Chinese Software Developer Network) |
原 文: | https://blog.csdn.net/Hello_World_QWP/article/details/98331238 |
版權聲明: | 本文爲博主原創文章,請在轉載時務必註明博文出處! |