什麼是solrjjava
solrj 是訪問Solr 服務的java客戶端,提供索引(增刪改)和搜索(查)的請求方法,Solrj 一般在嵌入在業務系統中,經過Solrj的API接口操做Solr服務,以下圖:apache
solr的使用服務器
第一步:配置pom.xmlmaven
內容以下:ui
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.chen</groupId> <artifactId>solr</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>solr</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/commons-io/commons-io --> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.7</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.solr/solr-solrj --> <dependency> <groupId>org.apache.solr</groupId> <artifactId>solr-solrj</artifactId> <version>7.2.1</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>RELEASE</version> </dependency> </dependencies> </project>
第二步:java代碼url
/** * FileName: SolrManager * Author: GET_CHEN * Date: 2018/4/3 14:29 * Description: */ package com.chen; 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.junit.Test; import java.io.IOException; import java.util.List; import java.util.Map; public class SolrManager { @Test public void testAdd() throws IOException, SolrServerException { String baseSolrUrl = "http://localhost:8080/solr/collection2"; //solr的訪問路徑。collection2是solr的核。默認是collection1. //鏈接solr服務器 HttpSolrClient httpSolrClient = new HttpSolrClient.Builder(baseSolrUrl).build(); SolrInputDocument document = new SolrInputDocument(); document.addField("id", "333"); document.addField("blog_title", "solrj的使用"); document.addField("blog_keyWord", "solrj"); httpSolrClient.add(document); httpSolrClient.commit(); httpSolrClient.close(); } @Test public void testDelete() throws IOException, SolrServerException { String baseSolrUrl = "http://localhost:8080/solr/collection2"; //鏈接solr服務器 HttpSolrClient httpSolrClient = new HttpSolrClient.Builder(baseSolrUrl).build(); // httpSolrClient.deleteById("333");//經過id刪除 httpSolrClient.deleteByQuery("*:*");//經過語法刪,刪除所有 httpSolrClient.commit(); httpSolrClient.close(); } @Test public void testQuery() throws Exception { String baseSolrUrl = "http://localhost:8080/solr/collection2"; System.out.println(baseSolrUrl); //鏈接solr服務器 HttpSolrClient httpSolrClient = new HttpSolrClient.Builder(baseSolrUrl).build(); SolrQuery solrQuery = new SolrQuery(); solrQuery.setQuery("*:*"); QueryResponse queryResponse = httpSolrClient.query(solrQuery); SolrDocumentList results = queryResponse.getResults(); for (SolrDocument document: results) { System.out.println(document.get("id")); System.out.println(document.get("blog_title")); System.out.println(document.get("blog_summary")); System.out.println(document.get("blog_keyWord")); System.out.println("=========================="); } httpSolrClient.commit(); httpSolrClient.close(); } @Test public void testDiffQuery() throws Exception{ String baseSolrUrl = "http://localhost:8080/solr/collection2"; //鏈接solr服務器 HttpSolrClient httpSolrClient = new HttpSolrClient.Builder(baseSolrUrl).build(); SolrQuery solrQuery = new SolrQuery(); //關鍵字 solrQuery.setQuery("大學"); //設置默認域 solrQuery.set("df","blog_title"); //設置查詢域 solrQuery.set("fl","id,blog_summary"); //排序 solrQuery.setSort("id", SolrQuery.ORDER.desc); //分頁 solrQuery.setStart(0); solrQuery.setRows(3); //高亮 solrQuery.setHighlight(true); //指定高亮域 solrQuery.addHighlightField("blog_title"); solrQuery.setHighlightSimplePre("<span style='color:red'>"); solrQuery.setHighlightSimplePost("</span>"); QueryResponse queryResponse = httpSolrClient.query(solrQuery); SolrDocumentList results = queryResponse.getResults(); //獲取高亮內容 Map<String, Map<String, List<String>>> highlighting = queryResponse.getHighlighting(); for (SolrDocument document: results) { System.out.println(document.get("id")); System.out.println(document.get("blog_title")); System.out.println(document.get("blog_summary")); System.out.println(document.get("blog_keyWord")); System.out.println("=========================="); Map<String, List<String>> map = highlighting.get(document.get("id")); List<String> blog_summary = map.get("blog_title"); System.out.println(blog_summary.get(0)); System.out.println("==========highlighting==============="); } httpSolrClient.commit(); httpSolrClient.close(); } }