代码之家  ›  专栏  ›  技术社区  ›  Suresh AK

使用新的键值对更新弹性搜索数据

  •  3
  • Suresh AK  · 技术社区  · 6 年前
    {
       "took": 0,
       "timed_out": false,
       "_shards": {
       "total": 5,
       "successful": 5,
       "skipped": 0,
       "failed": 0
    },
    "hits": {
    "total": 25,
    "max_score": 1,
    "hits": [
      {
        "_index": "surtest1",
        "_type": "catalog",
        "_id": "prod_9876561740",
        "_score": 1,
        "_source": {
          "id": "01",
          "type": "product" 
        }
      },
      {
        "_index": "surtest1",
        "_type": "catalog",
        "_id": "prod_9876543375",
        "_score": 1,
        "_source": {
          "id": "02",
          "type": "product" 
        }
      }
    ]
    }
    }
    

    这是弹性搜索中搜索的json响应示例。 我们需要在所有json对象中再添加一个键值对(“spec”:“4gb”),例如,

    "_source": {
          "id": "01",
          "type": "product" ,
          "spec": "4gb"
        },
    "_source": {
          "id": "02",
          "type": "product" ,
          "spec": "4gb"
        }
    

    此更新应在单个命令中进行。请指导我们执行此操作。

    2 回复  |  直到 6 年前
        1
  •  2
  •   sramalingam24    6 年前

    尝试

    POST /surtest1/_update_by_query?refresh
    {
    "script": {
        "source": "ctx._source['spec']='4gb'"
     }
    }
    
        2
  •  0
  •   Piotr Pradzynski    6 年前

    看看 Update By Query API 。您可以准备 a query to match all documents 和使用 scripting 添加所需的属性。

    示例:

    POST twitter/_update_by_query
    {
      "script": {
        "source": "ctx._source.likes++",
        "lang": "painless"
      },
      "query": {
        "term": {
          "user": "kimchy"
        }
      }
    }