ElasticSearch常用DSL语句学习

es操作版本6.5

创建索引

1、创建索引,等价于mysql创建一个库

PUT /student_test

2、创建表和映射mapping

POST /student_test/table/_mapping
{
    //指定需要创建的字段和类型
  "properties": {
    "name": {
      "type": "text",
      /指定分词器,这里是指定默认分词器
      "analyzer": "standard"
    },
    "age": {
      "type": "long"
    },
    "height": {
      "type": "float"
    },
    "isHealth": {
      "type": "boolean"
    },
    "remark": {
      "type": "keyword"
    }
  }
}

 3、查询字段类型

GET /student_test/_mapping

 增删改操作

插入数据

1、单条插入 //post请求 指定插入的索引的类型。/1 是指定es给的id 等同于insert into table value ('','','') 

POST /student_test/table/1
{
  "name":"李大",
  "age":50,
  "height":152.2,
  "isHealth":"true",
  "remark":"唐高祖"
}

2、多条插入 等同于  INSERT INTO testTable (xx,xx) VALUES ('xx','xx'),('xx','xx'),('xx','xx')          

POST /student_test/table/_bulk
//指定id
{"index":{"_id":"6"}}
{"name":"李豫","age":50,"height":170,"isHealth":"false","remark":"唐代宗"}
{"index":{"_id":"7"}}
{"name":"李适","age":40,"height":160,"isHealth":"false","remark":"唐德宗"}
{"index":{"_id":"8"}}
{"name":"李诵","age":30,"height":165,"isHealth":"false","remark":"唐顺宗"}

更新数据

1、指定更新的id 更新某些字段 doc必须要有 等同于 update from table set name = "测试"... where id = 1

POST /student_test/table/1/_update
{
  "doc": {
    "name": "测试"
  }
}

2、更新并新增一个新字段

POST /student_test/table/5/_update
{
  "doc": {
    "name": "111",
    "sex":1
  }
}

3、更新这条数据的全数据 如果不指定_update这个参数 es会把id=5这条数据全量更新成请求的json数据


POST /student_test/table/5
{
  "doc": {
    "name": "111",
    "sex":1
  }
}

4、根据id更新,如果没有就新增。原数据里没有tets字段,所以会直接新增这个字段(忽略我写了个错别字test)

PUT /student_test/table/5
{
  "name":"李亨",
  "age":60,
  "height":170,
  "isHealth":"true",
  "remark":"唐肃宗",
  "tets":"测试更新新增字段"
}

 删除

1、根据指定的id删除 等同于  delete from table where id = 1 

DELETE /student_test/table/5 

2、根据条件删除 等同于  delete from table where id in (select id from table where name like '%张%')                   

POST /student_test/table/_delete_by_query
{
   "query": {
      "match": {
         "name": "张"
      }
   }
}

查询操作

一般查询根据数据库字段来选择用哪种查询API的,如果查询字段是text类型,需要模糊查询可以用match,精准查询用term。

如果查询字段类型是keywrod类型,无法进行分词,模糊查询可以用wildcard或者query_string来查询,精准查询用term。

1、根据id查询 等同于 select * from table where id = 1

GET /student_test/table/1

2、多字段查询 精准查询 等同于 select * from table where height = 170

term参数用于精准查询

//查询height = 170的数据
GET /student_test/table/_search
{
  "query": {
    "term": {
      "height": {
        "value": 170
      }
    }
  }
}

3、精准查询 or查询 select * from table where age in ( 50,40)

GET /student_test/table/_search
{
  "query": {
    "terms": {
      "age": [
        "50",
        "40"
      ]
    }
  }
}

 4、精准查询 and关系查询 等同于 select name,remark,age,height1 from table where age = 50 and height = 170

_source字段,控制返回哪些字段

GET /student_test/table/_search
{
  "_source": ["name","remark","age","height1"], 
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "age": {
              "value": "50"
            }
          }
        },
        {
          "term": {
            "height": {
              "value": "170"
            }
          }
        }
      ]
    }
  }
}

 5、精准查询 范围查询 等同于 select * from table where age between 50 and 60

查询年龄范围在50-60之间的

GET /student_test/table/_search
{
  "query": {
    "range": {
      "age": {
        "gte": 50,
        "lte": 60
      }
    }
  }
}

 6、排序查询 支持多字段排序

GET /student_test/table/_search
{
  "query": {
    "match_all": {}
  }
  , "sort": [
    {
      "height": {
        "order": "asc"
      }
    },
    {
      "age": {
        "order": "asc"
      }
    }
  ]
}

 7、模糊查询 关键词高亮

GET /student_test/table/_search
{
  "query": {
    "match": {
      "name": "李"
    }
  },
  "highlight": {
    "pre_tags": "<b color='red'",
    "post_tags": "<b>",
    "fields": {
      "name":{}
    }
  }
}

被检索到的关键词会高亮

8、对keyword类型的字段 进行模糊查询

keyword类型字段不会分词,所以需要加 * 等同select * from table where remark like '唐%'

GET /student_test/table/_search
{
  "query": {
    "wildcard": {
      "remark": {
        "value": "唐*"
      }
    }
  }
}

 9、query_string也可以模糊查询keyword类型的字段 ,支持多字段模糊查询

GET /student_test/table/_search
{
  "query": {
    "query_string": {
      "default_field": "remark",
      "query": "唐*"
    }
  }
}

模糊查询remark和name里带李字的,是or的关系查询

GET /student_test/table/_search
{
  "query": {
    "query_string": {
      "query": "李*",
      "fields": ["remark","name"],
      "default_operator": "OR"
    }
  }
}

match_phrase匹配keyword字段。这个同上必须跟keywork一致才可以。

GET /student_test/table/_search
{
  "query": {
    "match_phrase": {
      "remark": "唐德宗"
    }
  }
}

match_phrase匹配text字段。match_phrase是分词的,text也是分词的。match_phrase的分词结果必须在text字段分词中都包含,而且顺序必须相同,而且必须都是连续的。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值