elasticsearch-script-painless-实践

Painless实践

以下案例来源工作中实际需求,或者自己想到例子。

案例一

需求描述:搜索数据时返回一个新增的常量字段

GET hockey/_search
{
  "_source": true, 
  "script_fields": {
    "area": {
      "script": {
        "lang": "painless",
        "source": """
        return "CN"
"""
      }
    }
  }
}

搜索结果

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 12,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "hockey",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "first" : "sean",
          "last" : "monohan",
          "goals" : [
            7,
            54,
            26
          ],
          "assists" : [
            11,
            26,
            13
          ],
          "gp" : [
            26,
            82,
            82
          ],
          "born" : "1994/10/12"
        },
        "fields" : {
          "area" : [
            "CN"
          ]
        }
      },
      {
        "_index" : "hockey",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "first" : "jiri",
          "last" : "hudler",
          "goals" : [
            5,
            34,
            36
          ],
          "assists" : [
            11,
            62,
            42
          ],
          "gp" : [
            24,
            80,
            79
          ],
          "born" : "1984/01/04"
        },
        "fields" : {
          "area" : [
            "CN"
          ]
        }
      }
    ]
  }
}

案例二

需求描述:抽样多个地区数据,查询某个地区数据,要求某些字段(数组)元素个数不超过固定数量,返回指定字段、至少返回多少个字段、必须有哪几个字段及返回数据条数

GET vip/_search
{
  "_source": ["gender","birth","phone","email","address","names","friends"], 
  "query": {
    "bool": {
      "should": [
        {
          "exists": {
            "field": "gender"
          }
        },
        {
          "exists": {
            "field": "birth"
          }
        },
        {
          "exists": {
            "field": "phone"
          }
        },
        {
          "exists": {
            "field": "email"
          }
        },
        {
          "exists": {
            "field": "friends"
          }
        }
      ],
      "minimum_should_match": 3,
      "must": [
        {
          "match": {
            "address": "北京"
          }
        },
        {
          "exists": {
            "field": "names"
          }
        },
        {
          "exists": {
            "field": "address"
          }
        },{
          "script": {
            "script": "doc['names.raw'].length <= 10"
          }
        },{
          "script": {
            "script": "doc.friends.size() <= 10"
          }
        }
      ]
    }
  },
  "size": 10
}

案例三

需求描述:把案例二查询出的数据reindex到另一个索引,并新增一个字段地区字段该值就是案例二中查询的地区参数值。

POST _reindex
{
  "size": 3,
  "source": {
    "index": "vips",
    "_source": [
      "gender",
      "birth",
      "phone",
      "email",
      "address",
      "names",
      "friends"
    ],
    "query": {
      "bool": {
      "should": [
        {
          "exists": {
            "field": "gender"
          }
        },
        {
          "exists": {
            "field": "birth"
          }
        },
        {
          "exists": {
            "field": "phone"
          }
        },
        {
          "exists": {
            "field": "email"
          }
        },
        {
          "exists": {
            "field": "friends"
          }
        }
      ],
      "minimum_should_match": 3,
      "must": [
        {
          "match": {
            "address": "北京"
          }
        },
        {
          "exists": {
            "field": "names"
          }
        },
        {
          "exists": {
            "field": "address"
          }
        },{
          "script": {
            "script": "doc['names.raw'].length <= 10"
          }
        },{
          "script": {
            "script": "doc.friends.size() <= 10"
          }
        }
      ]
    }
  },
  "dest": {
    "index": "reindex_test"
    , "op_type": "create"
  }
}

案例四

需求描述:根据条件查询出的数据新增字段

POST index_reindex/_update_by_query
{
  "query": {
    "match_phrase": {
      "address": "北京"
    }
  },
  "script": {
    "source": "ctx._source['area'] = \"北京\""
  }
}

案例五

需求描述:删除字段

posted on 2021-03-15 23:05  0x153_小波  阅读(191)  评论(0编辑  收藏  举报