在elasticsearch中重命名字段名称

时间:2016-08-17 06:51:12

标签: elasticsearch lucene

我试图在弹性搜索中重命名字段名称,下面是样本数据,我需要更改部分/全部字段名称以及嵌套对象

{   
   "took": 63,  
   "timed_out": false,  
   "_shards": {  
      "total": 5,  
      "successful": 5,  
      "failed": 0  
   },  
   "hits": {  
      "total": 1000,  
      "max_score": 1,  
      "hits": [  
         {  
            "_index": "course",  
            "_type": "product",  
            "_id": "14",  
            "_score": 1,  
            "_source": {  
               "quantity": 25,  
               "price": "137.62",  
               "name": "Veal - Leg",  
               "description": "Morbi non lectus. Aliquam sit amet diam in magna bibendum imperdiet. Nullam orci pede, venenatis non, sodales sed, tincidunt eu, felis. Fusce posuere felis sed lacus. Morbi sem mauris, laoreet ut, rhoncus aliquet, pulvinar sed, nisl. Nunc rhoncus dui vel sem.",  
               "categories": [  
                  {   
                     "name": "Sport"  
                  },  
                  {  
                     "name": "Clothing"  
                  }  
               ],  
               "tagNames": [  
                  "phones",  
                  "elasticsearch"  
               ],  
               "status": "inactive" 
            }  
         }
}        

现在我想用新的字段名重命名以上所有现有字段我试过这个解决方案

  POST /_reindex  
{  
     "source": {  
    "index": "course"  
},  
"dest": {  
    "index": "course_new"  
  },  
 "script": {  
    "inline": "ctx._source.tags = ctx._source.remove(\"tagNames\")"  

  }  }   

但是此解决方案将仅重命名/修改一个字段(甚至不是嵌套字段),如何重命名弹性搜索索引中的多个/所有上述字段。

感谢您的快速回复并感谢您的快速回复

1 个答案:

答案 0 :(得分:0)

重命名 字段名称的最简单方法是使用 _update_by_query API:

示例:

"_source": {  
           "quantity": 25,  
           "price": "137.62",  
           "name": "Veal - Leg",
           "categories": [  
              {   
                 "name": "Sport"  
              },  
              {  
                 "name": "Clothing"  
              }  
           ] 
        }  

categories.name 重命名为 categories.categoryName

POST http:// localhost:9200 / INDEX_NAME / _update_by_query

{
  "query": { 
    "bool": {
        "must_not": {
            "exists": {
                "field": "categoryName"
            }
        }
    }
  },
  "script" : {
    "inline": "ctx._source.categories.categoryName= ctx._source.categories.name; ctx._source.categories.remove(\"name\");"
  }
}