当前位置:网站首页>Using solrj to add, delete, modify, and query Solr is too simple
Using solrj to add, delete, modify, and query Solr is too simple
2022-06-11 00:44:00 【A music loving programmer】
One 、SolrJ What is it? ?
SolrJ yes Solr Provided Java client API. adopt SolrJ Can achieve Java Program pair Solr Operation of data in .
The big premise : add to SolrJ rely on . Depending on the version and Solr The version strictly corresponds to
The version I use : According to their own solr The version corresponds to
dependencies>
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>7.7.2</version>
</dependency>
</dependencies>Two 、 test
1. Import dependence

2. Write code
1. Add / modify implementation : The code is as follows
Add and modify the syntax , Newly added id If the id, That will modify the existing id, If it doesn't exist, add
@Test
public void testAdd() throws Exception {
//Solr Address
String url = "http://192.168.0.107:8983/solr/testcore";
HttpSolrClient solrClient = null;
try {
//Solr client
solrClient = new HttpSolrClient.Builder(url).build();
//Solr Input document
SolrInputDocument solrInputDocument = new SolrInputDocument();
solrInputDocument.addField("id", "6");
solrInputDocument.addField("name", " PHS mobile ");
solrInputDocument.addField("price", 10000);
// Add input document
solrClient.add(solrInputDocument);
// Submit
solrClient.commit();
} catch (Exception e) {
e.fillInStackTrace();
} finally {
// close resource
solrClient.close();
}
}design sketch :

2. Delete implementation :
@Test
public void deleteTest() throws Exception {
String url = "http://192.168.0.107:8983/solr/testcore";
HttpSolrClient httpSolrClient = null;
try {
httpSolrClient = new HttpSolrClient.Builder(url).build();
httpSolrClient.deleteById("5");
httpSolrClient.commit();
} catch (Exception e) {
e.fillInStackTrace();
} finally {
httpSolrClient.close();
}
}3. Query function
// Inquire about
@Test
public void showSolr() throws Exception {
String url = "http://192.168.0.107:8983/solr/testcore";
HttpSolrClient solrClient = null;
try {
solrClient = new HttpSolrClient.Builder(url).build();
// Encapsulate all query conditions
SolrQuery solrQuery = new SolrQuery();
// Query all
solrQuery.setQuery("*:*");
// Query... According to the conditions
//solrQuery.setQuery("name= Best Sellers );
QueryResponse response = solrClient.query(solrQuery);
SolrDocumentList results = response.getResults();
System.out.println(" Total number of articles " + results.getNumFound());
for (SolrDocument list : results){
System.out.println(list);
}
} catch (Exception e) {
e.fillInStackTrace();
} finally {
solrClient.close();
}
}design sketch
4. Highlight operation
// The highlighted
@Test
public void highlightest() throws IOException {
// Connect solr Address
String url = "http://192.168.0.107:8983/solr/testcore";
// establish solr client
HttpSolrClient solrClient = null;
try {
solrClient = new HttpSolrClient.Builder(url).build();
// Encapsulating queries
SolrQuery solrQuery = new SolrQuery();
// Query... According to the conditions
solrQuery.setQuery("name: Best Sellers ");
// Turn on highlight
solrQuery.setHighlight(true);
// Set highlight key
solrQuery.addHighlightField("name");
// Prefix
solrQuery.setHighlightSimplePre("<span>");
// suffix
solrQuery.setHighlightSimplePost("</span>");
QueryResponse response = solrClient.query(solrQuery);
// get data
SolrDocumentList results = response.getResults();
// Get the highlighted return value
Map<String, Map<String, List<String>>> highlighting = response.getHighlighting();
// Traverse
for (SolrDocument list : results) {
System.out.println(list.get("id"));
Map<String, List<String>> map = highlighting.get(list.get("id"));
List<String> HLList = map.get("name");
// Judge
if (HLList != null && HLList.size() > 0) {
System.out.println(HLList.get(0));
} else {
System.out.println(list.get("name"));
}
System.out.println("===============");
System.out.println(list.get("price"));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// close resource
solrClient.close();
}
}边栏推荐
- Njupt Nanyou Discrete Mathematics_ Experiment 3
- Njupt Nanyou Discrete Mathematics_ Experiment 1
- [network planning] 2.1.2 transport layer services that can be selected by the application
- JVM 垃圾回收机制和常见的垃圾回收器
- MD5Util
- VTK example -- three intersecting planes
- 网络工程师必修课防火墙安全区域及安全策略基础操作
- [database] MySQL index interview questions
- Random points in non overlapping rectangles
- 对象作为点(Objects as Points) 个人总结
猜你喜欢
![[network planning] 2.5 brief introduction to P2P architecture](/img/a8/74a1b44ce4d8b0b1a85043a091a91d.jpg)
[network planning] 2.5 brief introduction to P2P architecture

Random points in non overlapping rectangles

The driver has not received any packets from the server

Download Google gcr IO image

Unable to return to the default page after page Jump
![[JVM] class loading mechanism](/img/62/24b6fbec273b5cbf2338b6f4b6fe6a.png)
[JVM] class loading mechanism
![[network planning] 2.4 DNS: directory service of the Internet](/img/a8/74a1b44ce4d8b0b1a85043a091a91d.jpg)
[network planning] 2.4 DNS: directory service of the Internet

VTK例子--三個相交的平面

MESI cache consistency protocol for concurrent programming
![[database] MySQL index interview questions](/img/ff/8713465293f728f57840237242e227.png)
[database] MySQL index interview questions
随机推荐
市值215亿,这个四川80后会让电视机成为历史?
Installation of phpstudy
How word removes the header line
网络工程师必修课防火墙安全区域及安全策略基础操作
Philips coo will be assigned to solve the dual crisis of "supply chain and product recall" in the face of crisis due to personnel change
Multipass Chinese documentation - Tutorial
LeetCode 1673. Find the most competitive subsequence**
[network counting] 1.4 network delay, packet loss and throughput
Kubernetes入门介绍与基础搭建
On the quality assurance system of youzan search V2021
[JVM] thread
With a market value of 21.5 billion yuan, will the post-80s generation in Sichuan make TV history?
阻塞隊列 — DelayedWorkQueue源碼分析
Dual wing layout
BGP基础概念及IBGP基本配置
Décomposition détaillée du problème de chemin le plus court du graphique
[database] types of NoSQL database
阿里云配置SLB(负载均衡)实例
Unity custom folder icon color personalized unity compiler
动态规划经典题目三角形最短路径
