ElasticSearch - 脚本 - 循环 - 无痛

时间:2017-05-17 19:27:37

标签: elasticsearch groovy elasticsearch-painless

\我无法在Painless中的子文档上执行LOOP。 这只是为运行LOOP而创建的一个示例场景,场景是另一个(这种情况可能是运行过滤器,但问题是子文档中的循环)。

GET /_stats

GET /product/_mapping
GET /product/_settings

GET /product/table/_search
{"size":99}

创建

DELETE /product

PUT /product
{
"mappings":{
    "table":{
        "properties":{
            "name":{"type":"string","index":"not_analyzed"},
            "price":{"type":"float"},
            "subproduct":{
                "type":"nested",
                "properties":{
                    "category":{"type":"long"},
                    "subname":{"type":"string","index":"not_analyzed"},
                    "subprice":{"type":"float"}
                }
            }
        }
    }
}
}

DATAS

PUT /product/table/1
{"name":"Product 1",
    "price":123,
    "subproduct":[
        {"category":1,
            "subname":"SubProduct 1.1",
            "subprice":1.1},
        {"category":2,
            "subname":"SubProduct 1.2",
            "subprice":1.2},
        {"category":3,
            "subname":"SubProduct 1.2",
            "subprice":1.3}
    ]
}

PUT /product/table/2
{"name":"Product 2",
    "price":234,
    "subproduct":[
        {"category":1,
            "subname":"SubProduct 2.1",
            "subprice":2.1},
        {"category":2,
            "subname":"SubProduct 2.2",
            "subprice":2.2},
        {"category":3,
            "subname":"SubProduct 2.2",
            "subprice":2.3}
    ]
}

PUT /product/table/3
{"name":"Product 1",
    "price":345,
    "subproduct":[
        {"category":1,
            "subname":"SubProduct 3.1",
            "subprice":3.1},
        {"category":2,
            "subname":"SubProduct 3.2",
            "subprice":3.2},
        {"category":3,
            "subname":"SubProduct 3.2",
            "subprice":3.3}
    ]
}

QUERYs

GET /product/table/_search
{
    "_source":true,
    "size":9,
    "script_fields":{
        "sub":{
            "script":{
                "lang":"groovy",
                "inline":"price=0; for(s in _source.subproduct){if(s.category==2) price=s.subprice;}; return price;"
            }
        }
    }
}

GET /product/table/_search
{
    "_source":true,

    "size":9,
    "script_fields":{
        "sub":{
            "script":{
                "lang":"painless",
                "inline":"def price=0; for(s in _source.subproduct){if(s.category==2) price=s.subprice;} return price;"
            }
        }
    }
}

GET /product/table/_search
{
    "_source":true,
    "size":9,
    "script_fields":{
        "sub":{
            "script":{
                "lang":"painless",
                "inline":"def price=0; for(s in doc['subproduct']){if(s.category==2) price=s.subprice;} return price;"
            }
        }
    }
}

GET /product/table/_search
{
    "_source":true,
    "size":9,
    "script_fields":{
        "sub":{
            "script":{
                "lang":"painless",
                "inline":"def price=0; for (int i=0; i<doc['subproduct'].length; ++i){if(doc['subproduct'][i].category==2) price=doc['subproduct'][i].subprice;} return price;"
            }
        }
    }
}

0 个答案:

没有答案