美文网首页
Elasticsearch7.6.x:使用详解

Elasticsearch7.6.x:使用详解

作者: 简书网abc | 来源:发表于2020-06-22 23:43 被阅读0次

    1,基于Rest风格的增删改查

    method url地址 描述
    PUT|POST :9200/索引名称/类型名称/文档id 创建文档(存在即修改)
    POST :9200/索引名称/类型名称 创建文档 (随机文档id)
    POST :9200/索引名称/类型名称/文档id/_update 修改文档
    DELETE :9200/索引名称/类型名称/文档id 删除文档
    GET :9200/索引名称/类型名称/文档id 查询文档通过文档id
    POST :9200/索引名称/类型名称/_search 查询所有数据

    2,创建具体数据类型映射的索引

    PUT /test2
    {
        "settings": {
              "number_of_shards": "3",
              "number_of_replicas": "0"
        },
       "mappings": {
             "properties": {
                  "name":{
                    "type": "text"
                  },
                  "age":{
                    "type": "long"
                  },
                  "birthday":{
                    "type": "date"
                  }
            }
      }
    }
    

    3,删除索引

    DELETE /test2
    

    4,新增操作

    有文档ID的新增操作,若该id在库中存在,则覆盖式修改
    put | post /test2/_doc/1 
    {
        "name":"liming",
        "age":28,
        "sort":33
    }
    
    无文档id的新增操作
    post /test2/_doc/
    {
        "name":"page",
        "page":28
    }
    

    5,更新:

    PUT方法会覆盖原有数据,若新数据为空同样覆盖,建议使用POST /…/_update不会覆盖原有数据

    如果id存在,无论post还是put方法,都会覆盖原有的数据
    PUT|post /test2/_doc/1
    {
      "name":"狂神",
      "age":23,
      "birthday":"1997-12-01"
    }
    
    该方法不会覆盖原有数据,建议使用该方法
    POST /test2/_doc/1/_update
    {
      "doc":{
        "name":"法外狂徒"
      }
    }
    

    6,查询所有数据

    POST|GET /test2/_doc/_search
    

    6.1 match为模糊查询,会进行相应的分词操作,term为精准查询

    POST|GET /test2/_search
    {
      "query": {
        "match": {
          "name": "神"
        }
      }
    }
    
    

    6.2 排序与分页

    from为查询的开始,size为查询返回个数

    POST|GET  /test2/_search
    {
      "query": {
        "match": {
          "name": "神"
        }
      },
      "sort": [
        {
          "age": {
            "order": "desc"
          }
        }
      ],
      "from": 0,
      "size": 10
    }
    

    6.3布尔查询

    must相对于and

    POST|GET  /test2/_search
    {
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "name": "神"
              }
            },
            {
              "match": {
                "age": "25"
              }
            }
          ]
        }
      },
      "from": 0,
      "size": 10
    }
    

    should相对于or

    POST|GET  /test2/_search
    {
      "query": {
        "bool": {
          "should": [
            {
              "match": {
                "name": "神"
              }
            },
            {
              "match": {
                "age": "25"
              }
            }
          ]
        }
      },
      "from": 0,
      "size": 10
    }
    

    must_not相对于not

    POST|GET  /test2/_search
    {
      "query": {
        "bool": {
          "must_not": [
            {
              "match": {
                "name": "神"
              }
            },
            {
              "match": {
                "age": "25"
              }
            }
          ]
        }
      },
      "from": 0,
      "size": 10
    }
    

    过滤器与多条件查询(有过滤器,必须先满足过滤器)
    gt 大于,gte 大于等于,lt 小于,lte 小于等于!

    POST|GET  /test2/_search
    {
      "query": {
        "bool": {
          "should": [
            {
              "match": {
                "name": "神"
              }
            },
            {
              "match": {
                "name": "wuhan"
              }
            }
          ],
          "filter": {
            "range": {
              "age": {
                "gt": 25
              }
            }
          }
        }
      },
      "from": 0,
      "size": 10
    }
    

    同字段 or 搜索

    POST|GET  /test2/_search
    {
      "query": {
        "match": {
          "name": "想 神"
        }
      },
      "from": 0,
      "size": 10
    }
    

    keyword和text

    keyword不会被分词器拆分,text类型会

    GET _analyze
    {
    "analyzer": "keyword",
    "text":"狂神说"
    }
    
    GET _analyze
    {
    "analyzer": "standard",
    "text":"狂神说"
    }
    
    

    相关文章

      网友评论

          本文标题:Elasticsearch7.6.x:使用详解

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