如何使用fastjson替换JacksonJsonpMapper;Elasticsearch v7.16.2x1c 0d1x JSON对象Map器。这将您的应用程序类Map到JSON,并将它们与API客户端无缝集成。
u3r8eeie1#
1. Elasticsearch_Low_Level_Client_Local_Host_Connection:
import java.io.IOException; import java.util.Collections; import org.apache.http.HttpEntity; import org.apache.http.HttpHost; import org.apache.http.entity.ContentType; import org.apache.http.nio.entity.NStringEntity; import org.apache.http.util.EntityUtils; import org.elasticsearch.client.Response; import org.elasticsearch.client.RestClient; public class LowLevelClientLocalServer { public static void main(String[] args) throws IOException { RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build(); HttpEntity entity = new NStringEntity("{\n \"query\": {\n \"match_all\": {}\n }\n}", ContentType.APPLICATION_JSON); Response response = restClient.performRequest( "GET", "/"INDEX_NAME"/_doc/"SEARCH_KEYWORD", Collections.emptyMap()); System.out.println(EntityUtils.toString(response.getEntity())); restClient.close(); } }
**2. Elasticsearch_Low_Level_Client_Remote_Server_Connection:**此项要求输入index_name和search_keyword
index_name
search_keyword
import java.io.IOException; import java.util.Collections; import java.util.Scanner; import org.apache.http.HttpEntity; import org.apache.http.HttpHost; import org.apache.http.auth.AuthScope; import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.entity.ContentType; import org.apache.http.impl.client.BasicCredentialsProvider; import org.apache.http.nio.entity.NStringEntity; import org.apache.http.util.EntityUtils; import org.elasticsearch.client.Response; import org.elasticsearch.client.RestClient; import com.fasterxml.jackson.databind.ObjectMapper; public class LowLevelClientRemoteServer { 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(); } }
1条答案
按热度按时间u3r8eeie1#
1. Elasticsearch_Low_Level_Client_Local_Host_Connection:
**2. Elasticsearch_Low_Level_Client_Remote_Server_Connection:**此项要求输入
index_name
和search_keyword