具有匹配ID的Elasticsearch更新文档

时间:2018-03-19 15:05:00

标签: node.js elasticsearch

我正在尝试更新elasticsearch中与id字段匹配的文档。如果formData.id与elasticsearch中的任何文档匹配,我希望更新mobile_numberprimary_email_id。我还想在匹配结果中插入一个新字段website。为此,我尝试了以下查询:

function updateForm(formData) {
var deferred = Q.defer();
client.update({
    index: 'collections',
    type: 'automobiles',
    id: '8s5WEbYZ6NUAliwOBf',
     body: {
            query: {
                must: { 
                  match :  {
                      id: formData.id
                    } 
                },
            },
            doc: {
                'mobile_number' : formData.phone,
                'primary_email_id': formData.email,
                'website' : formData.website
            }

    }
},function (error, response) {
    if(error){
        console.log(error);
        return error;
    }else {
        deferred.resolve(response);
    }

  }); 
  return deferred.promise;
}

这给了我以下错误:

Error: [action_request_validation_exception] Validation Failed: 1: script or doc is missing;

我的查询中已经有一个doc字段。我应该做哪些修改才能执行此类更新?这种操作还有其他更好的方法吗?

2 个答案:

答案 0 :(得分:1)

我对JS API不是很熟悉,但我认为它应该是这样的:

function updateForm(formData) {
  var deferred = Q.defer();
  client.update(
    {
      index: 'collections',
      type: 'automobiles',
      id: formData.id,
      body: {
        doc: {
          'mobile_number' : formData.phone,
          'primary_email_id': formData.email,
          'website' : formData.website
        }
      }
    },  
    function (error, response) {
      if(error) {
        console.log(error);
        return error;
      } else {
        deferred.resolve(response);
     }
    }
  ); 
  return deferred.promise;
}

基于:https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/api-reference.html#api-update

答案 1 :(得分:0)

我修改了我的查询,现在正在运行。 # myawesomeapp/nginx/default.config server { listen 80; server_name localhost; root /usr/share/nginx/html; index index.html index.htm; add_header 'Access-Control-Allow-Origin' '*'; location = / { } location = /index.html { } location /img { } location /css { } location /js { } location /tos { } location / { rewrite ^ $scheme://$http_host/#$request_uri last; break; } } id中每个文档的唯一唯一字段。所以,我必须在collections中包括它。更新的功能:

doc

此处,正文字段外的function putForm(formData) { console.log(formData); var deferred = Q.defer(); client.update({ index: 'collections', type: 'automobile', id: '8s5WEbYZ6NUAliwOBf', body: { doc: { 'id' : formData.id, 'mobile_number' : formData.mobile_number, 'primary_email_id': formData.primary_email_id, 'website' : formData.website, } } },function (error, response) { if(error){ console.log(error); return error; }else { deferred.resolve(response); } }); return deferred.promise; } id的ID,而collections指的是集合中的个别文档。