1. 程式人生 > >SpringDataSolr操作Solr(一)

SpringDataSolr操作Solr(一)

1、內容如下:

  • Sorl基本增刪查
  • 從資料庫匯入到Solr

在這裡就不闡述如何安裝Solr以及中文分詞器

2、為了方便後續的講述這裡先貼出Solr域內容:

	<!--基本域-->
	<field name="id" type="long" indexed="true" stored="true"/>
	<field name="item_title" type="text_ik" indexed="true" stored="true"/>
	<field name="item_price" type="double" indexed="true" stored
="true"/>
<field name="item_image" type="string" indexed="false" stored="true" /> <field name="item_category" type="string" indexed="true" stored="true" /> <!--複製域--> <field name="item_keywords" type="text_ik" indexed="true" stored="false" multiValued="true"/> <copyField
source="item_title" dest="item_keywords"/>
<copyField source="item_category" dest="item_keywords"/>

3、配置pojo

public class TbItem implements Serializable{

	@Field
    private Long id;

	@Field("item_title")
    private String title;
	    
    @Field("item_price")
	private BigDecimal price;
@Field("item_image") private String image; @Field("item_category") private String category; /*注意: 屬性使用@Field註解標識 。 如果屬性與配置檔案定義的域名稱不一致,需要在註解中指定域名稱。*/ }

3、引入pom檔案

	<dependency>
	    <groupId>org.springframework.data</groupId>
	    <artifactId>spring-data-solr</artifactId>
	    <version>1.5.5.RELEASE</version>
	</dependency>

4、Sorl配置檔案

	<!-- solr伺服器地址 -->
	<solr:solr-server id="solrServer" url="http://127.0.0.1:8080/solr" />
	<!-- solr模板,使用solr模板可對索引庫進行CRUD的操作 -->
	<bean id="solrTemplate" class="org.springframework.data.solr.core.SolrTemplate">
		<constructor-arg ref="solrServer" />
	</bean>

5、‘單個’、‘批量’、'資料庫’新增到Solr庫

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext-solr.xml")
public class TestTemplate {
	@Autowired
	private SolrTemplate solrTemplate;

