kibana调用elasticsearch案例大全

1.elasticsearch-Rest风格说明

method

url地址

描述

PUTlocalhost:9200/索引名称/类型名称/文档id创建文档(指定文档id)
POSTlocalhost:9200/索引名称/类型名称创建文档(随机文档id)
POSTlocalhost:9200/索引名称/类型名称/文档id/_update修改文档
DELETElocalhost:9200/索引名称/类型名称/文档id删除文档
GETlocalhost:9200/索引名称/类型名称/文档id查询文档(通过文档id)
POSTlocalhost:9200/索引名称/类型名称/_search查询所有数据

2.字段类型,elasticsearch是关系型数据库,需要指定数据类型

字符串类型text 、 keyword
数值类型long, integer, short, byte, double, flfloat, half_flfloat, scaled_flfloat
日期类型date
{请求体}te布尔值类型boolean
二进制类型binary
等等......

3.调用案例

#分析器使用
GET _analyze
{
  "analyzer": "ik_smart",
  "text" : "中间件集成组"
}

GET _analyze
{
  "analyzer": "ik_max_word",
  "text" : "中间件集成组"
}

#创建一个索引
PUT /test/type1/1
{
  "name":"zcx",
  "age":18
}

#指定的字段类型
PUT /test2
{
  "mappings": {
    "properties": {
      "name":{
        "type": "text"
      },
      "age":{
        "type": "integer"
      },
      "birthday":{
        "type":"date"
      }
    }
  }
}

#获取具体的信息 
GET test2

#不设置类型的情况下查看默认类型
PUT /test3/_doc/1
{
  "name":"赵春雪",
  "age":18,
  "birthday":"1992-07-12"
}
#不指定类型的情况下,es会给我们设置默认的类型
GET /test3


#通过下边的命令,获取es的更多索引信息
GET _cat/indices?v

#覆盖方式第一种
PUT /test3/_doc/1
{
  "name":"赵春雪",
  "age":"19",
  "birthday":"1992-07-12",
  "desc":["外向","内向","神经"]
}

#覆盖方式第二种,推荐使用第二种,灵活性更好
POST /test3/_doc/1/_update
{
  "doc":{
    "name":"徐春军"
  }
}

PUT /test3/_doc/2
{
  "name":"徐春军大神",
  "age":28,
  "birthday":"1992-07-12"
}
#查询
GET /test3/_doc/_search?q=name:徐春军

###json查询 复杂操作搜索 排序 分页 高亮 模糊查询 精准查询 

#_source过滤字段
GET /test3/_doc/_search
{
  "query":{
    "match":{
      "name":"徐春军"
    }
  },
  "_source":["name","age"]
}

#sort'根据字段进行排序'
GET /test3/_doc/_search
{
  "query":{
    "match":{
      "name":"徐春军"
    }
  },
  "sort":{
    "age":{
      "order":"asc"
    }
  }
}

#分页查询
GET /test3/_doc/_search
{
  "query":{
    "match":{
      "name":"徐春军"
    }
  },
  "from":0,
  "size":2
}

#布尔值查询 
#must (and),所有的条件都要符合 where id = 1 and name = xxx
GET /test3/_doc/_search
{
  "query":{
    "bool":{
      "must":[{
        "match":{
          "name":"徐春军"
        }
      },
      {
        "match":{
          "age":19
        }
      }]
    }
  }
}

#should(or),所有的条件都要符合 where id = 1 or name = xxx
GET /test3/_doc/_search
{
  "query":{
    "bool":{
      "should":[{
        "match":{
          "name":"徐春军"
        }
      },
      {
        "match":{
          "age":19
        }
      }
      ]
    }
  }
}

#must_not (not)
GET /test3/_doc/_search
{
  "query":{
    "bool":{
      "must_not":{
        "match":{
          "age":19
        }
      }
    }
  }
}


#过滤器 filter  
#gt 大于
#gte 大于等于
#lt 小于
#lte 小于等于!
GET /test3/_doc/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": "徐春军"
          }
        }
      ],
      "filter": {
        "range": {
          "age": {
            "lt": 22
          }
        }
      }
    }
  }
}

#匹配多个条件!
GET /test3/_doc/_search
{
  "query":{
    "match":{
      "desc":"内向 外向"
    }
  }
}

#精确查询! term 查询是直接通过倒排索引指定的词条进程精确查找的
#关于分词
#term ,直接查询精确的
#match,会使用分词器解析!(先分析文档,然后在通过分析的文档进行查询!)
#两个类型 text keyword 不被拆分 standard 被拆分
GET _analyze
{
  "analyzer": "keyword",
  "text":"中间件集成组"
}
GET _analyze
{
  "analyzer": "standard",
  "text":"中间件集成组"
}


PUT testdb/_doc/4
{
  "t1":"22",
  "t2":"2020-06-16"
}
PUT testdb/_doc/3
{
  "t1":"33",
  "t2":"2020-06-15"
}

#多个值匹配精确查询
GET testdb/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "term": {
            "t1": "22"
          }
        },
        {
          "term": {
            "t1": "33"
          }
        }
      ]
    }
  }
}

#高亮查询!
GET /test3/_doc/_search
{
  "query":{
    "match":{
      "name":"徐春军"
    }
  },
  "highlight":{
    "fields":{
      "name":{}
    }
  }
}

#自定义搜索高亮条件
GET /test3/_doc/_search
{
  "query":{
    "match":{
      "name":"徐春军"
    }
  },
  "highlight":{
    "pre_tags":"<p class='key' style='color:red'>",
    "post_tags":"</p>",
    "fields":{
      "name":{}
    }
  }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值