lucene 重新命名ElasticSearch中的字段名称

suzh9iv8  于 2022-11-07  发布在  Lucene
关注(0)|答案(1)|浏览(116)

我正在尝试重命名ElasticSearch中的文件名,下面是示例数据,其中我需要更改一些/所有字段名和嵌套对象

{   
   "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\")"  

  }  }

但是这个解决方案只会重命名/修改一个字段(甚至不是嵌套的字段),我如何在ElasticSearch索引中重命名多个/所有上述字段。
感谢您的快速响应,并感谢您的快速响应

vdgimpew

vdgimpew1#

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

示例:

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

categories.name重新命名为categories.categoryName

POSThttp://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\");"
  }
}

相关问题