美文网首页
ElasticSearch语法

ElasticSearch语法

作者: itkkanae | 来源:发表于2019-11-15 16:51 被阅读0次

一、索引操作

  • 建立名为“demo”的索引,为分片数与副本数配置默认值
PUT /demo
  • 删除“demo”索引
DELETE /demo
  • 建立“demo”索引同时,设置分片数(number_of_shards)为1,副本数(number_of_replicas)为0
PUT /demo
{
  "settings": {
    "number_of_shards" : 1,
    "number_of_replicas" : 0
  }
}
  • 修改“demo”副本数(number_of_replicas)
PUT /demo/_settings
{
  "settings": 
    {
      "number_of_replicas":1
    }
}
  • 查看“demo”索引设置
GET /demo/_settings
  • 查看全部索引设置
GET /_all/_settings
  • 建立“demo”索引同时,设置索引结构(mapping)
    "demo"索引中存放类型“type-a”,“type-a”类型中包含两个字段分别是,数据类型为“text”、分词器为“ik_smart”的字段“info-a”,数据类型为“integer”的字段“info-b”
PUT /demo
{
  "mappings": {
    "type-a":{
      "properties":{
        "info-a":{
          "type":"text",
          "analyzer":"ik_smart"
        },
        "info-b":{
          "type":"integer"
        }
      }
    }
  }
}
  • 查看“demo”结构
GET /demo/_mapping

二、类型操作

  • 向“demo”索引中插入一条“type-a”,指定id为1
PUT /demo/type-a/1
{
  "info-a":"a",
  "info-b":1
}
  • 使用POST,向“demo”索引中插入一条“type-a”,自动生成id
POST /demo/type-a
{
  "info-a":"a",
  "info-b":1
}
  • 删除“demo”索引中id为1的“type-a”
DELETE /demo/type-a/1
  • 修改id是1的“info-a”字段为“b”
POST /demo/type-a/1/_update
{
  "doc": {
    "info-a":"b"
  }
}

三、查询语句

  • 查询“demo”索引中id为1的“type-a”
GET /demo/type-a/1
  • 查询“demo”索引中全部的“type-a”
GET /demo/type-a/_search
  • 模糊查询“type-a”中“info-a”带有“a”的文本
GET /demo/type-a/_search
{
  "query": {
    "match": {
      "info-a":"a"
    }
  }
}
  • 模糊查询“type-a”中“info-a”带有“a”“b”分词的文本
    (match通过分词器将“a b”分为“a”、“b”再进行查询)
GET /demo/type-a/_search
{
  "query": {
    "match": {
      "info-a":"a b"
    }
  }
}
  • 模糊查询“type-a”中“info-a”带有“a”“b”分词的文本
GET /demo/type-a/_search
{
  "query": {
    "match_phrase": {
      "info-a":"a b"
    }
  }
}
  • 精确查询“type-a”中“info-a”带有“a b”分词的文本
    (事实上可能出现这种情况,文档的“info-a”字段值为“a b”,存在“a”、“b”分词,但“a b”分词却不存在,无法查到结果)
GET /demo/type-a/_search
{
  "query": {
    "term": {
      "info-a":"a b"
    }
  }
}
  • 精确查询“type-a”中“info-a”带有“a b”分词的文本
    (事实上可能出现这种情况,文档的“info-a”字段值为“a b”,存在“a”、“b”分词,但“a b”分词却不存在,无法查到结果)
GET /demo/type-a/_search
{
  "query": {
    "term": {
      "info-a":"a b"
    }
  }
}
  • 范围查询“type-a”中“info-b”大于等于1、小于9的文本
    (gt:大于、gte:大于等于、lt:小于、lte:小于等于)
GET /demo/type-a/_search
{
  "query": {
    "range": {
      "info-b":{
        "gte":1,
        "lt":9
      }
    }
  }
}

四、多条件查询、分页

  • 查询满足多个条件的文档,“must”内可编写“match”、“term”、“range”条件数组(下同)
GET /demo/type-a/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {...}
        },
        {
          "term": {...}
        },
        {
          "range": {...}
        }
      ]
    }
  }
}
  • 查询不满足多个条件的文档
GET /demo/type-a/_search
{
  "query": {
    "bool": {
      "must_not": [...]
    }
  }
}
  • 为满足多个条件的文档增加“score”,影响结果顺序,不会影响结果集
GET /demo/type-a/_search
{
  "query": {
    "bool": {
      "should": [...]
    }
  }
}
  • 过滤结果,满足“filter”条件的文档会被保留并返回
GET /demo/type-a/_search
{
  "query": {
    "bool": {
      "filter": [...]
    }
  }
}
  • 多条件混用
GET /demo/type-a/_search
{
  "query": {
    "bool": {
      "must": [...],
      "must_not": [...],
      "should": [...],
      "filter": [...]
    }
  }
}
  • 分页查询,直接添加“from”、“size”参数
GET /demo/type-a/_search
{
  "from": 0,
  "size": 10, 
  "query": {...}
}

相关文章

网友评论

      本文标题:ElasticSearch语法

      本文链接:https://www.haomeiwen.com/subject/uznsictx.html