0

您好我正在尝试使用 spring RestTemplate 在弹性搜索中搜索数据。ElasticSearch 有用户名和密码,我想通过 json 进行搜索。

我为此编写了代码,但没有得到任何结果或异常。我有生以来第一次这样做,如果其中有一些愚蠢的错误,我深表歉意。

@Override
    protected List<JobPosts> doInBackground(Object[] objects) {
        List list = null;

        try {

            SearchForm searchForms = (SearchForm) objects[0];




            String plainCreds = "******:********";

            final String url = "*******";
            RestTemplate restTemplate = new RestTemplate();
            restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());

            HttpEntity<String> request = new HttpEntity<>(searchJson, headers);
            Log.d("location", "before exchange");
            ResponseEntity<JobPosts[]> response = restTemplate.exchange(url, HttpMethod.GET, request, JobPosts[].class);
            JobPosts[] jobPosts = response.getBody();

            Log.d("location", "after exchange");
            list = Arrays.asList(jobPosts);


        } catch (Exception e) {
            Log.d("location", e.getMessage());
        }
4

2 回答 2

1

与其他关系数据库不同,您不需要 Spring RestTemplate 来查询弹性数据库。ElasticSearch 带有内置的 Java API 库。您可以直接使用这些函数来创建查询并获取结果。

签出此链接。它有关于如何使用 API 的文档。

弹性搜索 Java API 5.1

于 2017-02-15T11:01:10.057 回答
0

我会推荐使用Tanay 提到的ES Java API 。

像这样设置你的连接

//Create the ES clien
org.elasticsearch.client.Client client;

//Setup the connection. Make sure you use port 9300 and not 9200 here.
client = new PreBuiltTransportClient(Settings.EMPTY)
                    .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), "9300"));

//Interact with your index, for example getting an object by its ID
GetResponse response = client.prepareGet("index", "type", "id")
    .setOperationThreaded(false)
    .get();

//Close the connection
client.close();
于 2017-03-07T07:40:22.280 回答