Elastic Search

  1. 下载 https://www.elastic.co/cn/downloads/elasticsearch

    # elasticsearch.yml
    ingest.geoip.downloader.enabled: false
    

    也可以在jvm.options里设置启动内存,默认4G

    1. 重置密码 elasticsearch-reset-password -uelastic
  2. 数据格式

    1. es是面向文档文档型数据库
    2. 倒排索引,关键字和文档编号的关系
  3. 基础操作

    # 创建索引
    PUT http://127.0.0.1:9200/shopping
    # 获取索引
    GET http://127.0.0.1:9200/_cat/indices?v
    # 删除索引
    DELETE http://127.0.0.1:9200/shopping
    
    # 写数据  body里加JSON数据
    POST http://127.0.0.1:9200/shopping/_doc
    {
        "title":"小米手机",
        "catepory":"小米",
        "price":998
    }
    # 写数据 id固定为1001
    POST http://127.0.0.1:9200/shopping/_doc/1001
    
    # 读数据 主键查询
    GET http://127.0.0.1:9200/shopping/_doc/1001
    # 读数据 全部查询
    GET http://127.0.0.1:9200/shopping/_search
    
    # 修改数据 全量更新
    PUT http://127.0.0.1:9200/shopping/_doc/1001
    {
        "title":"小米手机",
        "catepory":"小米",
        "price":999
    }
    # 修改数据 局部更新
    POST http://127.0.0.1:9200/shopping/_update/1001
    {
        "doc":{"title":"华为手机"}
    }
    
    # 删除数据
    DELETE http://127.0.0.1:9200/shopping/_doc/1001
    
    # 条件查询
    GET http://127.0.0.1:9200/shopping/_search?q=小米
    
    # 条件查询
    GET http://127.0.0.1:9200/shopping/_search
    {
        "query":{
            "match":{
                "catepory": "小米"
            }
        }
    }
    # 全部查询
    {
        "query":{
            "match_all":{
            }
        }
    }
    # 分页查询,排序
    {
        "query":{
            "match_all":{
            }
        },
        "from":0,
        "size":2,
        "_source":["title"],
        "sort":{
            "price":{
                "order":"desc"
            }
        }
    }
    
    # 条件查询  must
    {
        "query":{
            "bool":{
                "must":[
                    {
                        "match":{
                            "category":"小米"
                        }
                    },
                    {
                        "match":{
                            "price":999
                        }
                    }
                ]
            }
        }
    }
    # 条件查询  should  match  match_phrase
    {
        "query":{
            "bool":{
                "should":[
                    {
                        "match":{
                            "category":"小米"
                        }
                    },
                    {
                        "match":{
                            "category":"华为"
                        }
                    }
                ],
                "filter":{
                    "range":{
                        "price":{
                            "gt":998
                        }
                    }
                }
            }
        },
        "highlight":{
            "fields":{
                "category":{}
            }
        }
    }
    
    # 聚合操作
    {
        "aggs":{ // 聚合操作
            "price_group":{ // 名称,随意
                "terms":{ // 分组
                    "field":"price" // 分组字段
                }
            }
        },
        "size":0  // 其他信息不要了
    }
    
    # 求平均值
    {
        "aggs":{ // 聚合操作
            "price_avg":{ // 名称,随意
                "avg":{ // 分组
                    "field":"price" // 分组字段
                }
            }
        },
        "size":0  // 其他信息不要了
    }
    
    
    # 映射关系  创建索引 
    GET http://127.0.0.1:9200/user/_mapping
    {
        "properties":{
            "name":{
                "type":"text", // 可以分词
                "index":true // 可以索引查询
            },
            "sex":{
                "type":"keyword", // 不可以分词
                "index":true
            },
            "tel":{
                "type":"keyword", // 不可以分词
                "index":false // 不可以索引查询
            }
        }
    }
    
    
    
posted @ 2024-01-30 12:54  停不下的时光  阅读(4)  评论(0编辑  收藏  举报