	//單個新增
	@Test
	public void testAdd(){
		TbItem item=new TbItem();
		item.setId(123L);
		item.setCategory("手機");
		item.setTitle("這裡是solr手機");
		item.setPrice(new BigDecimal(2000));		
		solrTemplate.saveBean(item);
		solrTemplate.commit();
	}
	//批量
	@Test
	public void testAddList(){
		List<TbItem> list=new ArrayList();
		for(int i=0;i<100;i++){
			TbItem item=new TbItem();
			item.setId(i+1L);
			item.setCategory("手機");
			item.setTitle("這裡是solr手機"+i);
			item.setPrice(new BigDecimal(2000+i));	
			list.add(item);
		}
		
	//資料庫匯入到solr
	@Test
		public void importItemData(){
		//先從資料庫查詢
		List<TbItem> itemList = itemMapper.selectByExample();
		System.out.println("===商品列表===");
		for(TbItem item:itemList){
			System.out.println(item.getTitle());	
		}
		//插入solr中
		solrTemplate.saveBeans(itemList);
		solrTemplate.commit();
		System.out.println("===結束===");	
	}
}

6、刪除Solr

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext-solr.xml")
public class TestTemplate {
	@Autowired
	private SolrTemplate solrTemplate;
	//按主鍵刪除
	@Test
	public void testDelete(){
		solrTemplate.deleteById("1");
		solrTemplate.commit();
	}
	//刪除所有
	@Test
	public void testDeleteAll(){
		Query query=new SimpleQuery("*:*");
		solrTemplate.delete(query);
		solrTemplate.commit();
	}
}

7、查詢操作

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext-solr.xml")
public class TestTemplate {
	//按主鍵查詢
	@Test
	public void testFindOne(){
		TbItem item = solrTemplate.getById(1, TbItem.class);
		System.out.println(item.getTitle());
	}
	//分頁查詢
	@Test
	public void testPageQuery(){
		Query query=new SimpleQuery("*:*");
		query.setOffset(0);//開始索引(預設0)
		query.setRows(20);//每頁記錄數(預設10)
		ScoredPage<TbItem> page = solrTemplate.queryForPage(query, TbItem.class);
		System.out.println("總記錄數:"+page.getTotalElements());
		//獲取查詢內容
		List<TbItem> list = page.getContent();
		//迴圈顯示
		for(TbItem item:list){
			System.out.println(item.getTitle() +item.getPrice());
		}		
	}	
	//條件查詢
	@Test
	public void testPageQueryMutil(){	
		Query query=new SimpleQuery("*:*");
		Criteria criteria=new Criteria("item_title").contains("2");
		criteria=criteria.and("item_title").contains("5");		
		query.addCriteria(criteria);
		ScoredPage<TbItem> page = solrTemplate.queryForPage(query, TbItem.class);
		List<TbItem> list = page.getContent();
		//迴圈顯示
		for(TbItem item:list){
			System.out.println(item.getTitle() +item.getPrice());
		}
	}
	//條件查詢可以和分頁查詢相結合
}

上述只是說了SpringDataSolr的簡單運用,其他知識需要自行掌握,程式碼可能無法直接執行,需要根據專案進行相應的一些調整。

相關推薦

SpringDataSolr操作Solr

1、內容如下: Sorl基本增刪查 從資料庫匯入到Solr 在這裡就不闡述如何安裝Solr以及中文分詞器 2、為了方便後續的講述這裡先貼出Solr域內容: <!--基本域--> <field name="id" type="long"

30天自制操作系統從計算機結構到匯編程序入門

ios 是什麽 program 信號 解釋 根目錄 自己 保存 音樂 學習這本書不代表我要親自動手把這本書中的代碼敲一遍哦,只是借機來學習一下操作系統。之前有段時間也在看操作系統的知識,怎麽說呢之前的看的書都是偏講理論多一些,對於我這個非科班出身的來說,由於之前的計算機知識

python記錄—入門到實踐—字符串操作

span java、 浮點 大寫 數位 tab cnblogs 保留小數 字符串操作 我就跟著書學,總結執行要記錄的,不要覺得簡單就不去試一試,敲一敲代碼。執行力是成為好的程序員的寶劍。如果你是新手在掌握其他的語言情況下比如java、c或者php等等任意一種基本語法,程序都

原子操作原子操作類詳細介紹

expected 文章 span del 也有 pair 上一個 ride 操作類 引言 ??Java從JDK1.5開始提供了java.util.concurrent.atomic包,方便程序員在多線程環境下,無鎖的進行原子操作。原子變量的底層使用了處理器提供的原子指令,但

Python操作MySQL PyMysql篇

pymysql mysqldb python mysql pymsql是Python中操作MySQL的模塊,其使用方法和MySQLdb幾乎相同,但是mysqldb不支持Python3 下載安裝 pip3 install pymysql 使用操作 1. 執行sql #!/usr/bin/env p

python之操作mysql

mod 它的 utf8 連接數 mode char PE class 是個 使用python操作mysql的思路: 1. 連接數據庫:ip,端口號,密碼,賬號,數據庫 2. 建立遊標 3.執行sql語句 4.獲取執行結果 5.關閉遊標,關閉連接 conn = pymysql

python操作json

數字 其他 是不是 print employee ict 語法規則 浮點 3.0 Python 操作jsonJson語法規則:數據在名稱/值對中數據由逗號分隔花括號保存對象方括號保存數組Json字符串本質上是一個字符串,用單引號表示 Json數據的書寫格式名稱--值對,包括

DOM操作總結

UNC 標簽 頁面加載 當前 urn 對象 自己的 var ole (一)innerText 凡是成對的標簽,中間的文本內容,設置的時候,都是用innerText這個屬性方法 (二)在某個元素的事件中,自己的事件中的this就是當前的這個元素對象 var btn=ducou

Python 二級模擬操作

中國 正方 range 進制數 存儲 字符 light 包含 pri 1.計算下列式子,結果保留小數點後保留3位 代碼: >>> x = pow((3**4 + 5*(6**7))/8, 0.5) >>> print("%.

node總結之檔案操作系列

Node.js 提供一組類似 UNIX(POSIX)標準的檔案操作API,我們來看下Node 匯入檔案系統模組(fs)語法,如下: var fs = require("fs") Node.js 檔案系統(fs 模組)模組中的方法均有非同步和同步版本,例如讀取檔案內容的函式有非同步的 fs

基於Curator操作ZooKeeper-基本操作

Java原生API操作ZooKeeper可參看: Java原生API操作Zookeeper(一) Java原生API操作Zookeeper(二) 相關內容: 基於Curator操作ZooKeeper(二)-Watcher操作 基於Curator操作ZooKeeper(二)-W

python3入門教程操作資料庫

概述   最近在準備寫一個爬蟲的練手專案,基本想法是把某新聞網站的內容分類爬取下來,儲存至資料庫,再通過介面對外輸出(提供後臺查詢介面)。那麼問題就來了,python到底是怎麼去操作資料庫的呢?我們今天就來研究下。   準備 我這邊資料庫使用的是mysql5.7,python去操作mysql

Git介紹及常用操作演示--技術流ken

Git介紹及常用操作演示(一)--技術流ken   Git介紹    Git(讀音為/gɪt/。)是一個開源的分散式版本控制系統,可以有效、高速的處理從很小到非常大的專案版本管理。 Git 是 Linus Torvalds 為了幫助管理 Linux 核心

Photoshop操作總結

第一次用ps畫圖        在學習ps之前,我用ps軟體畫了一個圖(有些不忍直視嘻嘻),在學完之後,可以作一個比較,希望自己會有很大的進步。 透視 定義        繪畫理論

簡單maven操作使用

簡單maven入門(一) --------------------使用Ecilpse建立JAVA SE專案(後面總結) 1.下載maven外掛:http://maven.apache.org/download.cgi 2.解壓,放到資料夾裡: 3.使用Eclipse建立一個Maven專

LeetCode 連結串列操作相關

連結串列操作對於我來說是一個相對薄弱的環節,需要好好練習。 一、刪除連結串列中的節點 刪除指定節點這種題目是連結串列的基本操作,本題題目如下: 請編寫一個函式,使其可以刪除某個連結串列中給定的(非末尾)節點,你將只被給定要求被刪除的節點。 現有一個連結串列 -- he

Oracle資料庫常用操作總結

--oracle cs架構軟體 --客戶端 --tns  --協議 --ip --埠 --資料庫名字 --監聽如果出了問題,先刪除所有監聽,再重建。netca。tns檔案中名字不能重複, --oracle預設自帶兩個管理員使用者 sys system 這兩個使用者在登入時

python 二級考試操作

一、參照程式碼模板完善程式碼,實現下述功能。從鍵盤輸入一個整數和一個字元,以逗號隔開,在螢幕上顯示輸出一條資訊。 —————————————————————————————– 示例如下: 輸入:10,@ 輸出:@@@@@@@@@@ 10 @@@@@

Linux系統操作練習

用一條命令把redhat_versionX中帶奇數的檔案複製到桌面到SINGLE中 命令如下: 效果如下: 用一條命令把redhat_versionX中帶偶數的檔案複製到/DOUBLE中 命令如下: 效果如下: 用一條命令把WESTOS_classX_linuxY中class1的檔案移動到當前使用者

Python利用openpyxl來操作Excel

最近一直在做專案裡的自動化的工作,為了是從繁瑣重複的勞動中掙脫出來,把精力用在資料分析上。自動化方面python是在好不過了,不過既然要提交報表, 就不免要美觀什麼的。pandas雖然很強大,但是無法對Excel完全操作,現學vba有點來不及。於是就找到這個openpyxl包,用python