【弄nèng - Elasticsearch】DSL入门篇(十一)—— Script 脚本查询

脚本查询,语法跟版本有关,我是用的es是5.2.2

1. Script Query 比较大小过滤

POST  schools/classes/_search
{
    "query": {
        "bool" : {
            "must" : {
                "script" : {
                    "script" : {
                        "inline": "doc['price'].value > 7",
                        "lang": "painless"
                     }
                }
            }
        }
    }
}

过滤价格大于7的文档

效果

{
  "took": 18,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 1,
    "hits": [
      {
        "_index": "schools",
        "_type": "classes",
        "_id": "5",
        "_score": 1,
        "_source": {
          "classes_id": 5,
          "name": "班级5",
          "address": "7-深圳市福田区福华路1",
          "price": 7.4,
          "timestamp": 1572573660000
        }
      },
      {
        "_index": "schools",
        "_type": "classes",
        "_id": "11",
        "_score": 1,
        "_source": {
          "classes_id": 11,
          "name": "班级11",
          "address": "深圳市福田区福华路11",
          "price": 11.4,
          "timestamp": 1572573660000,
          "users": [
            {
              "name": "SIMA",
              "age": "11",
              "price": 13.4
            },
            {
              "name": "LIYAO",
              "age": "13",
              "price": 13.2
            }
          ]
        }
      }
    ]
  }
}

使用参数

POST  schools/classes/_search
{
    "query": {
        "bool" : {
            "must" : {
                "script" : {
                    "script" : {
                        "inline" : "doc['price'].value > params.param1",
                        "lang"   : "painless",
                        "params" : {
                            "param1" : 7
                        }
                    }
                }
            }
        }
    }
}

效果跟上面一样

2. Script Query 数据处理

查询数据,简单处理后返回,如下地址+ “hello”

POST  schools/classes/_search
{
  "query":{
    "match_all": {}
  }, 
  "script_fields": {
    "test1": {
      "script": "doc['address'].value +'hello'"
    }
  }
}

效果

[
      {
        "_index": "schools",
        "_type": "classes",
        "_id": "4",
        "_score": 1,
        "fields": {
          "test1": [
            "6-深圳市福田区福华路1hello"
          ]
        }
      },
      {
        "_index": "schools",
        "_type": "classes",
        "_id": "5",
        "_score": 1,
        "fields": {
          "test1": [
            "7-深圳市福田区福华路1hello"
          ]
        }
      }
]

项目推荐

IT-CLOUD :IT服务管理平台,集成基础服务,中间件服务,监控告警服务等。
IT-CLOUD-ACTIVITI6 :Activiti教程源码。博文在本CSDN Activiti系列中。
IT-CLOUD-ELASTICSEARCH :elasticsearch教程源码。博文在本CSDN elasticsearch系列中。

开源项目,持续更新中,喜欢请 Star~

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值