【问题标题】:Re-name the field names in elasticsearch重命名elasticsearch中的字段名称
【发布时间】:2016-08-17 06:51:12
【问题描述】:

我正在尝试重命名弹性搜索中的文件名,下面是示例数据,其中我需要更改部分/所有字段名以及嵌套对象

{   
   "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 lucene


【解决方案1】:

重命名字段名称的最简单方法是使用_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\");"
  }
}

【讨论】:

  • 在 Elasticsearch 7.1.11 中将内联更改为源 "**source**": "ctx._source.categories.categoryName= ctx._source.categories.name; ctx._source.categories.remove(\"name\");"
猜你喜欢
  • 2015-04-14
  • 2020-02-14
  • 1970-01-01
  • 2022-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多