简体   繁体   English

从 Elastic Search 的索引中删除现有字段

[英]Removing an existing field from an index in Elastic Search

I am a newbie in Elastic search.我是弹性搜索的新手。 I have an unwanted field in my index say index name "test_index".我的索引中有一个不需要的字段,例如索引名称“test_index”。 This contains almost 155154 documents.这包含近 155154 个文档。 I want to remove an unwanted field "B" from my index.我想从我的索引中删除不需要的字段“B”。 This is how my index pattern look like in json format这就是我的索引模式在 json 格式中的样子

{A: {B: {C: } } {A:{B:{C:}}

I believe removing B will automatically remove C from my index as well.我相信删除 B 也会自动从我的索引中删除 C 。 In order to do that, I used the following query but it didn't seem to work.为了做到这一点,我使用了以下查询,但它似乎不起作用。

POST test_index/_update_by_query?conflicts=proceed {
      "script" : "ctx._source.A.remove('B')", 
       "query" : {
        "exists": { "field": "A.B" }
      }
} 

Please let me know where I am doing the mistake.请让我知道我在哪里做错了。

Thank you谢谢

Your syntax is correct.你的语法是正确的。 You get timeout since its running the process in the background and times out before completing the task .由于它在后台运行进程并在完成任务之前超时,因此您会超时。

You can run the query asynchronously by specifying wait_for_completion=false您可以通过指定wait_for_completion=false异步运行查询

POST test_index/_update_by_query?conflicts=proceed&wait_for_completion=false
{
      "script" : "ctx._source.A.remove('B')", 
       "query" : {
        "exists": { "field": "A.B" }
      }
} 

Above will give a response with taskId上面将给出一个带有 taskId 的响应

{
  "task" : "{taskId:node}"
}

Now you can use task api to get the status of the task using the value from above现在您可以使用任务 api 使用上面的值获取任务的状态

GET _tasks/{taskId:node}

Alternatively, if you dont specify wait_for_completion=false and get a time out, you can still get all tasks by actions like below.或者,如果您不指定wait_for_completion=false并获得超时,您仍然可以通过以下操作获取所有任务。 But, i'd recommend do the first one.但是,我建议做第一个。

GET _tasks?actions=*byquery&detailed.

From comments: Now say if I have 100 index having a similar name pattern for example an index having a name "test_date - MM/DD/YYYY" here the prefix is same "test"来自评论:现在说如果我有 100 个具有相似名称模式的索引,例如一个名称为“test_date - MM/DD/YYYY”的索引,这里的前缀是相同的“test”

In order to handle multiple indices, you can use the wild card syntax and replace index name with prefix and *为了处理多个索引,您可以使用通配符语法并将索引名称替换为前缀和 *

For example, below query will run on all indices that start with test :例如,下面的查询将在所有以test开头的索引上运行:

POST test*/_update_by_query?conflicts=proceed&wait_for_completion=false
{
      "script" : "ctx._source.A.remove('B')", 
       "query" : {
        "exists": { "field": "A.B" }
      }
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM