Skip to content

Instantly share code, notes, and snippets.

@bejean
Created February 28, 2024 09:52
Show Gist options
  • Save bejean/5a9238e07b8f43844a32b39d2c341b0b to your computer and use it in GitHub Desktop.
Save bejean/5a9238e07b8f43844a32b39d2c341b0b to your computer and use it in GitHub Desktop.
Update Solr avec HttpClinet en Java
package com.taligentia.solrj;
import org.apache.solr.client.solrj.SolrResponse;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.CloudSolrClient;
import org.apache.solr.client.solrj.request.UpdateRequest;
import org.apache.solr.common.SolrInputDocument;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpResponse;
import java.net.http.HttpRequest;
import java.util.*;
import java.util.Map.Entry;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class SolrJ {
// https://solr.apache.org/guide/solr/latest/indexing-guide/partial-document-updates.html#updating-child-documents
// https://solr.apache.org/guide/solr/latest/indexing-guide/indexing-nested-documents.html
// https://solr.apache.org/guide/8_1/basic-authentication-plugin.html#global-jvm-basic-auth-credentials
/* Usage ---
SolrJ solrj = new SolrJ("localhost:9983", "localhost:8983", "admin", "passwd");
solrj.connect();
solrj.addDocJson(collection, jsonDoc);
*/
final String zkHost;
final String solrHost;
final String user;
final String passwd;
CloudSolrClient client;
HttpClient httpClient;
public SolrJ(String zkHost, String solrHost, String user, String passwd) {
this.zkHost = zkHost;
this.solrHost = solrHost;
this.user = user;
this.passwd = passwd;
}
public void connect() {
final List<String> zkServers = new ArrayList<String>();
zkServers.add(zkHost);
client = new CloudSolrClient.Builder (zkServers, Optional.empty()).build();
httpClient = HttpClient.newHttpClient();
}
public void addDocJson(String collection, String json) throws SolrServerException, IOException, InterruptedException {
String serviceUrl = "http://" + solrHost + "/solr/" + collection + "/update";
serviceUrl += "?commitWithin=1000";
String body = "[" + json + "]";
HttpRequest request;
if (user != null && !"".equals(user))
request = HttpRequest.newBuilder()
.uri(URI.create(serviceUrl))
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(body))
.header("Authorization", "Basic " +
Base64.getEncoder().encodeToString((user + ":" + passwd).getBytes()))
.build();
else
request = HttpRequest.newBuilder()
.uri(URI.create(serviceUrl))
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(body))
.build();
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
int code = response.statusCode();
}
public void addDoc(String collection, SolrInputDocument doc) throws SolrServerException, IOException {
UpdateRequest req = new UpdateRequest();
req.setBasicAuthCredentials(user,passwd);
req.add(doc);
if( !req.getDocuments().isEmpty() ) {
req.setCommitWithin(500);
SolrResponse solrResponse = req.process(client, collection);
}
}
public SolrInputDocument buildSolrInputDocument(Map<String, Object> values) {
final SolrInputDocument doc = new SolrInputDocument();
for (Entry<String, Object> entry : values.entrySet()) {
doc.addField(entry.getKey(), entry.getValue());
}
if (!values.containsKey("id")) {
String id = (String) values.get("idContact");
// Si Solrcloud mode alors, on construit un composite id pour la
// colocalisation futur des nested children sur un même noeud solr
//if (talidataSearchManager.getConfiguration().getZkServer().length() > 0)
// id = values.get("idContact") + "!" + id;
doc.addField("id", id);
}
return doc;
}
public static String mapToJsonString (Map<String, Object> map, boolean pretty) {
if (map==null)
return null;
ObjectMapper objectMapper = new ObjectMapper();
try {
if (pretty)
return objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(map);
else
return objectMapper.writeValueAsString(map);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment