当前位置:网站首页>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();
}
}边栏推荐
- [kingcraft] 3.1 link layer - functions of data link layer
- The mystery of number idempotent and perfect square
- How about the CSC account of qiniu business school? Is it safe?
- Multipass中文文档-使用指引(目录页)
- BGP基础概念及IBGP基本配置
- 【无标题】测试下啊
- 333333
- LeetCode 1996. Number of weak characters in the game*
- Dynamic programming classical topic triangle shortest path
- 海贼oj#448.抽奖
猜你喜欢

With a market value of 21.5 billion yuan, will the post-80s generation in Sichuan make TV history?

Décomposition détaillée du problème de chemin le plus court du graphique

安全培训管理办法

AQS explanation of concurrent programming

JVM 垃圾回收机制和常见的垃圾回收器

JVM garbage collection mechanism and common garbage collectors

DevOps到底是什么意思?

五大类型负载均衡的原理场景详解

Dynamic programming classical topic triangle shortest path

Block queue - delayedworkqueue Source Analysis
随机推荐
系统应用安装时,签名校验失败问题
Unity custom folder icon color personalized unity compiler
哈工大软件构造复习——LSP原则,协变和逆变
Rich text activity test 1
Unable to return to the default page after page Jump
[database] MySQL index interview questions
Blocking queue - delayedworkqueue source code analysis
Network Engineer required course firewall security zone and basic operation of security policy
数的奥秘之幂数与完全平方数
B 树的简单认识
Deploy netron services through kubernetes and specify model files at startup
五大类型负载均衡的原理场景详解
自动化测试系列
12324243242
Can I buy annuity insurance? Is annuity insurance safe?
canvas绘画折线段
452. detonate the balloon with the minimum number of arrows
动态规划经典题目三角形最短路径
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
[database] types of NoSQL database
