1.elasticsearch-Rest风格说明
method | url地址 | 描述 |
---|---|---|
PUT | localhost:9200/索引名称/类型名称/文档id | 创建文档(指定文档id) |
POST | localhost:9200/索引名称/类型名称 | 创建文档(随机文档id) |
POST | localhost:9200/索引名称/类型名称/文档id/_update | 修改文档 |
DELETE | localhost:9200/索引名称/类型名称/文档id | 删除文档 |
GET | localhost:9200/索引名称/类型名称/文档id | 查询文档(通过文档id) |
POST | localhost: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":{}
}
}
}