ElasticSearch常用DSL命令

1、查询索引字段的映射关系

GET /索引/_mapping

2、查询索引的分词器等设置

GET /索引/_settings

3、根据指定分词器分词

GET /索引/_analyze
{
  "analyzer" : "standard",
  "text" : "层系"
}

4、term查询

POST /索引/_search
{
  "query": {
    "term": {
      "dbId": "101545"
    }
  }
}

5、match查询 

POST /tb/_search
{
  "query": {
    "match": {
      "dbId": "19841159f25845008fad2512aa91f48b"
    }
  }
}

 6、bool查询

        6.1 must组合
GET /tb/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "terms": {
            "dbId": [
              287994
            ]
          }
        },
        {
          "match": {
            "original": "层"
          }
        }
      ]
    }
  }
}

must类似于mysql里面的and命令,上述命令相当于MySQL里面的 where dbId in(287994) and original like '%层%'。注意此处把match命令当成了mysql的like命令,其实这两个命令还是有明显区别的。

        6.2must与should组合
POST /tb/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "dbId": {
              "value": "19841159f25845008fad2512aa91f48b"
            }
          }
        }
      ],
      "should": [
        {
          "match": {
            "original": "边水"
          }
        }
      ],
      "minimum_should_match": 1
    }
  }
}

上述命令类似于MySQL命令:where dbId = '19841159f25845008fad2512aa91f48b' and original like '%边水%'。如果上述命令不加上minimum_should_match: 1,则类似于MySQL命令:where dbId = '19841159f25845008fad2512aa91f48b'。

7、自定义脚本过滤

GET /tb/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "terms": {
            "dbId": [
              "19841159f25845008fad2512aa91f48b"
            ]
          }
        },
        {
            "match_phrase": {
              "original.standard": {
                "query": "层系"
              }
            }
          }
      ],
      "filter": [
        {
          "script": {
            "script": {
              "lang": "painless",
              "source": "String str = params.val;String strLower = params.valLower;String val = doc['original.keyword'].value;return str.contains(val) || strLower.contains(val.toLowerCase());",
              "params": {
                "val": "侏罗层系主力区块年注采比变化",
                "valLower": "侏罗层系主力区块年注采比变化"
              }
            }
          }
        }
      ]
    }
  }
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值