elasticsearch 学习

elasticsearch 学习 https://www.cnblogs.com/qdhxhz/p/11493677.html
    es 基础概念:
        索引index: 案由相同属性的文档集合
        类型type: 索引可以定义一个或多个类型,文档必须属于一个类型
        文档document: 文档是可以被索引的基本数据单位
        3者的关系类似(索引=数据库,类型=表,文档=数据)
        分片: 每个索引都有多个分片,每个分片都是一个Lucene索引 (如果一个索引量很大,就需要分片)
        备份: 拷贝一份分片就完成了分片的备份
    es API
        api基本格式: http:// <ip>:<port>/<索引>/<类型>/<文档id>
        常用http动作词: GET/PUT/POST/DELETE 
    es 基本用法:
        非结构化创建: 关键词 mappings:{} 空数据就是非结构化
        结构化创建: (注: 7.x 移除type类型 都统一改为_doc)
            {
                "settings": {
                    "number_of_shards": 3,
                    "number_of_replicas": 1
                },
                "mappings": {
                    "properties": {
                        "name": {
                            "type": "text"
                        },
                        "country": {
                            "type": "keyword"
                        },
                        "age": {
                            "type": "integer"
                        },
                        "date": {
                            "type": "date",
                            "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
                        }
                    }
                }
            }
        插入 post:
            指定文档Id插入
                url: http://127.0.0.1:9200/people/_doc/1
                {
                    "name": "wali2",
                    "age": 40,
                    "country": "china",
                    "date": "2021-03-16"
                }
            自动产生文档id插入
                url: http://127.0.0.1:9200/people/_doc/
                {
                    "name": "wali2",
                    "age": 40,
                    "country": "china",
                    "date": "2021-03-16"
                }
        修改 post put: 
            直接修改文档: 
                url: http://127.0.0.1:9200/people/_doc/1/_update
                {
                    "doc": {
                        "name": "谁是瓦力"
                    }
                }
            脚本修改文档:
                url: http://127.0.0.1:9200/people/_doc/1/_update
                {
                    "_index": "people",
                    "_type": "_doc",
                    "_id": "1",
                    "_version": 5,
                    "result": "updated",
                    "_shards": {
                        "total": 2,
                        "successful": 2,
                        "failed": 0
                    },
                    "_seq_no": 5,
                    "_primary_term": 1
                }
        删除 delete: 
            删除文档 请求方式: 
                http://127.0.0.1:9200/people/_doc/1
            删除索引(也会删除数据)
                http://127.0.0.1:9200/book
        查询:
            简单查询 get:
                http://127.0.0.1:9200/people/_doc/CzNtOXgB1o6hJzpfqHGJ
                _cat/count/people?v 查看数据条数
            条件查询 post/get, 关键词query:
                http://127.0.0.1:9200/people/_doc/_search
                1.{
                    "query": {
                        "match_all": {} 返回全部数据
                    },
                    "from": 1, 起始返回数据
                    "size": 1  返回数据条数
                }
                2.{
                    "query": {
                        "match": {
                            "name": "wali2" 查询name包含wali2的数据
                        }
                    }
                }
                排序等.
            聚合查询,关键词 aggs:
            http://127.0.0.1:9200/people/_doc/_search
            {
                "aggs": {
                    "自定义聚合名称": { 
                        "terms": { 
                            "field": "age" 列名
                        }
                    }
                }
            }
            计算
            {
                "aggs": {
                    "xxx": {
                        "stats": { min max
                            "field": "age"
                        }
                    }
                }
            }
        高级查询:
            子条件查询: 特定字段查询特定值
                Query context
                    全文本查询 针对文本类型数据
                        模糊匹配(会查询ElasticSearch,入门两个关键词)
                            {
                                "query": {
                                    "match": {
                                        "title": "ElasticSearch入门"
                                    }
                                }
                            }
                        习语匹配
                            把 match 改成 match_phrase 只查询 ElasticSearch入门
                        多个字段匹配
                            {
                                "query": {
                                    "multi_match": {
                                        "query": "瓦力",
                                        "fields": ["author", "title"] 这两个字段中包含"瓦力"的
                                    }
                                }
                            }
                        语法查询
                            查询包含 ElasticSearch 打发 / Python 的数据
                            {
                                "query": {
                                    "query_string": {
                                        "query": "(ElasticSearch AND 打发) OR Python"
                                    }
                                }
                            }
                            查询"author", "title"中包含瓦力 或者 xxx
                            {
                                "query": {
                                    "query_string": {
                                        "query": "瓦力 OR xxx",
                                        "fields": ["author", "title"]
                                    }
                                }
                            }
                    字段级别查询 针对结构化数据,如数字,日期等
                        { 查询年龄100的
                            "query": {
                                "term": {
                                    "age": 100
                                }
                            }
                        }
                        { 查询年龄大于10 且 小于200的数据(e 是等于包含)
                            "query": {
                                "range": {
                                    "age": {
                                        "get": 10,
                                        "lte": 200
                                    }
                                }
                            }
                        }
                Filter context : 在查询中,只判断该文档是否满足条件,只有yes,no
                    {
                        "query": {
                            "bool": {
                                "filter": {
                                    "term": {
                                        "age": 100
                                    }
                                }
                            }
                        }
                    }

            复合条件查询: 以一定的逻辑组合子条件查询

elasticsearch 目录结构:
    bin 启动文件目录
    config  配置文件目录
        elasticsearch.yml
            # elasticsearch 和 head 可视化插件跨域问题 (启动不显示es)
            http.cors.enabled: true 
            http.cors.allow-origin: "*"
            # 集群配置
            cluster.name: wali # 集群名字(集群名称是一样的)
            node.name: master # 节点名称 
            node.master: true # master指挥官主节点
            # 设置主机ip
            network.host: 127.0.0.1
            # 端口
            http.port: 8200
            # 设置主节点ip(就是主节点指挥官的ip)
            discovery.zen.ping.unicast.hosts: ["127.0.0.1"]
    lib 第三方库目录
    modules 模块目录
    plugins 第三方插件目录
        head 插件(可视化插件):
            颜色: 绿色(健康) 黄色(不健康) 红色(很差)

  

原文地址:https://www.cnblogs.com/412013cl/p/14544970.html