Lucene是一個Java語言編寫的利用倒排原理實現的文本檢索類庫;java
Solr是以Lucene爲基礎實現的文本檢索應用服務。Solr部署方式有單機方式、多機Master-Slaver方式、Cloud方式。node
SolrCloud是基於Solr和Zookeeper的分佈式搜索方案。當索引愈來愈大,一個單一的系統沒法知足磁盤需求,查詢速度緩慢,此時就須要分佈式索引。在分佈式索引中,原來的大索引,將會分紅多個小索引,solr能夠將這些小索引返回的結果合併,而後返回給客戶端。git
CentOs7.3 搭建 SolrCloud 集羣服務github
代碼我已放到 Github ,導入ymq-solr-cloud-spring-boot
項目 spring
github https://github.com/souyunku/ymq-example/tree/master/ymq-solr-cloud-spring-bootapache
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-solr</artifactId> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-jpa</artifactId> </dependency>
@Configuration @EnableSolrRepositories(basePackages = {"io.ymq.solr"}, multicoreSupport = true) public class SolrConfig { @Value("${spring.data.solr.zk-host}") private String zkHost; @Bean public CloudSolrClient solrClient() { return new CloudSolrClient(zkHost); } }
@SolrDocument(solrCoreName = "test_collection") public class Ymq implements Serializable { @Id @Field private String id; @Field private String ymqTitle; @Field private String ymqUrl; @Field private String ymqContent; get 。。。 set 。。。 }
public interface YmqRepository extends SolrCrudRepository<Ymq, String> { /** * 經過標題查詢 * * @param ymqTitle * @return */ @Query(" ymqTitle:*?0* ") public List<Ymq> findByQueryAnnotation(String ymqTitle); }
package io.ymq.solr.utils; import io.ymq.solr.pagehelper.PageInfo; import io.ymq.solr.pagehelper.PageInfoFacet; import io.ymq.solr.pagehelper.RowBounds; import org.apache.solr.client.solrj.SolrQuery; import org.apache.solr.client.solrj.SolrServerException; import org.apache.solr.client.solrj.response.QueryResponse; import java.io.IOException; import java.util.Collection; import java.util.List; /** * 描述: CloudSolrClient 工具類 * * @author yanpenglei * @create 2017-10-19 10:56 **/ public interface BaseSolr { /** * 添加數據 * * @param defaultCollection solr 庫 * @param bean 對象 * @throws IOException * @throws SolrServerException */ public void add(String defaultCollection, Object bean) throws IOException, SolrServerException; /** * 添加一組數據 * * @param defaultCollection solr 庫 * @param beans list集合數據添加 * @throws IOException * @throws SolrServerException */ public void adds(String defaultCollection, Collection<?> beans) throws IOException, SolrServerException; /** * 根據多個id刪除數據 * * @param defaultCollection * @param ids * @throws IOException * @throws SolrServerException */ public void deleteByIds(String defaultCollection, List<String> ids) throws IOException, SolrServerException; /** * 根據ID刪除數據 * * @param defaultCollection solr 庫 * @param id 要刪除的文檔的id * @throws IOException * @throws SolrServerException */ public void deleteById(String defaultCollection, String id) throws IOException, SolrServerException; /** * 根據指定索引(字段)模糊刪除數據 * * @param defaultCollection solr 庫 * @param field * @param fieldValue * @throws IOException * @throws SolrServerException */ public void deleteByField(String defaultCollection, String field, String fieldValue) throws IOException, SolrServerException; /** * 查詢數據 * * @param defaultCollection solr 庫 * @param clazz 對象Po * @param query 查詢條件 * @param <T> 返回查詢集合 * @return * @throws IOException * @throws SolrServerException */ public <T> List<T> query(String defaultCollection, Class<T> clazz, SolrQuery query) throws IOException, SolrServerException; /** * 查詢 * * @param defaultCollection solr 庫 * @param query 查詢條件 * @return 返回response對象 * @throws IOException * @throws SolrServerException */ public QueryResponse query(String defaultCollection, SolrQuery query) throws IOException, SolrServerException; /** * @param defaultCollection solr 庫 * @param clazz 查詢的數據對應的對象 * @param query 查詢條件 * @param rowBounds 分頁參數 * @param <T> * @return * @throws IOException * @throws SolrServerException */ public <T> PageInfo query(String defaultCollection, Class<T> clazz, SolrQuery query, RowBounds rowBounds) throws IOException, SolrServerException; /** * 查詢數據 * * @param defaultCollection solr 庫 * @param query 查詢條件 * @param rowBounds 分頁 * @return * @throws IOException * @throws SolrServerException */ public PageInfo query(String defaultCollection, SolrQuery query, RowBounds rowBounds) throws IOException, SolrServerException; /** * solrj的facet結果集查詢 * * @param defaultCollection solr 庫 * @param query 查詢條件 * @param rowBounds 分頁數 * @return * @throws IOException * @throws SolrServerException */ public PageInfoFacet queryFacet(String defaultCollection, SolrQuery query, RowBounds rowBounds) throws IOException, SolrServerException; }
application.properties
json
#SolrCloud zookeeper spring.data.solr.zk-host=node1:2181,node2:2181,node3:2181
package io.ymq.solr.test; import com.alibaba.fastjson.JSONObject; import io.ymq.solr.YmqRepository; import io.ymq.solr.pagehelper.PageInfo; import io.ymq.solr.pagehelper.RowBounds; import io.ymq.solr.po.Ymq; import io.ymq.solr.run.Startup; import io.ymq.solr.utils.BaseSolr; import org.apache.solr.client.solrj.SolrQuery; import org.apache.solr.client.solrj.SolrServerException; import org.apache.solr.client.solrj.impl.CloudSolrClient; import org.apache.solr.client.solrj.response.QueryResponse; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import java.io.IOException; import java.util.List; /** * 描述: 測試 solr cloud * * @author yanpenglei * @create 2017-10-17 19:00 **/ @RunWith(SpringRunner.class) @SpringBootTest(classes = Startup.class) public class BaseTest { @Autowired private YmqRepository ymqRepository; @Autowired private CloudSolrClient cloudSolrClient; @Autowired private BaseSolr baseSolr; /** * 使用 ymqRepository 方式新增 * * @throws Exception */ @Test public void testAddYmqRepository() { Ymq ymq1 = new Ymq(); ymq1.setId("1"); ymq1.setYmqTitle("penglei"); ymq1.setYmqUrl("www_ymq_io"); ymq1.setYmqContent("ymqContent"); Ymq ymq2 = new Ymq(); ymq2.setId("2");// ymq2.setYmqTitle("penglei"); ymq2.setYmqUrl("www_ymq_io"); ymq2.setYmqContent("ymqContent"); ymqRepository.save(ymq1); ymqRepository.save(ymq2); } /** * 使用 cloudSolrClient 方式新增 * * @throws Exception */ @Test public void testAddCloudSolrClient() throws IOException, SolrServerException { Ymq ymq = new Ymq(); ymq.setId("3"); ymq.setYmqTitle("penglei"); ymq.setYmqUrl("www_ymq_io"); ymq.setYmqContent("ymqContent"); cloudSolrClient.setDefaultCollection("test_collection"); cloudSolrClient.connect(); cloudSolrClient.addBean(ymq); cloudSolrClient.commit(); } /** * 刪除數據 */ @Test public void testDelete() { Ymq ymq = new Ymq(); ymq.setId("4"); ymq.setYmqTitle("delete_penglei"); ymq.setYmqUrl("www_ymq_io"); ymq.setYmqContent("ymqContent"); // 添加一條測試數據,用於刪除的測試數據 ymqRepository.save(ymq); // 經過標題查詢數據ID List<Ymq> list = ymqRepository.findByQueryAnnotation("delete_penglei"); for (Ymq item : list) { System.out.println("查詢響應 :" + JSONObject.toJSONString(item)); //經過主鍵 ID 刪除 ymqRepository.delete(item.getId()); } } /** * data JPA 方式查詢 * * @throws Exception */ @Test public void testYmqRepositorySearch() throws Exception { List<Ymq> list = ymqRepository.findByQueryAnnotation("penglei"); for (Ymq item : list) { System.out.println(" data JPA 方式查詢響應 :" + JSONObject.toJSONString(item)); } } /** * SolrQuery 語法查詢 * * @throws Exception */ @Test public void testYmqSolrQuery() throws Exception { SolrQuery query = new SolrQuery(); String ymqTitle = "penglei"; query.setQuery(" ymqTitle:*" + ymqTitle + "* "); cloudSolrClient.setDefaultCollection("test_collection"); cloudSolrClient.connect(); QueryResponse response = cloudSolrClient.query(query); List<Ymq> list = response.getBeans(Ymq.class); for (Ymq item : list) { System.out.println("SolrQuery 語法查詢響應 :" + JSONObject.toJSONString(item)); } } /** * 使用 baseSolr 工具類 查詢 * * @throws Exception */ @Test public void testBaseSolrQuery() throws Exception { SolrQuery query = new SolrQuery(); String ymqTitle = "penglei"; String defaultCollection = "test_collection"; query.setQuery(" ymqTitle:*" + ymqTitle + "* "); List<Ymq> list = baseSolr.query(defaultCollection, Ymq.class, query); for (Ymq item : list) { System.out.println("baseSolr 工具類 查詢響應 :" + JSONObject.toJSONString(item)); } } /** * 使用 baseSolr 工具類 分頁 查詢 * * @throws Exception */ @Test public void testBaseSolrPageInfoQuery() throws Exception { SolrQuery query = new SolrQuery(); String ymqTitle = "penglei"; String defaultCollection = "test_collection"; query.setQuery(" ymqTitle:*" + ymqTitle + "* "); PageInfo pageInfo = baseSolr.query(defaultCollection, Ymq.class, query,new RowBounds(0,2)); System.out.println("使用 baseSolr 工具類 分頁 查詢響應 :" + JSONObject.toJSONString(pageInfo)); } }
一些查詢,響應segmentfault
data JPA 方式查詢響應 :{"id":"1","ymqContent":"ymqContent","ymqTitle":"penglei","ymqUrl":"www_ymq_io"} data JPA 方式查詢響應 :{"id":"2","ymqContent":"ymqContent","ymqTitle":"penglei","ymqUrl":"www_ymq_io"} data JPA 方式查詢響應 :{"id":"3","ymqContent":"ymqContent","ymqTitle":"penglei","ymqUrl":"www_ymq_io"}
代碼我已放到 Github ,導入ymq-solr-cloud-spring-boot
項目 app
github https://github.com/souyunku/ymq-example/tree/master/ymq-solr-cloud-spring-boot分佈式