# Painlessの配列の使用方法について

**URL:** <https://discuss.elastic.co/t/painless/282804>\
**Category:** 日本語による質問・議論はこちら\
**Tags:** painless\
**Created:** [August 30, 2021, 11:50am UTC](https://discuss.elastic.co/t/painless/282804 "2021-08-30T11:50:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![t-nakata](https://avatars.discourse-cdn.com/v4/letter/t/8491ac/32.png) [@t-nakata](https://discuss.elastic.co/u/t-nakata)\
**Post date:** [August 30, 2021, 11:50am UTC](https://discuss.elastic.co/t/painless/282804/1 "2021-08-30T11:50:17Z")

</div>

painlessで以下のようなデータを扱っています。

```auto
{
    "_shards": {
        "total": 6,
        "failed": 0,
        "successful": 6,
        "skipped": 0
    },
    "hits": {
        "hits": [
            {
                "_index": "test-2021.07.20",
                "_type": "_doc",
                "_source": {
                    "response_status": "403",
                    "time_cw": "1623984183618",
                    "src_ip": "10.111.16.62",
                    "port": "443",
                },
                "_id": "H_XawnoBjxCrCBvt7vpr",
                "_score": 1
            },
            {
                "_index": "test-2021.07.20",
                "_type": "_doc",
                "_source": {
                    "response_status": "200",
                    "time_cw": "1623984201618",
                    "src_ip": "10.111.68.10",
                    "port": "443",
                },
                "_id": "IPXawnoBjxCrCBvt7vpr",
                "_score": 1
            },
            {
                "_index": "test-2021.07.20",
                "_type": "_doc",
                "_source": {
                    "response_status": "403",
                    "time_cw": "1623984183618",
                    "src_ip": "10.111.16.62",
                    "port": "443",
                },
                "_id": "JfXcwnoBjxCrCBvtX_p_",
                "_score": 1
            }
        ],
        "total": {
            "value": 3,
            "relation": "eq"
        },
        "max_score": 1
    },
    "took": 1,
    "timed_out": false
}

```

ドキュメント数分ループさせ、条件に一致するドキュメントのみ別の配列に入れたいです。

■実現したいこと  
(script例) response\_statusが403のドキュメントのみ、別の配列に入れる  
int score = 0;  
for (int i = 0; i \< ctx.results[0].hits.hits.length; i++) {  
if (ctx.results[0].hits.hits[i].\_source.response\_status == "403") {  
★ここで「ctx.results[0].hits.hits[i]」以下を別配列に入れたい  
}  
}

結果、以下のみを別配列に格納し、以降の処理で使用したいです。

```auto
{
    "hits": {
        "hits": [
            {
                "_index": "test-2021.07.20",
                "_type": "_doc",
                "_source": {
                    "response_status": "403",
                    "time_cw": "1623984183618",
                    "src_ip": "10.111.16.62",
                    "port": "443",
                },
                "_id": "H_XawnoBjxCrCBvt7vpr",
                "_score": 1
            },
            {
                "_index": "test-2021.07.20",
                "_type": "_doc",
                "_source": {
                    "response_status": "403",
                    "time_cw": "1623984183618",
                    "src_ip": "10.111.16.62",
                    "port": "443",
                },
                "_id": "JfXcwnoBjxCrCBvtX_p_",
                "_score": 1
            }
        ]
}

```

painlessでこのような実装は可能でしょうか

---

<div class="post-metadata">

**Author:** ![Koji\_Kawamura](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/koji_kawamura/32/93938_2.png) [@Koji\_Kawamura](https://discuss.elastic.co/u/Koji_Kawamura)\
**Post date:** [September 1, 2021, 2:20am UTC](https://discuss.elastic.co/t/painless/282804/2 "2021-09-01T02:20:50Z")

</div>

@t-nakata さん、

はい、 Painless で実装可能です。 Lambda 式を使うと簡単に書くことができます。  
Kibana の Painless Lab の Parameters にテスト用の JSON を貼り付け、 params で参照してから処理するサンプルコードを書いてみました。ご参考まで。

```auto
List hits = params['hits']['hits'];
List filtered = hits.stream().filter(hit -> hit['_source']['response_status'] == "403").collect(Collectors.toList());
return filtered;

```

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/d/6/d63a9c3797676b089ad3185b1760de30d8f58af3.png)

Output:

```auto
[{_index=test-2021.07.20, _type=_doc, _source={src_ip=10.111.16.62, response_status=403, port=443, time_cw=1623984183618}, _id=H_XawnoBjxCrCBvt7vpr, _score=1}, {_index=test-2021.07.20, _type=_doc, _source={src_ip=10.111.16.62, response_status=403, port=443, time_cw=1623984183618}, _id=JfXcwnoBjxCrCBvtX_p_, _score=1}]

```

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/f/b/fb347cc138b3bbcaee442de73ff8ad4e7c924b65.png)

---

<div class="post-metadata">

**Author:** ![t-nakata](https://avatars.discourse-cdn.com/v4/letter/t/8491ac/32.png) [@t-nakata](https://discuss.elastic.co/u/t-nakata)\
**Post date:** [September 10, 2021, 4:58am UTC](https://discuss.elastic.co/t/painless/282804/3 "2021-09-10T04:58:06Z")

</div>

ご回答ありがとうございます。  
同様のことをPainless Labで試してみたのですが、Outputで以下のエラーが発生してしまいます。理由をご教示頂けないでしょうか

{  
"root\_cause": [  
{  
"type": "x\_content\_parse\_exception",  
"reason": "[20:29] [script] failed to parse field [params]"  
}  
],  
"type": "x\_content\_parse\_exception",  
"reason": "[20:29] [painless\_execute\_request] failed to parse field [script]",  
"caused\_by": {  
"type": "x\_content\_parse\_exception",  
"reason": "[20:29] [script] failed to parse field [params]",  
"caused\_by": {  
"type": "json\_parse\_exception",  
"reason": "Unexpected character ('}' (code 125)): was expecting double-quote to start field name\n at [Source: (org.elasticsearch.common.bytes.AbstractBytesReference$MarkSupportingStreamInputWrapper); line: 21, column: 18]"  
}  
}  
}

Parameters

```auto
{
    "_shards": {
        "total": 6,
        "failed": 0,
        "successful": 6,
        "skipped": 0
    },
    "hits": {
        "hits": [
            {
                "_index": "test-2021.07.20",
                "_type": "_doc",
                "_source": {
                    "response_status": "403",
                    "time_cw": "1623984183618",
                    "src_ip": "10.111.16.62",
                    "port": "443",
                },
                "_id": "H_XawnoBjxCrCBvt7vpr",
                "_score": 1
            },
            {
                "_index": "test-2021.07.20",
                "_type": "_doc",
                "_source": {
                    "response_status": "200",
                    "time_cw": "1623984201618",
                    "src_ip": "10.111.68.10",
                    "port": "443",
                },
                "_id": "IPXawnoBjxCrCBvt7vpr",
                "_score": 1
            },
            {
                "_index": "test-2021.07.20",
                "_type": "_doc",
                "_source": {
                    "response_status": "403",
                    "time_cw": "1623984183618",
                    "src_ip": "10.111.16.62",
                    "port": "443",
                },
                "_id": "JfXcwnoBjxCrCBvtX_p_",
                "_score": 1
            }
        ],
        "total": {
            "value": 3,
            "relation": "eq"
        },
        "max_score": 1
    },
    "took": 1,
    "timed_out": false
}

```

ソース

```auto
List hits = params['hits']['hits'];

List filtered = hits.stream().filter(hit -> hit['_source']['response_status'] == "403").collect(Collectors.toList());

return filtered;

```

---

<div class="post-metadata">

**Author:** ![t-nakata](https://avatars.discourse-cdn.com/v4/letter/t/8491ac/32.png) [@t-nakata](https://discuss.elastic.co/u/t-nakata)\
**Post date:** [September 10, 2021, 8:51am UTC](https://discuss.elastic.co/t/painless/282804/4 "2021-09-10T08:51:16Z")

</div>

すみません、解決しました

---

<div class="post-metadata">

**Author:** ![t-nakata](https://avatars.discourse-cdn.com/v4/letter/t/8491ac/32.png) [@t-nakata](https://discuss.elastic.co/u/t-nakata)\
**Post date:** [September 13, 2021, 1:24am UTC](https://discuss.elastic.co/t/painless/282804/5 "2021-09-13T01:24:04Z")

</div>

度々申し訳ございません。ご教示頂いたソースについて追加で教えてください

`List filtered = hits.stream().filter(hit -> hit['_source']['response_status'] == "403").collect(Collectors.toList());`

比較対象の「403」を配列に格納し、比較したいです。  
例えば、別途、以下のような配列を定義し、  
`List states = [200,400,403];`

「403」部分を定数と比較するのでなく、states配列をループして200,400,403とそれぞれ比較したいです。(ループが二重になるイメージです)  
このような記載は出来るのでしょうか？

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [October 11, 2021, 1:24am UTC](https://discuss.elastic.co/t/painless/282804/6 "2021-10-11T01:24:57Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
