简体   繁体   中英

Elasticsearch - Pick up a new property with calculation in bulk

I'm trying to wrap my head around how ES works, but don't seem to succeed. I'd like it to read JSON files and append a new property to each entry via calculating a value from the existing ones. What would be the syntax for this? I can't seem to put together the information from the documentation to get it to work. Thank you!

I'm trying to do something like

POST /mystuff/1/_update
{
"doc": {
     "recency": (DateTime.now().getMillis() - doc['maxdate'].value)/(24*60*60*1000)
     }
}

to all the docs under /mystuff . (This obviusly doesn't work.)

[EDIT] Sample input:

PUT /mystuff/1
{
...
  "maxdate": "2016-06-14",
...
}

ES Version: 2.3.4

You have to use _update_by_query API by Elasticsearch to update more than one document. You can read more about that here .

Your code will change in following way

POST mystuff/_update_by_query
{
  "script": {
    "inline": "ctx._source.recency = (DateTime.now().getMillis() - DateTime.parse(ctx._source.maxdate).getMillis())/(24*60*60*1000)"
  },
  "query": {
    "match_all": {
    }
  }
}

You should have script inline and script update enabled for this to work.

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