1

输入图片说明

一、索引的增删改查
rest 一定要大写哦哈哈
增加索引 : customer 索引名,_doc  type类型 ,1 文档id 大括号中的为 请求体(及数据)
1、创建索引
PUT /索引名/类型名(会删除)/文档id  
{
    请求体
}
说明:PUT手动指定索引
  
例:创建索引
PUT /customer/_doc/1
{
  "name": "John Doe"
}
2、查询索引
GET /customer/_doc/1
3、删除索引
DELETE /customer

4、修改
PUT /customer/_doc/1 {
    "name": "大帅逼"
}
说明:这种方式会将version版本号增加,但是添加时需要与前面文档中数据数目一致不然会丢失数据

POST  /test20210304/_doc/1/_update
{
    "doc":{
      "name": "大帅逼"
    }
}
二、索引的属性修改
创建索引规则,就像设置数据库的一些参数一样,
PUT /test1
{
  "mappings": {
    "properties": {
      "name": {
        "type":"text"
      }
    }
  }
}

说明:设置文档中对应词组的类型

GET /test1  查看索引的信息
说明:属性的设置大部分中途不支持修改,副分片支持修改~~~~

1、设置索引分片数:默认主分片5,副分片1

PUT /my_temp_index
{
    "settings": {
        "number_of_shards" :   1,  主分片
        "number_of_replicas" : 1    副分片
    }
}

#支持动态修改副分片
PUT /my_temp_index/_settings
{
    "number_of_replicas": 2
}

2、设置分词器
PUT /my_temp_index
{
    "settings": {
        "analysis": {
            "analyzer": {
                "es_std": {
                    "type":      "standard",
                    "stopwords": "_spanish_"
                }
            }
        }
    }
}

#设置自定义分词器

3、设置类型属性

PUT /my_temp_index1
{
  "mappings": {
    "properties": {
      "name": {
        "type":"text",
         "analyzer": "ik_max_word",
         "search_analyzer": "ik_smart"
      },
      "age": {
        "type": "long"
        , "index": false
      },
      "like":{
        "type":"text",
        "analyzer": "ik_max_word",
        "search_analyzer": "ik_smart"
        , "index": true
      },
      "desc":{
        "type":"text",
        "analyzer": "ik_max_word",
         "search_analyzer": "ik_smart"
         , "index": true
      }
    }
  }
}




GET  /test20210304

GET  /test20210304/_doc/1

DELETE /test20210304

4、移除掉:不将字段存储到_source 即磁盘,使用查询时不能看见_source中的数据

PUT /test20210304 
{
  "mappings": {
      "_source": {
                "enabled":  false
            }
    }
  
}

5、动态映射:动态匹配fields类型如果符合就使用模板中的配置 ,定义一个模板符合条件的fields使用该模板的配置
# "dynamic": "strict" 废弃
# "date_detection": false 废弃


PUT /test20210304 
{
  "mappings": {
       "dynamic_templates": [  
                {  
                    "all_field": {
                        "match_mapping_type": "string",
                        "mapping": {  
                            "index": false,   
                            "store": true,   
                            "type": "text",   
                            "include_in_all": false  
                        },   
                        "match": "*"  
                    }  
                }  
            ]
    }
  
}

6、索引别名,零停机:将原来 A的索引 a变为 B的索引a 同时移除掉被索引的A

#创建别名
PUT /test20210304/_alias/my_index

PUT /test20210409/_alias/my_index

GET /my_index
GET /test20210304/_doc/1
GET /test20210409/_doc/1

