简体   繁体   English

Elasticsearch curl请求无法在Angular 4中按预期工作

[英]Elasticsearch curl request not working as expected in Angular 4

I installed the curl npm package ( https://www.npmjs.com/package/curl ) in my Angular project and I have been using the curl.postJSON() method to post to my Elasticsearch cluster on the cloud. 我在Angular项目中安装了curl npm软件包( https://www.npmjs.com/package/curl ),并且我一直在使用curl.postJSON()方法发布到云上的Elasticsearch集群中。 I need to use the curl.get() method now and it isn't working as expected. 我现在需要使用curl.get()方法,它无法按预期工作。

In the terminal, this is the curl request that works and I need to use in my Angular project: 在终端中,这是有效的curl请求,我需要在Angular项目中使用:

curl -XGET https://search-example-xxxxxxxxxxxxxxxxxxx.us-east-2.es.amazonaws.com/myproject/_search? -d '
{ 
    "query" :
    { "match": { "myfield": "NJ-PA-xxxx-x" } }

}
'

This is the method in my main component that isn't working: 这是我主要组件中无法使用的方法:

myMethod(){
    var getData =  { "query" : { "match": { "myfield": "NJ-PA-xxxx-x" } } };

    var circLink = curl.get("https://search-example-xxxxxxxxxxxxxxxxxxx.us-east-2.es.amazonaws.com/myproject/_search", getData,
      function (err, httpResponse, body) {
          console.log(err, body);
      });
   console.log('this is the circLink: ' + circLink );  
}

This returns "this is the circLink: undefined" 这将返回“这是CircLink:未定义”

because of the console.log(err, body), it is also returning every document (all 33) in my elasticsearch cluster. 由于console.log(err,body),它还返回了我的Elasticsearch集群中的每个文档(共33个)。 Meanwhile when I run the curl command in the terminal it returns only 3 documents. 同时,当我在终端中运行curl命令时,它仅返回3个文档。 Any ideas how I can get the proper curl request returned? 有什么想法可以使我获得正确的卷曲请求吗?

I think it might have to do with the " -d " in my terminal curl request and not in angular, because when I run the get request without the " -d " in terminal, it returns all of the documents in the elasticsearch cluster. 我认为这可能与终端curl请求中的“ -d”有关,而不与角度有关,因为当我在终端中运行不带“ -d”的get请求时,它将返回elasticsearch集群中的所有文档。 I still don't know how to incorporate the " -d " with curl.get() though 我仍然不知道如何将curl -get()与“ -d”合并。

  1. The curl package has no change since 2011. I recommend the official elasticsearch.js client ( https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/index.html ). curl软件包自2011年以来没有变化。我推荐使用官方的elasticsearch.js客户端( https://www.elastic.co/guide/zh-CN/elasticsearch/client/javascript-api/current/index.html )。

  2. You can send technically a HTTP GET request with a body, but this has downsides for various reasons ( GET HTTP request payload ) 您可以从技术上发送带有主体的HTTP GET请求,但是由于各种原因,这样做有缺点( GET HTTP请求有效负载

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM