美文网首页学习
Elasticsearch 实战入门

Elasticsearch 实战入门

作者: 刀鱼要到岛上掉 | 来源:发表于2020-11-02 14:08 被阅读0次

    Elasticsearch 入门

    • 新建 PUT
        PUT 'localhost:9200/weather'
    
        @return {
            "acknowledged": true,
            "shards_acknowledged": true,
            "index": "weather"
        }
    
    - 新增一条记录 
    ```
    PUT localhost:9200/accounts/person/2
    注:POSTMAN ->body ->raw (json)
        {
          "user": "张三",
          "title": "工程师",
          "desc": "数据库管理"
        }
    
    @return{
        "_index": "accounts",
        "_type": "person",
        "_id": "2",
        "_version": 1,
        "result": "created",
        "_shards": {
            "total": 2,
            "successful": 1,
            "failed": 0
        },
        "_seq_no": 2,
        "_primary_term": 1
    }
    ```
    
    • 删除 DELETE
        DELETE 'localhost:9200/weather'
    
        @return {
            "acknowledged": true
        }
    
    • POST 添加一个记录,不指定 Id
        localhost:9200/accounts/person
    
        {
          "user": "张si",
          "title": "工程师",
          "desc": "数据库管理"
        }
        生成随机id
        @return {
            "_index": "accounts",
            "_type": "person",
            "_id": "vFL2fXUBQ-J0hUIur3oG",
            "_version": 1,
            "result": "created",
            "_shards": {
                "total": 2,
                "successful": 1,
                "failed": 0
            },
            "_seq_no": 4,
            "_primary_term": 1
        }
    
    • 查看记录 GET
        GET localhost:9200/accounts/person/1?pretty=true
    
        @return {
            "_index": "accounts",
            "_type": "person",
            "_id": "1",
            "_version": 2,
            "_seq_no": 1,
            "_primary_term": 1,
            "found": true,  //查询成功
            "_source": {    //原始记录
                "user": "张三",
                "title": "工程师",
                "desc": "数据库管理"
            }
        }
    
        GET localhost:9200/accounts/person/_search   //获取/Index/Type下所有数据
        
        @return {
            "took": 762,
            "timed_out": false,
            "_shards": {
                "total": 1,
                "successful": 1,
                "skipped": 0,
                "failed": 0
            },
            "hits": {
                "total": {
                    "value": 4,
                    "relation": "eq"
                },
                "max_score": 1,
                "hits": [
                    {
                        "_index": "accounts",
                        "_type": "person",
                        "_id": "2",
                        "_score": 1,
                        "_source": {
                            "user": "张si",
                            "title": "工程师",
                            "desc": "数据库管理"
                        }
                    },
                    {
                        "_index": "accounts",
                        "_type": "person",
                        "_id": "u1LzfXUBQ-J0hUIuaXp9",
                        "_score": 1,
                        "_source": {
                            "user": "张si",
                            "title": "工程师",
                            "desc": "数据库管理"
                        }
                    }
                ]
            }
        }
    
    - Elastic 的查询非常特别,使用自己的查询语法,要求 GET 请求带有数据体。
    ```
        GET localhost:9200/accounts/person/_search
        //带有数据体
        @request{
          "query" : { "match" : { "desc" : "软件" }}
        }
    ```
    - Elastic 默认一次返回10条结果,可以通过size字段改变这个设置
    ```
        @request{
          "query" : { "match" : { "desc" : "管理" }},
          "size": 1
        }
    ```
    - 逻辑运算 OR 
    ```
        {
          "query" : { "match" : { "desc" : "软件 系统" }}
        }
    ```
    - 逻辑运算 AND
    ```
        {
          "query": {
            "bool": {
              "must": [
                { "match": { "desc": "软件" } },
                { "match": { "desc": "系统" } }
              ]
            }
          }
        }
    ```
    - 

    相关文章

      网友评论

        本文标题:Elasticsearch 实战入门

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