#查看my_index别名对应的索引
GET /*/_alias/my_index
#查看test20210304索引的别名
GET /test20210304/_alias/*

#零停机:
POST /_aliases
{
   "actions": [
        { "remove": { "index": "test20210304", "alias": "my_index" }},
        { "add":    { "index": "test20210409", "alias": "my_index" }}
    ]
}

#为了保证数据属性的一致性,创建与test20210304一致的属性,并且将 查询出的数据存入到这里面。因为es不支持索引的修改。
PUT /test20210409
{
  "mappings" : {
      "dynamic_templates" : [
        {
          "all_field" : {
            "match" : "*",
            "match_mapping_type" : "string",
            "mapping" : {
              "include_in_all" : false,
              "index" : false,
              "store" : true,
              "type" : "text"
            }
          }
        }
      ],
      "properties" : {
        "age" : {
          "type" : "text",
          "index" : false,
          "store" : true
        },
        "desc" : {
          "type" : "text",
          "index" : false,
          "store" : true
        },
        "name" : {
          "type" : "text",
          "index" : false,
          "store" : true
        }
      }
    }
}


PUT /test20210409/_doc/1
{
    "name":"文烨",
    "age":"23",
    "desc":"帅"
}
三、查看elasticsearch状态
GET _cat/   获取es的信息状态

四、文档的操作

1、基本操作
1)添加数据

PUT /testbasedoc/_doc/1
{
  "name":"大帅比大水笔",
  "age":"23",
  "desc":"喜之郎果冻,盼盼我爱你",
  "likes":["java 爪哇","可爱","刘月半"]
  
}
PUT /testbasedoc/_doc/2
{
  "name":"鸡蛋",
  "age":"23",
  "desc":"喜之冻,牙医媒体",
  "likes":["电影ae,ar","胖胖","倩妹子"]
}
PUT /testbasedoc/_doc/3
{
  "name":"鸭蛋",
  "age":"23",
  "desc":"兴趣使然的理发师",
  "likes":["打游戏","剪头发","妹子"]
} 

2)查询
GET testbasedoc/_doc/1

3)更新

POST  /test20210304/_doc/1/_update
{
    "doc":{
      "name": "大帅比2"
    }
}

4)删除文档
DELETE /tehero_index/_doc/1

5)批量操作文档
a、查询多个_mget

POST /_mget
{
   "docs":[
      {
         "_index": "testbasedoc", "_id": "1"
      },
      {
         "_index":"testbasedoc", "_id": "2"
      }
   ]
}

b、批量执行多个操作

POST _bulk
## 替换指定文档
{ "index" : { "_index" : "testbasedoc", "_type" : "_doc", "_id" : "1" } }
{ "this_is_field1" : "this_is_index_value" }
##删除指定文档
{ "delete" : { "_index" : "testbasedoc", "_type" : "_doc", "_id" : "1" } }
##创建文档
{ "create" : { "_index" : "testbasedoc", "_type" : "_doc", "_id" : "1" } }
{   "name":"大帅比大水笔","age":"23","desc":"喜之郎果冻,盼盼我爱你","likes":["java 爪哇","可爱","刘月半"]}
##更新文档
{ "update" : {"_id" : "1", "_type" : "_doc", "_index" : "testbasedoc"} }
{ "doc" : {"age" : "24"} }

在7.X中也可以去掉"_type" : "_doc" 

2、复杂操作 (排序,分页,高亮,模糊查询,精准查询)

1)简单搜索_search?q=name:大帅比java

a、GET testbasedoc/_doc/_search?q=name:大帅比jav
b、GET /testbasedoc,customer/_search?q=name:"wenye" 多索引查询统一条件

GET testbasedoc/_doc/_search
{
  "query": {
    "match": {
      "name":"大帅比"
    }
  }
}

查询结果
输入图片说明

2)聚合查询 metric,bucket

metric:avf 、min、max、cardinality、sum、stats等
Bucket:想当与group by
1)平均值
POST  /testbasedoc/_search
{
   "aggs":{
      "avg_age":{"avg":{"field":"age"}}
   }
}

2)计数

POST /schools*/_search
{
   "aggs":{
      "distinct_name_count":{"cardinality":{"field":"name"}}
   }
}

3)统计

POST  /schools/_search
{
   "aggs" : {
      "fees_stats" : { "stats" : { "field" : "fees" } }
   }
}

4) 最大聚合

POST /schools*/_search
{
   "aggs" : {
      "max_fees" : { "max" : { "field" : "fees" } }
   }
}

5) 最小聚合

POST /schools*/_search
{
   "aggs" : {
      "min_fees" : { "min" : { "field" : "fees" } }
   }
}

6)特定字段的总和

