英文:
Update of a record in elastic using elastic 8.7 javaclient
问题
I attempted to add a couple new fields to an existing record in elastic search. The record is being updated, however all previously indexed fields have been cleared.
Is there something am I doing wrong?
Maven dependency
<dependency>
<groupId>co.elastic.clients</groupId>
<artifactId>elasticsearch-java</artifactId>
<version>8.7.1</version>
</dependency>
My code:
RestClientBuilder builder = RestClient.builder(new HttpHost(esIP, Integer.parseInt(esPort)));
RestClient restClient = builder.build();
// jsonMap indicates one record to be indexed
br.operations(op -> op.index(idx -> idx.index((String) jsonMap.get("index_name")).id(jsonMap.get("id").toString()).document(jsonMap)));
英文:
I attempted to add a couple new fields to an existing record in elastic search. The record is being updated, however all previously indexed fields have been cleared.
Is there something am I doing wrong?
Maven dependency
<dependency>
<groupId>co.elastic.clients</groupId>
<artifactId>elasticsearch-java</artifactId>
<version>8.7.1</version>
</dependency>
Me code:
RestClientBuilder builder = RestClient.builder(new HttpHost(esIP, Integer.parseInt(esPort)));
RestClient restClient = builder.build();
//jsonMap indecates one record to be indexed
br.operations(op -> op.index(idx -> idx.index((String) jsonMap.get("index_name")).id(jsonMap.get("id").toString()).document(jsonMap)));
答案1
得分: 0
Here's the translated code part without the comments:
RestClient restClient = RestClient.builder(
new HttpHost("localhost", 9200)).build();
ElasticsearchTransport transport = new RestClientTransport(
restClient, new JacksonJsonpMapper());
ElasticsearchClient client = new ElasticsearchClient(transport);
HashMap jsonMap = new HashMap();
jsonMap.put("field", "new value");
UpdateRequest.Builder builder = new UpdateRequest.Builder();
builder.index("index_name")
.id("id")
.doc(jsonMap);
UpdateResponse response = client.update(builder.build(), Object.class);
System.out.println("Indexed with version " + response.version());
英文:
Try following, you shoud exclude meta data (like index, id) from doc param.
RestClient restClient = RestClient.builder(
new HttpHost("localhost", 9200)).build();
// Create the transport with a Jackson mapper
ElasticsearchTransport transport = new RestClientTransport(
restClient, new JacksonJsonpMapper());
// And create the API client
ElasticsearchClient client = new ElasticsearchClient(transport);
HashMap jsonMap = new HashMap();
jsonMap.put("field", "new value");
UpdateRequest.Builder builder = new UpdateRequest.Builder();
builder.index("index_name")
.id("id")
.doc(jsonMap); // exclude meta data (like index, id)
UpdateResponse response = client.update(builder.build(), Object.class);
System.out.println("Indexed with version " + response.version());
答案2
得分: 0
以下是您提供的代码的中文翻译:
public class ElasticLowLevelRemote {
public static void main(String[] args) throws IOException {
Scanner input = new Scanner(System.in);
System.out.print("输入索引:");
String indexInput = input.nextLine();
System.out.print("输入 ID:");
String id = input.nextLine();
RestClient restClient = RestClient.builder(
new HttpHost("IP地址", 端口, "http"))
.setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(
new BasicCredentialsProvider() {{
setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials("用户名", "密码"));
}})).build();
HttpEntity entity = new NStringEntity("{" +
"\n \"query\": {" +
"\n \"match\": {" +
"\n \"_id\": \"" + id + "\"" +
"\n }" +
"\n }" +
"}",
ContentType.APPLICATION_JSON);
Response response = restClient.performRequest(
"GET", "/" + indexInput + "/_search",
Collections.emptyMap(),
entity);
String jsonResponse = EntityUtils.toString(response.getEntity());
ObjectMapper mapper = new ObjectMapper();
Object json = mapper.readValue(jsonResponse, Object.class);
String formattedJson = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(json);
System.out.println(formattedJson);
restClient.close();
}
}
希望这对您有所帮助。如果您有任何其他问题,请随时提出。
英文:
So you are developing low level client. And as your provided source code is not full enough to understand, I'm giving a sample Low Level Rest API over Remote Elasticsearch:
public class ElasticLowLevelRemote {
public static void main(String[] args) throws IOException {
Scanner input = new Scanner(System.in);
System.out.print("enter index ");
String indexInput = input.nextLine();
System.out.print("enter id ");
String id = input.nextLine();
RestClient restClient = RestClient.builder(
new HttpHost("ipaddress", port, "http"))
.setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(
new BasicCredentialsProvider() {{
setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials("username", "password"));}})).build();
HttpEntity entity = new NStringEntity("{\n \"query\": {\n \"match\": {\n \"_id\": \"" + id + "\"\n }\n }\n}",
ContentType.APPLICATION_JSON);
Response response = restClient.performRequest(
"GET", "/"+indexInput+"/_search",
Collections.emptyMap(),
entity);
String jsonResponse = EntityUtils.toString(response.getEntity());
ObjectMapper mapper = new ObjectMapper();
Object json = mapper.readValue(jsonResponse, Object.class);
String formattedJson = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(json);
System.out.println(formattedJson);
restClient.close();
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论