简体   繁体   中英

Elasticsearch: upsert working on older version but not on newer version

I have an upsert query running in bulk. Final document is to be stored like this:

{
  "email": "abc@xyz.com",
  "sources": [1,2]
}

Here is the code:

var doc = {
  "source": parseInt(id),
  "email": email
}

var upsert_query = {
  "script": "if (ctx._source.containsKey(\"sources\")) { if (!ctx._source.sources.contains(source)) { ctx._source.sources += source; } } else {ctx._source.sources = [source] }",
  "params": {
    "source": doc.source
  },
  "upsert": {
    "email": doc.email,
    "sources": [doc.source]
  }
}

bulkRequestBody.push({"update": {"_index": "my_index", "_type": "email", "_id": doc.email, "_retry_on_conflict": 3}});
bulkRequestBody.push(upsert_query);

The code works perfectly fine on elasticsearch version 1.4 but not working on version 2.1.1.

I also tried to restructure my query:

var upsert_query = {
  "script": {
    "inline": "if (ctx._source.containsKey(\"sources\")) { if (!ctx._source.sources.contains(source)) { ctx._source.sources += source; } } else {ctx._source.sources = [source] }",
    "params": {
      "source": doc.source
    }
  },
  "upsert": {
    "email": doc.email,
    "sources": [doc.source]
  }
}

but still no luck. Any help ?

Scripting needs to be enabled to run scripts like this:

in the elasticsearch.yml file in config, add the following lines:

script.inline: on
script.indexed: on

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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