POST /schools*/_search
{
   "aggs" : {
      "total_fees" : { "sum" : { "field" : "fees" } }
   }
}

7)terms 桶 精确查询 根据倒排索引进行精确索引


GET /testbasedoc1/_search
{
  "aggs": {                    # 使用聚合的必须带如设置_mapping 必须以properties开头
    "boyfirl_term": {
      "terms": {
        "field": "age"
      },
      "aggs" :{
        "avg_age":{
          "avg":{"field":"age"}
        }
      }
    }
  }
}

3、搜索相关


#搜索
#简单搜索 match_all
POST /testbasedoc1/_search
{
  "query": {
    "match_all": {}
  }
, "_source": ["name","age"] # 指定显示的字段
}

#分页查询
POST /testbasedoc1/_search
{
  "from":0,     #哪里开始
  "size": 2,   #查询出的总个数
  "query": {
    "match_all": {}
  }
}

#排序
POST /testbasedoc1/_search
{
  "sort":[{"age":"asc"}],
  "from":0,     
  "size": 20,   
  "query": {
    "match_all": {}
  }
}

#match 匹配查询
POST /testbasedoc1/_search
{
  "query": {
    "match": {
      "age": 23
    }
  }
}

#(multi_mathc)多种匹配查询

POST /testbasedoc1/_search
{
  "query": {
    "multi_match": {
      "query": "23",  #value
      "fields": ["name","age"] #column 数组
    }
  }
}

#布尔查询
POST /testbasedoc1/_search
{
  "query": { 
   "bool":{     #返回的会是boolean值
     "must":[   #must 必须相等 must_not 不相等 should (or)
        { 
          "match":{
            "name":"大帅比"
          }
        },
       {
         "match":
         {
           "age":"23"
          }
       }
     ]
   }
  }
}

#过滤
POST /testbasedoc1/_search
{
  "query": {
     "bool":{
       "must":[  
        { 
          "match":{
            "name":"鸭蛋"
          }
        },
       {
         "match":
         {
           "age":"24"
          }
       }
     ],
     "filter": [  #过滤
       {
         "range": {
           "FIELD": {
             "gte": 10,
             "lte": 20
           }
         }
       }
     ]
   }
  }
}
#query :查询包含str的文档

POST /testbasedoc1/_search
{
   "query":{
      "query_string":{
         "query":"爱"
      }
   }
}

#短语搜索 - Match Phrase  搞不清
POST testbasedoc1/_search
{
  "query": {
    "match_phrase": {
      "desc":{
        "query": "兴趣使,然理发师"
        , "slop": 1
      }
    }
  }
}
#term 查询 主要用于数字日期相关
POST /testbasedoc/_search
{
   "query":{
      "term":{"age":"23"}
   }
}

#range 查询范围查询

POST /testbasedoc*/_search
{
   "query":{
      "range":{
         "age":{
            "gte":3.5   #gte:大于等于  gt:大于 lte:小于等于  lt:小于
         }
      }
   }
}

4、关于分词

  • term : 精确查询,不会对词组进行解析
  • match: 先进行文档解析,然后进行匹配
  • 类型为keyword的不会被分词器解析

5、高亮显示

默认:
POST /testbasedoc/_search
{
   "query":{
      "match":{"name":"大帅比"}
   },
   "highlight": {
     "fields": {
       "name": {}
     }
   }
}

自定义 高亮条件 pre_tags post_tages:
POST /testbasedoc/_search
{
   "query":{
      "match":{"name":"大帅比"}
   },
   "highlight": {
     "pre_tags":"<p class='key' style='color:red'>",
     "post_tags":"</p>",
    "fields": {
       "name": {}
     }
   }
}

匹配的结果
"hits" : [
      {
        "_index" : "testbasedoc",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 2.0487494,
        "_source" : {
          "name" : "大帅比大水笔",
          "age" : 23,
          "desc" : "喜之郎果冻,盼盼我爱你",
          "likes" : [
            "java 爪哇",
            "可爱",
            "刘月半"
          ]
        },
        "highlight" : {
          "name" : [
            "<em>文</em><em>烨</em>大水笔"   高亮
          ]

文nKkCJ
1 声望1 粉丝