美文网首页
es的增删改查

es的增删改查

作者: Fizz_YF | 来源:发表于2024-01-18 12:18 被阅读0次

    1、新增索引

    #请求
    put 192.168.1.139:9200/test11
    {
      "mappings": {
        "properties": {
          "title": {
              "type": "text"
            },
            "year": {
              "type": "date",
              "format": "yyyy"
            },
            "type": {
              "type": "integer"
            }
          }
        },
    "settings": {
        "index": {
          "number_of_shards": 5,
          "number_of_replicas": 1
        }
      }
    }
    
    # 返回
    {
        "acknowledged": true,
        "shards_acknowledged": true,
        "index": "test11"
    }
    

    2、新增文档

    #请求
    post 192.168.1.139:9200/test11/_doc/
    {
      "title": "标题",
      "year": "2024-01-18 12:00:34",
      "type": 1,
      "star": 5.2,
      "director": "啦啦啦"
    }
    #返回
    {
        "_index": "test11",
        "_id": "QlS7Go0BnqotedfyWh-N",
        "_version": 1,
        "result": "created",
        "_shards": {
            "total": 2,
            "successful": 1,
            "failed": 0
        },
        "_seq_no": 0,
        "_primary_term": 1
    }
    

    3、 查询文档

    ########### 查询所有
    get 192.168.1.139:9200/test11/_search/
    {
        "query":{
            "match_all":{
            }
        }
    }
    #返回
    {
        "took": 163,
        "timed_out": false,
        "_shards": {
            "total": 5,
            "successful": 5,
            "skipped": 0,
            "failed": 0
        },
        "hits": {
            "total": {
                "value": 1,
                "relation": "eq"
            },
            "max_score": 1.0,
            "hits": [
                {
                    "_index": "test11",
                    "_id": "QlS7Go0BnqotedfyWh-N",
                    "_score": 1.0,
                    "_source": {
                        "title": "标题",
                        "year": "2024-01-18 12:00:34",
                        "type": 1,
                        "star": 5.2,
                        "director": "啦啦啦"
                    }
                }
            ]
        }
    }
    
    ########### 匹配查询(match)
    # 查看title 字段有"标题"的
    GET /test11/_search
    {
        "query":{
            "match": {
              "title": "标题"
            }
        }
    }
    # 查看所有字段有"标题"
    body = {
        "query": {
            "multi_match": {
                "query": "标题",
            }
        }
    }
    # 查看title字段有"标"or"题"的
    GET /test11/_search
    {
      "query": {
        "match": {
          "title": {
            "query": "标题",
            "analyzer": "standard"
          }
        }
      }
    }
    3. 多字段查询
    # 查询 title or director 含有标的
    GET /test11/_search/
    {
      "query": {
        "multi_match": {
          "query": "标",
          "fields": [
            "title",
            "director"
          ]
        }
      }
    }
    4. 词条匹配(term)
    # term 查询被用于精确值 匹配,
    # 这些精确值可能是数字、时间、布尔或者那些未分词的字符串(keyword)
    GET /test11/_search/
    {
        "query":{
            "term":{
                "director":"标题"
            }
        }
    }
    
    # term只能完整值匹配,这样就查询不出来
    GET /test11/_search/
    {
        "query":{
            "term":{
                "director":"标"
            }
        }
    }
    对于未分词的字符串查找
    GET /test11/_search/
    {
        "query":{
            "term":{
                "director.keyword":"标"
            }
        }
    }
    5. 范围
    GET test11/_search
    {
      "query": {
      "range": {
        "type": {
          "gte": 0,
          "lte": 1
        }
      }
      }
    }
    6. 复杂查询
    # bool有3类查询关系,must(都满足),should(其中一个满足),must_not(都不满足)
    GET test11/_search
    {
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "type": 1
              }
            },
            {
              "match": {
                "title": "标"
              }
            },
            {
              "range": {
                "type": {
                  "gte": 1,
                  "lte": 2
                }
              }
            }
          ]
        }
      }
    }
    7.通配符
    # 诸如我的丧失女友,逆我者亡等等
    GET test11/_search
    {
        "query":{
            "wildcard":{
                "title":"*标*"
            }
        }
    }
    8.多字段搜索
    GET /test11/_search 
    { 
        "query": { 
            "match": { 
                "title": { 
                    "query": "腾讯 收入", 
                    "operator": "and"
                 } 
            }
       }
    } 
    9.类似数据表一样,条件、范围、排序
    GET test11/_search
    {
      "sort":{// 排序
          "type":{"order":"asc"}// 类型排序,asc正序,desc倒叙
      }
    #“sort”: { “update_time”: { “order”: “asc”,”mode”:”min” }}//对数字或者日期可以查询多个值当中min、max、avg、sum排序
      "from":0,//条目开始位置
      "size":10,// 每页数量
      "query": {// 查询条件
        "bool": {
          "must": [
            {
              "match": {
                "type": 1
              }
            },
            {
              "match": {
                "title": "标"
              }
            },
            {
              "range": {
                "type": {
                  "gte": 1,
                  "lte": 2
                }
              }
            }
          ]
        }
      }
    }
    
    10、精准匹配match_phrase
    GET test11/_search
    {
        "query":{
            "match_phrase":{
                "title":"标题1"
            }
        }
    }
    

    4、指定id插入文档

    PUT /test11/1
    {
      "title": "标题11",
      "year": "2024-01-18 12:00:34",
      "type": 1,
      "star": 5.2,
      "director": "啦啦啦"
    }
    
    # 返回
    {
      "_index" : "test11",
      "_type" : "_doc",
      "_id" : "1",
      "_version" : 1,
      "result" : "created",
      "_shards" : {
        "total" : 2,
        "successful" : 2,
        "failed" : 0
      },
      "_seq_no" : 26,
      "_primary_term" : 4
    }
    

    5、指定id获取文档

    GET /test11/_doc/1
    
    # 返回
    {
      "_index" : "customer",
      "_type" : "_doc",
      "_id" : "1",
      "_version" : 1,
      "_seq_no" : 26,
      "_primary_term" : 4,
      "found" : true,
      "_source" : {
          "title": "标题11",
          "year": "2024-01-18 12:00:34",
          "type": 1,
          "star": 5.2,
          "director": "啦啦啦"
    }
    }
    

    6、修改文档

    # 单个文档单个字段修改
    post test11/_update/1
    {
        "doc": {
            "title":"标题2222"
        }
    }
     
    #单个文档全部字段修改
    put test11/_doc/1
    {
          "title": "标题333",
          "year": "2024-01-19 12:00:34",
          "type": 2,
          "star": 5.3,
          "director": "啦e啦"
    }
    
    # 全部文档统一修改
    POST /test11/_doc/_update_by_query
    {
        "query": {// 根据查询条件修改,若不需要,在这里不需要query,只要script
            "match": {
                "director": "啦e啦"
            }
        },
        "script": {
            "inline": "ctx._source['title'] = '都是这个标题'"
        }
    }
    
    

    7、删除文档

    delete test11/_doc/1
     
    

    8、获取索引信息

    # 查询所有索引
    get /_cat/indices
    
    # 查询单个索引
    get /test11/
    
    查询对应索引的数据结构
    `GET /test11/_mapping`
    
    查询对应索引的数据条数
    GET /test11/_count
    

    9、删除索引

    delete /test11
    

    10、修改索引

    # 修改名字
    ## 方法1
    为原索引增加别名,则就可以用别名作为新名字
    ## 方法2
    新建一个索引,然后把就数据copy到新索引
    # 迁移数据
    POST _reindex
    {
      "source": {
        "index": "test1"// 旧索引
      },
      "dest": {
        "index": "test2"// 新索引
      }
    }
    
    # 修改字段类型
    由于es无法修改mapping,则需要曲线救国,可通过新建索引的方式来修改字段类型
     
    先新增新的索引
    
    然后吧旧数据倒过来
    POST _reindex
    {
      "source": {
        "index": "test1"// 旧索引
      },
      "dest": {
        "index": "test2"// 新索引
      }
    }
    
    ## 此外还可以进行字段名修改,新创建的索引字段名为create_time
     
    PUT test2
    {
      "mappings": {
        "properties": {
          "create_time": {
            "type": "text"
          }
        }
      }
    }
     
    POST _reindex
    {
      "source": {
        "index": "test1
      },
      "dest": {
        "index": "test2"
      },
      "script": {
        "source": "ctx._source.create_time= ctx._source.remove(\"create_date\")"
      }
    }
    

    相关文章

      网友评论

          本文标题:es的增删改查

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