Home > Mobile >  How remove one element from list type field from ElasticSearch
How remove one element from list type field from ElasticSearch

Time:01-21

How to remove one element from the list type field from ElasticSearch.

There are some of my data and I want to remove some elements from a list type field.

// The data of user list with book field
[
    {
        "userId": "1",
        "books": [
            {
                "id": "1",
                "name": "book1"
            },
            {
                "id": "2",
                "name": "book2"
            }
        ]
    },
    {
        "userId": "2",
        "books": [
            {
                "id": "2",
                "name": "book2"
            },
            {
                "id": "3",
                "name": "book3"
            }
        ]
    },
    {
        "userId": "13",
        "books": [
            {
                "id": "2",
                "name": "book2"
            },
            {
                "id": "5",
                "name": "book5"
            }
        ]
    }
]

What I want to do:

find the users whose userId in [1, 2, 3], and delete the book with id 2 from their books list.

The result expected:

[
    {
        "userId": "1",
        "books": [
            {
                "id": "1",
                "name": "book1"
            }
        ]
    },
    {
        "userId": "2",
        "books": [
            {
                "id": "3",
                "name": "book3"
            }
        ]
    },
    {
        "userId": "13",
        "books": [
            {
                "id": "2",
                "name": "book2"
            },
            {
                "id": "5",
                "name": "book5"
            }
        ]
    }
]

I'm newer of ElasticSearch, it's a very difficult problem for me. It will be better if you can provide a request command of the Curl command.

CodePudding user response:

You can use below update_by_query query with painless script.

POST book/_update_by_query
    {
      "query": {
       "match_all": {}
      },
      "script": {
        "source": """
        for (int i = 0; i < ctx._source.booklist.length;   i)
        {
          if(params.userid.contains(ctx._source.booklist[i]['userId']))
          {
            for (int j = 0; j < ctx._source.booklist[i]['books'].length;   j)
            {
              if(ctx._source.booklist[i]['books'][j]["id"]=="2"){
                ctx._source.booklist[i]['books'].remove(j);
              }
            }
          }
        }""",
        "lang": "painless",
        "params": {
          "userid":["1","2","3"]
        }
      }
    }

Below is document before executing above query:

{
        "_index" : "book",
        "_type" : "_doc",
        "_id" : "aA1Vd34BATX2P1U_9ugg",
        "_score" : 1.0,
        "_source" : {
          "booklist" : [
            {
              "userId" : "1",
              "books" : [
                {
                  "id" : "1",
                  "name" : "book1"
                },
                {
                  "id" : "2",
                  "name" : "book2"
                }
              ]
            },
            {
              "userId" : "2",
              "books" : [
                {
                  "id" : "2",
                  "name" : "book2"
                },
                {
                  "id" : "3",
                  "name" : "book3"
                }
              ]
            },
            {
              "userId" : "13",
              "books" : [
                {
                  "id" : "2",
                  "name" : "book2"
                },
                {
                  "id" : "5",
                  "name" : "book5"
                }
              ]
            }
          ]
        }
      }

Below is document after executing above query:

{
        "_index" : "book",
        "_type" : "_doc",
        "_id" : "aA1Vd34BATX2P1U_9ugg",
        "_score" : 1.0,
        "_source" : {
          "booklist" : [
            {
              "books" : [
                {
                  "name" : "book1",
                  "id" : "1"
                }
              ],
              "userId" : "1"
            },
            {
              "books" : [
                {
                  "name" : "book3",
                  "id" : "3"
                }
              ],
              "userId" : "2"
            },
            {
              "books" : [
                {
                  "name" : "book2",
                  "id" : "2"
                },
                {
                  "name" : "book5",
                  "id" : "5"
                }
              ],
              "userId" : "13"
            }
          ]
        }
      }
  •  Tags:  
  • Related